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 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): 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_add_rcovery_transaction(self, ontid: str, pubkey: bytes, recovery: str, payer: str, gas_limit: int, gas_price: int): contract_address = ONTID_CONTRACT_ADDRESS args = { "ontid": ontid.encode(), "recovery": Address.decodeBase58(recovery).to_array(), "pubkey": pubkey } invoke_code = build_vm.build_native_invoke_code( contract_address, bytes([0]), "addRecovery", args) print("invoke_code", invoke_code.hex()) 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_withdraw_ong_transaction(self, claimer_addr: str, recv_addr: str, amount: int, payer_addr: str, gas_limit: int, gas_price: int): ont_contract_address = util.get_asset_address("ont") ong_contract_address = util.get_asset_address("ong") args = { "sender": Address.decodeBase58(claimer_addr).to_array(), "from": ont_contract_address, "to": Address.decodeBase58(recv_addr).to_array(), "value": amount } invoke_code = build_native_invoke_code(ong_contract_address, bytes([0]), "transferFrom", args) unix_timenow = int(time()) return Transaction(0, 0xd1, unix_timenow, gas_price, gas_limit, Address.decodeBase58(payer_addr).to_array(), invoke_code, bytearray(), [], bytearray())
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_registry_ontid_transaction(self, ontid: str, pubkey: str, payer: str, gas_limit: int, gas_price: int): contract_address = ONTID_CONTRACT_ADDRESS args = {"ontid": ontid.encode(), "pubkey": pubkey} invoke_code = build_vm.build_native_invoke_code( contract_address, bytes([0]), "regIDWithPublicKey", 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_add_attribute_transaction(self, ontid: str, pubkey: str, attris: list, payer: str, gas_limit: int, gas_price: int): contract_address = ONTID_CONTRACT_ADDRESS args = {"ontid": ontid.encode(), "length": len(attris)} for i in range(len(attris)): args["key" + str(i)] = bytes(attris[i]["key"].encode()) args["type" + str(i)] = bytes(attris[i]["type"].encode()) args["value" + str(i)] = bytes(attris[i]["value"].encode()) args["pubkey"] = pubkey invoke_code = build_vm.build_native_invoke_code( contract_address, bytes([0]), "addAttributes", args) unix_timenow = int(time()) return Transaction(0, 0xd1, unix_timenow, gas_price, gas_limit, Address.decodeBase58(payer).to_array(), invoke_code, bytearray(), [], bytearray())
sig_data = self.sign_to_data(tx_hash, signer) sig = [Sig([signer.get_public_key()], 1, [sig_data])] tx.sigs = sig return tx def sign_to_data(self, data, signer: Account): signed_data = signer.generateSignature(data, SignatureScheme.SHA256withECDSA) return signed_data def send_raw_transaction(self, tx): buf = tx.serialize() tx_data = buf.hex() rpc_struct = self.set_json_rpc_version(RPC_SEND_TRANSACTION, [tx_data]) print(rpc_struct) r = HttpRequest.request("post", self.addr, rpc_struct) print(r.content.decode()) res = json.loads(r.content.decode())["result"] return res if __name__ == '__main__': cli = RpcClient(0, rpc_address) private_key = "523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f" acc = Account(private_key, KeyType.ECDSA) print(acc.get_address_base58()) print(acc.get_public_key().hex()) toAddr = Address.decodeBase58("AKFMnJT1u5pyPhzGRuauD1KkyUvqjQsmGs") print(toAddr.to_array().hex()) res = cli.transfer(500, 20000, "ont", acc, toAddr.to_array(), 1)