diff --git a/Readme.md b/Readme.md index f5e2116..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'); @@ -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]; @@ -141,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: @@ -250,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, 100, payer); + // Payer will buy cpu and network for account2 for 100 EOS + await account2.buyBandwidth('100.0000 EOS', '100.0000 EOS'', payer); ``` *Defaults:* * `payer` - current account @@ -261,34 +256,47 @@ 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, 10); + // The account will buy cpu and net by self for 10 EOS + await account.buyBandwidth('10.0000 EOS', '10.0000 EOS'); ``` -* **send (toAccount, amount)** - send EOS tokens(SYS) to another account +* **send (receiver, amount, symbol)** - 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 - await sender.send(receiver, 100); + // The sender will send 100 EOS tokens to receiver + await sender.send(receiver, `100.0000`, 'EOS'); ``` -* **getBalance (code, symbol)** - get the account balance for token with symbol +*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 ```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 +307,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/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-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..f5f3874 100644 --- a/src/account/account.js +++ b/src/account/account.js @@ -41,26 +41,26 @@ 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 }); } - 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} SYS`, + receiver.name, + `${amount} ${symbol}`, 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/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 b13bfb3..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) { @@ -146,7 +156,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,12 +164,12 @@ 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(); // 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'); @@ -176,22 +186,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'); + await eosAccount.send(account, '10.0000', 'SYS'); - 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 +351,7 @@ describe('Account', function () { let encryptedJSONAccount; const PASSWORD = 'secret password'; - before(async () => { + beforeEach(async () => { encryptedJSONAccount = await Account.createEncrypted(PASSWORD); });