def subtract_ong(address, amount): byte_address = Base58ToAddress(address) assert (CheckWitness(byte_address)) if len(byte_address) != 20: Notify(['Invalid address']) return False if amount < 0: Notify(['Negative amount']) return False param = state(byte_address) if Invoke(0, contract_address_ONG, 'balanceOf', param) < amount: Notify(['Not enough ONG in wallet']) return False else: from_acct = byte_address to_acct = contract_address supply = Get(ctx, ONGKEY) supply += amount Put(ctx, ONGKEY, supply) Notify(['ONG supply increased by', amount]) params = state(from_acct, to_acct, amount) return Invoke(1, contract_address_ONG, 'transfer', [params])
def upgrade(code): """ upgrade current smart contract to new smart contract. :param code:new smart contract avm code. :return: True or raise exception. """ owner = getOwner() assert (CheckWitness(owner)) ongBalance = Invoke(0, ONG_ADDRESS, 'balanceOf', state(CONTRACT_ADDRESS)) res = Invoke(0, ONG_ADDRESS, "transfer", [state(CONTRACT_ADDRESS, owner, ongBalance)]) if res != b'\x01': assert (False) ontBalance = Invoke(0, ONT_ADDRESS, 'balanceOf', state(CONTRACT_ADDRESS)) res = Invoke(0, ONT_ADDRESS, "transfer", [state(CONTRACT_ADDRESS, owner, ontBalance)]) if res != b'\x01': assert (False) # upgrade smart contract res = Migrate(code, "", "", "", "", "", "") if not res: assert (False) Notify(["upgrade smart contract"]) return True
def upgrade(code): """ migrate data to new contract, transfer asset to admin address :param code: :return: """ assert CheckWitness(ADMIN) # transfer ont ont_balance = Invoke(VERSION, ONT_ADDRESS, "balanceOf", state(CONTRACT_ADDRESS)) ont_response = Invoke(VERSION, ONT_ADDRESS, "transfer", [state(CONTRACT_ADDRESS, ADMIN, ont_balance)]) if ont_response != INVOKE_SUCCESS: raise Exception("contract upgrade - transfer ont failed") # transfer ong ong_balance = Invoke(VERSION, ONG_ADDRESS, "balanceOf", state(CONTRACT_ADDRESS)) ong_response = Invoke(VERSION, ONG_ADDRESS, "transfer", [state(CONTRACT_ADDRESS, ADMIN, ong_balance)]) if ong_response != INVOKE_SUCCESS: raise Exception("contract upgrade - transfer ong failed") # migrate avm migrate_response = Migrate(code, "", "", "", "", "", "") if not migrate_response: raise Exception("contract upgrade - migrate avm failed") Notify(["upgrade", "success"]) return True
def upgrade(code, needStorage, name, version, author, email, description): """ upgrade current smart contract to new smart contract. :param code:new smart contract avm code. :return: True or raise exception. """ owner = getOwner() assert (CheckWitness(owner)) ontBalance = _getSelfONTBalance() if ontBalance > 0: res = Invoke(0, ONT_ADDRESS, "transfer", [state(CONTRACT_ADDRESS, owner, ontBalance)]) assert (res) assert (_tryUnboundOng()) ongBalance = _getSelfOngBalance() if ongBalance > 0: res = Invoke(0, ONG_ADDRESS, "transfer", [state(CONTRACT_ADDRESS, owner, ongBalance)]) assert (res) # upgrade smart contract res = Migrate(code, needStorage, name, version, author, email, description) if not res: assert (False) Notify(["upgrade", AddressFromVmCode(code)]) return True
def checkBalanceOf(ongFlag, account): param = state(ContractAddress) # do not use [param] res = Invoke(0, ONGAddress, 'balanceOf', param) Notify(["ContractAddress", ContractAddress, res]) param = state(account) if ongFlag == 1: Notify(["ongFlag", ongFlag]) res = Invoke(0, ONGAddress, 'balanceOf', param) else: Notify(["ongFlag", ongFlag]) res = Invoke(0, ONTAddress, 'balanceOf', param) Notify(["checkBalanceOf", account, res]) return res
def lock(toChainId, fromAddress, toAddress, amount): """ Decrease token supply from deducter address. :param amount: decreased token amount. :return: """ fee = 0 assert (amount >= 0) assert (CheckWitness(fromAddress)) assert (not isPaused()) # eth address format:0x673dfa9caf9145fdbef98e9d9874f36e63d8a5b4,length is 42 assert (len(toAddress) != 0) Put(ctx, concat(BALANCE_KEY, fromAddress), Sub(balanceOf(fromAddress), amount)) Put(ctx, TOTAL_SUPPLY_KEY, Sub(totalSupply(), amount)) # construct args for proxy contract in target chain toAssetHash = getAssetHash(toChainId) argsList = [toAddress, amount] input_bytes = _serialzieArgs(argsList) param = state(toChainId, toAssetHash, "unlock", input_bytes) assert (Invoke(0, CROSS_CHAIN_CONTRACT_ADDRESS, "createCrossChainTx", param)) LockEvent(toChainId, fromAddress, toAddress, amount) return True
def _tranferNativeAsset(_nativeAssetAddress, _from, _to, _amount): param = state(_from, _to, _amount) res = Invoke(0, _nativeAssetAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def _transferONGFromContact(toAcct, amount): param = state(ContractAddress, toAcct, amount) res = Invoke(0, ONGAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def VerifyCaller(operation, caller, keyNo): authContractAddr = bytearray( b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06' ) param = state(GetExecutingScriptHash(), caller, operation, keyNo) res = Invoke(0, authContractAddr, "verifyToken", param) Require(res)
def add_ong(address, amount): byte_address = Base58ToAddress(address) assert (CheckWitness(byte_address)) if len(byte_address) != 20: Notify(['Invalid address']) return False if amount < 0: Notify(['Negative amount']) return False supply = Get(ctx, ONGKEY) if supply < amount: Notify(['Not enough ONG in supply']) return False else: from_acct = contract_address to_acct = byte_address supply -= amount Put(ctx, ONGKEY, supply) Notify(['ONG supply decreased by', amount]) params = state(from_acct, to_acct, amount) return Invoke(1, contract_address_ONG, 'transfer', [params])
def lock(fromAssetHash, fromAddress, toChainId, toAddress, amount): """ Decrease token supply from deducter address. :param amount: decreased token amount. :return: """ assert (amount >= 0) assert (CheckWitness(fromAddress)) assert (len(toAddress) != 0) # transfer asset from fromAddress to lock proxy contract assert (_transferToContract(fromAssetHash, fromAddress, amount)) # construct args for proxy contract in target chain toAssetHash = getAssetHash(fromAssetHash, toChainId) # make sure the toAssetHash is not empty assert (len(toAssetHash) != 0) argsList = [toAssetHash, toAddress, amount] # invoke the native cross chain manager contract to make transaction to target chain inputArgs = _serialzieArgs(argsList) toProxyHash = getProxyHash(toChainId) # make sure the toProxyHash is not empty assert (len(toProxyHash) != 0) param = state(toChainId, toProxyHash, "unlock", inputArgs) assert (Invoke(0, CROSS_CHAIN_CONTRACT_ADDRESS, "createCrossChainTx", param)) LockEvent(fromAssetHash, fromAddress, toChainId, toAssetHash, toAddress, amount) return True
def view_ong(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the above checks hold, we can check the user's wallet. else: param = state(byte_address) ong_balance = Invoke(0, contract_address_ONG, 'balanceOf', param) if ong_balance == '': ong_balance = 0 Notify([ong_balance]) return ong_balance
def transferOntOng(from_acct, to_acct, ont, ong): param = state(from_acct, to_acct, ont) res = Invoke(0, OntContract, "transfer", [param]) if res != b'\x01': raise Exception("transfer ont error.") param = state(from_acct, to_acct, ong) Notify("transferONT succeed") res = Invoke(0, OngContract, "transfer", [param]) if res and res == b'\x01': Notify('transfer succeed') return True else: Notify('transfer failed') return False
def _transferONT(fromAcct, toAcct, amount): param = state(fromAcct, toAcct, amount) res = Invoke(0, ONTAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def _tryUnboundOng(): unboundOng = _getUnboundOngBalance() if unboundOng > 0: params = state(CONTRACT_ADDRESS, ONT_ADDRESS, CONTRACT_ADDRESS, _getUnboundOngBalance()) return Invoke(0, ONG_ADDRESS, 'transferFrom', params) return True
def deposit_ong(address, ong_amount): assert (CheckWitness(address)) assert (ong_amount >= 0) params = state(address, contract_address, ong_amount) res = Invoke(0, contract_address_ONG, 'transfer', [params]) assert (res) Notify(["depositONG", address, ong_amount]) return True
def withdrawOng(toAcct): param = state(ONTAddress, ContractAddress) unboundOngAmount = Invoke(0, ONGAddress, 'allowance', param) Notify(["unboundOngAmount", unboundOngAmount]) if unboundOngAmount > 0: unboundOngAmount = 147 params = state(ContractAddress, ONTAddress, toAcct, unboundOngAmount) res = Invoke(0, ONGAddress, "transferFrom", params) if res and res == b'\x01': Notify(["withdraw ong successful!"]) return True else: Notify(["withdraw ong failed!"]) return False else: Notify(["Not enough unboundOngAmount", unboundOngAmount]) return False
def checkWithdraw(account): allowanceOng = checkAllowance(account) withdrawOngAmount = allowanceOng / 2 params = state(account, ONTAddress, account, withdrawOngAmount) res = Invoke(0, ONGAddress, 'transferFrom', params) if res and res == b'\x01': Notify(["withdraw ong successful!"]) return True else: Notify(["withdraw ong failed!"]) return False
def makeState(fromacct, toacct, amount): """ make a tranfer state parameter currently due to the compiler problem, must be created as this format :param fromacct: :param toacct: :param amount: :return: """ return state(fromacct, toacct, amount)
def lock(fromAssetHash, fromAddress, toChainId, toAddress, amount, feeAmount): assert (amount >= 0) assert (feeAmount >= 0) assert (CheckWitness(fromAddress)) assert (len(toAddress) > 0) #call lock contract lock res = Invoke( 0, LOCKER_CONTRACT_ADDRESS, 'lock', state(fromAssetHash, fromAddress, toChainId, toAddress, amount)) assert (res == True) toAssethash = Invoke(0, LOCKER_CONTRACT_ADDRESS, 'getAssetHash', state(toChainId)) # transfer fee to fee collector res = Invoke(0, fromAssetHash, 'transfer', state(fromAddress, FEE_COLLECTOR_ADDRESS, feeAmount)) assert (res == True) WrapLockEvent(toChainId, toAssethash, fromAddress, fromAssetHash, toAddress, amount, feeAmount) return True
def transferOngToContract(account, ongAmount): selfContractAddress = GetExecutingScriptHash() Notify(["111_transferOngToContract", selfContractAddress]) param = state(account, selfContractAddress, ongAmount) ongContractAddress = ToScriptHash("AFmseVrdL9f9oyCzZefL9tG6UbvhfRZMHJ") res = Invoke(0, ongContractAddress, 'transfer', [param]) if res and res == b'\x01': Notify('transfer succeed') return True else: Notify('transfer failed') return False
def upgrade(code, needStorage, name, version, author, email, description): """ upgrade current smart contract to new smart contract. :param code:new smart contract avm code. :return: True or raise exception. """ assert (CheckWitness(Get(GetContext(), OPERATOR_PREFIX))) newContractHash = AddressFromVmCode(code) newContractAddr = bytearray_reverse(newContractHash) ontBalance = _getSelfONTBalance() if ontBalance > 0: res = Invoke(0, ONT_ADDRESS, "transfer", [state(CONTRACT_ADDRESS, newContractAddr, ontBalance)]) assert (res) assert (_tryUnboundOng()) ongBalance = _getSelfOngBalance() if ongBalance > 0: res = Invoke(0, ONG_ADDRESS, "transfer", [state(CONTRACT_ADDRESS, newContractAddr, ongBalance)]) assert (res) # transfer all the asset locked within lockproxy contract to the new contract fahListInfo = Get(GetContext(), FROM_ASSET_LIST_KEY) if len(fahListInfo) > 0: fahList = Deserialize(fahListInfo) for e in fahList: amount = getBalanceFor(e) if amount > 0: assert (_transferFromContract(e, newContractAddr, newContractAddr)) # upgrade smart contract res = Migrate(code, needStorage, name, version, author, email, description) assert (res) Notify(["upgrade smart contract"]) return True
def _transferNativeAsset(nativeAssetHash, fromAcct, toAcct, amount): """ transfer native asset, including ont or ong :param fromacct: :param toacct: :param amount: :return: """ param = state(fromAcct, toAcct, amount) res = Invoke(0, nativeAssetHash, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def _transferONG(fromAcct, toAcct, amount): """ transfer ONG :param fromacct: :param toacct: :param amount: :return: """ param = state(fromAcct, toAcct, amount) res = Invoke(0, ONGAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def main(): a = 1 d = 4 c = 3 b = 2 t = state(a, b, c, d) VaasAssert(t[0] == 1) VaasAssert(t[1] == 2) VaasAssert(t[2] == 3) VaasAssert(t[3] == 4 + 5) print(t[0]) print(t[1]) print(t[2]) print(t[3])
def _transferONT(fromAcct, toAcct, amount): """ transfer ONT :param fromacct: :param toacct: :param amount: :return: """ assert (CheckWitness(fromAcct)) param = state(fromAcct, toAcct, amount) res = Invoke(0, ONTAddress, 'transfer', [param]) if res and res == b'\x01': return True else: return False
def checkAllowance(account): # param = state(ONTAddress, account) # if ongFlag == 1: # Notify(["ongFlag", ongFlag]) # unboundOngAmount = Invoke(0, ONGAddress, 'allowance', param) # else: # Notify(["ongFlag", ongFlag]) # unboundOngAmount = Invoke(0, ONTAddress, 'allowance', param) # Notify(["checkAllowance", account, unboundOngAmount]) param = state(ONTAddress, account) unboundOngAmount = Invoke(0, ONGAddress, 'allowance', param) Notify(["checkAllowance", account, unboundOngAmount]) return unboundOngAmount
def transferFromNative(contractAddress, spender, fromAcct, toAcct, amount): """ transferFrom ONT/ONG :param spender: :param fromacct: :param toacct: :param amount: :return: """ param = state(spender, fromAcct, toAcct, amount) res = Invoke(0, contractAddress, 'transferFrom', param) if res and res == b'\x01': return True else: return False
def balanceOf1(acct): """ transfer ONG from contract :param acct: :return: """ # return ongContract("balanceOf", [acct]) # return True # ONT native contract address # contractAddress = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02') contractAddress = ToScriptHash("AFmseVrdL9f9oyCzZefL9tG6UbvhfRZMHJ") param = state(acct) res = Invoke(0, contractAddress, 'balanceOf', acct) return res
def transferONT(fromAcct, toAcct, ontAmount): """ transfer ONG :param fromacct: :param toacct: :param amount: :return: """ param = state(fromAcct, toAcct, ontAmount) res = Invoke(0, ONTAddress, 'transfer', [param]) if res and res == b'\x01': Notify(["transfer ONT successful!"]) return True else: Notify(["transfer ONT failed!"]) return False