API class
- class convex_api.API(url: str)
Bases:
object
- create_account(key_pair: KeyPair, sequence_retry_count: int = 20) Account
Create a new account address on the convex network.
- Parameters:
key_pair (KeyPair) –
KeyPair
object that you wish to use as the signing account. TheKeyPair
object contains the public/private keys to access and submit commands on the convex network.sequence_retry_count (int, optional) – Number of retries to create the account. If too many clients are trying to create accounts on the same node, then we will get sequence errors.
- Returns:
A new
Account
object, or copy of theAccount
object with a new address property value set
>>> from convex_api import API >>> convex = API('https://convex.world') >>> # Create a new account with new public/private keys and address >>> key_pair = KeyPair() >>> account = convex.create_account(key_pair) >>> print(account.address) >>> 42 >>> #create a new account address, but use the same keys as `account` >>> new_account_address = convex.create_account(account=account) >>> print(new_account.address) >>> 43
- get_account_info(address_account: Account | int | str) AccountDetailsResponse
Get account information. This will only work with an account that has a balance or has had some transactions processed on the convex network. New accounts with no transfer or transactions will raise:
ConvexRequestError(404, ‘The Account for this Address does not exist.’) error
The returned information is dictionary of account information.
- Parameters:
address_account (Account, int, str) –
Account
object or address of an account to get current information on.- Returns:
dict of information, such as
>>> # Create a new account with new public/private keys and address >>> account = convex_api.create_account() >>> # get the balance of the contract >>> print(convex_api.get_account_info(account)) {'environment': {}, 'address': 1178, 'memorySize': 0, 'balance': 0, 'isLibrary': False, 'isActor': False, 'allowance': 0, 'sequence': 0, 'type': 'user'}
- get_address(function_name: str, address_account: Account | int | str)
Query the network for a contract ( function ) address. The contract must have been deployed by the account address provided. If not then no address will be returned
- Parameters:
- Returns:
Returns address of the contract
>>> # Create a new account with new public/private keys and address >>> account = convex_api.create_account() >>> # find the address of a contract >>> print(convex_api.get_address('my_contract', account))
- get_balance(address_account: Account | int | str, account_from: Account | int | str | None = None)
Get a balance of an account.
At the moment the account needs to have a balance to get the balance of it’s account or any other account. Event though this is using a query request.
- Parameters:
- Returns:
Return the current balance of the address or account address_account
>>> # Create a new account with new public/private keys and address >>> account = convex_api.create_account() >>> # get the balance of the contract >>> print(convex_api.get_balance(account)) 0 >>> print(convex_api.request_funds(100000, account)) 100000 >>> print(convex_api.get_balance(account)) 100000
- load_account(name: str, key_pair: KeyPair) Account | None
Load an account using the correct name. If successful return the
Account
object with the address set.This is a Query operation, so no convex tokens are used in loading the account.
- Parameters:
- Results:
Account
object with the address and name set and key_pair, if not found then return None
>>> # Create a new account with new public/private keys and address >>> import_account = Account.import_from_file('my_account.pem', 'secret') >>> account = convex.load_account('my_account, import_account) >>> print(account.name) my_account >>> print(account.address) 930
- query(transaction: str, address_account: int | str | Account)
Run a query transaction on the block chain. Since this does not change the network state, and the account does not need to sign the transaction. No funds will be used when executing this query. For this reason you can just pass the account address, or if you want to the
Account
object.- Parameters:
- Returns:
Return the resultant query transaction
>>> # Create a new account with new public/private keys and address >>> account = convex_api.create_account() >>> # submit a query transaction using the account address >>> print(convex_api.query(f'(balance {account.address})', account.address)) {'value': 0} >>> # request some funds to do stuff >>> print(convex_api.request_funds(100000, account)) 100000 >>> print(convex_api.query(f'(balance {account.address})', account.address)) {'value': 100000}
- register_account_name(name: str, address_account: Account | int | str, account: Account | None = None) Account
Register or update an account address with an account name.
This call will submit to the CNS (Convex Name Service), a name in the format “account.<your_name>”. You need to have some convex balance in your account, and a valid account address.
- Parameters:
>>> # load the register account >>> register_account = convex.load_account('register_account', key_pair) >>> account = convex.create_account(key_pair) >>> print(account.address) 1024 >>> account = convex.register_account('my_new_account', account.address, register_account) >>> print(account.address) 1024 # or you can call with only one account, this will use the address of that account >>> print(register_account.address) 404 >>> account = convex.register_account('my_new_account', register_account) >>> print(account.address) 404
- property registry: Registry
- request_funds(amount: int, account: Account) int
Request funds for an account from the block chain faucet.
- Parameters:
- Returns:
The amount transferred to the account
>>> # Create a new account with new public/private keys and address >>> account = convex_api.create_account() >>> # request some funds to do stuff >>> print(convex_api.request_funds(100000, account)) 100000
- resolve_account_name(name: str) int | None
Resolves an account name to an address. :param string name Name of the account to resolve.
>>> convex.resolve_account_name('my_account') 405
- resolve_name(name: str) int | None
Resolves any Convex Name Services to an address. :param string name Name of the the CNS Service.
>>> convex.resolve_name('convex.nft-tokens') 25
- send(transaction: str, account: Account, sequence_retry_count: int = 20)
Send transaction code to the block chain node.
- Parameters:
transaction (str) – The transaction as a string to send
account (Account) – The account that needs to sign the message to send
sequence_retry_count (int, optional) – Number of retries to do if a SEQUENCE error occurs. When sending multiple send requests on the same account, you can get SEQUENCE errors, This send method will automatically retry again
- Returns:
The dict returned from the result of the sent transaction.
>>> from convex_api import API, KeyPair >>> convex = API('https://convex.world') >>> # Create a new account with new public/private keys and address >>> key_pair = KeyPair() >>> account = convex.create_account(key_pair) >>> # request some funds to do stuff >>> print(convex.request_funds(100000, account)) 100000 >>> # submit a transaction using the new account >>> print(convex.send('(map inc [ 1 2 3 4])', account)) {'value': [2, 3, 4, 5]}
- setup_account(name: str, key_pair: KeyPair, register_account: Account | None = None) Account | None
Convenience method to create or load an account based on the account name. If the account name cannot be found then account will be created and account name registered, if the name is found, then account and it’s address with that name will be loaded.
- Parameters:
- Results:
Account
object with the address and name set, if not found then return None
Note If you do not provide a register_account, then this method calls the
topup_account()
method to get enough funds to register an account name.>>> import_key_pair = KeyPair.import_from_file('my_account.pem', 'secret') >>> # create or load the account named 'my_account' >>> account = convex.setup_account('my_account', import_key_pair) >>> print(account.name) my_account
- topup_account(account: Account, min_balance: int = 10000000, retry_count: int = 8)
Topup an account from the block chain faucet, so that the balance of the account is above or equal to the min_balance.
- Parameters:
- Returns:
The amount transferred to the account
>>> # Create a new account with new public/private keys and address >>> account = convex_api.create_account() >>> # request some funds to do stuff >>> print(convex_api.topup_account(account, 100000)) 100000 >>> # try again, but only return 0 topup amount credited >>> print(convex_api.topup_account(account, 100000)) 0
- transfer(to_address_account: Account | int | str, amount: int | float, account: Account)
Transfer funds from on account to another.
- Parameters:
- Returns:
The transfer record sent back after the transfer has been made
>>> # Create a new account with new public/private keys and address >>> account = convex_api.create_account() >>> print(convex_api.request_funds(10000000, account)) 10000000 >>> print(convex_api.get_balance(account)) 10000000 >>> my_account = convex_api.create_account() >>> # transfer some funds to my_account >>> print(convex_api.transfer(my_account, 100, account)) 100 >>> print(convex_api.get_balance(my_account)) 100 >>> print(convex_api.get_balance(account)) 9998520
- transfer_account(from_account: Account | int | str, to_account: Account | int | str)
WARNING If you do not save the from_account keys for later use, this call can stop you from accessing your transferred to_account.
Transfer and copy the keys from the from_account over too the to_account.
- Parameters:
use the to_account access keys.
- Returns:
A new account object that has the same name and address as the to_account, but with
the access keys of the from_account.
# Create a new account with new public/private keys and address >>> account = convex_api.create_account() >>> account.public_key '0xae4c019e68591e085d52cdb41924bde067d864cecb1780faa37142054b0fd8ef' # get a saved account keys >>> import_account = Account.import_from_file('my_account.pem', 'secret') >> import_account.public_key '5288fec4153b702430771dfac8aed0b21cafca4344dae0d47b97f0bf532b3306' # transfer the loaded keys from the `import_account`, to `account` >>> account = convex.transfer_account(import_account, account) >>> account.public_key '5288fec4153b702430771dfac8aed0b21cafca4344dae0d47b97f0bf532b3306'