Class Contract

The contract class contains the contract ID and contract entries definition needed to encode/decode operations during the interaction with the user and the communication with the RPC node.

Example

const { Contract, Provider, Signer, utils } = require("koilib");
const rpcNodes = ["http://api.koinos.io"];
const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
const provider = new Provider(rpcNodes);
const signer = new Signer({ privateKey, provider });
const koinContract = new Contract({
id: "15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL",
abi: utils.tokenAbi,
provider,
signer,
});
const koin = koinContract.functions;

async funtion main() {
// Get balance
const { result } = await koin.balanceOf({
owner: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
});
console.log(result)

// Transfer
const { transaction, receipt } = await koin.transfer({
from: signer.getAddress(),
to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
value: "1000010000", // 10.00010000
});
console.log(`Transaction id ${transaction.id} submitted. Receipt:`);
console.log(receipt);

// wait to be mined
const blockNumber = await transaction.wait();
console.log(`Transaction mined. Block number: ${blockNumber}`);
}

main();

Hierarchy

  • Contract

Constructors

Properties

abi?: Abi

Application Binary Interface

bytecode?: Uint8Array

Bytecode. Needed to deploy the smart contract.

functions: { [x: string]: (<T>(args?: any, opts?: CallContractOptions) => Promise<{ operation: OperationJson; receipt?: TransactionReceipt; result?: T; transaction?: TransactionJsonWait }>) }

Set of functions to interact with the smart contract. These functions are automatically generated in the constructor of the class

Example

const owner = "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb";
await koinContract.functions.balanceOf({ owner });

Example

using options

await koinContract.functions.transfer({
from: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",
to: "15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL",
value: "1",
},{
chainId: "EiB-hw5ABo-EXy6fGDd1Iq3gbAenxQ4Qe60pRbEVMVrR9A==",
rcLimit: "100000000",
nonce: "OAI=",
payer: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
payee: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",
signTransaction: true,
sendTransaction: true,
broadcast: true,
sendAbis: true,
});

Type declaration

id: Uint8Array

Contract ID

Options to apply when creating transactions. By default it set rc_limit to 1e8, sendTransaction true, sendAbis true, and nonce undefined (to get it from the blockchain)

provider?: Provider

Provider to connect with the blockchain

serializer?: Serializer

Serializer to serialize/deserialize data types

Signer interacting with the smart contract

Methods

  • Decode an event received in a receipt

    Example

    const contract = new Contract({
    id: "15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL",
    abi: utils.tokenAbi,
    });
    const event = {
    sequence: 1,
    source: "15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL",
    name: "koinos.contracts.token.mint_event",
    data: "ChkAxjdqxuwS-B50lPQ-lqhRBA3bf2b2ooAHENrw3Ek=",
    impacted: ["1K55BRw87nd64a7aiRarp6DLGRzYvoJo8J"],
    };
    const eventDecoded = await contract.decodeEvent(event);
    console.log(eventDecoded);
    // {
    // sequence: 1,
    // source: "15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL",
    // name: "koinos.contracts.token.mint_event",
    // data: "ChkAxjdqxuwS-B50lPQ-lqhRBA3bf2b2ooAHENrw3Ek=",
    // impacted: ["1K55BRw87nd64a7aiRarp6DLGRzYvoJo8J"],
    // args: {
    // to: "1K55BRw87nd64a7aiRarp6DLGRzYvoJo8J",
    // value: "154613850",
    // },
    // }

    Parameters

    Returns Promise<DecodedEventData>

  • Decodes a contract operation to be human readable

    Example

    const opDecoded = await contract.decodeOperation({
    call_contract: {
    contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
    entry_point: 0x27f576ca,
    args: "ChkAEjl6vrl55V2Oym_rzsnMxIqBoie9PHmMEhkAQgjT1UACatdFY3e5QRkyG7OAzwcCCIylGOgH",
    }
    });
    console.log(opDecoded);
    // {
    // name: "transfer",
    // args: {
    // from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
    // to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
    // value: "1000",
    // },
    // }

    Parameters

    Returns Promise<DecodedOperationJson>

  • Function to deploy a new smart contract. The Bytecode must be defined in the constructor of the class

    Example

    const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
    const provider = new Provider(["http://api.koinos.io"]);
    const signer = new Signer({ privateKey, provider });
    const bytecode = new Uint8Array([1, 2, 3, 4]);
    const contract = new Contract({ signer, provider, bytecode });
    const { transaction, receipt } = await contract.deploy();
    console.log(receipt);
    // wait to be mined
    const blockNumber = await transaction.wait();
    console.log(`Contract uploaded in block number ${blockNumber}`);

    Example

    using options

    const { transaction, receipt } = await contract.deploy({
    // contract options
    abi: "CssCChRrb2lub3Mvb3B0aW9ucy5wc...",
    authorizesCallContract: true,
    authorizesTransactionApplication: true,
    authorizesUploadContract: true,

    // transaction options
    chainId: "EiB-hw5ABo-EXy6fGDd1Iq3gbAenxQ4Qe60pRbEVMVrR9A==",
    rcLimit: "100000000",
    nonce: "OAI=",
    payer: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
    payee: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",

    // sign and broadcast
    signTransaction: true,
    sendTransaction: true,
    broadcast: true,
    });
    console.log(receipt);
    // wait to be mined
    const blockNumber = await transaction.wait();
    console.log(`Contract uploaded in block number ${blockNumber}`);

    Parameters

    Returns Promise<{ operation: OperationJson; receipt?: TransactionReceipt; transaction?: TransactionJsonWait }>

  • Encondes a contract operation using Koinos serialization and taking the contract entries as reference to build it

    Returns

    Operation encoded

    Example

    const opEncoded = await contract.encodeOperation({
    name: "transfer",
    args: {
    from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
    to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
    value: "1000",
    }
    });

    console.log(opEncoded);
    // {
    // call_contract: {
    // contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
    // entry_point: 670398154,
    // args: "ChkAEjl6vrl55V2Oym_rzsnMxIqBoie9PHmMEhkAQgjT1UACatdFY3e5QRkyG7OAzwcCCIylGOgH",
    // }
    // }

    Parameters

    Returns Promise<OperationJson>

  • Fetch the ABI from the contract meta store and save it in the abi of the contract. The provider must have contract_meta_store microservice enabled.

    Returns

    the new ABI saved in the contract

    Parameters

    • opts: { updateFunctions: boolean; updateSerializer: boolean } = ...

      options object with 2 boolean: 1) updateFunctions to specify if the contract functions should be regenerated based on the new ABI, and 2) updateSerializer to determine if the serializer should be updated with the types in the new ABI.

      • updateFunctions: boolean
      • updateSerializer: boolean

    Returns Promise<undefined | Abi>

  • Create the contract functions based on the ABI

    Returns boolean

Generated using TypeDoc