Eoslime
Search…
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!
1
const assert = require('assert');
2
3
const TOKEN_WASM_PATH = './example/eosio-token/contract/eosio.token.wasm';
4
const TOKEN_ABI_PATH = './example/eosio-token/contract/eosio.token.abi';
5
6
describe('EOSIO Token', function (eoslime) {
7
8
// Increase mocha(testing framework) time, otherwise tests fails
9
this.timeout(15000);
10
11
let tokenContract;
12
let tokensIssuer;
13
let tokensHolder;
14
15
const TOTAL_SUPPLY = '1000000000.0000 SYS';
16
const HOLDER_SUPPLY = '100.0000 SYS';
17
18
before(async () => {
19
let accounts = await eoslime.Account.createRandoms(2);
20
tokensIssuer = accounts[0];
21
tokensHolder = accounts[1];
22
});
23
24
beforeEach(async () => {
25
/*
26
`deploy` creates for you a new account behind the scene
27
on which the contract code is deployed
28
29
You can access the contract account as -> tokenContract.executor
30
*/
31
tokenContract = await eoslime.Contract.deploy(TOKEN_WASM_PATH, TOKEN_ABI_PATH);
32
});
33
34
it('Should create a new token', async () => {
35
await tokenContract.actions.create(tokensIssuer.name, TOTAL_SUPPLY);
36
37
/*
38
You have access to the EOS(eosjs) instance:
39
eoslime.Provider.eos
40
*/
41
let tokenInitialization = await tokenContract.provider.eos.getCurrencyStats(tokenContract.name, 'SYS');
42
43
assert.equal(tokenInitialization.SYS.max_supply, TOTAL_SUPPLY, 'Incorrect tokens supply');
44
assert.equal(tokenInitialization.SYS.issuer, tokensIssuer.name, 'Incorrect tokens issuer');
45
});
46
47
it('Should issue tokens', async () => {
48
await tokenContract.actions.create(tokensIssuer.name, TOTAL_SUPPLY);
49
await tokenContract.actions.issue(tokensHolder.name, HOLDER_SUPPLY, 'memo', { from: tokensIssuer });
50
51
let holderBalance = await tokensHolder.getBalance('SYS', tokenContract.name);
52
assert.equal(holderBalance[0], HOLDER_SUPPLY, 'Incorrect holder balance');
53
});
54
55
it('Should throw if tokens quantity is negative', async () => {
56
await tokenContract.actions.create(tokensIssuer.name, TOTAL_SUPPLY);
57
const INVALID_ISSUING_AMOUNT = '-100.0000 SYS';
58
59
/*
60
eoslime provides you testing utils (Available only if testing with `eoslime test`)
61
*/
62
await eoslime.tests.expectAssert(
63
tokenContract.actions.issue(tokensHolder.name, INVALID_ISSUING_AMOUNT, 'memo', { from: tokensIssuer })
64
);
65
66
let holderBalance = await tokensHolder.getBalance('SYS', tokenContract.name);
67
assert.equal(holderBalance.length, 0, 'Incorrect holder balance');
68
});
69
});
Copied!

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
1
git clone https://github.com/theblockstalk/eosio-contracts.git
2
cd commit_hash_reveal
3
npm install
Copied!
  • Install mocha if you don't have it
1
npm install mocha
Copied!
  • Have running local nodeos
  • Compile the contract
1
eosio-cpp -I contract/include -R resource -o build/commitreveal.wasm -contract commitreveal ./contract/src/commitreveal.cpp --abigent
Copied!
  • Run the tests
1
mocha helloworld11.test.js
Copied!
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
1
const assert = require('assert');
2
const crypto = require("./crypto");
3
4
const CONTRACT_WASM_PATH = '../build/commitreveal.wasm';
5
const CONTRACT_ABI_PATH = '../build/commitreveal.abi';
6
7
describe('Commit and reveal', function (eoslime) {
8
// Increase mocha(testing framework) time, otherwise tests fails
9
this.timeout(15000);
10
11
let contractAccount;
12
let myAccount;
13
let messagesTable;
14
15
before(async () => {
16
// Create a random account on chain
17
myAccount = await eoslime.Account.createRandom();
18
});
19
20
beforeEach(async () => {
21
// Deploy a contract
22
contractAccount = await eoslime.Contract.deploy(CONTRACT_WASM_PATH, CONTRACT_ABI_PATH);
23
messagesTable = contractAccount.tables.messages;
24
});
25
26
it('Should store the hash', async () => {
27
// Find last ten messages
28
let messages = await messagesTable.limit(10).find();
29
assert.equal(messages.length, 0, "Should not have any rows yet");
30
31
const messageString = "hello world";
32
const messageHash = crypto.sha256(messageString);
33
34
// Broadcast a 'commit' transaction to the chain
35
await contractAccount.actions.commit(myAccount.name, messageHash, { from: myAccount });
36
37
// Get all messages related to my
38
messages = await messagesTable.equal(myAccount.name).find();
39
40
const message = messages[0];
41
assert.equal(message.user, myAccount.name, "account name not correct");
42
assert.equal(message.hash, messageHash, "hash was not stored in the table");
43
});
44
45
it('Should verify the hash', async () => {
46
const messageString = "hello world";
47
const messageHash = crypto.sha256(messageString);
48
49
await contractAccount.actions.commit(myAccount.name, messageHash, { from: myAccount });
50
51
messages = await messagesTable.equal(myAccount.name).find();
52
assert.equal(messages.length, 1, "hash was not added to the table");
53
54
await contractAccount.actions.reveal(myAccount.name, messageString);
55
56
messages = await messagesTable.equal(myAccount.name).find();
57
assert.equal(messages.length, 0, "hash was not removed from the table");
58
});
59
});
Copied!

Last modified 1yr ago