Account
Quick start
from pynear.account import Account
import asyncio
from pynear.dapps.core import NEAR
ACCOUNT_ID = "bob.near"
PRIVATE_KEY = "ed25519:..."
async def main():
acc = Account(ACCOUNT_ID, PRIVATE_KEY)
await acc.startup()
print(await acc.get_balance() / NEAR)
print(await acc.get_balance("bob.near") / NEAR)
transaction = await acc.send_money("bob.near", NEAR * 2)
print(tr.transaction.hash)
print(tr.logs)
transaction = await acc.phone.send_near_to_phone("+15626200911", NEAR // 10)
print(tr.transaction.hash)
asyncio.run(main())
Documentation
- class Account
This class implement all blockchain functions for your account. Only one parallel request can be made from one private key. All transaction calls execute sequentially.
acc = Account("bob.near", private_key1) # requests time ~18s tasks = [ asyncio.create_task(acc.send_money("alisa.near", 1)), asyncio.create_task(acc.send_money("alisa.near", 1)), asyncio.create_task(acc.send_money("alisa.near", 1)), ] for t in task: await t
Account() support multikeys. In this case you can make a few parallel requests.
acc = Account("bob.near", [private_key1, private_key2, private_key3]) # requests time ~6s tasks = [ asyncio.create_task(acc.send_money("alisa.near", 1)), asyncio.create_task(acc.send_money("alisa.near", 1)), asyncio.create_task(acc.send_money("alisa.near", 1)), ] for t in task: await t
- get_access_key()
Get access key for current account
- Returns:
AccountAccessKey
await acc.get_access_key()
- get_access_key_list(account_id=None)
Send fungible token to phone number. Reciver will get sms with link to claim tokens. Get access key list for account_id, if account_id is None, get access key list for current account
- Parameters:
account_id – if account_id is None, return balance of current account
- Returns:
list of PublicKey
keys = await acc.get_access_key_list() print(len(keys))
- fetch_state(phone)
Fetch state for given account
- Returns:
state dict
state = await acc.fetch_state() print(state)
- send_money(account_id: str, amount: int, nowait=False)
Send money to account_id
- Parameters:
account_id – receiver account id
amount – amount in yoctoNEAR
nowait – if nowait is True, return transaction hash, else wait execution
- Returns:
transaction hash or TransactionResult
await acc.send_money('bob.near', NEAR * 3)
- view_function(contract_id: str, method_name: str, args: dict)
Call view function on smart contract. View function is read only function, it can’t change state
- Parameters:
contract_id – smart contract account id
method_name – method name to call
args – json args to call method
- Returns:
result of view function call
result = await acc.view_function("usn.near", "ft_balance_of", {"account_id": "bob.near"}) print(result)
- function_call(contract_id: str, method_name: str, args: dict, gas=DEFAULT_ATTACHED_GAS, amount=0, nowait=False)
Call function on smart contract
- Parameters:
contract_id – smart contract adress
method_name – call method name
args – json params for method
gas – amount of attachment gas
amount – amount of attachment NEAR
nowait – if nowait is True, return transaction hash, else wait execution
- Returns:
transaction hash or TransactionResult
await acc.function_call('usn.near', "ft_transfer", {"receiver_id": "bob.near", "amount": "1000000000000000000000000"})
- create_account(account_id: str, public_key: Union[str, bytes], initial_balance: int, nowait=False)
- Create new account in subdomian of current account. For example, if current account is “test.near”,
you can create “wwww.test.near”
- Parameters:
account_id – new account id
public_key – add public key to new account
initial_balance – amount to transfer NEAR to new account
nowait – is nowait is True, return transaction hash, else wait execution
- Returns:
transaction hash or TransactionResult
await acc.create_account('test.bob.near', "5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj", NEAR * 3)
- add_public_key(public_key: Union[str, bytes], receiver_id: str, method_names: List[str] = None, allowance: int = 25000000000000000000000, nowait=False)
Add public key to account with access to smart contract methods
- Parameters:
public_key – public_key to add
receiver_id – smart contract account id
method_names – list of method names to allow
allowance – maximum amount of gas to use for this key
nowait – if nowait is True, return transaction hash, else wait execution
- Returns:
transaction hash or TransactionResult
await acc.add_public_key("5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj", "usn.near", [])
- add_full_access_public_key(public_key: Union[str, bytes], nowait=False)
Add public key to account with full access
- Parameters:
public_key – public_key to add
nowait – if nowait is True, return transaction hash, else wait execution
- Returns:
transaction hash or TransactionResult
await acc.add_full_access_public_key("5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj")
- delete_public_key(public_key: Union[str, bytes], nowait=False)
Delete public key from account
- Parameters:
public_key – public_key to delete
nowait – is nowait is True, return transaction hash, else wait execution
- Returns:
transaction hash or TransactionResult
await acc.delete_public_key("5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj")
- deploy_contract(contract_code: bytes, nowait=False)
Deploy smart contract to account
- Parameters:
contract_code – smart contract code
nowait – if nowait is True, return transaction hash, else wait execution
- Returns:
transaction hash or TransactionResult
with open("contract.wasm", "rb") as f: contract_code = f.read() await acc.deploy_contract(contract_code, nowait=True)
- stake(contract_code: bytes, nowait=False)
Stake NEAR on account. Account must have enough balance to be in validators pool
- Parameters:
public_key – public_key to stake
amount – amount of NEAR to stake
nowait – if nowait is True, return transaction hash, else wait execution
- Returns:
transaction hash or TransactionResult
- get_balance(account_id: str = None)
Get account balance
- Parameters:
account_id – if account_id is None, return balance of current account
- Returns:
balance of account in yoctoNEAR
result = await acc.get_balance("usn.near") print(result)
- property phone
Get client for phone.herewallet.near
- Returns:
Phone(self)
- property ft
Get client for fungible tokens
- Returns:
FT(self)