ChatGPT knows how to use the nano rpc!

Introduction:

In this blog post, we'll dive into Nano and we'll learn how to us the nano_node RPC to receive our first nano transaction and forward it to a new address.

Nano is a near-instant cryptocurrency with no fees and a small ecological footprint.
To interact with the network, the nano_node software exposes a variety of remote procedure calls (RPCs) that let you browse the ledger, create Nano transactions and more.

To help me with this task, I asked ChatGPT how to use the Nano RPC to create a wallet and receive a transaction from a faucet.
While the example it created wasn't quite perfect, it was easy to make it work with a few adjustments.

All the additions and corrections I made are highlighted like this.

If you want to follow along and try out the Nano RPC for yourself, you can

I created a fully synced node with public access to the RPC so you don't need to setup your own nano_node.
If you're interested in setting up your own Nano node, you can read this blog article, or spin up a dockerized node with just one command.

Adjusted example created by ChatGPT

1) Creating a New Wallet

Follow along on your own : Postman collection

To create a new wallet using the Nano RPC, we will send a POST request to the endpoint https://rpcproxy.bnano.info/live with the following payload:

{
    "action": "wallet_create"
}
curl -X POST --data '{"action":"wallet_create"}' https://rpcproxy.bnano.info/live

The response will contain the seed for our new wallet:

{
    "wallet": "E861D8_truncated_for_brevity"
}

2) Adding an Account

To add an account to our wallet, we will send a POST request to the endpoint https://rpcproxy.bnano.info/live with the following payload:

{
    "action": "account_create",
    "wallet": "E861D8_truncated_for_brevity"
}
curl -X POST --data '{"action":"account_create","wallet":"E861D8_truncated_for_brevity"}' https://rpcproxy.bnano.info/live

The response will contain the private key and the public address for our new account:

{
    "accounts": [
        {
            "private": "45A3_truncated_for_brevity",
            "public": "nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4"
        }
    ]
}

3) Receiving a First Transaction from a Faucet

To receive funds, we will need to provide the public address of our account to the faucet.

You can visit https://freenanofaucet.com/faucet to receive some free nano

4) Check your account balance

Once the faucet has sent the funds, we can check the balance of our account using the following payload:

{   
	"action": "account_balance",   
	"account": "nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4" 
}
curl -X POST --data '{"action":"account_balance","account":"nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4"}' https://rpcproxy.bnano.info/live

The response will contain the current balance of the account:

{
    "balance": "0", //corrected version
    "pending": "12000000000000000000000000",
    "receivable": "12000000000000000000000000"
}

This is where chatGPT was wrong for the first time. Your account will show "balance": "0" . The funds are in a "receivable" state.
Before you can use and forward the funds to another account, you first need to actively receive what has been sent to your account.
I told ChatGPT it missed a step and asked to receive the funds. It got the receive command right, but listed the wrong arguments.

5) Query the receivable block

Again ChatGPT was missing a step. To receive the funds, we need to query the receivable blocks in our wallet first like this:

{  
 "action": "wallet_receivable",  
 "wallet": "E861D8_truncated_for_brevity" 
}

The response will contain all the accounts in our wallet with their receivable blocks

{
  "blocks": {
    "nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4": ["AD628C1F_truncated_for_brevity"]
  }
} 

6) Create the receivable block

To receive the funds, we need to specify the source of the funds (i.e., the public address of the sender), the wallet and the account that the funds should be received into, and the amount of funds block being received.
Here is the payload for the "receive" action:

{  
 "action": "receive",  
 "wallet": "E861D8_truncated_for_brevity",  
 "account": "nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4",  //ChatGPT suggested "source" instead of "account"
 "block": "AD628C1F_truncated_for_brevity"  // ChatGPT suggested "amount" instead of "block"
}

And here is the curl command:

curl -X POST --data '{"action":"receive","wallet":"E861D8_truncated_for_brevity","account":"nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4","block":"AD628C1F_truncated_for_brevity"}' https://rpcproxy.bnano.info/live 

The response will contain a block hash, which can be used to track the confirmation of the transaction on the Nano network. The funds will now be available in your account and you can verify the balance using the "balance" action.

With just a few tweaks we got a working RPC example from ChatGPT.
We'll now look at the RPC commands to send our funds to a new account

7) Check our wallet

To see all the funds in our wallet, we can use the "wallet_info" command

{
    "action": "wallet_info",
    "wallet": "E861D8_truncated_for_brevity"
}

The result will list the total balance of all our accounts. In our current example, we only have a single account "accounts_count": "1"

{
    "balance": "12000000000000000000000000",
    "pending": "0",
    "receivable": "0",
    "accounts_count": "1",
    "accounts_block_count": "1",
    "accounts_cemented_block_count": "1",
    "deterministic_count": "1"
    ...  
}

8) Send to a new address

To send our funds to a new address we have to provide our wallet, the source account we want to send from, the destination account and the amount.
We can provide an optional "id" that makes sure that even if we execute the send command twice, we don't actually send our funds twice.

{
  "action": "send",
  "wallet": "E861D8_truncated_for_brevity",
  "source": "nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4",
  "destination": "nano_118tih7f81iiuujdezyqnbb9aonybf6y3cj7mo7hbeetqiymkn16a67w8rkp",
  "amount": "12000000000000000000000000",
  "id": "a_unique_id_62386187gd"
} 

The response is a block. We will query the content of the block in the next step

{
  "block": "6ADE56_truncated_for_brevity"
}

9) Query the content of our send block

To validate that our block has been confirmed by the network, we can use the "block_info" command, which will return all the information of our transaction.

{  
  "action": "block_info",
  "json_block": "true",
  "hash": "6ADE56_truncated_for_brevity"
}

The result has "confirmed" field that is "true" if the block has been confirmed by the network.
At this point the transaction is final and can't be reveresed.

{
  "block_account": "nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4",
  "amount": "12000000000000000000000000",
  "balance": "0",
  "height": "2",
  "local_timestamp": "0",
  "successor": "0000000000000000000000000000000000000000000000000000000000000000",
  "confirmed": "true",
  "contents": {
    "type": "state",
    "account": "nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4",
    "previous": "CE898C131AAEE25E05362F247760F8A3ACF34A9796A5AE0D9204E86B0637965E",
    "representative": "nano_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4",
    "balance": "12000000000000000000000000",
    "link": "5D1AA8A45F8736519D707FCB375976A7F9AF795091021D7E9C7548D6F45DD8D5",
    "link_as_account": "nano_118tih7f81iiuujdezyqnbb9aonybf6y3cj7mo7hbeetqiymkn16a67w8rkp",
    "signature": "82D41BC16F313E4B2243D14DFFA2FB04679C540C2095FEE7EAE0F2F26880AD56DD48D87A7CC5DD760C5B2D76EE2C205506AA557BF00B60D8DEE312EC7343A501",
    "work": "8a142e07a10996d5"
  },
  "subtype": "send"
}

I hope you enjoyed. Make sure to try it for yourself!

Conclusion

In this article, we have seen how to interact with the Nano RPC to perform various actions such as creating a wallet, adding an account, receiving funds and forwarding the funds to a new address.
The Nano RPC is a powerful tool for developers looking to build applications on top of the Nano network, and the use of Postman or curl allows for quick and easy testing of the API.

If you like my work, please show your love 💛