-
Notifications
You must be signed in to change notification settings - Fork 71
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
8 changed files
with
2,337 additions
and
1 deletion.
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,2 @@ | ||
node_modules | ||
build |
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,58 @@ | ||
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/contract.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('UnorderedMap is empty by default', async t => { | ||
const { root, jsvm, testContract } = t.context.accounts; | ||
const result = await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [root.accountId])); | ||
t.is(result, 0); | ||
}); | ||
|
||
test('UnorderedMap set() get()', async t => { | ||
const { ali, jsvm, testContract } = t.context.accounts; | ||
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' | ||
); | ||
}); |
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,8 @@ | ||
require('util').inspect.defaultOptions.depth = 5; // Increase AVA's printing depth | ||
|
||
module.exports = { | ||
timeout: '300000', | ||
files: ['**/*.ava.js'], | ||
failWithoutAssertions: false, | ||
extensions: ['js'], | ||
}; |
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,6 @@ | ||
{ | ||
"plugins": [ | ||
"near-sdk-js/src/build-tools/near-bindgen-exporter", | ||
["@babel/plugin-proposal-decorators", {"version": "legacy"}] | ||
] | ||
} |
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,20 @@ | ||
{ | ||
"name": "tests", | ||
"version": "1.0.0", | ||
"description": "near-sdk-js tests", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"build": "near-sdk build", | ||
"test": "ava" | ||
}, | ||
"author": "Near Inc <hello@nearprotocol.com>", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"near-sdk-js": "file:../" | ||
}, | ||
"devDependencies": { | ||
"ava": "^4.2.0", | ||
"near-workspaces": "^2.0.0" | ||
} | ||
} |
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,79 @@ | ||
import { | ||
NearContract, | ||
NearBindgen, | ||
call, | ||
view, | ||
UnorderedMap, | ||
Vector | ||
} from 'near-sdk-js' | ||
|
||
@NearBindgen | ||
class UnorderedMapTestContract extends NearContract { | ||
constructor() { | ||
super() | ||
this.unorderedMap = new UnorderedMap('a'); | ||
} | ||
|
||
deserialize() { | ||
super.deserialize() | ||
this.unorderedMap.keys = Object.assign(new Vector, this.unorderedMap.keys) | ||
this.unorderedMap.values = Object.assign(new Vector, this.unorderedMap.values) | ||
this.unorderedMap = Object.assign(new UnorderedMap, this.unorderedMap) | ||
} | ||
|
||
@view | ||
len() { | ||
return this.unorderedMap.len(); | ||
} | ||
|
||
@view | ||
isEmpty() { | ||
return this.unorderedMap.isEmpty(); | ||
} | ||
|
||
@view | ||
serializeIndex(index) { | ||
return this.unorderedMap.serializeIndex(index); | ||
} | ||
|
||
@view | ||
deserializeIndex(rawIndex) { | ||
return this.unorderedMap.deserializeIndex(rawIndex); | ||
} | ||
|
||
@view | ||
getIndexRaw(key) { | ||
return this.unorderedMap.getIndexRaw(key); | ||
} | ||
|
||
@view | ||
get(key) { | ||
return this.unorderedMap.get(key); | ||
} | ||
|
||
@call | ||
set(key, value) { | ||
this.unorderedMap.set(key, value); | ||
} | ||
|
||
@call | ||
remove(key) { | ||
this.unorderedMap.remove(key); | ||
} | ||
|
||
@call | ||
clear() { | ||
this.unorderedMap.clear(); | ||
} | ||
|
||
@view | ||
toArray() { | ||
this.unorderedMap.toArray(); | ||
} | ||
|
||
@view | ||
extend(kvs) { | ||
this.unorderedMap.extend(kvs); | ||
} | ||
} | ||
|
Oops, something went wrong.