diff --git a/Cargo.lock b/Cargo.lock index f7dc074..54d90eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -530,7 +530,7 @@ checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" [[package]] name = "linear" -version = "1.6.0" +version = "1.6.1" dependencies = [ "near-contract-standards", "near-sdk 4.0.0-pre.7", diff --git a/README.md b/README.md index c24032b..190551e 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ The LiNEAR smart contracts have been audited by [BlockSec](https://www.blocksect We adopt unit tests and heavily used the [`workspace-js`](https://github.com/near/workspaces-js) test framework to test the major scenarios and workflow of the LiNEAR smart contract in the [Sandbox](https://docs.near.org/docs/develop/contracts/sandbox) environment. Lint with `rustfmt` and `clippy` is also required when making changes to contract. -- Install node v16 +- Install Node.js v20 - Run `npm i` to set up the environment - Run lint with `rustfmt` and `clippy`: `make lint` - Run all tests: `make test` diff --git a/bin/commands/common.js b/bin/commands/common.js index 78c9a2e..cefd0bf 100644 --- a/bin/commands/common.js +++ b/bin/commands/common.js @@ -1,5 +1,37 @@ -module.exports.networkOption = { +const prompts = require('prompts'); +const base58 = require('bs58'); +const sha256 = require('sha256'); + +exports.networkOption = { describe: 'network ID', default: 'testnet', choices: ['testnet', 'mainnet', 'localnet'] }; + +exports.doubleCheck = async () => { + const res = await prompts({ + type: 'toggle', + name: 'value', + message: 'Confirm?', + initial: true, + active: 'yes', + inactive: 'no' + }); + if (!res.value) process.exit(1); +} + +exports.parseHashReturnValue = (outcome) => { + const status = outcome.status; + const data = status.SuccessValue; + if (!data) { + throw new Error('bad return value'); + } + + const buff = Buffer.from(data, 'base64'); + return buff.toString('ascii').replaceAll('"', ""); +} + +exports.getBase58CodeHash = (code) => { + const hash = Buffer.from(sha256(code), 'hex'); + return base58.encode(hash); +} diff --git a/bin/commands/propose-upgrade.js b/bin/commands/propose-upgrade.js index bf4bb58..ceb8dee 100644 --- a/bin/commands/propose-upgrade.js +++ b/bin/commands/propose-upgrade.js @@ -1,9 +1,8 @@ -const base58 = require('bs58'); -const sha256 = require('sha256'); const { readFileSync, appendFileSync, existsSync } = require("fs"); const { NEAR, Gas } = require("near-units"); const { init } = require("../near"); -const { networkOption } = require("./common"); +const nearAPI = require('near-api-js'); +const { networkOption, doubleCheck, parseHashReturnValue, getBase58CodeHash } = require("./common"); exports.command = 'propose-upgrade
'; exports.desc = 'Propose an upgrade in DAO'; @@ -19,7 +18,7 @@ exports.builder = yargs => { default: 'res/linear.wasm' }) .option('signer', { - describe: 'signer account ID to call new' + describe: 'signer account ID' }) .option('dao', { describe: 'DAO account Id' @@ -72,13 +71,13 @@ exports.handler = async function (argv) { console.error(`Old blob with ${lastHash} doesn't exist. The blob might have been removed. Continue?`); await doubleCheck(); } else { - console.log(`Remove blob with hash ${lastHash}. Are you sure?`); + console.log(`Remove outdated blob with hash ${lastHash}. Are you sure?`); await doubleCheck(); await signer.functionCall({ contractId: dao, methodName: 'remove_blob', args: { - hash, + hash: lastHash, }, }); console.log(`Removed blob with hash ${lastHash}`); @@ -96,15 +95,19 @@ exports.handler = async function (argv) { // store new blob console.log(`Store blob with hash ${codeHash}. Are you sure?`); await doubleCheck(); - const outcome = await signer.functionCall({ - contractId: dao, - methodName: 'store_blob', - args: { - code, - }, - gas: Gas.parse('100 Tgas'), - attachedDeposit: deposit, - }); + const outcome = await signer.signAndSendTransaction( + { + receiverId: dao, + actions: [ + nearAPI.transactions.functionCall( + 'store_blob', + code, + Gas.parse('100 Tgas'), + deposit + ) + ] + } + ); const hash = parseHashReturnValue(outcome); console.log(`Stored blob with hash ${hash}`); } @@ -136,19 +139,3 @@ exports.handler = async function (argv) { console.log('proposed!'); } - -function parseHashReturnValue(outcome) { - const status = outcome.status; - const data = status.SuccessValue; - if (!data) { - throw new Error('bad return value'); - } - - const buff = Buffer.from(data, 'base64'); - return buff.toString('ascii').replaceAll('"', ""); -} - -function getBase58CodeHash(code) { - const hash = Buffer.from(sha256(code), 'hex'); - return base58.encode(hash); -} diff --git a/bin/commands/store-dao-contract.js b/bin/commands/store-dao-contract.js new file mode 100644 index 0000000..e050d57 --- /dev/null +++ b/bin/commands/store-dao-contract.js @@ -0,0 +1,86 @@ +const { writeFileSync } = require("fs"); +const { NEAR, Gas } = require("near-units"); +const { init } = require("../near"); +const nearAPI = require('near-api-js'); +const { networkOption, doubleCheck, parseHashReturnValue, getBase58CodeHash } = require("./common"); + +exports.command = 'store-dao-contract