Koilib is a Koinos Library for Javascript (and Typescript). This new version comes new features to facilitate the work of developers.
https://www.npmjs.com/package/koilib
Deploy smart contracts
Now it's possible to deploy contracts or update them. First, follow the instructions in koinos-cdt to compile the contracts as wasm files. You will need to install wasi-sdk.
git clone https://github.com/koinos/koinos-cdt.git
cd koinos-cdt
export KOINOS_CDT_ROOT=~/opt/koinos-cdt
export KOINOS_WASI_SDK_ROOT=~/opt/wasi-sdk-12.0
ci/install.sh
ci/build.sh
Then you can use koilib to deploy the WASM files.
(async () => {
// define signer, provider and wallet
const signer = Signer.fromSeed("my seed");
const provider = new Provider("http://45.56.104.152:8080");
const wallet = new Wallet({ signer, provider });
// encode operation to upload the contract
const bytecode = fs.readFileSync("my_contract.wasm");
const op = wallet.encodeUploadContractOperation(bytecode);
// create a transaction
const tx = await wallet.newTransaction({
operations: [op],
});
// sign and send transaction
await wallet.signTransaction(tx);
await wallet.sendTransaction(tx);
const contractId = Wallet.computeContractId(wallet.getAddress());
console.log(`Deployed. contract id: ${contractId}`);
})();
Changes on ABI
Now you can define "outputs" in the ABI definitions. This means that when you read data from a contract the library apart from querying the RPC node and get a serialized response (previous version of koilib) it will deserialize the data using the "outputs" definitions in the ABI.
Apart from that, the name field "args" has been changed to "inputs" to be consistent with inputs/outputs.
(async () => {
// define signer, provider, and contract
const signer = Signer.fromSeed("my seed");
const provider = new Provider("http://45.56.104.152:8080");
const contract = new Contract({
id: "Mkw96mR+Hh71IWwJoT/2lJXBDl5Q=",
entries: {
transfer: {
id: 0x62efa292,
inputs: {
type: [
{
name: "from",
type: "string",
},
{
name: "to",
type: "string",
},
{
name: "value",
type: "uint64",
},
],
},
},
balance_of: {
id: 0x15619248,
inputs: { type: "string" },
outputs: { type: "uint64" },
},
},
});
// create a wallet with signer, provider and contract
const wallet = new Wallet({ signer, provider, contract });
// encode a contract operation to make a transfer
const opTransfer = wallet.encodeOperation({
name: "transfer",
args: {
from: wallet.getAddress(),
to: "137JWvg4UeD4v87W1UMbtScJXiUR4WWrCr",
value: BigInt(1000),
},
});
// create a transaction
const tx = await wallet.newTransaction({
operations: [opTransfer],
});
// sign and send transaction
await wallet.signTransaction(tx);
await wallet.sendTransaction(tx);
// read the balance
const balance = await wallet.readContract({
name: "balance_of",
args: wallet.getAddress(),
});
console.log(Number(balance) / 1e8);
})();
Set system call operations
As it name indicates, these operations are used to interact with the system. For instance, they are used to change a user contract into a system contract. This part is still not clear for me, so I can not show an example, but the library already implements the ABI called abiSetSystemCallOperation
needed to serialize an operation and create a transaction.
Future work: Protobuffers
As Koinos Group is evaluating the implementation of Protobuffers as the mechanism for serialization, Koilib will need to be refactored to support protobuffers as well.
Documentation
The complete documentation can be found at https://joticajulian.github.io/koilib/