Keys for testing
Base Url
Example: Transfer FIO tokens using a FIO Crypto Handle
Typescript SDK version compatability
The following example uses Version 1.x of the Typescript SDK. Version 2.x introduced non-backward compatible changes and a different syntax for pushing transactions to the blockchain.The following is an example of how to use the FIO Typescript SDK to transfer FIO from a payer (the user sending the funds) to a payee (the user receiving the funds) using the payee’s FIO Crypto Handle. Use the FIO Testnet Monitor to register your Testnet private/public keys and fund your Testnet account.
Import FIO
Importing using commonJS syntax is supported by Node.js out of the box:
const { FIOSDK } = require('@fioprotocol/fiosdk');
const { fetch } = require('node-fetch');
Initialize the SDK
The Typescript SDK uses a singleton model requiring initialization in the constructor as these parameters are referenced in subsequent SDK Calls.
const fetchJson = async (uri, opts = {}) => {
return fetch(uri, opts)
}
const privateKey = 'your_private_key';
const publicKey = 'your_public_key';
const baseUrl = 'http://testnet.fioprotocol.io/v1/';
fioSdk = new FIOSDK(
privateKey,
publicKey,
baseUrl,
fetchJson
)
privateKey/publicKey
- The wallet user’s private/public keysbaseURL
- The base URL to a FIO Protocol blockchain API nodefetchjson
- A reference to fetchJson, used for http post/get calls
Look up token public address
In this step we use the payee’s FIO Crypto Handle and the token and chain code for FIO to look up the payee’s FIO Public Key.
const payeeFioAddress = '[email protected]'
const result = await fioSdk.getPublicAddress(payeeFioAddress, "FIO", "FIO")
This returns the following JSON:
{
"public_address": "0xab5801a7d398351b8be11c439e05c5b3259aec9b"
}
We capture the payee’s FIO Public Key:
payeePublicKey = result.public_address
Get FIO fee
Use /get_fee to look up the payer fee for /transfer_tokens_pub_key (trnsfiopubky)
const { fee } = await fioSdk.getFee('transfer_tokens_pub_key');
Transfer FIO
pushTransaction is used to sign and push transactions onto the blockchain.
const transferAmount = 1000000000 // 1 FIO
await fioSdk.pushTransaction(
'fio.token',
'trnsfiopubky',
{
payee_public_key: payeePublicKey,
amount: transferAmount,
max_fee: fee,
tpid: "[email protected]"
}
)
Final code
The following summarizes the steps to transfer FIO tokens using a FIO Crypto Handle:
const { FIOSDK } = require('@fioprotocol/fiosdk');
const { fetch } = require('node-fetch');
const fetchJson = async (uri, opts = {}) => {
return fetch(uri, opts)
}
const privateKey = 'your_private_key';
const publicKey = 'your_public_key';
const baseUrl = 'http://testnet.fioprotocol.io/v1/';
const payeeFioAddress = '[email protected]';
async function main() {
const fioSdk = new FIOSDK(
privateKey,
publicKey,
baseUrl,
fetchJson
)
try {
const { public_address: payeePublicKey } = await fioSdk.getPublicAddress(payeeFioAddress, "FIO", "FIO")
const { fee } = await fioSdk.getFee('transfer_tokens_pub_key');
const transferAmount = 1000000000 // 1 FIO
await fioSdk.pushTransaction(
'fio.token',
'trnsfiopubky',
{
payee_public_key: payeePublicKey,
amount: transferAmount,
max_fee: fee,
tpid: "[email protected]"
}
)
} catch (e) {
console.log(e);
}
}
main()
id | Payee FIO Crypto Handle | Payer FIO Crypto Handle | Memo | Amount |
---|