def tx_call(self, keypair, function, arg, amount=10, gas=CONTRACT_DEFAULT_GAS, gas_price=CONTRACT_DEFAULT_GAS_PRICE, fee=DEFAULT_FEE, vm_version=CONTRACT_DEFAULT_VM_VERSION, tx_ttl=DEFAULT_TX_TTL): """Call a sophia contract""" if not utils.is_valid_hash(self.address, prefix="ct"): raise ValueError("Missing contract id") try: call_data = self.encode_calldata(function, arg) txb = TxBuilder(self.client, keypair) tx, sg, tx_hash = txb.tx_contract_call(self.address, call_data, function, arg, amount, gas, gas_price, vm_version, fee, tx_ttl) # post the transaction to the chain txb.post_transaction(tx, tx_hash) # wait for transaction to be mined txb.wait_tx(tx_hash) # unsigned transaction of the call call_obj = self.client.cli.get_transaction_info_by_hash( hash=tx_hash) return call_obj except OpenAPIClientException as e: raise ContractError(e)
def tx_create(self, keypair, amount=1, deposit=CONTRACT_DEFAULT_DEPOSIT, init_state="()", gas=CONTRACT_DEFAULT_GAS, gas_price=CONTRACT_DEFAULT_GAS_PRICE, fee=DEFAULT_FEE, vm_version=CONTRACT_DEFAULT_VM_VERSION, tx_ttl=DEFAULT_TX_TTL): """ Create a contract and deploy it to the chain :return: address """ try: call_data = self.encode_calldata("init", init_state) # get the transaction builder txb = TxBuilder(self.client, keypair) # create spend_tx tx, sg, tx_hash, contract_id = txb.tx_contract_create( self.bytecode, call_data, amount, deposit, gas, gas_price, vm_version, fee, tx_ttl) # post the transaction to the chain txb.post_transaction(tx, tx_hash) if self.client.blocking_mode: txb.wait_tx(tx_hash) # store the contract address in the instance variabl self.address = contract_id return tx except OpenAPIClientException as e: raise ContractError(e)
def update(self, account, target, name_ttl=DEFAULT_NAME_TTL, client_ttl=NAME_CLIENT_TTL, fee=DEFAULT_FEE, tx_ttl=DEFAULT_TX_TTL): if self.status != NameStatus.CLAIMED: raise NameUpdateError('Must be claimed to update pointer') if isinstance(target, oracle.Oracle): if target.oracle_id is None: raise ValueError( 'You must register the oracle before using it as target') target = target.oracle_id # TODO: check the value for name ttl? # get the name_id and pointers name_id = hashing.namehash_encode("nm", self.domain) pointers = self._get_pointers(target) # get the transaction builder txb = TxBuilder(self.client, account) # create claim transaction tx, sg, tx_hash = txb.tx_name_update(name_id, pointers, name_ttl, client_ttl, fee, tx_ttl) # post the transaction to the chain txb.post_transaction(tx, tx_hash) # ensure tx if self.client.blocking_mode: txb.wait_tx(tx_hash) return tx_hash
def revoke(self, account, fee=DEFAULT_FEE, tx_ttl=DEFAULT_TX_TTL): """ revoke a name :return: the transaction """ # get the name_id and pointers name_id = hashing.namehash_encode("nm", self.domain) # get the transaction builder txb = TxBuilder(self.client, account) # create claim transaction tx, sg, tx_hash = txb.tx_name_revoke(name_id, fee, tx_ttl) # post the transaction to the chain txb.post_transaction(tx, tx_hash) # ensure tx if self.client.blocking_mode: txb.wait_tx(tx_hash) self.status = NameStatus.REVOKED return tx_hash
def claim(self, account, fee=DEFAULT_FEE, tx_ttl=DEFAULT_TX_TTL): if self.preclaimed_block_height is None: raise MissingPreclaim( 'You must call preclaim before claiming a name') # name encoded TODO: shall this goes into transactions? name = AEName._encode_name(self.domain) # get the transaction builder txb = TxBuilder(self.client, account) # create claim transaction tx, sg, tx_hash = txb.tx_name_claim(name, self.preclaim_salt, fee, tx_ttl) # post the transaction to the chain txb.post_transaction(tx, tx_hash) # ensure tx if self.client.blocking_mode: txb.wait_tx(tx_hash) # update status self.status = AEName.Status.CLAIMED return tx_hash
def preclaim(self, account, fee=DEFAULT_FEE, tx_ttl=DEFAULT_TX_TTL): """ Execute a name preclaim """ # check which block we used to create the preclaim self.preclaimed_block_height = self.client.get_current_key_block_height( ) # calculate the commitment hash commitment_hash = self._get_commitment_hash() # get the transaction builder txb = TxBuilder(self.client, account) # create spend_tx tx, sg, tx_hash = txb.tx_name_preclaim(commitment_hash, fee, tx_ttl) # post the transaction to the chain txb.post_transaction(tx, tx_hash) if self.client.blocking_mode: txb.wait_tx(tx_hash) # update local status self.status = AEName.Status.PRECLAIMED self.preclaim_tx_hash = tx_hash return tx_hash
def transfer_ownership(self, account, recipient_pubkey, fee=DEFAULT_FEE, tx_ttl=DEFAULT_TX_TTL): """ transfer ownership of a name :return: the transaction """ # get the name_id and pointers name_id = hashing.namehash_encode("nm", self.domain) # get the transaction builder txb = TxBuilder(self.client, account) # create claim transaction tx, sg, tx_hash = txb.tx_name_transfer(name_id, recipient_pubkey, fee, tx_ttl) # post the transaction to the chain txb.post_transaction(tx, tx_hash) # update local status self.status = NameStatus.TRANSFERRED # ensure tx if self.client.blocking_mode: txb.wait_tx(tx_hash) return tx_hash