Skip to content

Calling a Contract

Summary

This guide will walk you through the process of calling a smart contract using the FSCO Contract Service. By the end of this guide, you will know how to:

  • Query a contract deployment (read-only operation)
  • Call a contract deployment (state-changing operation)
  • Retrieve transaction receipts for contract calls

Prerequisites

Before you begin, make sure you have:

  • Registered your contract ABI and deployment as described in the Registering a Contract guide
  • The deployment ID of your registered contract

Querying a Contract Deployment

To perform a read-only operation on your contract, use the io.fsco.contract.query.deployment.{deploymentId}.query endpoint. This is suitable for view functions that don’t change the contract’s state.

import axios from 'axios';
const deploymentId = 'your-deployment-id'; // Replace with your actual deployment ID
const queryResponse = await axios.post(`https://staging.api.fsco.io/v2/contract/deployment/${deploymentId}/query`, {
function: 'retrieve', // The name of the function you want to call
args: [] // Any arguments the function requires (none for 'retrieve' in our SimpleStorage example)
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const queryResult = queryResponse.data;
console.log('Query result:', queryResult);

Calling a Contract Deployment

For state-changing operations, use the io.fsco.contract.command.deployment.{deploymentId}.call endpoint. This is used for functions that modify the contract’s state and require a transaction to be sent.

const callResponse = await axios.post(`https://staging.api.fsco.io/v2/contract/deployment/${deploymentId}/call`, {
function: 'store', // The name of the function you want to call
args: [42], // Arguments for the function (storing the value 42 in our SimpleStorage example)
from: '0xYourWalletAddress', // The address initiating the transaction
value: '0' // Amount of native currency to send with the transaction (in wei)
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const callResult = callResponse.data;
console.log('Call result:', callResult);

Retrieving Transaction Receipts

After calling a contract, you may want to retrieve the transaction receipt to confirm its status. Use the io.fsco.contract.query.tx.receipt.{txId}.get endpoint for this.

const txId = callResult.txId; // Assuming the call result includes a transaction ID
const receiptResponse = await axios.get(`https://staging.api.fsco.io/v2/contract/tx/${txId}`, {
headers: {
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const txReceipt = receiptResponse.data;
console.log('Transaction receipt:', txReceipt);

Conclusion

You’ve now learned how to interact with your registered smart contracts using the FSCO Contract Service. This includes performing both read-only queries and state-changing calls, as well as retrieving transaction receipts for your calls.

These operations allow you to seamlessly interact with blockchain contracts through a simple API interface, without needing to manage blockchain connections or ABI encoding directly in your application.

Full Script
import axios from 'axios';
const deploymentId = 'your-deployment-id'; // Replace with your actual deployment ID
// Query the contract (read-only)
const queryResponse = await axios.post(`https://staging.api.fsco.io/v2/contract/deployment/${deploymentId}/query`, {
function: 'retrieve',
args: []
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const queryResult = queryResponse.data;
console.log('Query result:', queryResult);
// Call the contract (state-changing)
const callResponse = await axios.post(`https://staging.api.fsco.io/v2/contract/deployment/${deploymentId}/call`, {
function: 'store',
args: [42],
from: '0xYourWalletAddress',
value: '0'
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const callResult = callResponse.data;
console.log('Call result:', callResult);
// Retrieve transaction receipt
const txId = callResult.txId;
const receiptResponse = await axios.get(`https://staging.api.fsco.io/v2/contract/tx/${txId}`, {
headers: {
'Authorization': `Bearer ${process.env.TOKEN}`
}
});
const txReceipt = receiptResponse.data;
console.log('Transaction receipt:', txReceipt);