Examples

eosio.token

One of the most used contracts in the EOS world at this time is the eosio.token one.

Here are the steps you would need to run the test example

  • have running local nodeos

  • eoslime init --with-example

  • eoslime test

Let's test it!

const assert = require('assert');
const TOKEN_WASM_PATH = './example/eosio-token/contract/eosio.token.wasm';
const TOKEN_ABI_PATH = './example/eosio-token/contract/eosio.token.abi';
describe('EOSIO Token', function (eoslime) {
// Increase mocha(testing framework) time, otherwise tests fails
this.timeout(15000);
let tokenContract;
let tokensIssuer;
let tokensHolder;
const TOTAL_SUPPLY = '1000000000.0000 SYS';
const HOLDER_SUPPLY = '100.0000 SYS';
before(async () => {
let accounts = await eoslime.Account.createRandoms(2);
tokensIssuer = accounts[0];
tokensHolder = accounts[1];
});
beforeEach(async () => {
/*
`deploy` creates for you a new account behind the scene
on which the contract code is deployed
You can access the contract account as -> tokenContract.executor
*/
tokenContract = await eoslime.Contract.deploy(TOKEN_WASM_PATH, TOKEN_ABI_PATH);
});
it('Should create a new token', async () => {
await tokenContract.create(tokensIssuer.name, TOTAL_SUPPLY);
/*
You have access to the EOS(eosjs) instance:
eoslime.Provider.eos
*/
let tokenInitialization = await tokenContract.provider.eos.getCurrencyStats(tokenContract.name, 'SYS');
assert.equal(tokenInitialization.SYS.max_supply, TOTAL_SUPPLY, 'Incorrect tokens supply');
assert.equal(tokenInitialization.SYS.issuer, tokensIssuer.name, 'Incorrect tokens issuer');
});
it('Should issue tokens', async () => {
await tokenContract.create(tokensIssuer.name, TOTAL_SUPPLY);
await tokenContract.issue(tokensHolder.name, HOLDER_SUPPLY, 'memo', { from: tokensIssuer });
let holderBalance = await tokensHolder.getBalance('SYS', tokenContract.name);
assert.equal(holderBalance[0], HOLDER_SUPPLY, 'Incorrect holder balance');
});
it('Should throw if tokens quantity is negative', async () => {
await tokenContract.create(tokensIssuer.name, TOTAL_SUPPLY);
const INVALID_ISSUING_AMOUNT = '-100.0000 SYS';
/*
eoslime provides you testing utils (Available only if testing with `eoslime test`)
*/
await eoslime.tests.expectAssert(
tokenContract.issue(tokensHolder.name, INVALID_ISSUING_AMOUNT, 'memo', { from: tokensIssuer })
);
let holderBalance = await tokensHolder.getBalance('SYS', tokenContract.name);
assert.equal(holderBalance.length, 0, 'Incorrect holder balance');
});
});

The famous "Hello World"

For the very beginning of every introduction of something new, there is always the famous "Hello World" example. However, this time our "Hello World" example is going to be a bit more blockchain related.

We are going to use the code of one member of our community. With many thanks to the Jack Tanner who prepared the below example for you.

Here are the steps you would need to run the example

  • Clone the repo

git clone https://github.com/theblockstalk/eosio-contracts.git
cd commit_hash_reveal
npm install
  • Install mocha if you don't have it

npm install mocha
  • Have running local nodeos

  • Compile the contract

eosio-cpp -I contract/include -R resource -o build/commitreveal.wasm -contract commitreveal ./contract/src/commitreveal.cpp --abigent
  • Run the tests

mocha helloworld11.test.js

We will have a smart contract serving the following functionality

  • commit hash to blockchain

  • another account can reveal the hash with original digest

Check out the full contract code here.

Let's look how to test the contract's behaviour

const assert = require('assert');
const crypto = require("./crypto");
const CONTRACT_WASM_PATH = '../build/commitreveal.wasm';
const CONTRACT_ABI_PATH = '../build/commitreveal.abi';
describe('Commit and reveal', function (eoslime) {
// Increase mocha(testing framework) time, otherwise tests fails
this.timeout(15000);
let contractAccount;
let myAccount;
let messagesTable;
before(async () => {
// Create a random account on chain
myAccount = await eoslime.Account.createRandom();
});
beforeEach(async () => {
// Deploy a contract
contractAccount = await eoslime.Contract.deploy(CONTRACT_WASM_PATH, CONTRACT_ABI_PATH);
messagesTable = contractAccount.messages;
});
it('Should store the hash', async () => {
// Find last ten messages
let messages = await messagesTable.limit(10).find();
assert.equal(messages.length, 0, "Should not have any rows yet");
const messageString = "hello world";
const messageHash = crypto.sha256(messageString);
// Broadcast a 'commit' transaction to the chain
await contractAccount.commit(myAccount.name, messageHash, { from: myAccount });
// Get all messages related to my
messages = await messagesTable.equal(myAccount.name).find();
const message = messages[0];
assert.equal(message.user, myAccount.name, "account name not correct");
assert.equal(message.hash, messageHash, "hash was not stored in the table");
});
it('Should verify the hash', async () => {
const messageString = "hello world";
const messageHash = crypto.sha256(messageString);
await contractAccount.commit(myAccount.name, messageHash, { from: myAccount });
messages = await messagesTable.equal(myAccount.name).find();
assert.equal(messages.length, 1, "hash was not added to the table");
await contractAccount.reveal(myAccount.name, messageString);
messages = await messagesTable.equal(myAccount.name).find();
assert.equal(messages.length, 0, "hash was not removed from the table");
});
});