-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
306 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { Worker } from 'near-workspaces'; | ||
import { readFile } from 'fs/promises' | ||
import test from 'ava'; | ||
|
||
function encodeCall(contract, method, args) { | ||
return Buffer.concat([Buffer.from(contract), Buffer.from([0]), Buffer.from(method), Buffer.from([0]), Buffer.from(JSON.stringify(args))]) | ||
} | ||
|
||
test.beforeEach(async t => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etx. | ||
const root = worker.rootAccount; | ||
|
||
// Deploy the jsvm contract. | ||
const jsvm = await root.createAndDeploy( | ||
root.getSubAccount('jsvm').accountId, | ||
'../res/jsvm.wasm', | ||
); | ||
|
||
// Deploy test JS contract | ||
const testContract = await root.createSubAccount('test-contract'); | ||
let contract_base64 = (await readFile('build/lookup-map.base64')).toString(); | ||
await testContract.call(jsvm, 'deploy_js_contract', Buffer.from(contract_base64, 'base64'), { attachedDeposit: '400000000000000000000000' }); | ||
await testContract.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'init', []), { attachedDeposit: '400000000000000000000000' }); | ||
|
||
// Test users | ||
const ali = await root.createSubAccount('ali'); | ||
const bob = await root.createSubAccount('bob'); | ||
const carl = await root.createSubAccount('carl'); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { root, jsvm, testContract, ali, bob, carl }; | ||
}); | ||
|
||
test.afterEach(async t => { | ||
await t.context.worker.tearDown().catch(error => { | ||
console.log('Failed to tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
test('LookupMap set() get()', async t => { | ||
const { ali, jsvm, testContract } = t.context.accounts; | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])), | ||
null | ||
); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'containsKey', ['hello'])), | ||
false | ||
); | ||
|
||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello', 'world']), { attachedDeposit: '100000000000000000000000' }); | ||
|
||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])), | ||
'world' | ||
); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'containsKey', ['hello'])), | ||
true | ||
); | ||
}); | ||
|
||
|
||
test('LookupMap update, remove', async t => { | ||
const { ali, jsvm, testContract } = t.context.accounts; | ||
|
||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello', 'world']), { attachedDeposit: '100000000000000000000000' }); | ||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello1', 'world0']), { attachedDeposit: '100000000000000000000000' }); | ||
|
||
// update a value, len shouldn't change | ||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello1', 'world1']), { attachedDeposit: '100000000000000000000000' }); | ||
// update should have effect | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello1'])), | ||
'world1' | ||
); | ||
// not update key should not changed | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])), | ||
'world' | ||
); | ||
// remove non existing element should not error | ||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello3']), { attachedDeposit: '100000000000000000000000' }); | ||
// remove existing key should work | ||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello1'])); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'containsKey', ['hello1'])), | ||
false | ||
); | ||
// not removed key should not affected | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])), | ||
'world' | ||
); | ||
}); | ||
|
||
test('LookupMap extend', async t => { | ||
const { ali, jsvm, testContract } = t.context.accounts; | ||
|
||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'extend', [[['hello', 'world'], ['hello1', 'world1'], ['hello2', 'world2']]]), { attachedDeposit: '100000000000000000000000' }); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])), | ||
'world' | ||
); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello1'])), | ||
'world1' | ||
); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello2'])), | ||
'world2' | ||
); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Worker } from 'near-workspaces'; | ||
import { readFile } from 'fs/promises' | ||
import test from 'ava'; | ||
|
||
function encodeCall(contract, method, args) { | ||
return Buffer.concat([Buffer.from(contract), Buffer.from([0]), Buffer.from(method), Buffer.from([0]), Buffer.from(JSON.stringify(args))]) | ||
} | ||
|
||
test.beforeEach(async t => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etx. | ||
const root = worker.rootAccount; | ||
|
||
// Deploy the jsvm contract. | ||
const jsvm = await root.createAndDeploy( | ||
root.getSubAccount('jsvm').accountId, | ||
'../res/jsvm.wasm', | ||
); | ||
|
||
// Deploy test JS contract | ||
const testContract = await root.createSubAccount('test-contract'); | ||
let contract_base64 = (await readFile('build/lookup-set.base64')).toString(); | ||
await testContract.call(jsvm, 'deploy_js_contract', Buffer.from(contract_base64, 'base64'), { attachedDeposit: '400000000000000000000000' }); | ||
await testContract.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'init', []), { attachedDeposit: '400000000000000000000000' }); | ||
|
||
// Test users | ||
const ali = await root.createSubAccount('ali'); | ||
const bob = await root.createSubAccount('bob'); | ||
const carl = await root.createSubAccount('carl'); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { root, jsvm, testContract, ali, bob, carl }; | ||
}); | ||
|
||
test.afterEach(async t => { | ||
await t.context.worker.tearDown().catch(error => { | ||
console.log('Failed to tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
test('LookupSet set() contains()', async t => { | ||
const { ali, jsvm, testContract } = t.context.accounts; | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello'])), | ||
false | ||
); | ||
|
||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello']), { attachedDeposit: '100000000000000000000000' }); | ||
|
||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello'])), | ||
true | ||
); | ||
}); | ||
|
||
|
||
test('LookupSet remove', async t => { | ||
const { ali, jsvm, testContract } = t.context.accounts; | ||
|
||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello']), { attachedDeposit: '100000000000000000000000' }); | ||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello1']), { attachedDeposit: '100000000000000000000000' }); | ||
|
||
// remove non existing element should not error | ||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello3']), { attachedDeposit: '100000000000000000000000' }); | ||
// remove existing key should work | ||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'remove', ['hello1'])); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello1'])), | ||
false | ||
); | ||
// not removed key should not affected | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello'])), | ||
true | ||
); | ||
}); | ||
|
||
test('LookupSet extend', async t => { | ||
const { ali, jsvm, testContract } = t.context.accounts; | ||
|
||
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'extend', [['hello', 'world', 'hello1']]), { attachedDeposit: '100000000000000000000000' }); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello'])), | ||
true | ||
); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['hello1'])), | ||
true | ||
); | ||
t.is( | ||
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'contains', ['world'])), | ||
true | ||
); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
NearContract, | ||
NearBindgen, | ||
call, | ||
view, | ||
LookupMap | ||
} from 'near-sdk-js' | ||
|
||
@NearBindgen | ||
class LookupMapTestContract extends NearContract { | ||
constructor() { | ||
super() | ||
this.lookupMap = new LookupMap('a'); | ||
} | ||
|
||
deserialize() { | ||
super.deserialize(); | ||
this.lookupMap = Object.assign(new LookupMap, this.lookupMap); | ||
} | ||
|
||
@view | ||
get(key) { | ||
return this.lookupMap.get(key); | ||
} | ||
|
||
@view | ||
containsKey(key) { | ||
return this.lookupMap.containsKey(key); | ||
} | ||
|
||
@call | ||
set(key, value) { | ||
this.lookupMap.set(key, value); | ||
} | ||
|
||
@call | ||
remove(key) { | ||
this.lookupMap.remove(key); | ||
} | ||
|
||
@call | ||
extend(kvs) { | ||
this.lookupMap.extend(kvs); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
NearContract, | ||
NearBindgen, | ||
call, | ||
view, | ||
LookupSet | ||
} from 'near-sdk-js' | ||
|
||
@NearBindgen | ||
class LookupSetTestContract extends NearContract { | ||
constructor() { | ||
super() | ||
this.lookupSet = new LookupSet('a'); | ||
} | ||
|
||
deserialize() { | ||
super.deserialize(); | ||
this.lookupSet = Object.assign(new LookupSet, this.lookupSet); | ||
} | ||
|
||
@view | ||
contains(key) { | ||
return this.lookupSet.contains(key); | ||
} | ||
|
||
@call | ||
set(key) { | ||
this.lookupSet.set(key); | ||
} | ||
|
||
@call | ||
remove(key) { | ||
this.lookupSet.remove(key); | ||
} | ||
|
||
@call | ||
extend(keys) { | ||
this.lookupSet.extend(keys); | ||
} | ||
} | ||
|