Skip to content

Latest commit

 

History

History
64 lines (51 loc) · 2.05 KB

Contract.md

File metadata and controls

64 lines (51 loc) · 2.05 KB

Table of contents

Contract

This library for convenient work with Echo-contracts. Can be used to call contracts, sign and broadcast contracts' deployment and execution transactions.

Preparation

Launched echo node (https://github.com/echoprotocol/echo-core) with open port

Examples

Deploy contract

import { Echo, Contract, PrivateKey } from "echojs-lib";
const echo = new Echo();
await echo.connect('ws://127.0.0.1:6311');
const bytecode = '6080';
const privateKey = PrivateKey.fromWif('5K2YUVmWfxbmvsNxCsfvArXdGXm7d5DC9pn4yD75k2UaSYgkXTh');
const contractId = await Contract.deploy(bytecode, privateKey, { echo });
// or pass abi in options to get contract instance instead of new contract id
const contract = await Contract.deploy(bytecode, privateKey, { abi: require("abi.json"), echo });

Create contract instance by contract id

const contract = new Contract(abi, { echo, contractId: '1.14.123' });

Call contract no changing state

const result = await contract.methods.methodName(arg).call();

Execute contract

const broadcastingResult = await contract.methods.methodName(arg).broadcast({ privateKey });
const result = broadcastingResult.decodedResult;
// you can find more information about transaction in this fields:
const { transactionResult, contractResult, events } = broadcastingResult;

Using encoders/decoders

import { encode, BigNumber } from "echojs-contract";

encode({ value: false, type: 'bool' });
encode({ value: [1, '2', new BigNumber(3)], type: 'uint8[]' });

decode({
	value: '00000000000000000000000000000000fffffffffffffffffffb9d2ac2e07341',
	type: 'int128',
});
// returns new BigNumber(-1234567899876543)