Nil.js
: reading and writing info
The Nil.js
client library provides several 'helper' methods for reading and writing to =nil;
To retrieve the block with the given hash:
import { PublicClient } from '@nilfoundation/niljs';
const client = new PublicClient({
transport: new HttpTransport({
endpoint: "{NIL_ENDPOINT}",
}),
shardId: 1,
});
const block = await client.getBlockByHash(BLOCK_HASH);
To call an existing smart contract after initializing a smart account:
import {
encodeFunctionData,
type Abi,
} from "viem";
const data = encodeFunctionData({
abi: CONTRACT_ABI as Abi,
functionName: "{funcName}",
args: [],
}
);
const transactionHash = await smartAccount.sendTransaction({
to: '{CONTRACT_ADDRESS}',
data: data,
value: 100_000n,
feeCredit: 1_000_000n
});
await waitTillCompleted(client, transactionHash);
tip
The waitTillCompleted()
function enforces waiting for the results of the execution of the transaction whose hash is passed as an argument. It is crucial for avoiding null-type errors.
To read information about the resulting transaction:
const transaction = await client.getTransactionByHash(transactionHash);
To read the transaction receipt:
const receipt = await client.getTransactionReceiptByHash(transactionHash);