Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New NEAR API section - Python examples #2358

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions docs/4.tools/near-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ These examples are references to code snippets, feel free to explore the full co
cargo add near-api
```
</TabItem>
<TabItem value="python" label="🐍 Python">

```shell
pip install py-near
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand Down Expand Up @@ -85,6 +91,19 @@ These examples are references to code snippets, feel free to explore the full co
start="2" end="2" />

</TabItem>
<TabItem value="python" label="🐍 Python">

You can use the NEAR API by importing the `py_near` package, either entirely
```python
import py_near
```

or only the parts you need, for example:
```python
from py_near.account import Account
from py_near.providers import JsonProvider
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand Down Expand Up @@ -264,6 +283,9 @@ These examples are references to code snippets, feel free to explore the full co
</Tabs>

</TabItem>
<TabItem value="python" label="🐍 Python">
TODO: not exactly the same in Python, it's more and account + RPC URL, or a JSON RPC provider
</TabItem>
</Tabs>


Expand All @@ -281,6 +303,15 @@ These examples are references to code snippets, feel free to explore the full co
start="12" end="34" />

</TabItem>
<TabItem value="python" label="🐍 Python">
You can pass multiple RPC providers to `JsonRpcProvider`

```python
from py_near.providers import JsonProvider

provider = JsonProvider(["https://test.rpc.fastnear.com", "https://rpc.testnet.pagoda.co"])
```
</TabItem>
</Tabs>

---
Expand All @@ -306,6 +337,22 @@ This will return an Account object for you to interact with.
start="5" end="7" />

</TabItem>
<TabItem value="python" label="🐍 Python">
You can instantiate any account with the following code:

```python
from py_near.account import Account

account = Account(account_id="example-account.testnet", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()
```

If you want to use it to submit transactions later, you need to also pass the `private_key` param:

```python
account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand All @@ -329,6 +376,17 @@ Gets the available and staked balance of an account in yoctoNEAR.
start="13" end="18" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
from py_near.account import Account

account = Account(account_id="example-account.testnet", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()

account_balance = account.get_balance()
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand All @@ -352,6 +410,17 @@ Get basic account information, such as its code hash and storage usage.
start="21" end="21" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
from py_near.account import Account

account = Account(account_id="example-account.testnet", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()

account_state = account.fetch_state()
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand Down Expand Up @@ -448,6 +517,20 @@ Returns the authorized apps of an account. This is a list of contracts that the
start="65" end="82" />

</TabItem>
<TabItem value="python" label="🐍 Python">

Create a sub-account and fund it with your main account:

```python
from py_near.account import Account
from py_near.dapps.core import NEAR

account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()

res = account.create_account(account_id="sub.example-account.testnet", public_key="...", initial_balance=1 * NEAR))
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand Down Expand Up @@ -496,6 +579,18 @@ Transfer NEAR tokens between accounts.
start="22" end="28" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
from py_near.account import Account
from py_near.dapps.core import NEAR

account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()

await account.send_money("receiver-account.testnet", 1 * NEAR))
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand Down Expand Up @@ -530,6 +625,12 @@ A call function changes the contract's state and requires a signer/keypair.
start="37" end="49" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
await account.function_call("usn.near", "ft_transfer", {"receiver_id": "bob.near", "amount": "1000000000000000000000000"})
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand Down Expand Up @@ -575,6 +676,24 @@ Transactions can be sent in parallel to the network, so you don't have to wait f
url="https://github.com/PiVortex/near-api-examples/tree/main/rust/examples/simultaneous_transactions.rs#L23-L55"
start="23" end="55" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
import asyncio
from py_near.account import Account

account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()

# Prepare the transactions
tx1 = account.function_call("guestbook.near-examples.testnet", "add_message", { "text": "Hello, world!" })
tx2 = account.function_call("counter.near-examples.testnet", "increment", {})

# Send the transactions simultaneously
const transactionsResults = await asyncio.gather(tx1, tx2)
```

</TabItem>
</Tabs>

Expand All @@ -601,6 +720,20 @@ You can deploy a contract from a compiled WASM file.
start="54" end="61" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
import asyncio
from py_near.account import Account

account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()

with open("contract.wasm", "rb") as f:
contract_code = f.read()
await account.deploy_contract(contract_code)
```
</TabItem>
</Tabs>

---
Expand All @@ -626,6 +759,18 @@ View functions are read-only functions that don't change the state of the contra
start="22" end="33" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
view_call_result = await account.view_function("guestbook.near-examples.testnet", "total_messages", {})
# If args are required, they can be passed in like this in the 3rd argument:
# {
# "from_index": "0",
# "limit": "10"
# }
print(view_call_result)
```
</TabItem>
</Tabs>

---
Expand All @@ -651,6 +796,12 @@ List all the access keys for an account.
start="22" end="22" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
keys = await account.get_access_key_list()
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand All @@ -674,6 +825,12 @@ Add a new [full access key](../1.concepts/protocol/access-keys.md#full-access-ke
start="22" end="39" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
keys = await account.add_full_access_public_key("5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj")
```
</TabItem>
</Tabs>

<hr class="subsection" />
Expand All @@ -697,6 +854,18 @@ Add a new [function call key](../1.concepts/protocol/access-keys.md#function-cal
start="43" end="62" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
await account.add_public_key(
"5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj",
"example-contract.testnet", # Contract this key is allowed to call
["example_method"], # Methods this key is allowed to call (optional)
0.25 * NEAR # Gas allowance key can use to call methods (optional)
)
```
</TabItem>

</Tabs>

<hr class="subsection" />
Expand All @@ -720,6 +889,12 @@ When deleting an access key, you need to specify the public key of the key you w
start="67" end="72" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
await account.delete_public_key("5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj")
```
</TabItem>
</Tabs>

---
Expand All @@ -745,6 +920,15 @@ Convert an amount in NEAR to an amount in yoctoNEAR.
start="7" end="7" />

</TabItem>
<TabItem value="python" label="🐍 Python">

```python
from py_near.dapps.core import NEAR

amount_in_yocto = 1 * NEAR
```

</TabItem>
</Tabs>

<hr class="subsection" />
Expand Down Expand Up @@ -791,5 +975,10 @@ Convert an amount in NEAR to an amount in yoctoNEAR.
- [Github](https://github.com/near/near-api-rs)
- [Full Examples](https://github.com/PiVortex/near-api-examples/tree/main/rust)

</TabItem>
<TabItem value="python" label="🐍 Python">

- [Phone number transfer](https://py-near.readthedocs.io/en/latest/clients/phone.html)

</TabItem>
</Tabs>