Contract

Properties

  • abi

    For convenience you have access to the contract ABI in JSON format

  • name

    For convenience you have access to the contract name

  • provider

    For convenience you have access to the network provider

  • executor

    The account which will execute contract methods (transactions) on the blockchain

Functions

Blockchain Contract Actions

let contract = eoslime.Contract.at(ABI_PATH, CONTRACT_NAME, EXECUTOR_1);
// EXECUTOR_1 will execute `doSmth` transaction on the blockchain
await contract.actions.doSmth('Something');

eoslime is based on eosjs and when you are calling a contract action, options { broadcast: true, sign: true } are always set to true

Options

Each blockchain contract action has the following options which could be provided as an object after the function arguments

  • from

    If you want to call a contract method from another account(executor) you can do

// Local network initialization
const eoslime = require('eoslime').init();
const CONTRACT_NAME = 'mycontract';
const ABI_PATH = './contract/contract.abi';
// Pre-created local network accounts
const EXECUTOR_1 = eoslime.Account.load('myacc1', 'privateKey1');
const EXECUTOR_2 = eoslime.Account.load('myacc2', 'privateKey2');
let contract = eoslime.Contract.at(ABI_PATH, CONTRACT_NAME, EXECUTOR_1);
// EXECUTOR_1 will execute `doSmth` transaction on the blockchain
await contract.actions.doSmth('Something');
// EXECUTOR_2 will execute `doSmth` transaction on the blockchain
await contract.actions.doSmth('Something', { from: EXECUTOR_2 });
// EXECUTOR_1 will execute `doSmth` transaction on the blockchain
await contract.actions.doSmth('Something');
  • unique

    Nonce action support. Solve the duplicate transaction error

// Local network initialization
const eoslime = require('eoslime').init();
const CONTRACT_NAME = 'mycontract';
const ABI_PATH = './contract/contract.abi';
// Pre-created local network accounts
const EXECUTOR_1 = eoslime.Account.load('myacc1', 'privateKey1');
let contract = eoslime.Contract.at(ABI_PATH, CONTRACT_NAME, EXECUTOR_1);
await contract.actions.doSmth('Something');
// Execute `doSmth` one more time with same parameters
await contract.actions.doSmth('Something', { unique: true });
  • token

    There are cases, where you need to execute a contract function and pay some tokens, but this could be done by processing two transactions. The first one is to your contract, the second one is to eosio.token contract. But what about if the tokens transfer reverts and the transaction to your contract is successful. That is what payable contract actions are purposed for. You should be able to execute an atomic transaction constructed by both actions above.

// Local network initialization
const eoslime = require('eoslime').init();
const CONTRACT_NAME = 'mycontract';
const ABI_PATH = './contract/contract.abi';
// Pre-created local network accounts
const user1 = eoslime.Account.load('myacc1', 'privateKey1');
let contract = eoslime.Contract.at(ABI_PATH, CONTRACT_NAME, user1);
// Execute `doSmth` and transfer 5.0000 SYS tokens to the contract at once(atomically)
await contract.actions.doSmth('Your args here', { from: user1, tokens: '5.0000 SYS' });

makeInline

Add 'eosio.code' permission to the current account authority. It let the contract to make inline actions/calls to other contracts methods.

Will add eosio.code to the custom authority, if you have loaded your account with it.

let contract = eoslime.Contract.at(ABI_PATH, CONTRACT_NAME, CONTRACT_ACCOUNT);
// CONTRACT_ACCOUNT will obtains 'eosio.code' permission
await contract.makeInline();

getRawWASM()

Returns contract raw WASM. You could use it to deploy a new contract

const contract = eoslime.Contract.at(abi, CONTRACT_NAME);
const rawWASM = await contract.getRawWASM();
const newContract = await eoslime.Contract.deployRaw(rawWASM, abi);

Table getters

const faucetContract = eoslime.Contract.fromFile(FAUCET_ABI_PATH, faucetAccount.name, faucetAccount);
const withdrawers = faucetContract.tables.withdrawers;

In order to search in a contract table in an easy way - query chain style exists (Check Providers section). You can access a table by it's name from the ABI.

const faucetContract = eoslime.Contract.fromFile(FAUCET_ABI_PATH, faucetAccount.name, faucetAccount);
await faucetContract.produce(tokensHolder.name, "100.0000 TKNS", tokenContract.name, "memo");
const withdrawers = faucetContract.tables.withdrawers;
// With equal criteria
const equalResult = await withdrawers.equal(tokensHolder.name).find();
// With range criteria
const rangeResult = await withdrawers.range(0, 100 * TOKEN_PRECISION).index(2).find();
// With limit
const allWithdrawers = await withdrawers.limit(10).find();
// With different index (By Balance)
const balanceWithdrawers = await withdrawers.equal(100 * TOKEN_PRECISION).index(2).find();

withdrawers is a contract table