From 9e28b56b0b156c5cfbb55f331e1b14130e8432b8 Mon Sep 17 00:00:00 2001 From: Lyubomir Kiprov Date: Sun, 2 Jun 2019 13:24:38 +0300 Subject: [PATCH 1/4] Fix issue https://github.com/LimeChain/eoslime/issues/15 --- Readme.md | 21 +++++++++------------ example/eosio-token/usage-example.js | 5 ----- src/account/account-factory.js | 13 ++++++++++++- src/account/account.js | 8 ++++---- tests/account-tests.js | 14 +++++++------- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/Readme.md b/Readme.md index f5e2116..ffb0d1f 100644 --- a/Readme.md +++ b/Readme.md @@ -61,11 +61,6 @@ describe('EOSIO Token', function () { const HOLDER_SUPPLY = '100.0000 SYS'; before(async () => { - /* - Accounts loader generates random accounts for easier testing - But you could use it also to create new accounts on each network - For more details, visit the documentation - */ let accounts = await eoslime.Account.createRandoms(2); tokensIssuer = accounts[0]; tokensHolder = accounts[1]; @@ -251,7 +246,7 @@ Account is a class that provides an easy access to blockchain account endpoint. let account2 = eoslime.Account.load('myAcc2', 'myPrivateKey2'); // Payer will buy cpu and network for account2 for 100 SYS - await account2.buyBandwidth(100, 100, payer); + await account2.buyBandwidth('100 SYS', '100 SYS'', payer); ``` *Defaults:* * `payer` - current account @@ -262,33 +257,34 @@ Account is a class that provides an easy access to blockchain account endpoint. let account = eoslime.Account.load('myAcc', 'myPrivateKey'); // The account will buy cpu and net by self for 10 SYS - await account.buyBandwidth(10, 10); + await account.buyBandwidth('10 SYS', '10 SYS'); ``` -* **send (toAccount, amount)** - send EOS tokens(SYS) to another account +* **send (toAccount, amount)** - send EOS tokens to another account ```javascript const eoslime = require('eoslime').init(); // Existing accounts on local network let sender = eoslime.Account.load('myAcc1', 'myPrivateKey1'); let receiver = eoslime.Account.load('myAcc2', 'myPrivateKey2'); - // The sender will send 100 SYS tokens to receiver + // The sender will send 100 EOS tokens to receiver await sender.send(receiver, 100); ``` -* **getBalance (code, symbol)** - get the account balance for token with symbol +* **getBalance (symbol, code)** - get the account balance for token with symbol ```javascript const eoslime = require('eoslime').init(); // Existing accounts on local network let account = eoslime.Account.load('myAcc1', 'myPrivateKey1'); // custom.token is a contract account with a token deployed on it - await account.getBalance(custom.token, 'Custom'); + await account.getBalance('CUSTOM', custom.token); ``` *Defaults:* +* `symbol` - EOS * `code` - eosio.token -* `symbol` - SYS + ```javascript const eoslime = require('eoslime').init(); // Existing accounts on local network @@ -299,6 +295,7 @@ Account is a class that provides an easy access to blockchain account endpoint. #### 3. Static account properties +***Note! Each of the create-account functions, behind the scene executes also buyRam transaction for 8192 bytes*** * **createFromName (name, accountCreator)** - Creates a fresh new account for a given name **Important!** Keep in mind that this name may already exists on the network diff --git a/example/eosio-token/usage-example.js b/example/eosio-token/usage-example.js index 46c8d23..d1746dc 100644 --- a/example/eosio-token/usage-example.js +++ b/example/eosio-token/usage-example.js @@ -17,11 +17,6 @@ describe('EOSIO Token', function () { const HOLDER_SUPPLY = '100.0000 SYS'; before(async () => { - /* - Accounts loader generates random accounts for easier testing - But you could use it also to create new accounts on each network - For more details, visit the documentation - */ let accounts = await eoslime.Account.createRandoms(2); tokensIssuer = accounts[0]; tokensHolder = accounts[1]; diff --git a/src/account/account-factory.js b/src/account/account-factory.js index fd89f54..88e2254 100644 --- a/src/account/account-factory.js +++ b/src/account/account-factory.js @@ -70,9 +70,13 @@ class AccountFactory { let decryptedAccount = JSON.parse(JSON.stringify(encryptedAccount)); let pureData = crypto.decrypt(encryptedAccount.cipherText, password); - let dataParts = pureData.split('::'); + if (!pureData) { + throw new Error('Couldn\'t decrypt the data'); + } delete decryptedAccount.cipherText; + + let dataParts = pureData.split('::'); decryptedAccount.privateKey = dataParts[0]; decryptedAccount.network = this.provider.network; @@ -97,6 +101,13 @@ let createAccountOnBlockchain = async function (accountToBeCreated, accountCreat owner: accountToBeCreated.publicKey, active: accountToBeCreated.publicKey }); + + tr.buyrambytes({ + payer: accountCreator.name, + receiver: accountToBeCreated.name, + bytes: 8192 + }); + }, { keyProvider: accountCreator.privateKey }); } diff --git a/src/account/account.js b/src/account/account.js index 7c2d54d..dac0986 100644 --- a/src/account/account.js +++ b/src/account/account.js @@ -41,8 +41,8 @@ class Account { tr.delegatebw({ from: payer.name, receiver: this.name, - stake_cpu_quantity: `${cpu} SYS`, - stake_net_quantity: `${net} SYS`, + stake_cpu_quantity: cpu, + stake_net_quantity: net, transfer: 0 }); }, { keyProvider: payer.privateKey }); @@ -54,13 +54,13 @@ class Account { return this.provider.eos.transfer( this.name, toAccount.name, - `${amount} SYS`, + `${amount} EOS`, this.permissions.active, { broadcast: true, sign: true, keyProvider: this.privateKey } ); } - async getBalance(code = 'eosio.token', symbol = 'SYS') { + async getBalance(symbol = 'EOS', code = 'eosio.token') { return this.provider.eos.getCurrencyBalance(code, this.name, symbol); } } diff --git a/tests/account-tests.js b/tests/account-tests.js index b13bfb3..171555e 100644 --- a/tests/account-tests.js +++ b/tests/account-tests.js @@ -146,7 +146,7 @@ describe('Account', function () { } }); - it('Should load ram [payer]', async () => { + it('Should buy ram [payer]', async () => { let payer = Account.load(ACCOUNT_NAME, ACCOUNT_PRIVATE_KEY); let account = await Account.createRandom(); @@ -154,7 +154,7 @@ describe('Account', function () { assert(tx.transaction.transaction.actions[0].name == 'buyrambytes', 'Incorrect buy ram transaction'); }); - it('Should load ram by self', async () => { + it('Should buy ram by self', async () => { let eosAccount = Account.load(ACCOUNT_NAME, ACCOUNT_PRIVATE_KEY); let account = await Account.createRandom(); @@ -176,22 +176,22 @@ describe('Account', function () { } }); - it('Should load bandwidth [payer]', async () => { + it('Should buy bandwidth [payer]', async () => { let payer = Account.load(ACCOUNT_NAME, ACCOUNT_PRIVATE_KEY); let account = await Account.createRandom(); - let tx = await account.buyBandwidth(10, 10, payer); + let tx = await account.buyBandwidth('10.0000 SYS', '10.0000 SYS', payer); assert(tx.transaction.transaction.actions[0].name == 'delegatebw', 'Incorrect buy bandwidth transaction'); }); - it('Should load bandwidth by self', async () => { + it('Should buy bandwidth by self', async () => { let eosAccount = Account.load(ACCOUNT_NAME, ACCOUNT_PRIVATE_KEY); let account = await Account.createRandom(); // Send 10 EOS to the account in order to have enough balance to pay for his bandwidth await eosAccount.send(account, '10.0000'); - let tx = await account.buyBandwidth(10, 19); + let tx = await account.buyBandwidth('10 SYS', '10 SYS'); assert(tx.transaction.transaction.actions[0].name == 'delegatebw', 'Incorrect buy bandwidth transaction'); }); @@ -341,7 +341,7 @@ describe('Account', function () { let encryptedJSONAccount; const PASSWORD = 'secret password'; - before(async () => { + beforeEach(async () => { encryptedJSONAccount = await Account.createEncrypted(PASSWORD); }); From 2c8ca3a0e98dd882189d74cecbfee14d765b4bf9 Mon Sep 17 00:00:00 2001 From: Lyubomir Kiprov Date: Sun, 2 Jun 2019 13:27:05 +0300 Subject: [PATCH 2/4] Update from SYS to EOS --- Readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index ffb0d1f..197250c 100644 --- a/Readme.md +++ b/Readme.md @@ -245,8 +245,8 @@ Account is a class that provides an easy access to blockchain account endpoint. let payer = eoslime.Account.load('myAcc1', 'myPrivateKey1'); let account2 = eoslime.Account.load('myAcc2', 'myPrivateKey2'); - // Payer will buy cpu and network for account2 for 100 SYS - await account2.buyBandwidth('100 SYS', '100 SYS'', payer); + // Payer will buy cpu and network for account2 for 100 EOS + await account2.buyBandwidth('100 EOS', '100 EOS'', payer); ``` *Defaults:* * `payer` - current account @@ -256,8 +256,8 @@ Account is a class that provides an easy access to blockchain account endpoint. // Existing account on local network let account = eoslime.Account.load('myAcc', 'myPrivateKey'); - // The account will buy cpu and net by self for 10 SYS - await account.buyBandwidth('10 SYS', '10 SYS'); + // The account will buy cpu and net by self for 10 EOS + await account.buyBandwidth('10 EOS', '10 EOS'); ``` * **send (toAccount, amount)** - send EOS tokens to another account From 8457ac8a8b5ec02e7f0629d3b1cb5674dee74a23 Mon Sep 17 00:00:00 2001 From: Lyubomir Kiprov Date: Mon, 10 Jun 2019 11:00:28 +0300 Subject: [PATCH 3/4] Add support for Kylin testnet and change send method to accept symbol parameter --- package.json | 2 +- src/account/account.js | 8 ++++---- src/network-providers/kylin-provider.js | 16 ++++++++++++++++ src/network-providers/provider.js | 6 ++++-- tests/account-tests.js | 20 +++++++++++++++----- 5 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 src/network-providers/kylin-provider.js diff --git a/package.json b/package.json index 6fe3c9d..5ca5f51 100644 --- a/package.json +++ b/package.json @@ -35,4 +35,4 @@ "devDependencies": { "nyc": "^14.1.1" } -} +} \ No newline at end of file diff --git a/src/account/account.js b/src/account/account.js index dac0986..f5f3874 100644 --- a/src/account/account.js +++ b/src/account/account.js @@ -48,13 +48,13 @@ class Account { }, { keyProvider: payer.privateKey }); } - async send(toAccount, amount) { - is(toAccount).instanceOf(Account); + async send(receiver, amount, symbol = 'EOS') { + is(receiver).instanceOf(Account); return this.provider.eos.transfer( this.name, - toAccount.name, - `${amount} EOS`, + receiver.name, + `${amount} ${symbol}`, this.permissions.active, { broadcast: true, sign: true, keyProvider: this.privateKey } ); diff --git a/src/network-providers/kylin-provider.js b/src/network-providers/kylin-provider.js new file mode 100644 index 0000000..0aa2ea8 --- /dev/null +++ b/src/network-providers/kylin-provider.js @@ -0,0 +1,16 @@ + +const BaseProvider = require('./base-provider'); + +const KylinNetworkConfig = { + name: 'kylin', + url: 'https://kylin.eoscanada.com', + chainId: '5fff1dae8dc8e2fc4d5b23b2c7665c97f9e9d8edf2b6485a86ba311c25639191' +} + +class KylinProvider extends BaseProvider { + constructor() { + super(KylinNetworkConfig) + } +} + +module.exports = KylinProvider \ No newline at end of file diff --git a/src/network-providers/provider.js b/src/network-providers/provider.js index bf1c030..6493396 100644 --- a/src/network-providers/provider.js +++ b/src/network-providers/provider.js @@ -1,16 +1,18 @@ const BosProvider = require('./bos-provider') const MainProvider = require('./main-provider') +const KylinProvider = require('./kylin-provider') const LocalProvider = require('./local-provider') const JungleProvider = require('./jungle-provider') const WorbliProvider = require('./worbli-provider') const CustomProvider = require('./custom-provider') const NETWORKS = { + bos: () => { return new BosProvider() }, + main: () => { return new MainProvider() }, + kylin: () => { return new KylinProvider() }, local: () => { return new LocalProvider() }, jungle: () => { return new JungleProvider() }, - bos: () => { return new BosProvider() }, worbli: () => { return new WorbliProvider() }, - main: () => { return new MainProvider() }, custom: (networkConfig) => { return new CustomProvider(networkConfig) } } diff --git a/tests/account-tests.js b/tests/account-tests.js index 171555e..245d67d 100644 --- a/tests/account-tests.js +++ b/tests/account-tests.js @@ -37,6 +37,11 @@ const Networks = { url: 'https://eos.greymass.com', chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906' }, + kylin: { + name: 'kylin', + url: 'https://kylin.eoscanada.com', + chainId: '5fff1dae8dc8e2fc4d5b23b2c7665c97f9e9d8edf2b6485a86ba311c25639191' + }, custom: { name: 'custom', url: 'https://custom.com', @@ -118,6 +123,11 @@ describe('Account', function () { let CustomAccount = eoslime.init({ url: Networks.custom.url, chainId: Networks.custom.chainId }).Account; let customAccount = CustomAccount.load(ACCOUNT_NAME, ACCOUNT_PRIVATE_KEY); assertCorrectAccount(customAccount); + + // Kylin + let KylinAccount = eoslime.init('kylin').Account; + let kylinAccount = KylinAccount.load(ACCOUNT_NAME, ACCOUNT_PRIVATE_KEY); + assertCorrectAccount(kylinAccount); }); it('Should send EOS tokens', async () => { @@ -128,9 +138,9 @@ describe('Account', function () { let receiverBalanceBeforeSend = await receiverAccount.getBalance(); assert(receiverBalanceBeforeSend == 0, 'Incorrect tokens amount before send'); - await senderAccount.send(receiverAccount, SEND_AMOUNT); + await senderAccount.send(receiverAccount, SEND_AMOUNT, 'SYS'); - let receiverBalanceAfterSend = await receiverAccount.getBalance(); + let receiverBalanceAfterSend = await receiverAccount.getBalance('SYS'); assert(receiverBalanceAfterSend[0] == `${SEND_AMOUNT} SYS`, 'Incorrect tokens amount after send'); }); @@ -138,7 +148,7 @@ describe('Account', function () { try { const SEND_AMOUNT = '10.0000'; let senderAccount = Account.load(ACCOUNT_NAME, ACCOUNT_PRIVATE_KEY); - await senderAccount.send('Fake account', SEND_AMOUNT); + await senderAccount.send('Fake account', SEND_AMOUNT, 'SYS'); assert(false, 'Should throw'); } catch (error) { @@ -159,7 +169,7 @@ describe('Account', function () { let account = await Account.createRandom(); // Send 10 EOS to the account in order to have enough balance to pay for his ram - await eosAccount.send(account, '10.0000'); + await eosAccount.send(account, '10.0000', 'SYS'); let tx = await account.buyRam(1000); assert(tx.transaction.transaction.actions[0].name == 'buyrambytes', 'Incorrect buy ram transaction'); @@ -189,7 +199,7 @@ describe('Account', function () { let account = await Account.createRandom(); // Send 10 EOS to the account in order to have enough balance to pay for his bandwidth - await eosAccount.send(account, '10.0000'); + await eosAccount.send(account, '10.0000', 'SYS'); let tx = await account.buyBandwidth('10 SYS', '10 SYS'); assert(tx.transaction.transaction.actions[0].name == 'delegatebw', 'Incorrect buy bandwidth transaction'); From c68a1773e7cefd98574e09d0b0ceb9d650810dc5 Mon Sep 17 00:00:00 2001 From: Lyubomir Kiprov Date: Mon, 10 Jun 2019 11:10:59 +0300 Subject: [PATCH 4/4] Update the documentation --- Readme.md | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Readme.md b/Readme.md index 197250c..1c4e118 100644 --- a/Readme.md +++ b/Readme.md @@ -17,7 +17,7 @@ npm install eoslime ```javascript /* - Supported Networks: [ local ] [ jungle ] [ bos ] [ worbli ] [ main ] or { url: 'custom url', chainId: 'custom id' } + Supported Networks: [ local ] [ jungle ] [ bos ] [ worbli ] [ main ] [kylin] or { url: 'custom url', chainId: 'custom id' } */ const eoslime = require('eoslime').init('local'); @@ -136,7 +136,7 @@ describe('EOSIO Token', function () { ## Eoslime initialization --- ***Parameters:*** -* network name - **[ local ] [ jungle ] [ bos ] [ worbli ] [ main ] or { url: 'custom url', chainId: 'custom id' }** +* network name - **[ local ] [ jungle ] [ bos ] [ worbli ] [ main ] [kylin] or { url: 'custom url', chainId: 'custom id' }** --- ##### Initialization with default parameters: @@ -246,7 +246,7 @@ Account is a class that provides an easy access to blockchain account endpoint. let account2 = eoslime.Account.load('myAcc2', 'myPrivateKey2'); // Payer will buy cpu and network for account2 for 100 EOS - await account2.buyBandwidth('100 EOS', '100 EOS'', payer); + await account2.buyBandwidth('100.0000 EOS', '100.0000 EOS'', payer); ``` *Defaults:* * `payer` - current account @@ -257,10 +257,10 @@ Account is a class that provides an easy access to blockchain account endpoint. let account = eoslime.Account.load('myAcc', 'myPrivateKey'); // The account will buy cpu and net by self for 10 EOS - await account.buyBandwidth('10 EOS', '10 EOS'); + await account.buyBandwidth('10.0000 EOS', '10.0000 EOS'); ``` -* **send (toAccount, amount)** - send EOS tokens to another account +* **send (receiver, amount, symbol)** - send EOS tokens to another account ```javascript const eoslime = require('eoslime').init(); // Existing accounts on local network @@ -268,7 +268,19 @@ Account is a class that provides an easy access to blockchain account endpoint. let receiver = eoslime.Account.load('myAcc2', 'myPrivateKey2'); // The sender will send 100 EOS tokens to receiver - await sender.send(receiver, 100); + await sender.send(receiver, `100.0000`, 'EOS'); +``` + +*Defaults:* +* `symbol` - EOS +```javascript + const eoslime = require('eoslime').init(); + // Existing accounts on local network + let sender = eoslime.Account.load('myAcc1', 'myPrivateKey1'); + let receiver = eoslime.Account.load('myAcc2', 'myPrivateKey2'); + + // The sender will send 100 EOS tokens to receiver + await sender.send(receiver, `100.0000`); ``` * **getBalance (symbol, code)** - get the account balance for token with symbol