Skip to content

Commit

Permalink
examples use dict as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks committed Jun 2, 2022
1 parent 0312177 commit 9290e24
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 190 deletions.
8 changes: 4 additions & 4 deletions examples/clean-state/__tests__/test-clean-state.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ test.afterEach(async t => {

test('Clean state after storing', async t => {
const { jsvm, cleanStateContract } = t.context.accounts;
await cleanStateContract.call(jsvm, 'call_js_contract', encodeCall(cleanStateContract.accountId, 'put', ['1', 1]), { attachedDeposit: '400000000000000000000000' });
const value1 = await jsvm.view('view_js_contract', encodeCall(cleanStateContract.accountId, 'get', ['1']));
await cleanStateContract.call(jsvm, 'call_js_contract', encodeCall(cleanStateContract.accountId, 'put', { key: '1', value: 1 }), { attachedDeposit: '400000000000000000000000' });
const value1 = await jsvm.view('view_js_contract', encodeCall(cleanStateContract.accountId, 'get', { key: '1' }));
t.is(value1, '1');
await cleanStateContract.call(jsvm, 'call_js_contract', encodeCall(cleanStateContract.accountId, 'clean', [['1']]), { attachedDeposit: '400000000000000000000000' });
const value2 = await jsvm.view('view_js_contract', encodeCall(cleanStateContract.accountId, 'get', ['1']));
await cleanStateContract.call(jsvm, 'call_js_contract', encodeCall(cleanStateContract.accountId, 'clean', { keys: ['1'] }), { attachedDeposit: '400000000000000000000000' });
const value2 = await jsvm.view('view_js_contract', encodeCall(cleanStateContract.accountId, 'get', { key: '1' }));
t.is(value2, null);
});
6 changes: 3 additions & 3 deletions examples/clean-state/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { NearContract, NearBindgen, call, view, near, LookupMap } from 'near-sdk
@NearBindgen
class CleanState extends NearContract {
@call
clean(keys) {
clean({keys}) {
keys.forEach(key => near.jsvmStorageRemove(key))
}

@call
put(key, value) {
put({key, value}) {
near.jsvmStorageWrite(key, value)
}

@view
get(key) {
get({key}) {
return near.jsvmStorageRead(key)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Worker } from 'near-workspaces';
import {readFile} from 'fs/promises'
import { readFile } from 'fs/promises'
import test from 'ava';

// TODO: make this function part of the npm package when it is available
Expand All @@ -23,15 +23,15 @@ test.beforeEach(async t => {
// Deploy status-message JS contract
const statusMessageContract = await root.createSubAccount('status-message');
let statusContractBase64 = (await readFile('res/status-message.base64')).toString();
await statusMessageContract.call(jsvm, 'deploy_js_contract', Buffer.from(statusContractBase64, 'base64'), {attachedDeposit: '400000000000000000000000'});
await statusMessageContract.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'init', []), {attachedDeposit: '400000000000000000000000'});
await statusMessageContract.call(jsvm, 'deploy_js_contract', Buffer.from(statusContractBase64, 'base64'), { attachedDeposit: '400000000000000000000000' });
await statusMessageContract.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'init', []), { attachedDeposit: '400000000000000000000000' });

// Deploy on-call contrat
const onCallContract = await root.createSubAccount('on-call');
let cross_cc_contract_base64 = (await readFile('build/contract.base64')).toString();
await onCallContract.call(jsvm, 'deploy_js_contract', Buffer.from(cross_cc_contract_base64, 'base64'), {attachedDeposit: '400000000000000000000000'});
await onCallContract.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'init', []), {attachedDeposit: '400000000000000000000000'});
await onCallContract.call(jsvm, 'deploy_js_contract', Buffer.from(cross_cc_contract_base64, 'base64'), { attachedDeposit: '400000000000000000000000' });
await onCallContract.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'init', []), { attachedDeposit: '400000000000000000000000' });

