def unstake_delegate(): mns = election_house.get_policy('delegate') assert delegate_stakes[ctx.caller] >= DELEGATE_COST assert ctx.caller not in mns, "Can't unstake if in governance." currency.transfer(delegate_stakes[ctx.caller], ctx.caller)
def sell(contract: str, token_amount: float): assert pairs[contract] is not None, 'Market does not exist!' assert token_amount > 0, 'Must provide currency amount and token amount!' token = I.import_module(contract) assert I.enforce_interface(token, token_interface), 'Invalid token interface!' currency_reserve, token_reserve = reserves[contract] k = currency_reserve * token_reserve new_token_reserve = token_reserve + token_amount new_currency_reserve = k / new_token_reserve currency_purchased = currency_reserve - new_currency_reserve # MINUS FEE fee = currency_purchased * FEE_PERCENTAGE currency_purchased -= fee new_currency_reserve += fee assert currency_purchased > 0, 'Token reserve error!' token.transfer_from(amount=token_amount, to=ctx.this, main_account=ctx.caller) currency.transfer(amount=currency_purchased, to=ctx.caller) reserves[contract] = [new_currency_reserve, new_token_reserve] prices[contract] = new_currency_reserve / new_token_reserve
def remove_liquidity(contract: str, amount: float = 0): assert pairs[contract] is True, 'Market does not exist!' assert amount > 0, 'Must be a positive LP point amount!' assert lp_points[ contract, ctx.caller] >= amount, 'Not enough LP points to remove!' token = I.import_module(contract) assert I.enforce_interface(token, token_interface), 'Invalid token interface!' lp_percentage = amount / lp_points[contract] currency_reserve, token_reserve = reserves[contract] currency_amount = currency_reserve * decimal(lp_percentage) token_amount = token_reserve * decimal(lp_percentage) currency.transfer(to=ctx.caller, amount=currency_amount) token.transfer(to=ctx.caller, amount=token_amount) lp_points[contract, ctx.caller] -= amount lp_points[contract] -= amount assert lp_points[contract] > 1, 'Not enough remaining liquidity!' new_currency_reserve = currency_reserve - currency_amount new_token_reserve = token_reserve - token_amount assert new_currency_reserve > 0 and new_token_reserve > 0, 'Not enough remaining liquidity!' reserves[contract] = [new_currency_reserve, new_token_reserve] return currency_amount, token_amount
def transfer(to: str, amount: int, from_: str = None): # Transfer currency to caller if from_: currency.transfer_from(amount, to, from_) else: # transfer from contract currency.transfer(amount, to)
def test_smack_lose(): currency.transfer_from(amount=settings['cost'], to=ctx.this, main_account=ctx.caller) x = settings['odds'] if x == 19: currency.transfer(amount=balances[ctx.this], to=ctx.caller)
def smack(): currency.transfer_from(amount=settings['cost'], to=ctx.this, main_account=ctx.caller) x = random.randint(1, settings['odds']) if x == settings['odds']: currency.transfer(amount=balances[ctx.this], to=ctx.caller)
def unstake_masternode(): mns = election_house.get_policy('masternodes') assert masternode_stakes[ctx.caller] >= MASTER_COST assert ctx.caller not in mns, "Can't unstake if in governance." currency.transfer(masternode_stakes[ctx.caller], ctx.caller)
def determine_results(p_id: int): assert (proposal_details[p_id, "time"] + datetime.timedelta(days=1) * (proposal_details[p_id, "duration"])) <= now, "Proposal not over!" proposals_finished = finished_proposals.get() for x in proposals_finished: assert p_id != x proposals_finished.append(p_id) finished_proposals.set(proposals_finished) approvals = 0 for x in proposal_details[p_id, "voters"]: if sig[p_id, x] is True: approvals += 1 if (approvals / number_of_sig.get()) >= required_approval_percentage.get(): if proposal_details[p_id, "type"] == "transfer": currency.transfer(proposal_details[p_id, "amount"], proposal_details[p_id, "reciever"]) elif proposal_details[p_id, "type"] == "add_signature": sig[proposal_details[p_id, "reciever"]] = True number_of_sig.set(number_of_sig.get() + 1) elif proposal_details[p_id, "type"] == "remove_signature": sig[proposal_details[p_id, "reciever"]] = False number_of_sig.set(number_of_sig.get() - 1) elif proposal_details[p_id, "type"] == "change_approval_percentage": required_approval_percentage.set(proposal_details[p_id, "amount"]) elif proposal_details[p_id, "type"] == "change_minimum_duration": minimum_proposal_duration.set(proposal_details[p_id, "amount"]) return True else: return False
def redeem(amount: float): assert balances[ctx.caller] >= amount, 'Not enough tokens to redeem!' assert amount > 0, 'Invalid amount!' balances[ctx.caller] -= amount share = amount / supply.get() reward = share * currency.balance_of(ctx.this) if reward > 0: currency.transfer(reward, ctx.caller) supply.set(supply.get() - amount)
def unregister(): mns = election_house.current_value_for_policy(controller.get()) assert candidate_state['registered', ctx.caller], 'Not registered.' assert ctx.caller not in mns, "Can't unstake if in governance." currency.transfer(member_cost.get(), ctx.caller) candidate_state['registered', ctx.caller] = False candidate_state['votes', ctx.caller] = 0
def transfer(to: str): # Create wait period time delta WAIT_PERIOD = datetime.timedelta(hours=S['WAIT_PERIOD_HOURS']) # Check if signer has used the faucet before if S[ctx.signer]: # Assert last call was more than a day ago assert WAIT_PERIOD <= now - S[ ctx.signer], "Can only call once per day." # Prevent 1 account from initiating too many sends. assert S[ctx.signer, 'amount'] < S[ 'MAX_SEND'], "This account has given too much currency." # Check if the "to" account has used the faucet before if S[to]: # Assert the to account hasn't received currency today assert WAIT_PERIOD <= now - S[to], "Can only call once per day." # Prevent 1 account from receiving too much currency. assert S[to, 'amount'] < S[ 'MAX_SEND'], "This account has received MAX amount from faucet." # Transfer currency to caller currency.transfer(S['DRIP'], to) # Set set last call time to NOW for signer S[ctx.signer] = now if not S[ctx.signer, 'amount']: S[ctx.signer, 'amount'] = S['DRIP'] else: S[ctx.signer, 'amount'] = S[ctx.signer, 'amount'] + S['DRIP'] # Set the receiving account has already go sent currency today S[to] = now if ctx.signer != to: # Keep a running total of currency given to a specific address if not S[to, 'amount']: S[to, 'amount'] = S['DRIP'] else: S[to, 'amount'] = S[to, 'amount'] + S['DRIP']
def remove_malicious_bet(bet_id: str): # necessary as a precaution and preventative measure against malicious actors creating bad bets, # such as offensive bets or potential exploits. This is left at the discretion of the owner and will not be used # unless absolutely necessary. assert owner.get( ) == ctx.caller, 'Only the owner can remove a malicious bet' assert bets[bet_id] is not None, 'Bet does not exist' player_left = bets[bet_id, 'player_left'] amount_left = bets[bet_id, 'amount_left'] + ( bets[bet_id, 'validation_deposit_left'] * 0.8) bets[player_left, 'funds'] += amount_left owner_fee = (bets[bet_id, 'validation_deposit_left'] * 0.2) if bets[bet_id, 'player_right'] is not None: player_right = bets[bet_id, 'player_right'] amount_right = bets[bet_id, 'amount_right'] + ( bets[bet_id, 'validation_deposit_right'] * 0.8) bets[player_right, 'funds'] += amount_right owner_fee += (bets[bet_id, 'validation_deposit_right'] * 0.2) currency.transfer(amount=owner_fee, to=ctx.caller, main_account=ctx.this) remove_game(bet_id)
def token_swap(token_contract: str, tau_in: float, token_in: float, to: str): assert tau_in > 0 or token_in > 0, 'Invalid amount!' assert not (tau_in > 0 and token_in > 0), 'Swap only accepts one currecy!' assert not pairs[token_contract] is None, 'Invalid token ID!' assert pairs[token_contract, 'tau_reserve'] > 0 assert pairs[token_contract, 'token_reserve'] > 0 token = get_interface(token_contract) # 1 - Calculate trade outcome tau_out, token_out, tau_slippage, token_slippage = calculate_trade_details( token_contract, tau_in, token_in) # 2 - Transfer in tokens if tau_in > 0: currency.transfer(tau_in, ctx.this) if token_in > 0: token.transfer(token_in, ctx.this) # 3 - Swap/transfer out tokens + Update swap(token, tau_out, token_out, to)
def test_smack_win(bet_amount: int): # lock max bet to 8% of current pot bet = determine_cost(bet_amount) # Take bet currency.transfer_from(amount=bet, to=ctx.this, main_account=ctx.caller) # Get random number x = 6 # WIN if x >= 6: win_amount = bet * 2 currency.transfer(amount=win_amount, to=ctx.caller) return '{"bet":' + str(bet) + ', "won":' + str( win_amount) + ', "status": 1}' # LOSE else: give_dev_token() check_overflow() return '{"bet":' + str(bet) + ', "status": 0}'
def transfer_as_owner(to: str): # Create wait period time delta WAIT_PERIOD = datetime.timedelta(hours=S['WAIT_PERIOD_HOURS']) # Check if caller has previously used faucet if S[to]: # Prevent 1 vk from using facet too many times assert S[to, 'amount'] < S[ 'MAX_SEND'], "Account has received MAX amount from faucet." # Assert last call was more than a day ago assert WAIT_PERIOD <= now - S[to], "Can only call once per day." # Transfer currency to caller currency.transfer(S['DRIP'], to) # Set set last call time to NOW for caller S[to] = now # Keep a running total of currency given to a specific address if not S[to, 'amount']: S[to, 'amount'] = S['DRIP'] else: S[to, 'amount'] = S[to, 'amount'] + S['DRIP']
def withdraw(amount: float): assert ctx.signer == Owner.get(), 'You must be the owner of this contract to withdraw funds.' currency.transfer(amount=amount, to=to)
def transfer(amount: float, to: str): assert_operator() currency.transfer(amount=amount, to=to)
def check_overflow(): overflow = currency.balance_of(ctx.this) - settings['pot_max'] if overflow > 0: currency.transfer(amount=overflow, to=settings['dev_contract'])
def withdraw(amount: float): assert_owner() currency.transfer(amount=amount, to=ctx.caller)
def disperse(amount: float, to: str, hash: str): assert_owner() assert seen_hashes[hash] is None, 'Already processed this hash!' seen_hashes[hash] = True currency.transfer(amount=amount, to=to)
def withdraw(amount: float): assert amount > 0, 'Cannot send negative balances!' assert ctx.caller == owner.get(), 'Not owner!' currency.transfer(amount, ctx.caller)
def sell(contract: str, token_amount: float, minimum_received: float = 0, token_fees: bool = False): assert pairs[contract] is not None, 'Market does not exist!' assert token_amount > 0, 'Must provide currency amount and token amount!' token = I.import_module(contract) amm_token = I.import_module(state["TOKEN_CONTRACT"]) assert I.enforce_interface(token, token_interface), 'Invalid token interface!' if contract == state["TOKEN_CONTRACT"]: token.transfer_from(amount=token_amount, to=ctx.this, main_account=ctx.caller) currency_purchased = internal_sell( contract=state["TOKEN_CONTRACT"], token_amount=token_amount) currency.transfer(amount=currency_purchased, to=ctx.caller) return currency_purchased currency_reserve, token_reserve = reserves[contract] k = currency_reserve * token_reserve new_token_reserve = token_reserve + token_amount new_currency_reserve = k / new_token_reserve currency_purchased = currency_reserve - new_currency_reserve # MINUS FEE fee_percent = state["FEE_PERCENTAGE"] * discount[ ctx.caller] #Discount is applied here fee = currency_purchased * fee_percent if token_fees is True: fee = fee * state["TOKEN_DISCOUNT"] rswp_currency_reserve, rswp_token_reserve = reserves[ state["TOKEN_CONTRACT"]] rswp_k = rswp_currency_reserve * rswp_token_reserve rswp_new_currency_reserve = rswp_currency_reserve + fee rswp_new_currency_reserve += fee * fee_percent #Not 100% accurate, uses output currency instead of input currency rswp_new_token_reserve = rswp_k / rswp_new_currency_reserve sell_amount = rswp_token_reserve - rswp_new_token_reserve #SEMI-VOODOO MATH, PLEASE DOUBLE CHECK sell_amount_with_fee = sell_amount * state["BURN_PERCENTAGE"] amm_token.transfer_from(amount=sell_amount, to=ctx.this, main_account=ctx.caller) currency_received = internal_sell( contract=state["TOKEN_CONTRACT"], token_amount=sell_amount_with_fee) amm_token.transfer(amount=sell_amount - sell_amount_with_fee, to=state["BURN_ADDRESS"]) new_currency_reserve = decimal( new_currency_reserve) + currency_received else: currency_purchased = decimal(currency_purchased) - fee burn_amount = fee - fee * state["BURN_PERCENTAGE"] new_currency_reserve = decimal( new_currency_reserve) + fee * state["BURN_PERCENTAGE"] token_received = internal_buy(contract=state["TOKEN_CONTRACT"], currency_amount=burn_amount) amm_token.transfer(amount=token_received, to=state["BURN_ADDRESS"]) #Buy and burn here if minimum_received != None: #!= because the type is not exact assert currency_purchased >= minimum_received, "Only {} TAU can be purchased, which is less than your minimum, which is {} TAU.".format( currency_purchased, minimum_received) assert currency_purchased > 0, 'Token reserve error!' token.transfer_from(amount=token_amount, to=ctx.this, main_account=ctx.caller) currency.transfer(amount=currency_purchased, to=ctx.caller) reserves[contract] = [new_currency_reserve, new_token_reserve] prices[contract] = new_currency_reserve / new_token_reserve return currency_purchased
def withdrawFunds(amount: float): balance = Balances[ctx.caller] assert balance >= amount, 'You cannot withdraw more than ' + balance currency.transfer(amount=amount, to=ctx.caller) Balances[ctx.caller] = balance - amount
def withdraw(amount): assert ctx.caller == owner.get(), 'Not owner!' currency.transfer(amount, ctx.caller)
def withdraw(amount): if ctx.caller == owner.get(): currency.transfer(ctx.caller, amount)
def empty_faucet(): assert_owner() currency.transfer(currency.balance_of(ctx.this), S['OWNER'])
def unregister(): mns = election_house.current_value_for_policy(controller.get()) assert candidate_state['registered', ctx.signer], 'Not registered.' assert ctx.caller not in mns, "Can't unstake if in governance." currency.transfer(MASTER_COST, ctx.caller)
def unregister(): mns = election_house.get_policy('masternodes') assert ctx.caller not in mns, "Can't unstake if in governance." currency.transfer(MASTER_COST, ctx.caller)