def burnToken(account, tokenId, amount): Require(_whenNotPaused()) assert (_onlyCLevel() or isAuthorizedLevel(account)) # make sure the tokenId has been created already Require(_tokenExist(tokenId)) # make sure the amount is legal, which is greater than ZERO balance = balanceOf(account, tokenId) #assert (amount > 0 and amount <= balance) Require(amount <= balance) Require(amount > 0) # update the to account balance if amount == balance: Delete(GetContext(), _concatkey(_concatkey(BALANCE_PREFIX, tokenId), account)) else: Put(GetContext(), _concatkey(_concatkey(BALANCE_PREFIX, tokenId), account), Sub(balance, amount)) # update the total supply _totalSupply = totalSupply(tokenId) if _totalSupply == amount: Delete(GetContext(), _concatkey(TOTAL_SUPPLY_PREFIX, tokenId)) else: Put(GetContext(), _concatkey(TOTAL_SUPPLY_PREFIX, tokenId), Sub(_totalSupply, amount)) # Notify the event to the block chain TransferEvent(account, "", tokenId, amount) return True
def deleteDomain(fulldomain, idx): ''' delete domain ''' assert (len(fulldomain) > 0) #domain is exist owner = ownerOf(fulldomain) assert (owner) lowerdomain = lower(fulldomain) _checkParentAuth(lowerdomain, idx) Delete(ctx, _concatkey(OWNER_KEY, lowerdomain)) Delete(ctx, _concatkey(VALID_KEY, lowerdomain)) Delete(ctx, _concatkey(VALUE_KEY, lowerdomain)) recordkey = _concatkey(RECORDS_KEY, owner) records = Deserialize(Get(ctx, recordkey)) records = list_remove_elt(records, lowerdomain) if len(records) == 0: Delete(ctx, recordkey) else: Put(ctx, recordkey, Serialize(records)) DeleteDomainEvent(lowerdomain) return True
def transferFrom(spender, from_acct, to_acct, amount): """ spender spends amount of tokens on the behalf of from_acct, spender makes a transaction of amount of tokens from from_acct to to_acct :param spender: :param from_acct: :param to_acct: :param amount: :return: """ assert (len(spender) == 20 and len(from_acct) == 20 and len(to_acct) == 20) assert (CheckWitness(spender)) fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = Get(GetContext(), fromKey) assert (amount <= fromBalance and amount >= 0) approveKey = concat(concat(APPROVE_PREFIX, from_acct), spender) approvedAmount = Get(GetContext(), approveKey) toKey = concat(BALANCE_PREFIX, to_acct) assert (amount <= approvedAmount) if amount == approvedAmount: Delete(GetContext(), approveKey) Put(GetContext(), fromKey, fromBalance - amount) else: Put(GetContext(), approveKey, approvedAmount - amount) Put(GetContext(), fromKey, fromBalance - amount) toBalance = Get(GetContext(), toKey) Put(GetContext(), toKey, toBalance + amount) TransferEvent(from_acct, to_acct, amount) return True
def transfer(from_acct, to_acct, amount): if not CheckWitness(from_acct): return False fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) #添加判断条件 if amount < 0: return False if amount > fromBalance: return False # 检测转账余额是否满足要求 VaasAssert(amount >= 0) VaasAssert(fromBalance >= amount) 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) TransferEvent(from_acct, to_acct, amount) return True
def transferFrom(spender, from_acct, to_acct, amount): """ spender spends amount of tokens on the behalf of from_acct, spender makes a transaction of amount of tokens from from_acct to to_acct """ if len(spender) != 20 or len(from_acct) != 20 or len(to_acct) != 20: raise Exception("address length error") if CheckWitness(spender) == False: return False fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) if amount > fromBalance or amount < 0: return False approveKey = concat(concat(APPROVE_PREFIX, from_acct), spender) approvedAmount = Get(ctx, approveKey) toKey = concat(BALANCE_PREFIX, to_acct) if amount > approvedAmount: return False elif amount == approvedAmount: Delete(ctx, approveKey) Put(ctx, fromKey, fromBalance - amount) else: Put(ctx, approveKey, approvedAmount - amount) Put(ctx, fromKey, fromBalance - amount) toBalance = Get(ctx, toKey) Put(ctx, toKey, toBalance + amount) TransferEvent(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(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 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, concatkey(APPROVE_PREFIX, tokenID)) 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(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: raise Exception("Check Witness fail") fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) if amount > fromBalance: raise Exception("amount > fromBalance") 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) TransferEvent(from_acct, to_acct, amount)
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. """ assert (len(to_acct) == 20) assert (len(from_acct) == 20) assert (CheckWitness(from_acct)) assert (amount > 0) fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) assert (fromBalance >= amount) 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) TransferEvent(from_acct, to_acct, amount) return True
def transfer(fulldomain, idx, todid): ''' transfer domain to other did ''' assert (len(fulldomain) > 0) lowerdomain = lower(fulldomain) assert (isDomainValid(lowerdomain)) owner = ownerOf(lowerdomain) assert (_verifyOntid(owner, idx)) Put(ctx, mulconcat(OWNER_KEY, lowerdomain), todid) fromrecordkey = _concatkey(RECORDS_KEY, owner) fromrecords = Deserialize(Get(ctx, fromrecordkey)) fromrecords = list_remove_elt(fromrecords, lowerdomain) if len(fromrecords) == 0: Delete(ctx, fromrecordkey) else: Put(ctx, fromrecordkey, Serialize(fromrecords)) torecordkey = _concatkey(RECORDS_KEY, todid) torecordsRaw = Get(ctx, torecordkey) if not torecordsRaw: torecords = [lowerdomain] Put(ctx, torecordkey, Serialize(torecords)) else: torecords = Deserialize(torecordsRaw) torecords.append(lowerdomain) assert (len(torecords) <= MAX_COUNT) Put(ctx, torecordkey, Serialize(torecords)) TransferEvent(fulldomain, owner, todid) return True
def transfer(from_acct, to_acct, amount): if not CheckWitness(from_acct): return False Require(amount > 0) 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) """ BALANCE_PREFIX is Substring of APPROVE_PREFIX The "tokey" can be _BALANCE_APPROVE... """ #Data-Injection-Attack Put(ctx, toKey, toBalance + amount) TransferEvent(from_acct, to_acct, amount) return True
def unlock(account, idx): KEY_lockCount = concat(USER_LOCK_CNT_PREFIX, account) lockCount = Get(ctx, KEY_lockCount) # idx is smaller than count of lock # require(idx<lockCount,"idx out of range") if idx >= lockCount: return False # Get lock info and releaseAmount KEY_lockInfo = concat(concat(USER_LOCK_PREFIX, account), idx) serializedLockinfo = Get(ctx, KEY_lockInfo) lockInfo = Deserialize(serializedLockinfo) releaseAmount = lockInfo['releaseAmount'] releaseTime = lockInfo['releaseTime'] # releaseAmount is Added to balance balance = Get(ctx, concat(BALANCE_PREFIX, account)) Put(ctx, concat(BALANCE_PREFIX, account), balance + releaseAmount) # last lockinfo copy to this idx lastLockInfo = Get( ctx, concat(concat(USER_LOCK_PREFIX, account), lockCount - 1)) Put(ctx, KEY_lockInfo, lastLockInfo) # delete last lockinfo (duplicated) Delete(ctx, concat(concat(USER_LOCK_PREFIX, account), lockCount - 1)) # decrease lockup count Put(ctx, KEY_lockCount, lockCount - 1) Notify([releaseTime, releaseAmount]) return True
def transferFrom(spender, from_acct, to_acct, amount): if not CheckWitness(spender): return False Require(amount > 0) fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) if amount > fromBalance: return False approveKey = concat(concat(APPROVE_PREFIX, from_acct), spender) approvedAmount = Get(ctx, approveKey) if amount > approvedAmount: return False toKey = concat(BALANCE_PREFIX, to_acct) toBalance = Get(ctx, toKey) if amount == approvedAmount: Delete(ctx, approveKey) Put(ctx, fromKey, fromBalance - amount) else: Put(ctx, approveKey, approvedAmount - amount) Put(ctx, fromKey, fromBalance - amount) Put(ctx, toKey, toBalance + amount) TransferEvent(from_acct, to_acct, amount) return True
def unbind(ont_id, bucket): """ unbind ont id with address :param ont_id: :param bucket: bucket id or url :return: """ assert CheckWitness(ADMIN) bound_bucket = Get(ctx, concat(KEY_ONT, ont_id)) if not bound_bucket: raise Exception("ont id bind with nothing") assert bound_bucket == bucket bind_data = Get(ctx, concat(KEY_BUCKET, bucket)) bind_map = Deserialize(bind_data) bind_map.remove(ont_id) Put(ctx, concat(KEY_BUCKET, bucket), Serialize(bind_map)) Delete(ctx, concat(KEY_ONT, ont_id)) Notify(["unbind", ont_id, bucket]) return True
def removeProperty(removeList): """ :param removeList: [DNA1, DNA2] :return: bool """ DNACheck = removeList[0] account = Get(context, concatKey(DNA_PRE_KEY, DNACheck)) RequireScriptHash(account) RequireWitness(account) DNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account)) if DNAlist: DNAlist = Deserialize(DNAlist) else: raise Exception("NO DNA") removeListLen = len(removeList) removeListIndex = 0 while removeListIndex < removeListLen: DNA = removeList[removeListIndex] findInList = _findInList(DNA, DNAlist) if findInList >= 0: Delete(context, concatKey(DNA_PRE_KEY, DNA)) DNAlist.remove(findInList) else: raise Exception("Not found DNA to be removed") removeListIndex += 1 Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account), Serialize(DNAlist)) Notify(["Remove property successfully"]) return True
def transferFrom(spender, from_acct, to_acct, amount): """ spender spends amount of tokens on the behalf of from_acct, spender makes a transaction of amount of tokens from from_acct to to_acct :param spender: :param from_acct: :param to_acct: :param amount: :return: """ assert (amount > 0) assert (isAddress(spender) and isAddress(from_acct) and isAddress(to_acct)) assert (CheckWitness(spender)) fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = balanceOf(from_acct) assert (fromBalance >= amount) approveKey = concat(concat(APPROVE_PREFIX, from_acct), spender) approvedAmount = Get(ctx, approveKey) if amount > approvedAmount: return False elif amount == approvedAmount: Delete(ctx, approveKey) Put(ctx, fromKey, Sub(fromBalance, amount)) else: Put(ctx, approveKey, Sub(approvedAmount, amount)) Put(ctx, fromKey, Sub(fromBalance, amount)) toBalance = balanceOf(to_acct) Put(ctx, concat(BALANCE_PREFIX, to_acct), Add(toBalance, amount)) TransferFromEvent(spender, from_acct, to_acct, amount) 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,Sub(fromBalance, amount)) toKey = concat(BALANCE_PREFIX, to_acct) toBalance = Get(ctx, toKey) Put(ctx,toKey,Add(toBalance, amount)) TransferEvent(from_acct, to_acct, amount) return True
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 transfer(from_address, to_address, amount): """ Transfers an amount of tokens from from_acct to to_acct :param from_address: The address sending the tokens :param to_address: The address receiving the tokens :param amount: The amount being transferred Returns True on success, otherwise raises an exception """ RequireIsAddress(from_address) RequireIsAddress(to_address) RequireWitness(from_address) Require(amount >= 0) fromKey = getBalanceKey(from_address) fromBalance = Get(ctx, fromKey) Require(fromBalance >= amount) if amount == fromBalance: Delete(ctx, fromKey) else: Put(ctx, fromKey, fromBalance - amount) toKey = getBalanceKey(to_address) toBalance = Get(ctx, toKey) Put(ctx, toKey, toBalance + amount) TransferEvent(from_address, to_address, amount) 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. """ require(len(to_acct) == 20 and len(from_acct) == 20, "address length error") require(CheckWitness(from_acct) == True, "Invalid invoker") require(amount > 0, "Invalid Amount") whenNotPaused() requireNotFreeze(from_acct) requireNotFreeze(to_acct) fromKey = concat(BALANCE_PREFIX,from_acct) fromBalance = Get(ctx,fromKey) require(amount <= fromBalance, "Not enough balance") 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) TransferEvent(from_acct, to_acct, amount) 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. """ assert (not isPaused()) assert (amount > 0) assert (isAddress(to_acct)) assert (CheckWitness(from_acct)) fromKey = concat(BALANCE_KEY, from_acct) fromBalance = balanceOf(from_acct) if amount > fromBalance: return False if amount == fromBalance: Delete(ctx, fromKey) else: Put(ctx, fromKey, Sub(fromBalance, amount)) toKey = concat(BALANCE_KEY, to_acct) toBalance = balanceOf(to_acct) Put(ctx, toKey, Add(toBalance, amount)) TransferEvent(from_acct, to_acct, amount) return True
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 or amount <= 0: return False if amount == fromBalance: Delete(GetContext(), fromKey) else: Put(GetContext(), fromKey, Sub(fromBalance, amount)) toKey = concatkey(balanceKey, toAcct) toBalance = Get(GetContext(), toKey) Put(GetContext(), toKey, Add(toBalance, amount)) TransferEvent(fromAcct, toAcct, tokenId, amount) return True
def cancelChainlinkRequest(sender, requestId, payment, callbackFunctionId, expiration): RequireWitness(sender) oracle = Get(GetContext(), concatKey(PENDING_REQUESTS_PREFIX, requestId)) params = [sender, requestId, payment, GetCallingScriptHash(), callbackFunctionId, expiration] assert (DynamicCallFunction(bytearray_reverse(oracle), "cancelOracleRequest", params)) Delete(GetContext(), concatKey(PENDING_REQUESTS_PREFIX, requestId)) ChainlinkCancelledEvent(requestId) return True
def removeAuthorizedLevel(account): RequireWitness(CEOAddress) Require(len(account) == 20) # make sure account is authorized before. Require(isAuthorizedLevel(account)) Delete(GetContext(), _concatkey(AUTHORIZED_LEVEL_PREFIX, account)) Notify(["removeAuthorizedLevel", account]) 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, devShare]) return True
def _transfer(_from, _to, _amount): fromKey = concat(BALANCE_PREFIX, _from) fromBalance = Get(GetContext(), fromKey) assert (_amount <= fromBalance) if _amount == fromBalance: Delete(GetContext(), concat(BALANCE_PREFIX, _from)) else: Put(GetContext(), fromKey, fromBalance - _amount) Put(GetContext(), concat(BALANCE_PREFIX, _to), balanceOf(_to) + _amount) TransferEvent(_from, _to, _amount) return True
def clean_user_name(user_address): if len(user_address) == 34: user_address = Base58ToAddress(user_address) assert (len(user_address) == 20 and user_address != ZERO_ADDRESS) assert (CheckWitness(ADMIN)) if not Get(ctx, concat(user_address, USER_KEY)): raise Exception("username not exist") Delete(ctx, concat(user_address, USER_KEY)) cleanUserNameEvent(user_address) return True
def DoTransfer(t_from, t_to, amount): """ Method to transfer NEP5 tokens of a specified amount from one account to another :param t_from: the address to transfer from :type t_from: bytearray :param t_to: the address to transfer to :type t_to: bytearray :param amount: the amount of NEP5 tokens to transfer :type amount: int :return: whether the transfer was successful :rtype: bool """ if amount <= 0: ##Log("Cannot transfer negative amount") return False from_is_sender = CheckWitness(t_from) if not from_is_sender: ##Log("Not owner of funds to be transferred") return False if t_from == t_to: ##Log("Sending funds to self") return True context = GetContext() from_val = Get(context, t_from) if from_val < amount: ##Log("Insufficient funds to transfer") return False if from_val == amount: Delete(context, t_from) else: difference = from_val - amount Put(context, t_from, difference) to_value = Get(context, t_to) to_total = to_value + amount Put(context, t_to, to_total) DispatchTransferEvent(t_from, t_to, amount) return True
def transferFrom(spender, from_address, to_address, amount): """ The spender address sends amount of tokens from the from_address to the to_address :param spender: The address sending the funds :param from_address: The address whose funds are being sent :param to_address: The receiving address :param amount: The amounts of tokens being transferred Returns True on success, otherwise raises an exception """ RequireIsAddress(spender) RequireIsAddress(from_address) RequireIsAddress(to_address) RequireWitness(spender) Require(amount >= 0) fromKey = getBalanceKey(from_address) fromBalance = Get(ctx, fromKey) Require(amount <= fromBalance) approveKey = getApprovalKey(from_address, spender) approvedAmount = Get(ctx, approveKey) Require(amount <= approvedAmount) if amount == approvedAmount: Delete(ctx, approveKey) else: Put(ctx, approveKey, approvedAmount - amount) if amount == fromBalance: Delete(ctx, fromKey) else: Put(ctx, fromKey, fromBalance - amount) toKey = getBalanceKey(to_address) toBalance = Get(ctx, toKey) Put(ctx, toKey, toBalance + amount) TransferEvent(from_address, to_address, amount) return True