// Create test accounts
const ali = await root.createSubAccount('ali');
const bob = await root.createSubAccount('bob');
Expand All @@ -56,36 +56,36 @@ test.afterEach(async t => {

test('Nobody is on-call in the beginning', async t => {
const { jsvm, onCallContract } = t.context.accounts;
const result = await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', []));
const result = await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', {}));
t.is(result, 'undefined');
});

test('Person can be set on-call if AVAILABLE', async t => {
const { ali, bob, jsvm, statusMessageContract, onCallContract } = t.context.accounts;

// Ali set her status as AVAILABLE
await ali.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'set_status', ['AVAILABLE']), {attachedDeposit: '100000000000000000000000'});
await ali.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'set_status', { status: 'AVAILABLE' }), { attachedDeposit: '100000000000000000000000' });
// Bob sets Ali on-call
await bob.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'set_person_on_call', [ali.accountId]), {attachedDeposit: '100000000000000000000000'});
await bob.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'set_person_on_call', { accountId: ali.accountId }), { attachedDeposit: '100000000000000000000000' });

// Check that Ali is on-call
t.is(
await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', [])),
await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', {})),
ali.accountId
);
});

test('Person can NOT be set on-call if UNAVAILABLE', async t => {
const { ali, bob, jsvm, statusMessageContract, onCallContract } = t.context.accounts;

// Ali set her status as AVAILABLE
await ali.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'set_status', ['UNAVAILABLE']), {attachedDeposit: '100000000000000000000000'});
await ali.call(jsvm, 'call_js_contract', encodeCall(statusMessageContract.accountId, 'set_status', { status: 'UNAVAILABLE' }), { attachedDeposit: '100000000000000000000000' });
// Bob tries to sets Ali on-call
await bob.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'set_person_on_call', [ali.accountId]), {attachedDeposit: '100000000000000000000000'});
await bob.call(jsvm, 'call_js_contract', encodeCall(onCallContract.accountId, 'set_person_on_call', { accountId: ali.accountId }), { attachedDeposit: '100000000000000000000000' });

// Check that Ali is NOT on-call
t.not(
await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', [])),
await jsvm.view('view_js_contract', encodeCall(onCallContract.accountId, 'person_on_call', {})),
ali.accountId
);
});
2 changes: 1 addition & 1 deletion examples/cross-contract-call/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OnCall extends NearContract {
}

@call
set_person_on_call(accountId) {
set_person_on_call({accountId}) {
near.log(`Trying to set ${accountId} on-call`)
const status = near.jsvmCall('status-message.test.near', 'get_status', [accountId])
near.log(`${accountId} status is ${status}`)
Expand Down
18 changes: 9 additions & 9 deletions examples/fungible-token/__tests__/test-fungible-token.ava.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Worker } from 'near-workspaces';
import {readFile} from 'fs/promises'
import { readFile } from 'fs/promises'
import test from 'ava';

// TODO: make this function part of the npm package when it is available
Expand All @@ -23,8 +23,8 @@ test.beforeEach(async t => {
// Deploy fungible token contract
const fungibleTokenContract = await root.createSubAccount('fungible-token');
let ftContractBase64 = (await readFile('build/contract.base64')).toString();
await fungibleTokenContract.call(jsvm, 'deploy_js_contract', Buffer.from(ftContractBase64, 'base64'), {attachedDeposit: '400000000000000000000000'});
await fungibleTokenContract.call(jsvm, 'call_js_contract', encodeCall(fungibleTokenContract.accountId, 'init', ['a', '1000']), {attachedDeposit: '400000000000000000000000'});
await fungibleTokenContract.call(jsvm, 'deploy_js_contract', Buffer.from(ftContractBase64, 'base64'), { attachedDeposit: '400000000000000000000000' });
await fungibleTokenContract.call(jsvm, 'call_js_contract', encodeCall(fungibleTokenContract.accountId, 'init', ['a', '1000']), { attachedDeposit: '400000000000000000000000' });

// Create test accounts
const ali = await root.createSubAccount('ali');
Expand All @@ -49,24 +49,24 @@ test.afterEach(async t => {

test('Owner has all balance in the beginning', async t => {
const { jsvm, fungibleTokenContract } = t.context.accounts;
const result = await jsvm.view('view_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftBalanceOf', [fungibleTokenContract.accountId]));
const result = await jsvm.view('view_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftBalanceOf', { accountId: fungibleTokenContract.accountId }));
t.is(result, '1000');
});

test('Can transfer if balance is sufficient', async t => {
const { ali, jsvm, fungibleTokenContract } = t.context.accounts;
await fungibleTokenContract.call(jsvm, 'call_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftTransfer', [ali.accountId, '100']), {attachedDeposit: '400000000000000000000000'});
const aliBalance = await jsvm.view('view_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftBalanceOf', [ali.accountId]));

await fungibleTokenContract.call(jsvm, 'call_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftTransfer', { receiverId: ali.accountId, amount: '100' }), { attachedDeposit: '400000000000000000000000' });
const aliBalance = await jsvm.view('view_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftBalanceOf', { accountId: ali.accountId }));
t.is(aliBalance, '100');
const ownerBalance = await jsvm.view('view_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftBalanceOf', [fungibleTokenContract.accountId]));
const ownerBalance = await jsvm.view('view_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftBalanceOf', { accountId: fungibleTokenContract.accountId }));
t.is(ownerBalance, '900');
});

