Skip to content

Commit

Permalink
Merge pull request #18 from LimeChain/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
bakasura980 authored Jun 10, 2019
2 parents f777f36 + c68a177 commit c64bf6e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 45 deletions.
43 changes: 26 additions & 17 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions example/eosio-token/usage-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
"devDependencies": {
"nyc": "^14.1.1"
}
}
}
13 changes: 12 additions & 1 deletion src/account/account-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 });
}

Expand Down
14 changes: 7 additions & 7 deletions src/account/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/network-providers/kylin-provider.js
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions src/network-providers/provider.js
Original file line number Diff line number Diff line change
@@ -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) }
}

Expand Down
34 changes: 22 additions & 12 deletions tests/account-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 () => {
Expand All @@ -128,38 +138,38 @@ 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');
});

it('Should throw if one tries to send EOS tokens to incorrect account', async () => {
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) {
assert(error.message.includes('Provided String is not an instance of Account'));
}
});

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();

let tx = await account.buyRam(1000, payer);
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');
Expand All @@ -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');
});

Expand Down Expand Up @@ -341,7 +351,7 @@ describe('Account', function () {
let encryptedJSONAccount;
const PASSWORD = 'secret password';

before(async () => {
beforeEach(async () => {
encryptedJSONAccount = await Account.createEncrypted(PASSWORD);
});

Expand Down

0 comments on commit c64bf6e

Please sign in to comment.