def new_withdraw_ong_transaction(b58_claimer_address: str, b58_recv_address: str, amount: int, b58_payer_address: str, gas_limit: int, gas_price: int) -> Transaction: """ This interface is used to generate a Transaction object that allow one account to withdraw an amount of ong and transfer them to receive address. :param b58_claimer_address: a base58 encode address which is used to indicate who is the claimer. :param b58_recv_address: a base58 encode address which is used to indicate who receive the claimed ong. :param amount: the amount of asset that will be claimed. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which can be used for withdraw ong. """ if not isinstance(b58_claimer_address, str) or not isinstance( b58_recv_address, str) or not isinstance( b58_payer_address, str): raise SDKException( ErrorCode.param_err( 'the data type of base58 encode address should be the string.' )) if len(b58_claimer_address) != 34 or len( b58_recv_address) != 34 or len(b58_payer_address) != 34: raise SDKException( ErrorCode.param_err( 'the length of base58 encode address should be 34 bytes.')) if amount <= 0: raise SDKException( ErrorCode.other_error( 'the amount should be greater than than zero.')) if gas_price < 0: raise SDKException( ErrorCode.other_error( 'the gas price should be equal or greater than zero.')) if gas_limit < 0: raise SDKException( ErrorCode.other_error( 'the gas limit should be equal or greater than zero.')) ont_contract_address = util.get_asset_address('onyx') ong_contract_address = util.get_asset_address("oxg") args = { "sender": Address.b58decode(b58_claimer_address).to_array(), "from": ont_contract_address, "to": Address.b58decode(b58_recv_address).to_array(), "value": amount } invoke_code = build_native_invoke_code(ong_contract_address, bytes([0]), "transferFrom", args) unix_time_now = int(time()) payer_array = Address.b58decode(b58_payer_address).to_array() return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer_array, invoke_code, bytearray(), [], bytearray())
def new_withdraw_ong_transaction(claimer_addr: str, recv_addr: str, amount: int, payer_addr: str, gas_limit: int, gas_price: int) -> Transaction: ont_contract_address = util.get_asset_address("ont") ong_contract_address = util.get_asset_address("ong") args = {"sender": Address.b58decode(claimer_addr).to_array(), "from": ont_contract_address, "to": Address.b58decode(recv_addr).to_array(), "value": amount} invoke_code = build_native_invoke_code(ong_contract_address, bytes([0]), "transferFrom", args) unix_time_now = int(time()) return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, Address.b58decode(payer_addr).to_array(), invoke_code, bytearray(), [], bytearray())
def query_balance(self, asset: str, b58_address: str) -> int: """ This interface is used to query the account's ONT or ONG balance. :param asset: a string which is used to indicate which asset we want to check the balance. :param b58_address: a base58 encode account address. :return: account balance. """ raw_address = Address.b58decode(b58_address).to_array() contract_address = util.get_asset_address(asset) invoke_code = build_native_invoke_code(contract_address, bytes([0]), "balanceOf", raw_address) unix_time_now = int(time()) payer = Address(ZERO_ADDRESS).to_array() version = 0 tx_type = 0xd1 gas_price = 0 gas_limit = 0 attributes = bytearray() signers = list() hash_value = bytearray() tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, payer, invoke_code, attributes, signers, hash_value) balance = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) array = bytearray(binascii.a2b_hex(balance.encode('ascii'))) array.reverse() try: balance = int(binascii.b2a_hex(array).decode('ascii'), 16) except ValueError: balance = 0 return balance
def query_allowance(self, asset: str, b58_from_address: str, b58_to_address: str) -> int: """ :param asset: a string which is used to indicate which asset's allowance we want to get. :param b58_from_address: a base58 encode address which indicate where the allowance from. :param b58_to_address: a base58 encode address which indicate where the allowance to. :return: the amount of allowance in the from of int. """ contract_address = util.get_asset_address(asset) raw_from = Address.b58decode(b58_from_address).to_array() raw_to = Address.b58decode(b58_to_address).to_array() args = {"from": raw_from, "to": raw_to} invoke_code = build_native_invoke_code(contract_address, bytes([0]), "allowance", args) unix_time_now = int(time()) payer = Address(ZERO_ADDRESS).to_array() version = 0 tx_type = 0xd1 gas_price = 0 gas_limit = 0 attributes = bytearray() signers = list() hash_value = bytearray() tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, payer, invoke_code, attributes, signers, hash_value) allowance = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) array = bytearray(binascii.a2b_hex(allowance.encode('ascii'))) array.reverse() try: allowance = int(binascii.b2a_hex(array).decode('ascii'), 16) except ValueError: allowance = 0 return allowance
def get_allowance(self, addr): contract_address = get_asset_address("ont") rpc_struct = self.set_json_rpc_version( RPC_GET_ALLOWANCE, ["ong", Address((contract_address)).to_base58(), addr]) r = HttpRequest.request("post", self.addr, rpc_struct) res = json.loads(r.content.decode())["result"] return res
def query_decimals(self, asset: str): contract_address = util.get_asset_address(asset) invoke_code = build_native_invoke_code(contract_address, bytes([0]), "decimals", bytearray()) unix_timenow = int(time()) payer = Address(ZERO_ADDRESS).to_array() tx = Transaction(0, 0xd1, unix_timenow, 0, 0, payer, invoke_code, bytearray(), [], bytearray()) res = self.__sdk.rpc.send_raw_transaction_preexec(tx) return res
def new_transfer_transaction(self, gas_price, gas_limit, asset, from_addr, to_addr, amount): contract_address = util.get_asset_address(asset) # []bytes state = [{"from": from_addr, "to": to_addr, "amount": amount}] invoke_code = build_neo_vm.build_native_invoke_code( contract_address, bytes([0]), "transfer", state) unix_timenow = int(time()) return Transaction(0, 0xd1, unix_timenow, gas_price, gas_limit, bytearray(), invoke_code, bytearray(), [], bytearray())
def query_symbol(self, asset: str) -> str: contract_address = util.get_asset_address(asset) invoke_code = build_native_invoke_code(contract_address, bytes([0]), "symbol", bytearray()) unix_time_now = int(time()) payer = Address(ZERO_ADDRESS).to_array() tx = Transaction(0, 0xd1, unix_time_now, 0, 0, payer, invoke_code, bytearray(), [], bytearray()) res = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) return bytes.fromhex(res).decode()
def new_transfer_transaction(asset: str, b58_from_address: str, b58_to_address: str, amount: int, b58_payer_address: str, gas_limit: int, gas_price: int) -> Transaction: """ This interface is used to generate a Transaction object for transfer. :param asset: a string which is used to indicate which asset we want to transfer. :param b58_from_address: a base58 encode address which indicate where the asset from. :param b58_to_address: a base58 encode address which indicate where the asset to. :param amount: the amount of asset that will be transferred. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which can be used for transfer. """ if not isinstance(b58_from_address, str) or not isinstance( b58_to_address, str) or not isinstance(b58_payer_address, str): raise SDKException( ErrorCode.param_err( 'the data type of base58 encode address should be the string.' )) if len(b58_from_address) != 34 or len(b58_to_address) != 34 or len( b58_payer_address) != 34: raise SDKException( ErrorCode.param_err( 'the length of base58 encode address should be 34 bytes.')) if amount <= 0: raise SDKException( ErrorCode.other_error( 'the amount should be greater than than zero.')) if gas_price < 0: raise SDKException( ErrorCode.other_error( 'the gas price should be equal or greater than zero.')) if gas_limit < 0: raise SDKException( ErrorCode.other_error( 'the gas limit should be equal or greater than zero.')) contract_address = util.get_asset_address(asset) raw_from = Address.b58decode(b58_from_address).to_array() raw_to = Address.b58decode(b58_to_address).to_array() raw_payer = Address.b58decode(b58_payer_address).to_array() state = [{"from": raw_from, "to": raw_to, "amount": amount}] invoke_code = build_native_invoke_code(contract_address, bytes([0]), "transfer", state) unix_time_now = int(time()) version = 0 tx_type = 0xd1 attributes = bytearray() signers = list() hash_value = bytearray() return Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, raw_payer, invoke_code, attributes, signers, hash_value)
def query_balance(self, asset: str, addr: str): contract_address = util.get_asset_address(asset) invoke_code = build_native_invoke_code( contract_address, bytes([0]), "balanceOf", Address.decodeBase58(addr).to_array()) unix_timenow = int(time()) payer = Address(ZERO_ADDRESS).to_array() tx = Transaction(0, 0xd1, unix_timenow, 0, 0, payer, invoke_code, bytearray(), [], bytearray()) res = self.__sdk.rpc.send_raw_transaction_preexec(tx) return int(res, 16)
def new_approve(asset: str, send_addr: str, recv_addr: str, amount: int, payer: str, gas_limit: int, gas_price: int) -> Transaction: contract_address = util.get_asset_address(asset) # []bytes raw_send = Address.b58decode(send_addr).to_array() raw_recv = Address.b58decode(recv_addr).to_array() raw_payer = Address.b58decode(payer).to_array() args = {"from": raw_send, "to": raw_recv, "amount": amount} invoke_code = build_native_invoke_code(contract_address, bytes([0]), "approve", args) unix_time_now = int(time()) return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, raw_payer, invoke_code, bytearray(), [], bytearray())
def query_unbound_ong(self, base58_address: str) -> int: """ This interface is used to query the amount of account's unbound ong. :param base58_address: a base58 encode address which indicate which account's unbound ong we want to query. :return: the amount of unbound ong in the form of int. """ contract_address = util.get_asset_address('onyx') result = self.__sdk.rpc.get_allowance( "oxg", Address(contract_address).b58encode(), base58_address) return int(result)
def new_transfer_from(asset: str, send_addr: str, from_addr: str, recv_addr: str, amount: int, payer: str, gas_limit: int, gas_price: int) -> Transaction: raw_sender = Address.b58decode(send_addr).to_array() raw_from = Address.b58decode(from_addr).to_array() raw_to = Address.b58decode(recv_addr).to_array() raw_payer = Address.b58decode(payer).to_array() contract_address = util.get_asset_address(asset) args = {"sender": raw_sender, "from": raw_from, "to": raw_to, "amount": amount} invoke_code = build_native_invoke_code(contract_address, bytes([0]), "transferFrom", args) unix_time_now = int(time()) return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, raw_payer, invoke_code, bytearray(), [], bytearray())
def query_allowance(self, asset: str, from_addr: str, to_addr: str): contract_address = util.get_asset_address(asset) args = { "from": Address.decodeBase58(from_addr).to_array(), "to": Address.decodeBase58(to_addr).to_array() } invoke_code = build_native_invoke_code(contract_address, bytes([0]), "allowance", args) unix_timenow = int(time()) payer = Address(ZERO_ADDRESS).to_array() tx = Transaction(0, 0xd1, unix_timenow, 0, 0, payer, invoke_code, bytearray(), [], bytearray()) res = self.__sdk.rpc.send_raw_transaction_preexec(tx) return res
def query_allowance(self, asset: str, b58_from_address: str, b58_to_address: str) -> str: contract_address = util.get_asset_address(asset) raw_from = Address.b58decode(b58_from_address) raw_to = Address.b58decode(b58_to_address) args = {"from": raw_from, "to": raw_to} invoke_code = build_native_invoke_code(contract_address, bytes([0]), "allowance", args) unix_time_now = int(time()) payer = Address(ZERO_ADDRESS).to_array() tx = Transaction(0, 0xd1, unix_time_now, 0, 0, payer, invoke_code, bytearray(), [], bytearray()) res = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) return res
def new_transfer_transaction(asset: str, from_addr: str, to_addr: str, amount: int, payer: str, gas_limit: int, gas_price: int): contract_address = util.get_asset_address(asset) # []bytes raw_from = Address.b58decode(from_addr) raw_to = Address.b58decode(to_addr) raw_payer = Address.b58decode(payer) state = [{"from": raw_from, "to": raw_to, "amount": amount}] invoke_code = build_native_invoke_code(contract_address, bytes([0]), "transfer", state) unix_time_now = int(time()) return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, raw_payer, invoke_code, bytearray(), [], bytearray())
def new_approve_transaction(asset: str, b58_send_address: str, b58_recv_address: str, amount: int, b58_payer_address: str, gas_limit: int, gas_price: int) -> Transaction: """ This interface is used to generate a Transaction object for approve. :param asset: a string which is used to indicate which asset we want to approve. :param b58_send_address: a base58 encode address which indicate where the approve from. :param b58_recv_address: a base58 encode address which indicate where the approve to. :param amount: the amount of asset that will be approved. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which can be used for approve. """ if not isinstance(b58_send_address, str) or not isinstance( b58_recv_address, str): raise SDKException( ErrorCode.param_err( 'the data type of base58 encode address should be the string.' )) if len(b58_send_address) != 34 or len(b58_recv_address) != 34: raise SDKException( ErrorCode.param_err( 'the length of base58 encode address should be 34 bytes.')) if amount <= 0: raise SDKException( ErrorCode.other_error( 'the amount should be greater than than zero.')) if gas_price < 0: raise SDKException( ErrorCode.other_error( 'the gas price should be equal or greater than zero.')) if gas_limit < 0: raise SDKException( ErrorCode.other_error( 'the gas limit should be equal or greater than zero.')) contract_address = util.get_asset_address(asset) raw_send = Address.b58decode(b58_send_address).to_array() raw_recv = Address.b58decode(b58_recv_address).to_array() raw_payer = Address.b58decode(b58_payer_address).to_array() args = {"from": raw_send, "to": raw_recv, "amount": amount} invoke_code = build_native_invoke_code(contract_address, bytes([0]), "approve", args) unix_time_now = int(time()) return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, raw_payer, invoke_code, bytearray(), [], bytearray())
def new_transfer_transaction(asset: str, from_addr: str, to_addr: str, amount: int, payer: str, gas_limit: int, gas_price: int): contract_address = util.get_asset_address(asset) # []bytes state = [{ "from": Address.decodeBase58(from_addr).to_array(), "to": Address.decodeBase58(to_addr).to_array(), "amount": amount }] invoke_code = build_native_invoke_code(contract_address, bytes([0]), "transfer", state) unix_timenow = int(time()) return Transaction(0, 0xd1, unix_timenow, gas_price, gas_limit, Address.decodeBase58(payer).to_array(), invoke_code, bytearray(), [], bytearray())
def new_approve(self, asset: str, send_addr: str, recv_addr: str, amount: int, payer: str, gas_limit: int, gas_price: int) -> Transaction: contract_address = util.get_asset_address(asset) # []bytes args = { "from": Address.decodeBase58(send_addr).to_array(), "to": Address.decodeBase58(recv_addr).to_array(), "amount": amount } invoke_code = build_native_invoke_code(contract_address, bytes([0]), "approve", args) unix_timenow = int(time()) return Transaction(0, 0xd1, unix_timenow, gas_price, gas_limit, Address.decodeBase58(payer).to_array(), invoke_code, bytearray(), [], bytearray())
def new_transfer_transaction(asset: str, from_addr: str, to_addr: str, amount: int, payer: str, gas_limit: int, gas_price: int) -> Transaction: contract_address = util.get_asset_address(asset) raw_from = Address.b58decode(from_addr).to_array() raw_to = Address.b58decode(to_addr).to_array() raw_payer = Address.b58decode(payer).to_array() state = [{"from": raw_from, "to": raw_to, "amount": amount}] invoke_code = build_native_invoke_code(contract_address, bytes([0]), "transfer", state) unix_time_now = int(time()) version = 0 tx_type = 0xd1 attributes = bytearray() signers = list() hash_value = bytearray() return Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, raw_payer, invoke_code, attributes, signers, hash_value)
def query_decimals(self, asset: str) -> str: contract_address = util.get_asset_address(asset) method = 'decimals' invoke_code = build_native_invoke_code(contract_address, bytes([0]), method, bytearray()) unix_time_now = int(time()) payer = Address(ZERO_ADDRESS).to_array() version = 0 tx_type = 0xd1 gas_price = 0 gas_limit = 0 attributes = bytearray() signers = list() hash_value = bytearray() tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, payer, invoke_code, attributes, signers, hash_value) decimal = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) return decimal
def query_balance(self, asset: str, b58_address: str) -> int: raw_address = Address.b58decode(b58_address).to_array() contract_address = util.get_asset_address(asset) invoke_code = build_native_invoke_code(contract_address, bytes([0]), "balanceOf", raw_address) unix_time_now = int(time()) payer = Address(ZERO_ADDRESS).to_array() version = 0 tx_type = 0xd1 gas_price = 0 gas_limit = 0 attributes = bytearray() signers = list() hash_value = bytearray() tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, payer, invoke_code, attributes, signers, hash_value) res = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) r = bytearray.fromhex(res) r.reverse() return int(r.hex(), 16)
def query_allowance(self, asset: str, b58_from_address: str, b58_to_address: str) -> str: contract_address = util.get_asset_address(asset) raw_from = Address.b58decode(b58_from_address).to_array() raw_to = Address.b58decode(b58_to_address).to_array() args = {"from": raw_from, "to": raw_to} invoke_code = build_native_invoke_code(contract_address, bytes([0]), "allowance", args) unix_time_now = int(time()) payer = Address(ZERO_ADDRESS).to_array() version = 0 tx_type = 0xd1 gas_price = 0 gas_limit = 0 attributes = bytearray() signers = list() hash_value = bytearray() tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, payer, invoke_code, attributes, signers, hash_value) res = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) return res
def get_allowance(self, base58_address: str) -> str: """ This interface is used to get the the allowance from transfer-from account to transfer-to account in current network. Args: base58_address (str): a base58 encoded account address Return: the information of smart contract event in dictionary form. """ contract_address = get_asset_address("ont") b58_contract_address = Address(contract_address).b58encode() rpc_struct = RpcClient.set_json_rpc_version( RPC_GET_ALLOWANCE, ["ong", b58_contract_address, base58_address]) r = HttpRequest.request("post", self.addr, rpc_struct) allowance = json.loads(r.content.decode())["result"] return allowance
def new_transfer_from_transaction(asset: str, b58_send_address: str, b58_from_address: str, b58_recv_address: str, amount: int, b58_payer_address: str, gas_limit: int, gas_price: int) -> Transaction: """ This interface is used to generate a Transaction object that allow one account to transfer a amount of ONT or ONG Asset to another account, in the condition of the first account had been approved. :param asset: a string which is used to indicate which asset we want to transfer. :param b58_send_address: a base58 encode address which indicate where the asset from. :param b58_from_address: a base58 encode address which indicate where the asset from. :param b58_recv_address: a base58 encode address which indicate where the asset to. :param amount: the amount of asset that will be transferred. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which allow one account to transfer a amount of asset to another account. """ raw_sender = Address.b58decode(b58_send_address).to_array() raw_from = Address.b58decode(b58_from_address).to_array() raw_to = Address.b58decode(b58_recv_address).to_array() raw_payer = Address.b58decode(b58_payer_address).to_array() contract_address = util.get_asset_address(asset) args = { "sender": raw_sender, "from": raw_from, "to": raw_to, "amount": amount } invoke_code = build_native_invoke_code(contract_address, bytes([0]), "transferFrom", args) unix_time_now = int(time()) return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, raw_payer, invoke_code, bytearray(), [], bytearray())
def query_decimals(self, asset: str) -> int: """ This interface is used to query the asset's decimals of ONT or ONG. :param asset: a string which is used to indicate which asset's decimals we want to get :return: asset's decimals in the form of int """ contract_address = util.get_asset_address(asset) method = 'decimals' invoke_code = build_native_invoke_code(contract_address, bytes([0]), method, bytearray()) unix_time_now = int(time()) payer = Address(ZERO_ADDRESS).to_array() version = 0 tx_type = 0xd1 gas_price = 0 gas_limit = 0 attributes = bytearray() signers = list() hash_value = bytearray() tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, payer, invoke_code, attributes, signers, hash_value) decimal = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) return int(decimal)
def unbound_ong(self, base58_address: str) -> str: contract_address = util.get_asset_address("ont") return self.__sdk.rpc.get_allowance("ong", Address(contract_address).b58encode(), base58_address)