Optional
abi?: AbiOptional
bytecode?: Uint8ArrayOptional
id?: stringOptional
options?: ContractTransactionOptionsOptional
provider?: ProviderInterfaceOptional
serializer?: SerializerSet this option if you can not use eval functions in the current environment. In such cases, the serializer must come from an environment where it is able to use those functions.
Optional
signer?: SignerInterfaceOptional
abiApplication Binary Interface
Optional
bytecodeBytecode. Needed to deploy the smart contract.
Set of functions to interact with the smart contract. These functions are automatically generated in the constructor of the class
const owner = "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb";
await koinContract.functions.balanceOf({ owner });
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,
});
Optional
args: anyOptional
opts: CallContractOptionsContract 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)
Optional
providerProvider to connect with the blockchain
Optional
serializerSerializer to serialize/deserialize data types
Optional
signerSigner interacting with the smart contract
Decode an event received in a receipt
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",
// },
// }
Decodes a contract operation to be human readable
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",
// },
// }
Function to deploy a new smart contract. The Bytecode must be defined in the constructor of the class
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}`);
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}`);
Optional
options: DeployOptionsEncondes a contract operation using Koinos serialization and taking the contract entries as reference to build it
Operation encoded
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",
// }
// }
Operation to encode
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.
the new ABI saved in the contract
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.
Generated using TypeDoc
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