Examples

GetAccountMetadata()

Gets the account metadata for an IoTeX address.

AccountMeta accountMeta;
ResultCode result = connection.api.wallets.getAccount("io1xkx7y9ygsa3dlmvzzyvv8zm6hd6rmskh4dawyu", accountMeta);

If result is SUCCESS then the account metadata is be stored in accountMeta The account metadata contains the following fields:

  • address: the IoTeX address

  • balance: the balance in RAU

  • nonce: the account nonce

  • pendingNonce: the account's pending nonce

  • isContract: true if the address is a contract

GetTransactionByHash()

Gets the data for a transfer given the transaction hash.

ActionInfo_Transfer data;
ResultCode result = connection.api.wallets.getTransactionByHash("5444318f1a460c74d3e86918b640272d93d0b30e2bf2dc329dfd3faa636e52ec", data);

If result is SUCCESS then the action data is stored in data. The transfer action data contains the following fields:

{
  actHash
  blkHash
  timestamp
  blkHeight
  sender
  gasFee
  action {
    senderPublicKey
    signature
    core
    version
    nonce
    gasLimit
    gasPrice
    execution
    amount
    contract
    data
  }
}

Creating an account

We can create an account in the device from an existing private key, or generating a new private key.

From an existing private key

Generating a new private key

Getting account details

Now that the account is created, you can get the address in Ethereum or IoTeX format, and the public/private keys:

Sending a transfer of IOTX

Sends a transfer of IOTX tokens

First create the account object:

Then query the blockchain to get the account nonce:

Now broadcast the transfer with the retrieved nonce:

Signing a message

Signs a message.

First convert the private key to a byte array:

Now sign the message

The signature is stored as a byte array in signature.

Storing the private key to persistent memory in the device

You can store the private key in the EEPROM or flash memory of your Arduino device, or in a text file in Linux.

First convert the private key to a byte array:

First initialize the storage module (not needed for Nano33 IoT or Linux):

Now store the private key:

You can also read the private key that is stored in the device:

Sending a XRC20 token transfer

First convert the private key to a byte array:

Similarly, convert the destination address to a byte array:

Generate the contract call data:

Create the account object and get the account metadata in order to obtain the nonce:

Broadcast the XRC20 token transfer

Calling a contract function

This example shows how to call a contract function using the addData function of the contract with address io1n49gavyahsukdvvxxandkxephdx93n3atcrqur as an example. You can find the contract ABI here.

First convert the private key to a byte array:

Create the account object and get the account metadata in order to obtain the nonce:

Create the function call parameters:

Create the dictionary that contain the parameters and their values:

Create the contract object:

Generate the call data:

Broadcast the contract execution action