Skip to content

Running A Transaction

Summary

This guide will take you through the required steps to create a wallet and use it to run a transaction on-chain.

By the end of this guide, you will have completed the following:

  • Created a transaction.
  • Submitted the transaction to the blockchain using our Chain Service.
  • Queried the receipt of the transaction.
  • Viewed the transaction on Hashscan.

Prerequisites

  • Created a wallet

Creating A Custodial Wallet

To create your wallet, you will need to use the io.fsco.wallet.command.create operation. This operation will create a new wallet for you to use in your transactions.

import axios from 'axios';
const wallet_res = await axios.post('https://staging.api.fsco.io/v2/wallet', {
name: 'my_new_wallet',
custodian: 'fsco'
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const wallet_id = wallet_res.data.queryId;

Creating An Unsigned Transaction

Next, we will create an example transaction using the ethers.js library. Below, we encode a transaction that, when executed, will call the balanceOf function on the USDC contract for the Burn wallet.

import { ethers } from 'ethers';
const contract_address = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
// Define the interface that you want to use
const inter = new ethers.utils.Interface([
'function balanceOf(address _user) view returns (uint256)',
]);
// Serialize the transaction call data
const contract_call_data = inter.encodeFunctionData('balanceOf', [
'0x000000000000000000000000000000000000dEaD'
]);
// Serialize the transaction
const unsigned_transaction = ethers.utils.serializeTransaction({
to: contract_address,
data: contract_call_data,
chainId: 1
});

Signing Using Custodial Wallet

To sign a transaction you have serialized, you can use the io.fsco.wallet.query.signTx operation. This will take your transaction and use the wallet to sign it, returning the signed transaction.

ℹ️ This process will set the fields r, s, v, and from.

const signed_res = await axios.post(`https://staging.api.fsco.io/v2/wallet/${wallet_id}/sign/tx`, {
data: unsigned_transaction
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const signed_transaction = signed_res.data.signedData;

Submitting The Transaction

Once you have signed the transaction, you can submit it to the blockchain.

import { ethers } from 'ethers';
const provider = new ethers.providers.JsonRpcProvider();
const tx = await provider.sendTransaction(signed_transaction);
const receipt = await tx.wait();

Conclusion

You’ve successfully created a custodial wallet, generated and signed a transaction, and submitted it to the blockchain. These steps form the foundation for using our custodian wallet services, enabling you to confidently manage and execute transactions in your projects.

As a next step, consider leveraging our Chain Service, which allows you to submit transactions without needing to pre-sign them or connect to an RPC endpoint. This service simplifies the process even further, ensuring that your transactions are handled seamlessly within our platform.

Full Script
import axios from 'axios';
import { ethers } from 'ethers';
const wallet_res = await axios.post('https://staging.api.fsco.io/v2/wallet', {
name: 'my_new_wallet',
custodian: 'fsco'
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const wallet_id = wallet_res.data.queryId;
const contract_address = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
// Define the interface that you want to use
const inter = new ethers.utils.Interface([
'function balanceOf(address _user) view returns (uint256)',
]);
// Serialize the transaction call data
const contract_call_data = inter.encodeFunctionData('balanceOf', [
'0x000000000000000000000000000000000000dEaD'
]);
// Serialize the transaction
const unsigned_transaction = ethers.utils.serializeTransaction({
to: contract_address,
data: contract_call_data,
chainId: 1
});
const signed_res = await axios.post(`https://staging.api.fsco.io/v2/wallet/${wallet_id}/sign/tx`, {
data: unsigned_transaction
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const signed_transaction = signed_res.data.signedData;
const provider = new ethers.providers.JsonRpcProvider();
const tx = await provider.sendTransaction(signed_transaction);
const receipt = await tx.wait();
console.log(receipt);