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 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) return True
def _regis(addr): uIndex=userIdIndex() uIndex+=1 addr58=AddressToBase58(addr) user=User(0,'',[],0,addr58) saveUserIdIndex(uIndex) saveIndexToUser(uIndex,user) saveAddrToUserId(addr,uIndex) return uIndex
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: """ 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: return False approveKey = concat(concat(APPROVE_PREFIX,from_acct),spender) approvedAmount = Get(ctx,approveKey) toKey = concat(BALANCE_PREFIX,to_acct) toBalance = Get(ctx, toKey) if amount > approvedAmount: return False elif amount == approvedAmount: Delete(ctx,approveKey) Delete(ctx, fromBalance - amount) else: Put(ctx,approveKey,approvedAmount - amount) Put(ctx, fromKey, fromBalance - amount) Put(ctx, toKey, toBalance + amount) # Notify(["transfer", AddressToBase58(from_acct), AddressToBase58(to_acct), amount]) TransferEvent(AddressToBase58(from_acct), AddressToBase58(to_acct), amount) return True
def approve(owner,spender,amount): """ owner allow spender to spend amount of token from owner account Note here, the amount should be less than the balance of owner right now. :param owner: :param spender: :param amount: amount>=0 :return: True means success, False or raising exception means failure. """ if len(spender) != 20 or len(owner) != 20: raise Exception("address length error") if CheckWitness(owner) == False: return False if amount > balanceOf(owner): return False key = concat(concat(APPROVE_PREFIX,owner),spender) Put(ctx, key, amount) # Notify(["approval", AddressToBase58(owner), AddressToBase58(spender), amount]) ApprovalEvent(AddressToBase58(owner), AddressToBase58(spender), 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: 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) fromBase58 = AddressToBase58(from_acct) toBase58 = AddressToBase58(to_acct) Notify(['transfer', fromBase58, toBase58, amount]) # Notify(['transfer', from_acct, to_acct, amount]) return True
def init(): """ initialize the contract, put some important info into the storage in the blockchain :return: """ if Get(ctx, SUPPLY_KEY): Notify('Already initialized!') return False else: total = TOTAL_AMOUNT * FACTOR Put(ctx, SUPPLY_KEY, total) Put(ctx, concat(BALANCE_PREFIX, OWNER), total) ownerBase58 = AddressToBase58(OWNER) Notify(['transfer', '', OWNER, total]) return True
def init(): """ initialize the contract, put some important info into the storage in the blockchain :return: """ if Get(ctx,SUPPLY_KEY): Notify("Already initialized!") return False else: total = TOTAL_AMOUNT * FACTOR Put(ctx,SUPPLY_KEY,total) Put(ctx,concat(BALANCE_PREFIX,OWNER),total) # Notify(["transfer", "", Base58ToAddress(OWNER), total]) ownerBase58 = AddressToBase58(OWNER) TransferEvent("", ownerBase58, total) return True