Smart Contract Interactions with sdk-core JS library
Prerequisites
Before starting this tutorial, ensure the following software is installed on your machine:
Dependencies Used:
| Dependency | Version | Purpose | Installation Link |
|---|---|---|---|
| Node.js | 24.10.0 | Runtime environment for executing JavaScript outside the browser. | Install Node.js |
| npm | 11.6.1 | Package manager for managing and installing JavaScript libraries. | Included with Node.js installation |
| TypeScript | 5.7.3 | Adds static types to JavaScript to enhance developer productivity and code quality. | Install TypeScript |
You should also install and use NVM, to set a specific Node version in your project.
Once installed, you can use it like:
nvm use 24.10.0
This tutorial uses EOS Authority Block Explorer.
The install process has already set up the wallet for the root user. To interact with clio, ensure you are on the root user. Run sudo su - to switch to the root user before proceeding with the tutorial.
Steps
1. Create & set up ts-dapp-sdk-wire
mkdir -p ts-dapp-sdk-wire && cd ts-dapp-sdk-wire && npm init -y && npm install @wireio/[email protected] && npm install --save-dev [email protected] ts-node && mkdir -p src && touch src/index.ts && touch .gitignore tsconfig.json && echo -e "node_modules/\ndist/" >> .gitignore
1.1. Add tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"lib": ["ESNext"],
"allowJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "Node",
"strict": false,
"outDir": "dist"
}
}
1.2. Get the private key for jack account
You need the private key for the jack account (created in the Company Contract Tutorial). Output the key pairs stored in your wallet by running:
clio wallet keys_by_name --password "$(cat /opt/wire-network/secrets/wallet_password.txt)"
Find the entry for the key you used when creating the jack account and copy its private_key value.
1.3. Add scripts to package.json
Modify scripts in package.json to:
{
...
"scripts": {
"watch": "tsc -w",
"exec": "node dist/index.js"
}
...
}
2. Write the code
In this section, we will set up our application to interact with our local node from Company Contract Tutorial.
2.1. Initialize ApiClient()
import { API, APIClient, FetchProvider, PrivateKey, SignedTransaction, Transaction, AnyAction, Action, PackedTransaction, Checksum256 } from "@wireio/core";
const privateKey = "<your-private-key>" // private key for the `jack` account
const endpoint = "http://localhost:8888"
const apiClient = new APIClient(
{ provider: new FetchProvider(endpoint) }
);
API Client Initialization
- We import necessary modules like
APIClient,FetchProvider, and transaction-related classes. You can see full SDK reference here. - A private key is defined for signing transactions (make sure to replace it with your actual key).
- We specify the blockchain node’s API endpoint, in this case, a locally running node at
http://localhost:8888. - Finally, we initialize an instance of APIClient, which communicates with the blockchain via the FetchProvider, allowing us to interact with smart contracts for operations like signing and sending transactions.
Open up a terminal and run npm watch to run TypeScript compilation in watch mode. You should see the compiled dist folder.
2.2. Write API calls
code
import { API, APIClient, FetchProvider, PrivateKey, SignedTransaction, Transaction, AnyAction, Action, PackedTransaction, Checksum256 } from "@wireio/core";
const privateKey = "<your-private-key>" // private key for the `jack` account
const endpoint = "http://localhost:8888"
const contractAccount = "company"
const apiClient = new APIClient(
{ provider: new FetchProvider(endpoint) }
);
(async () => {
// Fetch Local Blockchain Info: Retrieves the current blockchain status
const info = await apiClient.v1.chain.get_info()
console.log("\nBlockchain Info:");
console.log(JSON.stringify(info, null, 2));
// Fetches the current state of the `employees` table from the `company` smart contract, including the number of rows and user details
const tableRows: API.v1.GetTableRowsResponse = await apiClient.call({
path: '/v1/chain/get_table_rows',
params: {
code: contractAccount,
table: "employees",
scope: contractAccount,
json: true
},
});
console.log(tableRows)
console.log(`\nEmployees Table Info:`);
console.log(`- Number of rows: ${tableRows.rows.length}`);
console.log(`- User emails: ${tableRows.rows.map(a => a.email).join(", ")}`);
// Obtains and prints the ABI (Application Binary Interface) of the `company` smart contract, detailing its methods and structures
const abiRes: API.v1.GetAbiResponse = await apiClient.call({
path: '/v1/chain/get_abi',
params: {account_name: contractAccount},
});
const { abi } = abiRes;
console.log("\nContract ABI:");
console.log(JSON.stringify(abi, null, 2));
// Constructs a transaction payload to update the details of an employee named "Jack"
const untypedAction: AnyAction = {
account: contractAccount,
name: "upsertemp",
authorization: [
{
actor: "jack",
permission: "sysio.payer" // authorizes RAM payment (must be first)
},
{
actor: "jack",
permission: "active" // authorizes the action
}
],
data: {
user: "jack",
name: "Jackson Smith!!",
email: "[email protected]",
status: "active"
}
}
const action = Action.from(untypedAction, abi);
const header = info.getTransactionHeader()
const transaction = Transaction.from({ ...header, actions: [action] })
const digest = transaction.signingDigest(info.chain_id)
const privKey = PrivateKey.from(privateKey)
const signature = privKey.signDigest(digest.msgDigest)
const signedTrx = SignedTransaction.from({ ...transaction, signatures: [signature] })
const packed = PackedTransaction.fromSigned(signedTrx);
// Sends the transaction to the blockchain.
const trx = await apiClient.call({
path: '/v1/chain/push_transaction',
params: packed
})
console.log("\nTransaction Result:");
console.log(JSON.stringify(trx, null, 2));
})();
3.Execute the code
You can inspect the employees table on the EOS Block Explorer before executing the script.

Run npm run exec and you should see output in the console with the logs like:
➜ ts-app-sdk-wire: npm run exec
Blockchain Info:
{
"server_version": "1fd21e2b",
"chain_id": "4b6ea11bff557dbaef04100a15ee8b5864bd0daf4103b98468bdb297356951fa",
"head_block_num": 1920,
"last_irreversible_block_num": 1919,
"last_irreversible_block_id": "0000077f97d283793011d0e11d270e2c3bcc0c9c2634a2620c96b55988780b0f",
"head_block_id": "0000078034dc84be41b08732fe8e0b40639db355f70b1ebcc4246fd69e669d53",
"head_block_time": "2025-04-17T16:47:14.500",
"head_block_producer": "sysio",
"virtual_block_cpu_limit": 1361215,
"virtual_block_net_limit": 7149134,
"block_cpu_limit": 100000,
"block_net_limit": 1048576,
"server_version_string": "v5.1.0",
"fork_db_head_block_num": 1920,
"fork_db_head_block_id": "0000078034dc84be41b08732fe8e0b40639db355f70b1ebcc4246fd69e669d53",
"server_full_version_string": "v5.1.0-1fd21e2b206a5a97d2c41b4afefa42b25c5c15be",
"total_cpu_weight": 500,
"total_net_weight": 500,
"earliest_available_block_num": 1,
"last_irreversible_block_time": "2025-04-17T16:47:14.000"
}
{
rows: [
{
user: 'jack',
name: 'Jackson Smith',
...
....
Check update on EOS Block Explorer.
.