Call a Function and Read Events
How to call an entry function, read a view function and read events with the TypeScript SDK.
The example uses the counter module from Publish a Package,
published at the address of the account that runs the script.
| Task | SDK function |
|---|---|
| Build an entry function | supra.transaction.build.rawTxnObject({ ... }) |
| Submit it | supra.transaction.submit.submitRawTransaction({ senderAccount, ... }) |
| Read a view function | supra.methods.view({ function, typeArguments, functionArguments }) |
| Read events of a type | supra.events.getEventsByType({ eventType, options }) |
rawTxnObject takes the sender’s current sequence number, from supra.account.getAccountInfo. Its functionArgs are
BCS-serialized values; bump takes none besides the signer. submitRawTransaction checks the chain ID of the node
before it signs and returns the committed transaction when enableWaitForTransaction is true; a transaction that ends
with Fail or Invalid throws SupraTransactionFailedError. Without enableWaitForTransaction it returns the
transaction as the node reports it right after the submission. view returns the values the function returns, as an
array. getEventsByType returns { response, cursor }, where response is a list of events and options takes
limit, startHeight, endHeight and start, the cursor of the previous page. Pass cursor back unchanged until it
is undefined; a page can be empty and still carry a cursor.
Max gas and gas unit price
rawTxnObject makes no request, so it can neither estimate the max gas nor read the node’s gas unit price estimate.
Without maxGas it uses 20,000 units, and the node requires the sender’s SUPRA balance to cover
max gas × gas unit price when it validates the transaction, which is 20 SUPRA at a gas unit price of 100,000 quants.
bump uses far fewer units, so the example sets maxGas to a smaller limit. Without a gasUnitPrice in
optionalTransactionPayloadArgs (or in the client’s configuration), rawTxnObject throws a RangeError, so the
example reads the estimate with supra.getEstimatedGasUnitPrice() and passes it. See
Gas price. The helpers that read the sender’s balance and
simulate the transaction (the coin and fungible asset transfers, publishPackage and the entry functions of
Contract.fromABI) estimate it; see Publish a Package.
Set up the project
mkdir supra-call && cd supra-call
npm init -y
npm install supra-ts-sdk@devnet
npm install --save-dev tsxCreate call_example.ts
The example uses Devnet. Use Network.TESTNET for an account and a module on Testnet.
import { HexString, Network, SupraAccount, SupraClient } from 'supra-ts-sdk'
async function main() {
const supra = new SupraClient({ network: Network.DEVNET })
const account = new SupraAccount(new HexString(process.env.PRIVATE_KEY!).toUint8Array())
const address = account.address().toString()
// Call the entry function `bump`, which takes no arguments besides the signer.
const { sequence_number } = await supra.account.getAccountInfo({ accountAddress: account.address() })
const rawTransaction = supra.transaction.build.rawTxnObject({
senderAddress: account.address(),
senderSequenceNumber: sequence_number,
function: `${address}::counter::bump`,
functionTypeArgs: [],
functionArgs: [],
optionalTransactionPayloadArgs: { maxGas: 5_000n, gasUnitPrice: await supra.getEstimatedGasUnitPrice() },
})
const response = await supra.transaction.submit.submitRawTransaction({
senderAccount: account,
rawTransaction,
enableTransactionWaitAndSimulationArgs: { enableWaitForTransaction: true },
})
console.log('bump:', response.status, response.hash)
// Read the view function `get`.
const [value] = await supra.methods.view({
function: `${address}::counter::get`,
typeArguments: [],
functionArguments: [address],
})
console.log('counter:', value)
// Read all `Bumped` events, one page at a time.
let cursor: string | undefined
do {
const page = await supra.events.getEventsByType({
eventType: `${address}::counter::Bumped`,
options: { limit: 5, start: cursor },
})
console.log(JSON.stringify(page.response, null, 2))
cursor = page.cursor
} while (cursor !== undefined)
}
main()Run it
PRIVATE_KEY=<PRIVATE_KEY> npx tsx call_example.tsEach run adds one to the counter. bump: prints Success for a committed transaction. counter: prints the new value
as a bigint, such as 2n. Each event carries the block_height and transaction_hash that emitted it and its
data, in which value is a string. The events print one page at a time, and the last page can be empty ([]).