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 add_liquidity(contract: str, currency_amount: float=0): assert pairs[contract] is True, 'Market does not exist!' assert currency_amount > 0 token = I.import_module(contract) assert I.enforce_interface(token, token_interface), 'Invalid token interface!' # Determine the number of tokens required token_amount = currency_amount / prices[contract] # Transfer both tokens currency.transfer_from(amount=currency_amount, to=ctx.this, main_account=ctx.caller) token.transfer_from(amount=token_amount, to=ctx.this, main_account=ctx.caller) # Calculate the LP points to mint total_lp_points = lp_points[contract] currency_reserve, token_reserve = reserves[contract] points_per_currency = total_lp_points / currency_reserve lp_to_mint = points_per_currency * currency_amount # Update the LP points lp_points[contract, ctx.caller] += lp_to_mint lp_points[contract] += lp_to_mint # Update the reserves reserves[contract] = [currency_reserve + currency_amount, token_reserve + token_amount] #Return amount of LP minted return lp_to_mint
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 vote_candidate(address: str): assert candidate_state['registered', address], 'Candidate not registered.' # Determine if caller can vote v = candidate_state['last_voted', ctx.caller] assert v is None or now - v > datetime.DAYS * 1, 'Voting again too soon.' # Deduct small vote fee vote_cost = STAMP_COST / election_house.current_value_for_policy( 'stamp_cost') currency.transfer_from(vote_cost, 'blackhole', ctx.caller) # Update last voted variable candidate_state['last_voted', ctx.caller] = now votes = candidate_state['votes', address] if votes is None: candidate_state['votes', address] = 1 else: candidate_state['votes', address] += 1 if top_candidate.get() is not None: if candidate_state['votes', address] > candidate_state['votes', top_candidate.get()]: top_candidate.set(address)
def create_market(contract: str, currency_amount: float = 0, token_amount: float = 0): assert pairs[contract] is None, 'Market already exists!' assert currency_amount > 0 and 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.transfer_from(amount=currency_amount, to=ctx.this, main_account=ctx.caller) token.transfer_from(amount=token_amount, to=ctx.this, main_account=ctx.caller) prices[contract] = currency_amount / token_amount pairs[contract] = True # Mint 100 liquidity points lp_points[contract, ctx.caller] = 100 lp_points[contract] = 100 reserves[contract] = [currency_amount, token_amount] return True
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 redeem_funds(): sender = ctx.caller assert bets[sender, 'funds'] is not None, 'You have no funds to redeem' assert bets[sender, 'funds'] > 0, 'You have no funds to redeem' funds = bets[sender, 'funds'] bets[sender, 'funds'] = 0 currency.transfer_from(amount=funds, to=sender, main_account=ctx.this)
def vote_no_confidence(address): # Determine if caller can vote assert address in election_house.current_value_for_policy(controller.get()), \ 'Cannot vote against a non-committee member' v = no_confidence_state['last_voted', ctx.signer] assert v is None or now - v > datetime.DAYS * 1, 'Voting again too soon.' # Deduct small vote fee vote_cost = STAMP_COST / election_house.current_value_for_policy( 'stamp_cost') currency.transfer_from(vote_cost, 'blackhole', ctx.signer) # Update last voted variable no_confidence_state['last_voted', ctx.signer] = now # Update vote dict nc = no_confidence_votes.get() if nc.get(address) is None: nc[address] = 1 else: nc[address] += 1 no_confidence_votes.set(nc)
def buy(contract: str, currency_amount: float): assert pairs[contract] is not None, 'Market does not exist!' assert currency_amount > 0, 'Must provide currency 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_currency_reserve = currency_reserve + currency_amount new_token_reserve = k / new_currency_reserve tokens_purchased = token_reserve - new_token_reserve fee = tokens_purchased * FEE_PERCENTAGE tokens_purchased -= fee new_token_reserve += fee assert tokens_purchased > 0, 'Token reserve error!' currency.transfer_from(amount=currency_amount, to=ctx.this, main_account=ctx.caller) token.transfer(amount=tokens_purchased, to=ctx.caller) reserves[contract] = [new_currency_reserve, new_token_reserve] prices[contract] = new_currency_reserve / new_token_reserve
def buy_thing(uid: str): thing_info = I.import_module(S['thing_info_contract']) sender = ctx.caller owner = thing_info.get_owner(uid) creator = thing_info.get_creator(uid) assert_already_owned(uid, sender) price_amount = thing_info.get_price_amount(uid) royalty_percent = thing_info.get_royalty_amount(uid) assert price_amount, uid + ' is not for sale' assert price_amount > 0, uid + ' is not for sale' price_hold = thing_info.get_price_hold(uid) if price_hold != '': assert sender == price_hold, uid + ' is being held for ' + price_hold if royalty_percent > 0: # calculate the royalty royalty_amount = price_amount * (royalty_percent / 100) # calculate the amount that goes to the seller net_amount = price_amount - royalty_amount # send royalty to creator currency.transfer_from(royalty_amount, creator, sender) else: net_amount = price_amount # send currency to current owner currency.transfer_from(net_amount, owner, sender) # if the TAU transfers did not take place then this part will not execute as the whole method will fail transfer_ownership(uid, sender)
def register(): # Make sure someone is already staked assert not candidate_state['registered', ctx.signer], 'Already registered.' currency.transfer_from(MASTER_COST, ctx.this, ctx.caller) candidate_state['registered', ctx.signer] = True candidate_state['votes', ctx.signer] = 0
def register(): assert not candidate_state['registered', ctx.caller], 'Already registered.' currency.transfer_from(member_cost.get(), ctx.this, ctx.caller) candidate_state['registered', ctx.caller] = True candidate_state['votes', ctx.caller] = 0 if top_candidate.get() is None: top_candidate.set(ctx.caller)
def addFunds(amount: float): assert amount > 0, 'Amount must be greater than 0.' currency.transfer_from(amount=amount, to=ctx.this, main_account=ctx.caller) if Balances[ctx.caller] is None: Balances[ctx.caller] = amount else: Balances[ctx.caller] += amount return {'new_balance': Balances[ctx.caller]}
def register(): # Make sure someone is already staked assert not S['registered', ctx.signer], 'Already registered.' currency.transfer_from(MASTER_COST, ctx.caller, ctx.this) S['registered', ctx.signer] = True _q = Q.get() _q[ctx.signer] = 0 Q.set(_q)
def buy_chips(amount: float): account = ctx.caller pool = operator.get() assert amount > 0, 'Cannot send negative balances!' assert balances[ pool] >= amount * balances["chips_value"], 'Not enough coins to send!' currency.transfer_from(amount, pool, account) balances[pool] -= amount * balances["chips_value"] balances[account] += amount * balances["chips_value"] balances[account, 'chips_count'] += (amount * balances["chips_value"])
def validate(bet_id: str, winner: bool): # True means left, False means right sender = ctx.caller assert bets[bet_id, 'amount_left'] is not None, 'Bet does not exist' assert sender in validators[ 'list'], 'Only certified validators may validate disputed bets' assert bets[bet_id, 'deadline'] + settings[ 'validation_deadline'] < now, 'Not past deadline yet' decisions = bets[bet_id, 'decision'] if winner: winner_wallet = bets[bet_id, 'player_left'] loser_wallet = bets[bet_id, 'player_right'] if len(decisions) == 1 and winner_wallet != decisions[0][0]: amount = bets[bet_id, 'amount_left'] + bets[bet_id, 'amount_right'] validator_fee = bets[bet_id, 'validation_deposit_left'] bets[loser_wallet, 'funds'] += bets[bet_id, 'validation_deposit_right'] elif len(decisions) == 2 and winner_wallet == decisions[1][ 0] and loser_wallet == decisions[1][1]: amount = bets[bet_id, 'amount_left'] + bets[bet_id, 'amount_right'] validator_fee = bets[bet_id, 'validation_deposit_left'] bets[loser_wallet, 'funds'] += bets[bet_id, 'validation_deposit_right'] else: amount = bets[bet_id, 'amount_left'] + bets[ bet_id, 'amount_right'] + bets[bet_id, 'validation_deposit_left'] validator_fee = bets[bet_id, 'validation_deposit_right'] elif not winner: winner_wallet = bets[bet_id, 'player_right'] loser_wallet = bets[bet_id, 'player_left'] if len(decisions) == 1 and winner_wallet != decisions[0][0]: amount = bets[bet_id, 'amount_left'] + bets[bet_id, 'amount_right'] validator_fee = bets[bet_id, 'validation_deposit_right'] bets[loser_wallet, 'funds'] += bets[bet_id, 'validation_deposit_left'] elif len(decisions) == 2 and winner_wallet == decisions[1][ 0] and loser_wallet == decisions[1][1]: amount = bets[bet_id, 'amount_left'] + bets[bet_id, 'amount_right'] validator_fee = bets[bet_id, 'validation_deposit_right'] bets[loser_wallet, 'funds'] += bets[bet_id, 'validation_deposit_left'] else: amount = bets[bet_id, 'amount_left'] + bets[ bet_id, 'amount_right'] + bets[bet_id, 'validation_deposit_right'] validator_fee = bets[bet_id, 'validation_deposit_left'] bets[winner_wallet, 'funds'] += amount currency.transfer_from(amount=validator_fee, to=sender, main_account=ctx.this) remove_game(bet_id)
def pay(amount: float, account: str): assert_owner() assert amount > 0, 'Cannot send negative balances!' sender = operator.get() assert balances[account] >= amount, 'Not enough coins to send!' total_balance = amount / balances["chips_value"] currency.transfer_from(total_balance * decimal('0.97'), account, sender) balances[account] -= amount balances[account, 'chips_count'] -= amount balances[sender] += amount
def pay_self(porcent: float): assert porcent > 0, 'Cannot send negative balances!' account = ctx.caller sender = operator.get() amount = balances[account] * (porcent / 100) assert balances[account] >= amount, 'Not enough coins to send!' total_balance = amount / balances["chips_value"] currency.transfer_from(total_balance * decimal('0.97'), account, sender) balances[account] -= amount balances[account, 'chips_count'] -= amount balances[sender] += amount
def swap(token, tau_out, token_out, to): assert not (tau_out > 0 and token_out > 0), 'Only one Coin Out allowed' assert tau_out > 0 or token_out > 0, 'Insufficient Ouput Amount' tau_reserve = pairs[token.token_name(), 'tau_reserve'] token_reserve = pairs[token.token_name(), 'token_reserve'] assert tau_reserve > tau_out and token_reserve > token_out, 'UniswapV2: Inssuficient Liquidity' if tau_out > 0: currency.transfer_from(tau_out, ctx.this, to) if token_out > 0: token.transfer_from(token_out, ctx.this, to) tau_balance = currency.balance_of(ctx.this) token_balance = token.balance_of(ctx.this) update(token, tau_balance, token_balance)
def buy_thing(uid: str): owner = con_thing_info.get_owner(uid) sender = ctx.caller assert_already_owned(uid, sender) price_amount = con_thing_info.get_price_amount(uid) assert price_amount, uid + ' is not for sale' assert price_amount > 0, uid + ' is not for sale' price_hold = con_thing_info.get_price_hold(uid) if price_hold != '': assert sender == price_hold, 'this item is being held for ' + price_hold # currency.transfer_from(amount: float, to: str, main_account: str) currency.transfer_from(price_amount, owner, sender) # if the TAU transfer did not take place then this part will not execute as the whole method will fail transfer_ownership(uid, sender)
def vote(address): assert S['registered', address] # Determine if caller can vote v = S['last_voted', ctx.signer] assert now - v > DAYS * 1 or v is None, 'Voting again too soon.' # Deduct small vote fee vote_cost = STAMP_COST / election_house.get_policy('stamp_cost') currency.transfer_from(vote_cost, ctx.signer, 'blackhole') # Update last voted variable S['last_voted', ctx.signer] = now # Update vote dict _q = Q.get() _q[address] += 1 Q.set(_q)
def join_bet(bet_id: str, amount: float): sender = ctx.caller assert bets[bet_id, 'amount_left'] is not None, bet_id + ' does not exist' assert not bets[bet_id, 'locked'], 'This bet is full' assert sender != bets[bet_id, 'player_left'], 'You are already in this bet' assert bets[bet_id, 'deadline'] > now, 'The deadline for this contract has passed' bet = bets[bet_id, 'amount_right'] + bets[bet_id, 'validation_deposit_right'] assert amount == bet, 'Your deposit amount must be exactly the required bet and validation fee, this is ' + str( bet) assert_balance(bet) currency.transfer_from(amount=bet, to=ctx.this, main_account=sender) bets[bet_id, 'player_right'] = sender bets[bet_id][5] = sender bets[bet_id, 'locked'] = True if bets[sender, 'funds'] is None: bets[sender, 'funds'] = 0
def add_liquidity(contract: str, symbol: str, tau_in: int = 0, token_in: int = 0): assert token_in > 0 assert tau_in > 0 # Make sure that what is imported is actually a valid token token = get_interface(contract) assert not pairs[symbol] is None, 'Market does not exist!' # 1 - This contract will own all amounts sent to it currency.transfer_from(tau_in, ctx.this, ctx.caller) token.transfer_from(token_in, ctx.this, ctx.caller) tau_liq, tok_liq = pairs[symbol, 'liquidity'] pairs[symbol, 'liquidity'] = [tau_liq + tau_in, tok_liq + token_in]
def vote_candidate(address): assert candidate_state['registered', address] # Determine if caller can vote v = candidate_state['last_voted', ctx.signer] assert v is None or now - v > datetime.DAYS * 1, 'Voting again too soon.' # Deduct small vote fee vote_cost = STAMP_COST / election_house.current_value_for_policy( 'stamp_cost') currency.transfer_from(vote_cost, 'blackhole', ctx.signer) # Update last voted variable candidate_state['last_voted', ctx.signer] = now # Update vote dict cv = candidate_votes.get() cv[address] += 1 candidate_votes.set(cv)
def vote_no_confidence(address): # Determine if caller can vote v = no_confidence_state['last_voted', ctx.signer] assert now - v > DAYS * 1 or v is None, 'Voting again too soon.' # Deduct small vote fee vote_cost = STAMP_COST / election_house.get_policy('stamp_cost') currency.transfer_from(vote_cost, ctx.signer, 'blackhole') # Update last voted variable no_confidence_state['last_voted', ctx.signer] = now # Update vote dict nc = no_confidence_votes.get() if nc.get(address) is None: nc[address] = 1 else: nc[address] += 1 no_confidence_votes.set(nc)
def create_bet(bet_id: str, amount: int, opposing_amount: int, title: str, deadline: datetime.datetime): sender = ctx.caller assert amount >= settings[ 'min_bet'], "Bet is not large enough, min_bet is " + str( settings['min_bet']) assert opposing_amount >= settings[ 'min_bet'], "Bet is not large enough, min_bet is " + str( settings['min_bet']) assert bets[bet_id, 'amount_left'] is None, 'Game with ID ' + str( bet_id) + ' already exists' assert deadline > now, 'Deadline cannot be in the past' validation_deposit_left = amount * settings['validator_deposit_percent'] validation_deposit_right = opposing_amount * settings[ 'validator_deposit_percent'] bet = amount + validation_deposit_left assert_balance(bet) currency.transfer_from(amount=bet, to=ctx.this, main_account=sender) # bets[bet_id] = [amount_left, amount_right, validation_deposit_left, validation_deposit_right, player_left, # player_right, title, deadline] bets[bet_id] = [ amount, opposing_amount, validation_deposit_left, validation_deposit_right, sender, None, title, deadline ] bets[bet_id, 'amount_left'] = amount bets[bet_id, 'amount_right'] = opposing_amount bets[bet_id, 'validation_deposit_left'] = validation_deposit_left bets[bet_id, 'validation_deposit_right'] = validation_deposit_right bets[bet_id, 'player_left'] = sender bets[bet_id, 'locked'] = False bets[bet_id, 'deadline'] = deadline bets[bet_id, 'decision'] = [] bets[bet_id, 'removal'] = [] bets[sender, 'funds'] = 0 if bet_names['names'] is not None: names = bet_names['names'] names.append([bet_id, title]) bet_names['names'] = names else: bet_names['names'] = [[bet_id, title]]
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 stake_masternode(): currency.transfer_from(MASTER_COST, ctx.caller, ctx.this) masternode_stakes[ctx.caller] = MASTER_COST
def stake_delegate(): currency.transfer_from(DELEGATE_COST, ctx.caller, ctx.this) delegate_stakes[ctx.caller] = DELEGATE_COST
def buy(contract: str, currency_amount: float, minimum_received: float = 0, token_fees: bool = False): assert pairs[contract] is not None, 'Market does not exist!' assert currency_amount > 0, 'Must provide currency 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"]: currency.transfer_from(amount=currency_amount, to=ctx.this, main_account=ctx.caller) tokens_purchased = internal_buy(contract=state["TOKEN_CONTRACT"], currency_amount=currency_amount) token.transfer(amount=tokens_purchased, to=ctx.caller) return tokens_purchased currency_reserve, token_reserve = reserves[contract] k = currency_reserve * token_reserve new_currency_reserve = currency_reserve + currency_amount new_token_reserve = k / new_currency_reserve tokens_purchased = token_reserve - new_token_reserve fee_percent = state["FEE_PERCENTAGE"] * discount[ ctx.caller] #Discount is applied here fee = tokens_purchased * fee_percent if token_fees is True: fee = fee * state["TOKEN_DISCOUNT"] rswp_k = currency_reserve * token_reserve rswp_new_token_reserve = token_reserve + fee rswp_new_currency_reserve = rswp_k / rswp_new_token_reserve rswp_currency_purchased = currency_reserve - rswp_new_currency_reserve # MINUS FEE rswp_currency_purchased += rswp_currency_purchased * fee_percent rswp_currency_reserve_2, rswp_token_reserve_2 = reserves[state[ "TOKEN_CONTRACT"]] #This converts fees in TAU to fees in RSWP rswp_k_2 = rswp_currency_reserve_2 * rswp_token_reserve_2 rswp_new_currency_reserve_2 = rswp_currency_reserve_2 + rswp_currency_purchased rswp_new_currency_reserve_2 += rswp_currency_purchased * fee_percent #Not 100% accurate, uses output currency instead of input currency rswp_new_token_reserve_2 = rswp_k_2 / rswp_new_currency_reserve_2 sell_amount = rswp_token_reserve_2 - rswp_new_token_reserve_2 #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"]) token_received = internal_buy(contract=contract, currency_amount=currency_received) new_token_reserve = decimal( new_token_reserve ) + token_received #This can probably be removed during production else: tokens_purchased = decimal(tokens_purchased) - fee burn_amount = internal_buy( contract=state["TOKEN_CONTRACT"], currency_amount=internal_sell(contract=contract, token_amount=fee - fee * state["BURN_PERCENTAGE"])) new_token_reserve = decimal( new_token_reserve) + fee * state["BURN_PERCENTAGE"] amm_token.transfer(amount=burn_amount, to=state["BURN_ADDRESS"]) #Burn here if minimum_received != None: assert tokens_purchased >= minimum_received, "Only {} tokens can be purchased, which is less than your minimum, which is {} tokens.".format( tokens_purchased, minimum_received) assert tokens_purchased > 0, 'Token reserve error!' currency.transfer_from(amount=currency_amount, to=ctx.this, main_account=ctx.caller) token.transfer(amount=tokens_purchased, to=ctx.caller) reserves[contract] = [new_currency_reserve, new_token_reserve] prices[contract] = new_currency_reserve / new_token_reserve return tokens_purchased