Skip to Content
Supra Layer 1SupraMVMTypeScript SDKGuidesCall a Function and Read Events

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.

TaskSDK function
Build an entry functionsupra.transaction.build.rawTxnObject({ ... })
Submit itsupra.transaction.submit.submitRawTransaction({ senderAccount, ... })
Read a view functionsupra.methods.view({ function, typeArguments, functionArguments })
Read events of a typesupra.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 returns the committed transaction when enableWaitForTransaction is true, and the transaction as Pending otherwise. 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.

The SDK’s default maxGas of 1000 units is enough for bump. Functions that do more need a larger optionalTransactionPayloadArgs: { maxGas } on rawTxnObject; see Publish a Package.

Set up the project

mkdir supra-call && cd supra-call npm init -y npm install supra-ts-sdk npm install --save-dev tsx

Create call_example.ts

import { HexString, Network, SupraAccount, SupraClient } from 'supra-ts-sdk' async function main() { const supra = new SupraClient({ network: Network.TESTNET }) 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: [], }) const response = await supra.transaction.submit.submitRawTransaction({ senderAccount: account, rawTransaction, enableTransactionWaitAndSimulationArgs: { enableWaitForTransaction: true }, }) console.log('bump:', response.status, response.output) // Read the view function `get`. const [value] = await supra.methods.view({ function: `${address}::counter::get`, typeArguments: [], functionArguments: [address], }) console.log('counter:', value) // Read the `Bumped` events. const { response: events } = await supra.events.getEventsByType({ eventType: `${address}::counter::Bumped`, options: { limit: 5 }, }) console.log(JSON.stringify(events, null, 2)) } main()

Run it

PRIVATE_KEY=<PRIVATE_KEY> npx tsx call_example.ts

Each 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.

Last updated on