Interact With Smart Contract
After you have deployed your smart contract, you can interact with them via SDK or via Kriptonio web interface.
Read
To read from a smart contract, use the read
method. It returns the result of the function call. Optionally, you can
provide a list of function arguments.
Please note that in this example, we did not provide a wallet. This is because reading from a smart contract does not trigger a blockchain transaction, and therefore does not require a wallet.
import { KriptonioSdk } from '@kriptonio/sdk';
const sdk = new KriptonioSdk({
accessToken: 'your-access-token',
});
const smartContract = await sdk.smartContract.get({ id: '<id>' });
const result = await smartContract.read('functionName', {
// optional list of function arguments
params: [],
});
Write
Writing to a smart contract creates an underlying blockchain transaction, and in this case, we will need to provide a wallet.
Kriptonio abstracts away the complexity of sending transactions for smart and classic wallets. You only need to call the
write
function, provide an optional list of arguments, and you get the transaction hash as a result.
import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
const sdk = new KriptonioSdk({
accessToken: 'your-access-token',
});
const wallet = await sdk.wallet.generate({ chainId: ChainId.BaseSepolia, type: 'kernel' });
const smartContract = await sdk.smartContract.get({ id: '<id>', wallet });
const transactionHash = await smartContract.write('functionName', {
params: [],
value: 0n,
});