Nil.js
: calling smart contracts
This tutorial shows how to call the previously deployed manufacturer and retailer contracts.
Via the contract factory
Nil.js
exposes the contract factory, a Hardhat-like tool for interacting with smart contracts.
To order a new product via the contract factory:
const hashFunds = await faucet.withdrawToWithRetry(retailerAddress, convertEthToWei(1));
await waitTillCompleted(client, hashFunds);
const retailerContract = getContract({
client,
RETAILER_ABI,
address: retailerAddress,
smartAccount: smartAccount,
});
const manufacturerContract = getContract({
client,
MANUFACTURER_ABI,
address: manufacturerAddress,
smartAccount: smartAccount,
});
const res = await retailerContract.read.orderProduct([manufacturerAddress, "new-product"]);
To attain the result:
const res2 = await manufacturerContract.read.getProducts();
console.log(res2);
Via internal transactions
To send funds to the retailer product and call the orderProduct
function:
const hashFunds = await faucet.withdrawToWithRetry(retailerAddress, convertEthToWei(1));
await waitTillCompleted(client, hashFunds);
const hashProduct = await smartAccount.sendTransaction({
to: retailerAddress,
data: encodeFunctionData({
abi: RETAILER_ABI,
functionName: "orderProduct",
args: [manufacturerAddress, "another-product"],
}),
feeCredit: 3_000_000n * gasPrice,
});
const productReceipts = await waitTillCompleted(client, hashProduct);
To receive and decode the currently created products:
const resultsCall = await client.call(
{
from: manufacturerAddress,
to: manufacturerAddress,
data: encodeFunctionData({
abi: MANUFACTURER_ABI,
functionName: "getProducts",
args: [],
}),
},
"latest",
);
console.log(
"getProducts",
decodeFunctionResult({
abi: MANUFACTURER_ABI,
functionName: "getProducts",
data: resultsCall,
}),
);
Via external transactions
To order a new product using the retailer:
const orderTransaction = new ExternalTransactionEnvelope({
isDeploy: false,
to: hexToBytes(addressRetailer),
chainId,
data: hexToBytes(
encodeFunctionData({
abi: RETAILER_ABI,
functionName: "orderProduct",
args: [addressManufacturer, "new-product"],
}),
),
authData: new Uint8Array(0),
seqno: await client.getTransactionCount(addressRetailer),
});
const encodedOrderTransaction = orderTransaction.encode();
let success = false;
let ordertransactionHash;
while (!success) {
try {
ordertransactionHash = await client.sendRawTransaction(
bytesToHex(encodedOrderTransaction),
);
success = true;
} catch (error) {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
const orderReceipts = await waitTillCompleted(client, ordertransactionHash);
To receive and decode the currently created products:
const resultsCall = await client.call(
{
from: manufacturerAddress,
to: manufacturerAddress,
data: encodeFunctionData({
abi: MANUFACTURER_ABI,
functionName: "getProducts",
args: [],
}),
},
"latest",
);
console.log(
"getProducts",
decodeFunctionResult({
abi: MANUFACTURER_ABI,
functionName: "getProducts",
data: resultsCall,
}),
);