def withdraw(account): """ account will withdraw his dividend and award to his own account :param account: :return: """ RequireWitness(account) updateDividendBalance(account) dividendBalance = getDividendBalance(account) awardBalance = getAwardBalance(account) referralBalance = getReferralBalance(account) assetToBeWithdrawn = Add(Add(dividendBalance, awardBalance), referralBalance) Require(assetToBeWithdrawn > 0) Require(transferONGFromContact(account, assetToBeWithdrawn)) # if assetToBeWithdrawn > 0: # if transferONGFromContact(account, assetToBeWithdrawn) == False: # Notify(["WithdrawError", "transfer ONG failed"]) # return False # else: # return False Delete(GetContext(), concatKey(TOTAL_DIVIDEND_OF_PREFIX, account)) Delete(GetContext(), concatKey(AWARD_BALANCE_OF_PREFFIX, account)) Delete(GetContext(), concatKey(REFERRAL_BALANCE_OF_PREFIX, account)) # update total ong amount Put(GetContext(), TOTAL_ONG_KEY, Sub(getTotalONGAmount(), assetToBeWithdrawn)) Notify(["withdraw", ContractAddress, account, assetToBeWithdrawn, GetTime()]) return True
def TransferFrom(sender, from_acct, to_acct, amount): if amount < 0: return False if CheckWitness(sender) == False: return False if len(to_acct) != 20: return False appoveKey = concat(concat(APPROVE_PREFIX, from_acct), sender) approvedAmount = Get(ctx, appoveKey) if approvedAmount < amount: return False if approvedAmount == amount: Delete(ctx, appoveKey) else: Put(ctx, appoveKey, approvedAmount - amount) fromKey = concat(TRANSFER_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) if fromBalance < amount: return False if fromBalance == amount: Delete(ctx, fromKey) else: Put(ctx, fromKey, fromBalance - amount) tokey = concat(TRANSFER_PREFIX, to_acct) toBalance = Get(ctx, tokey) Put(ctx, tokey, toBalance + amount) Notify(['transfer', from_acct, to_acct, amount]) return True
def withdraw(account): """ account will withdraw his dividend and award to his own account :param account: :return: """ RequireWitness(account) updateDividendBalance(account) dividendBalance = getDividendBalance(account) awardBalance = getAwardBalance(account) referralBalance = getReferralBalance(account) assetToBeWithdrawn = Add(Add(dividendBalance, awardBalance), referralBalance) if assetToBeWithdrawn > 0: Require(transferONGFromContact(account, assetToBeWithdrawn)) else: return True Delete(GetContext(), concatKey(TOTAL_DIVIDEND_OF_PREFIX, account)) Delete(GetContext(), concatKey(AWARD_BALANCE_OF_PREFFIX, account)) Delete(GetContext(), concatKey(REFERRAL_BALANCE_OF_PREFIX, account)) Put(GetContext(), concatKey(WITHDRAWN_BALANCEOF_PREFFIX, account), Add(assetToBeWithdrawn, getWithdrawnBalance(account))) Notify( ["withdraw", ContractAddress, account, assetToBeWithdrawn, GetTime()]) return True
def Reset(deleteBets): context = GetContext() Put(context, concat(BALANCE_KEY, OWNER_ADDRESS), INITIAL_OWNER_BALANCE) Delete(context, SETTLEMENT_BEGIN_TIMESTAMP_KEY) Delete(context, SETTLEMENT_END_TIMESTAMP_KEY) Delete(context, SETTLEMENT_COUNT_KEY) if deleteBets: count = Get(context, BETS_COUNT_KEY) if len(count) == 0: count = 0 i = 1 while i < count: betAddressKey = concat(BET_ADDRESS_KEY, i) Delete(context, betAddressKey) betAmountKey = concat(BET_AMOUNT_KEY, i) Delete(context, betAmountKey) betOutcomeKey = concat(BET_OUTCOME_KEY, i) Delete(context, betOutcomeKey) i += 1 Delete(context, BETS_COUNT_KEY) Delete(context, BET_BEGIN_TIMESTAMP_KEY) Delete(context, BET_END_TIMESTAMP_KEY) return True
def withdraw(account): RequireWitness(account) ongBalance = getOngBalanceOf(account) updateDividend(account) dividendBalance = getDividendBalanceOf(account) ongAmountToBeWithdraw = Add(ongBalance, dividendBalance) Require(ongAmountToBeWithdraw > 0) Require(_transferONGFromContact(account, ongAmountToBeWithdraw)) Delete(GetContext(), concatKey(ONG_BALANCE_KEY, account)) Delete(GetContext(), concatKey(DIVIDEND_BALANCE_KEY, account)) Notify(["withdraw", account, ongAmountToBeWithdraw]) return True
def withdraw(_account): """ Withdraw all the caller earning including dividends and referral bonus and the ong balance from selling the keys :param _account: :return: the withdrawn ong amount """ nouse = Require(CheckWitness(_account)) # put present dividend into dividend vault, update profit per token and dividend vault nouse = collectDividendOf(_account) # add dividend vault and referral balance together to get _dividends _dividends = dividendsOf(_account) # _account balance from selling keys _ongBalance = ongBalanceOf(_account) # add two together as earnings _accountEarnings = Add(_dividends, _ongBalance) # make sure _account has some earnings nouse = Require(_accountEarnings > 0) # transfer _dividends ( his ONG ) to _account params = state(selfContractAddr_, _account, _accountEarnings) res = Invoke(0, ONGContractAddress_, "transfer", [params]) if res and res == b'\x01': # emit event OnWithdraw(_account, _accountEarnings) else: raise Exception("withdraw ong error.") # Update dividend Delete(GetContext(), concatKey(_account, DIVIDEND_VAULT_SUFFIX)) # Update referral bonus Delete(GetContext(), concatKey(_account, REFERRAL_BALANCE_SUFFIX)) # # Update ong balance of _account # Delete(GetContext(), concatKey(_account, ONG_BALANCE_SUFFIX)) if balanceOf(_account) > 0: # update the _profitPerToken value after the _account withdraw to count his share till _profitPerToken later _profitPerToken = Get(GetContext(), PROFIT_PER_TOKEN_KEY) Put(GetContext(), concatKey(_account, PROFIT_PER_TOKEN_AFTER_SUFFIX), _profitPerToken) else: Delete(GetContext(), concatKey(_account, PROFIT_PER_TOKEN_AFTER_SUFFIX)) # Update withdrawn earnings ledger _newWithdrawnEarnings = Add(withdrawnEarnings(_account), _accountEarnings) Put(GetContext(), concatKey(_account, WITHDRAWN_EARNINGS_SUFFIX), _newWithdrawnEarnings) # Update ONG balance of this contract (need to be updated only when withdraw() is invoked) _totalOngBalance = Sub(totalOngBalance(), _accountEarnings) Put(GetContext(), TOTAL_ONG_KEY, _totalOngBalance) # Update ONG balance of this contract for key (no need to update since its done within sell method # Put(GetContext(), TOTAL_ONG_FOR_KEY_KEY, Add(totalOngForKey(), _ongBalance)) return _accountEarnings
def withdraw(account): RequireWitness(account) ongAmountToBeWithdraw = getOngBalanceOf(account) Require(_transferONGFromContact(account, ongAmountToBeWithdraw)) Delete(GetContext(), concatKey(ONG_BALANCE_KEY, account)) Notify(["withdraw", account, ongAmountToBeWithdraw]) return True
def bankerWithdrawDividend(account): # RequireWitness(account) if CheckWitness(account) == False: # bankerWithdrawDividend: Check witness failed!, Notify(["Error", 201]) return 0 # update the banker's dividend and earning updateBankerDividend(account) bankerDividend = getBankerDividend(account) # Require(bankerDividend > 0) if bankerDividend <= 0: # bankerWithdrawDividend: Banker has no dividend Notify(["noDividend", account]) return 0 # Require(_transferONGFromContact(account, bankerDividend)) res = _transferONGFromContact(account, bankerDividend) if res == False: # bankerWithdrawDividend: Transfer dividend to banker failed! Notify(["Error", 202]) return 0 Delete(GetContext(), concatKey(BANKER_DIVIDEND_BALANCE_PREFIX, account)) # update the total ong amount Put(GetContext(), TOTAL_ONG_KEY, Sub(getTotalONG(), bankerDividend)) Notify( ["bankerWithdrawDividend", getCurrentRound(), account, bankerDividend]) return bankerDividend
def takeOwnership(toAcct, tokenID): """ transfer the approved tokenId token to toAcct the invoker can be the owner or the approved account toAcct can be any address :param toAcct: the account that will be assigned as the new owner of tokenId :param tokenID: the tokenId token will be assigned to toAcct :return: False or True """ if len(toAcct) != 20: raise Exception("toAcct illegal!") tokenOwner = ownerOf(tokenID) if not tokenOwner: return False approveKey = concatkey(APPROVE_PREFIX, tokenID) approvedAcct = Get(ctx, approveKey) if CheckWitness(tokenOwner) != True and CheckWitness(approvedAcct) != True: return False Delete(ctx, approveKey) ownerKey = concatkey(OWNER_OF_TOKEN_PREFIX, tokenID) Put(ctx, ownerKey, toAcct) fromBalance = balanceOf(tokenOwner) toBalance = balanceOf(toAcct) # to avoid overflow if fromBalance >= 1 and toBalance < toBalance + 1: Put(ctx, concatkey(OWNER_BALANCE_PREFIX, tokenOwner), fromBalance - 1) Put(ctx, concatkey(OWNER_BALANCE_PREFIX, toAcct), toBalance + 1) Notify(['transfer', tokenOwner, toAcct, tokenID]) return True
def takeOwnership(toAcct, tokenID): """ take the approved token :param toAcct: spender :param tokenID: this tokenID should be approved by its owner to toAcct :return: False or True """ if CheckWitness(toAcct) == False: return False tokenOwner = ownerOf(tokenID) if not tokenOwner: return False approveKey = concatkey(APPROVE_PREFIX, tokenID) approvedAcct = Get(ctx, approveKey) if approvedAcct != toAcct: return False Delete(ctx, approveKey) ownerKey = concatkey(OWNER_OF_TOKEN_PREFIX, tokenID) Put(ctx, ownerKey, toAcct) fromBalance = balanceOf(tokenOwner) toBalance = balanceOf(toAcct) # to avoid overflow if fromBalance >= 1 and toBalance < toBalance + 1: Put(ctx, concatkey(OWNER_BALANCE_PREFIX, tokenOwner), fromBalance - 1) Put(ctx, concatkey(OWNER_BALANCE_PREFIX, toAcct), toBalance + 1) Notify(['transfer', tokenOwner, toAcct, tokenID]) return True
def transfer(toAcct, tokenID): """ transfer the token with tokenID to the toAcct :param toAcct: to account address :param tokenID: the unique token's ID, type should be ByteArray :return: False means failure, True means success. """ tokenOwner = ownerOf(tokenID) if CheckWitness(tokenOwner) == False: return False if len(toAcct) != 20: raise Exception('address length error!') ownerKey = concatkey(OWNER_OF_TOKEN_PREFIX, tokenID) fromAcct = Get(ctx, ownerKey) balanceKey = concatkey(OWNER_BALANCE_PREFIX, fromAcct) fromBalance = Get(ctx, balanceKey) if fromBalance >= 1: # decrease fromAccount token balance Put(ctx, balanceKey, fromBalance - 1) else: raise Exception('fromBalance error') # set the owner of tokenID to toAcct Put(ctx, ownerKey, toAcct) # increase toAccount token balance balanceKey = concatkey(OWNER_BALANCE_PREFIX, toAcct) Put(ctx, balanceKey, balanceOf(toAcct) + 1) Delete(ctx, concatkey(APPROVE_PREFIX, tokenID)) Notify(['transfer', fromAcct, toAcct, tokenID]) return True
def transfer(from_acct,to_acct,amount): """ Transfer amount of tokens from from_acct to to_acct :param from_acct: the account from which the amount of tokens will be transferred :param to_acct: the account to which the amount of tokens will be transferred :param amount: the amount of the tokens to be transferred, >= 0 :return: True means success, False or raising exception means failure. """ if len(to_acct) != 20 or len(from_acct) != 20: raise Exception("address length error") if CheckWitness(from_acct) == False or amount < 0: return False fromKey = concat(BALANCE_PREFIX,from_acct) fromBalance = Get(ctx,fromKey) if amount > fromBalance: return False if amount == fromBalance: Delete(ctx,fromKey) else: Put(ctx,fromKey,fromBalance - amount) toKey = concat(BALANCE_PREFIX,to_acct) toBalance = Get(ctx,toKey) Put(ctx,toKey,toBalance + amount) # Notify(["transfer", AddressToBase58(from_acct), AddressToBase58(to_acct), amount]) # TransferEvent(AddressToBase58(from_acct), AddressToBase58(to_acct), amount) Notify(['Transfer successful', from_acct, to_acct, amount]) TransferEvent(from_acct, to_acct, amount) return True
def Withdraw(address, amount): if len(address) != 20: return False if CheckWitness(address) != True: return False if amount % FACTOR != 0: ErrorNotify(ERROR_TONT_WITHDRAW) return False blakey = concat(BALANCE_PREFIX, address) fromBalance = Get(ctx, blakey) if amount > fromBalance: ErrorNotify(ERROR_BANLANCE) return False ontam = amount / FACTOR constracthash = GetExecutingScriptHash() if transferONT(constracthash, address, ontam): if amount == fromBalance: Delete(ctx, blakey) else: Put(ctx, blakey, fromBalance - amount) Notify(["success", ontam]) return True return False
def Transfer(from_acct, to_acct, amount): if from_acct == to_acct: return True if amount == 0: return True if amount < 0: return False if CheckWitness(from_acct) == False: return False if len(to_acct) != 20: return False fromKey = concat(TRANSFER_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) if fromBalance < amount: return False if fromBalance == amount: Delete(ctx, fromKey) else: Put(ctx, fromKey, fromBalance - amount) tokey = concat(TRANSFER_PREFIX, to_acct) toBalance = Get(ctx, tokey) Put(ctx, tokey, toBalance + amount) Notify(['transfer', from_acct, to_acct, amount]) return True
def main(op, args): if op == 'join': return join(args[0], args[1]) if op == 'start': pass if op == 'listen': return is_game_ready() if op == 'get_amounts': return get_amounts() if op == 'query': context = GetContext() value = Get(context, args[0]) if value is None: return 'no' return value if op == 'delete': context = GetContext() value = Get(context, args[0]) if value is None: return False Delete(context, args[0]) return True return False
def bankerWithdrawBeforeExit(account): if CheckWitness(account) == False: # bankerWithdrawBeforeExit: Check witness failed! Notify(["Error", 401]) return 0 ongShareInRunningVault = getRunVaultShare(account) if ongShareInRunningVault <= 0: # bankerWithdrawBeforeExit: banker's dividend is not greater than 0 Notify(["noShare", account]) return 0 currentRound = getCurrentRound() bankerBalanceInRunVault = getBankerBalanceInRunVault(currentRound, account) if getRoundGameStatus(currentRound) == STATUS_ON and bankerBalanceInRunVault > 0: # update the bankers' investment amount oldBankersInvestment = getBankersInvestment(currentRound) Put(GetContext(), concatKey(concatKey(ROUND_PREFIX, currentRound), BANKERS_INVESTMENT_KEY), Sub(oldBankersInvestment, getBankerInvestment(currentRound, account))) # delete the banker's investment balance Delete(GetContext(), concatKey(concatKey(ROUND_PREFIX, currentRound), concatKey(BANKER_INVEST_BALANCE_PREFIX, account))) Require(_transferONGFromContact(account, ongShareInRunningVault)) # update total ong Put(GetContext(), TOTAL_ONG_KEY, Sub(getTotalONG(), ongShareInRunningVault)) # update real time run vault Put(GetContext(), concatKey(concatKey(ROUND_PREFIX, currentRound), REAL_TIME_RUNNING_VAULT), Sub(getRealTimeRunningVault(currentRound), ongShareInRunningVault)) Notify(["bankerWithdrawShare", currentRound, account, ongShareInRunningVault]) return ongShareInRunningVault
def transfer(from_acct, to_acct, amount): """ Transfer amount of tokens from from_acct to to_acct :param from_acct: the account from which the amount of tokens will be transferred :param to_acct: the account to which the amount of tokens will be transferred :param amount: the amount of the tokens to be transferred, >= 0 :return: True means success, False or raising exception means failure. """ if len(to_acct) != 20 or len(from_acct) != 20: raise Exception("address length error") if CheckWitness(from_acct) == False: return False # This part is marked as commits since transferring of 0 MYT should fire event, too. # if from_acct == to_acct or amount == 0: # return True fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) if amount > fromBalance: return False if amount == fromBalance: Delete(ctx, fromKey) else: Put(ctx, fromKey, fromBalance - amount) toKey = concat(BALANCE_PREFIX, to_acct) toBalance = Get(ctx, toKey) Put(ctx, toKey, toBalance + amount) Notify(['transfer', from_acct, to_acct, amount]) return True
def TestStorage(): Put(ctx, "key", 100) v = Get(ctx, "key") Notify(v) Delete(ctx, "key") Notify(Get(ctx, "key"))
def transfer(fromAcct, toAcct, tokenId, amount): """ transfer amount of tokens in terms of tokenId token from fromAcct to the toAcct :param fromAcct: :param toAcct: :param tokenId: :param amount: :return: """ RequireWitness(fromAcct) Require(checkTokenId(tokenId)) RequireScriptHash(fromAcct) RequireScriptHash(toAcct) balanceKey = concatkey(tokenId, BALANCE) fromKey = concatkey(balanceKey, fromAcct) fromBalance = Get(GetContext(), fromKey) if amount > fromBalance: return False if amount == fromBalance: Delete(GetContext(), fromKey) else: Put(GetContext(), fromKey, fromBalance - amount) toKey = concatkey(balanceKey, toAcct) toBalance = Get(GetContext(), toKey) Put(GetContext(), toKey, toBalance + amount) # Notify(["transfer", fromAcct, toAcct, tokenId, amount]) TransferEvent(fromAcct, toAcct, tokenId, amount) return True
def bankerWithdrawEarning(account): # RequireWitness(account) if CheckWitness(account) == False: # bankerWithdrawEarning: Check witness failed!, Notify(["Error", 301]) return 0 updateBankerEarning(account) # update the banker's earning bankerEarning = getBankerEarning(account) # RequireWitness(bankerEarning > 0) if bankerEarning <= 0: # banker's dividend is not greater than 0 Notify(["noEarning", account]) return 0 # Require(_transferONGFromContact(account, bankerEarning)) res = _transferONGFromContact(account, bankerEarning) if res == False: # bankerWithdrawEarning: Transfer ONG failed! Notify(["Error", 302]) return 0 Delete(GetContext(), concatKey(BANKER_EARNING_BALANCE_PREFIX, account)) # update the total ong amount Put(GetContext(), TOTAL_ONG_KEY, Sub(getTotalONG(), bankerEarning)) Notify( ["bankerWithdrawEarning", getCurrentRound(), account, bankerEarning]) return bankerEarning
def transfer(from_acct,to_acct,amount): """ Transfer amount of tokens from from_acct to to_acct :param from_acct: the account from which the amount of tokens will be transferred :param to_acct: the account to which the amount of tokens will be transferred :param amount: the amount of the tokens to be transferred :return: True means success, False or raising exception means failure. """ if from_acct == to_acct or amount == 0: return True if CheckWitness(from_acct) == False: return False if len(to_acct) != 20 or len(from_acct) != 20: return False if amount < 0: return False fromKey = concat(BALANCE_PREFIX,from_acct) fromBalance = Get(ctx,fromKey) if amount > fromBalance: return False if amount == fromBalance: Delete(ctx,fromKey) else: Put(ctx,fromKey,fromBalance - amount) toKey = concat(BALANCE_PREFIX,to_acct) toBalance = Get(ctx,toKey) Put(ctx,toKey,toBalance + amount) Notify(['transfer',from_acct,to_acct,amount]) return True
def takeOwnership(toAcct, assetID): ''' take the approved asset :param toAcct: :param assetID: :return: ''' if CheckWitness(toAcct) == False: return False assetOwner = ownerOf(assetID) if not assetOwner: return False removeUserAsset(assetOwner, assetID) # key = concatkey(concatkey(APPROVE_PREFIX, assetOwner), assetID) key = concatkey(APPROVE_PREFIX, assetID) approver = Get(ctx, key) if approver != toAcct: return False Delete(ctx, key) ownerKey = concatkey(OWNER_OF_ASSET_PREFIX, assetID) Put(ctx, ownerKey, toAcct) newbalance = increaseOwnerBalance(toAcct) addUserAsset(toAcct, assetID, newbalance) Notify(['transfer', assetOwner, toAcct, assetID]) return True
def PlaceBet(address, amount, outcome): context = GetContext() #Get bets counter count = Get(context, "Count") if len(count) == 0: count = 0 #Save bet betAddressKey = concat(BET_ADDRESS_KEY, count) betAmountKey = concat(BET_AMOUNT_KEY, count) betOutcomeKey = concat(BET_OUTCOME_KEY, count) Put(context, betAddressKey, address) Put(context, betAmountKey, amount) Put(context, betOutcomeKey, outcome) OnBetPlaced(address, amount, outcome) #Update counter count += 1 Put(context, "Count", count) #Performance metrics date = currentTimestamp() firstBlockTime = Get(context, BET_BEGIN_TIMESTAMP_KEY) if firstBlockTime == None: Put(context, BET_BEGIN_TIMESTAMP_KEY, date) Delete(context, BET_END_TIMESTAMP_KEY) Put(context, BET_END_TIMESTAMP_KEY, date) return True
def testStorage(): Put(ctx, "key", 100) v = Get(ctx, "key") Notify(v) Delete(ctx, "key") Notify(Get(ctx, "key")) return True
def clear_item_list(ont_id): if not CheckWitness(ont_id): return False item_key = concat_key(ITEM_PREFIX, ont_id) item_list_info = Get(ctx, item_key) if item_list_info: Delete(ctx, item_key) return True return False
def withdrawGas(): """ Only admin can withdraw :return: """ RequireWitness(Admin) Require(transferONGFromContact(Admin, getGasVault())) Delete(GetContext(), GAS_VAULT_KEY) return True
def removeUserAsset(acct, assetID): balance = balanceOf(acct) key = concatkey(OWNER_BALANCE_PREFIX, acct) Put(ctx, key, balance - 1) for i in range(1, balance + 1): tmpkey = concatkey(concatkey(USER_ASSET_PREFIX, acct), i) tmpassetID = Get(ctx, tmpkey) if tmpassetID == assetID: if i == balance: Delete(ctx, tmpkey) else: lastkey = concatkey(concatkey(USER_ASSET_PREFIX, acct), balance) lastAssetID = Get(ctx, lastkey) Put(ctx, tmpkey, lastAssetID) Delete(ctx, lastkey) return True return True
def withdrawCommission(): RequireWitness(Admin) commissionAmountToBeWithdraw = getCommission() Require(_transferONGFromContact(Admin, commissionAmountToBeWithdraw)) # update commission amount Delete(GetContext(), COMMISSION_KEY) # update total ong amount Put(GetContext(), TOTAL_ONG_KEY, Sub(getTotalONG(), commissionAmountToBeWithdraw)) Notify(["withdrawCommission", commissionAmountToBeWithdraw]) return True
def Delete(domain): context = GetContext() occupy = Get(context, domain) if occupy == None: return False Delete(context, domain) Notify('Delete', domain) return True
def devWithdraw(devAddr): RequireWitness(devAddr) devShare = getDevProfit(devAddr) if devShare <= 0: return False Require(_transferONGFromContact(devAddr, devShare)) Delete(GetContext(), concatKey(DEV_PROFIT_PREFIX, devAddr)) Notify(["devWithdraw", devAddr, devAddr]) return True