-
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.
conflict fixed, CI fixed, package.lock deleted
- Loading branch information
Showing
11 changed files
with
4,359 additions
and
4,765 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,2 @@ | ||
build | ||
node_modules |
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,38 @@ | ||
# Clean state contract in JavaScript | ||
|
||
This is an equivalent JavaScript implementation of the clean state example. | ||
|
||
## Build the contract | ||
|
||
First ensure JSVM contract is build and deployed locally, follow [Local Installation](https://github.com/near/near-sdk-js#local-installation). Then run: | ||
``` | ||
npm i | ||
npm run build | ||
``` | ||
|
||
## Test the contract with workspaces-js | ||
``` | ||
npm run test | ||
``` | ||
|
||
## Guide on how to clean arbitrary account's state | ||
|
||
First, build the contract as described above and deploy it to the account which state you want to clean up (this will override the contract that is currently there, but you can then re-deploy the original contract after the cleanup is done). Assuming that you are using JSVM contract deployed to `jsvm.testnet`, run the following command: | ||
|
||
``` | ||
near call jsvm.testnet deploy_js_contract --accountId account-to-cleanup.testnet --args $(cat build/contract.base64) --base64 --deposit 0.1 | ||
``` | ||
|
||
Now, you can call the `clean` method to delete the unwanted keys: | ||
|
||
``` | ||
near call jsvm.testnet call_js_contract --accountId account-to-cleanup.testnet --base64 --args $(node encode_call.js account-to-cleanup.testnet clean '[["key1", "key2"]]') --deposit 0.1 | ||
``` | ||
|
||
If gas limit prevents you from deleting all keys you want at once, just split the array into multiple parts and make a few subsequent `clean` calls. | ||
|
||
Once you are done, just de-deploy the original contract and you should be all set: | ||
|
||
``` | ||
near call jsvm.testnet deploy_js_contract --accountId account-to-cleanup.testnet --args $(cat path-to-original-contract.base64) --base64 --deposit 0.1 | ||
``` |
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,52 @@ | ||
import { Worker } from 'near-workspaces'; | ||
import { readFile } from 'fs/promises' | ||
import test from 'ava'; | ||
|
||
// TODO: make this function part of the npm package when it is available | ||
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, | ||
'./node_modules/near-sdk-js/res/jsvm.wasm', | ||
); | ||
|
||
// Deploy clean state contract | ||
const cleanStateContract = await root.createSubAccount('clean-state'); | ||
let cleanStateContractBase64 = (await readFile('build/contract.base64')).toString(); | ||
await cleanStateContract.call(jsvm, 'deploy_js_contract', Buffer.from(cleanStateContractBase64, 'base64'), { attachedDeposit: '400000000000000000000000' }); | ||
await cleanStateContract.call(jsvm, 'call_js_contract', encodeCall(cleanStateContract.accountId, 'init', []), { attachedDeposit: '400000000000000000000000' }); | ||
|
||
// Save state for test runs, it is unique for each test | ||
t.context.worker = worker; | ||
t.context.accounts = { | ||
root, | ||
jsvm, | ||
cleanStateContract, | ||
}; | ||
}); | ||
|
||
test.afterEach(async t => { | ||
await t.context.worker.tearDown().catch(error => { | ||
console.log('Failed tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
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'])); | ||
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'])); | ||
t.is(value2, null); | ||
}); |
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"}] | ||
] | ||
} |
Oops, something went wrong.