Tutorial
A simple tutorial meeting you with eoslime
This tutorial is not runnable. Its purpose is only to show you some of the fundamental eoslime functionality
Let's up the level of difficulty.
Scenario: We are a simple Pizza shop. We have a contract and an account serving our shop.
Shop account has the following roles and responsibilities separated
- Cooker - only he should be able to notify when a new Pizza has been cooked
- Shop Manager - he could order more cooking products and withdraw the daily turnover. For the manager to withdraw the money, however, he should have the approval of the owners.
- 2 Owners - they could withdraw the daily turnover
The contract should have the following functionality
- Ready Pizza. It should send the cooked Pizza to its buyer.
- Order cooking products
- Order a Pizza. By ordering we are notifying our cooker to start making it
- Buy one piece of the whole Pizza per transaction
- Withdraw the daily turnover. At the end of the day, the shop manager should be able to withdraw the turnover
Our scenario has been set. Let's have fun and write some code
Define our tests border
const assert = require('assert');
const SHOP_WASM_PATH = './contracts/shopcontract.wasm';
const SHOP_ABI_PATH = './contracts/shopcontract.abi';
describe('Shop contract', function (eoslime) {
// Increase mocha(testing framework) time, otherwise tests fails
this.timeout(15000);
let shopContract;
let shopAccount;
let cooker;
before(async () => {
shopAccount = await eoslime.Account.createRandom();
shopContract = await eoslime.Contract.deploy(SHOP_WASM_PATH, SHOP_ABI_PATH);
});
});
Every owner will have a private/public keys pair. These pairs should be registered in the owner's authority of the account. In this way, only the owners will be able to manage the account.
const firstOwnerKeys = await eoslime.utils.generateKeys();
const secondOwnerKeys = await eoslime.utils.generateKeys();
We should load the shop account with the owner's authority so we will be able to modify the authorities in it. We need to load it because when creating a random account, it comes with generated private/public keys pair. This is some kind of a 'default' keys
shopAccount = eoslime.Account.load(shopAccount.name, shopAccount.executor.privateKey, 'owner');
await shopAccount.addAuthorityKey(firstOwnerKeys.publicKey)
await shopAccount.addAuthorityKey(secondOwnerKeys.publicKey);
Your final test suite for the above preparation could like such as
it('Should have two owners', async () => {
shopAccount = eoslime.Account.load(shopAccount.name, shopAccount.executor.privateKey, 'owner');
const firstOwnerKeys = await eoslime.utils.generateKeys();
const secondOwnerKeys = await eoslime.utils.generateKeys();
await shopAccount.addOnBehalfKey(firstOwnerKeys.publicKey)
await shopAccount.addOnBehalfKey(secondOwnerKeys.publicKey);
authorityInfo = await shopAccount.getAuthorityInfo();
// Check the owner authority has both owner keys
assert(authorityInfo.required_auth.keys.find((keyData) => { return keyData.key == keysPair.publicKey }));
});