test('Cannot transfer if balance is not sufficient', async t => {
const { ali, bob, jsvm, fungibleTokenContract } = t.context.accounts;
try {
await ali.call(jsvm, 'call_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftTransfer', [bob.accountId, '100']), {attachedDeposit: '400000000000000000000000'});
await ali.call(jsvm, 'call_js_contract', encodeCall(fungibleTokenContract.accountId, 'ftTransfer', { receiverId: bob.accountId, amount: '100' }), { attachedDeposit: '400000000000000000000000' });
} catch (e) {
t.assert(e.toString().indexOf('Smart contract panicked: assertion failed: The account doesn\'t have enough balance') >= 0);
}
Expand Down
12 changes: 6 additions & 6 deletions examples/fungible-token/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class FungibleToken extends NearContract {
this.accounts = Object.assign(new LookupMap, this.accounts)
}

internalDeposit(accountId, amount) {
internalDeposit({accountId, amount}) {
let balance = this.accounts.get(accountId) || '0'
let newBalance = BigInt(balance) + BigInt(amount)
this.accounts.set(accountId, newBalance.toString())
this.totalSupply = (BigInt(this.totalSupply) + BigInt(amount)).toString()
}

internalWithdraw(accountId, amount) {
internalWithdraw({accountId, amount}) {
let balance = this.accounts.get(accountId) || '0'
let newBalance = BigInt(balance) - BigInt(amount)
assert(newBalance >= 0n, "The account doesn't have enough balance")
Expand All @@ -47,7 +47,7 @@ class FungibleToken extends NearContract {
this.totalSupply = newSupply.toString()
}

internalTransfer(senderId, recieverId, amount, memo) {
internalTransfer({senderId, recieverId, amount, memo}) {
assert(senderId != recieverId, "Sender and receiver should be different")
let amountInt = BigInt(amount)
assert(amountInt > 0n, "The amount should be a positive number")
Expand All @@ -56,13 +56,13 @@ class FungibleToken extends NearContract {
}

@call
ftTransfer(receiverId, amount, memo) {
ftTransfer({receiverId, amount, memo}) {
let senderId = near.predecessorAccountId()
this.internalTransfer(senderId, receiverId, amount, memo)
}

@call
ftTransferCall(receiverId, amount, memo, msg) {
ftTransferCall({receiverId, amount, memo, msg}) {
let senderId = near.predecessorAccountId()
this.internalTransfer(senderId, receiverId, amount, memo)
let onTransferRet = near.jsvmCall(receiverId, 'ftOnTransfer', [senderId, amount, msg, receiverId])
Expand All @@ -77,7 +77,7 @@ class FungibleToken extends NearContract {
}

@view
ftBalanceOf(accountId) {
ftBalanceOf({accountId}) {
return this.accounts.get(accountId) || '0'
}
}
Loading

0 comments on commit 9290e24

Please sign in to comment.