From 4ef62c3f31aafa599e9a49280a77ee1acf25154f Mon Sep 17 00:00:00 2001 From: Den Date: Sun, 28 Jan 2024 17:02:28 +0200 Subject: [PATCH 1/3] test(provider): use standard config for constructing JsonRpcProvider --- packages/providers/package.json | 1 + packages/providers/test/config.js | 56 +++++++++++++++++++++++ packages/providers/test/providers.test.js | 16 +++---- 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 packages/providers/test/config.js diff --git a/packages/providers/package.json b/packages/providers/package.json index 00a5166451..2058cdff86 100644 --- a/packages/providers/package.json +++ b/packages/providers/package.json @@ -25,6 +25,7 @@ }, "devDependencies": { "@types/node": "18.11.18", + "@near-js/keystores": "workspace:*", "jest": "26.0.1", "near-workspaces": "3.4.0", "ts-jest": "26.5.6", diff --git a/packages/providers/test/config.js b/packages/providers/test/config.js new file mode 100644 index 0000000000..2d3e574260 --- /dev/null +++ b/packages/providers/test/config.js @@ -0,0 +1,56 @@ +const { Worker } = require("near-workspaces"); +const fs = require("fs"); +let worker; +module.exports = async function getConfig(env) { + switch (env) { + case "production": + case "mainnet": + return { + networkId: "mainnet", + nodeUrl: "https://rpc.mainnet.near.org", + walletUrl: "https://wallet.near.org", + helperUrl: "https://helper.mainnet.near.org", + }; + case "development": + case "testnet": + return { + networkId: "default", + nodeUrl: "https://rpc.testnet.near.org", + walletUrl: "https://wallet.testnet.near.org", + helperUrl: "https://helper.testnet.near.org", + masterAccount: "test.near", + }; + case "betanet": + return { + networkId: "betanet", + nodeUrl: "https://rpc.betanet.near.org", + walletUrl: "https://wallet.betanet.near.org", + helperUrl: "https://helper.betanet.near.org", + }; + case "local": + return { + networkId: "local", + nodeUrl: "http://localhost:3030", + keyPath: `${process.env.HOME}/.near/validator_key.json`, + walletUrl: "http://localhost:4000/wallet", + }; + case "test": + case "ci": { + if (!worker) worker = await Worker.init(); + const keyFile = fs.readFileSync( + `${worker.rootAccount.manager.config.homeDir}/validator_key.json` + ); + const keyPair = JSON.parse(keyFile.toString()); + return { + networkId: worker.config.network, + nodeUrl: worker.manager.config.rpcAddr, + masterAccount: worker.rootAccount._accountId, + secretKey: keyPair.secret_key || keyPair.private_key, + }; + } + default: + throw Error( + `Unconfigured environment '${env}'. Can be configured in src/config.js.` + ); + } +}; diff --git a/packages/providers/test/providers.test.js b/packages/providers/test/providers.test.js index 509860b9e3..c98f3905df 100644 --- a/packages/providers/test/providers.test.js +++ b/packages/providers/test/providers.test.js @@ -1,23 +1,23 @@ const { getTransactionLastResult } = require('@near-js/utils'); -const { Worker } = require('near-workspaces'); +const { InMemoryKeyStore } = require('@near-js/keystores'); const { JsonRpcProvider } = require('../lib'); jest.setTimeout(20000); describe('providers', () => { - let worker; let provider; beforeAll(async () => { - worker = await Worker.init(); - provider = new JsonRpcProvider({ url: worker.manager.config.rpcAddr }); + const networkId = 'unittest'; + const keyStore = new InMemoryKeyStore(); + const config = Object.assign(await require('./config')(process.env.NODE_ENV || 'test'), { + networkId, + keyStore + }); + provider = new JsonRpcProvider({ url: config.nodeUrl }); await new Promise(resolve => setTimeout(resolve, 2000)); }); - afterAll(async () => { - await worker.tearDown(); - }); - test('json rpc fetch node status', async () => { let response = await provider.status(); expect(response.chain_id).toBeTruthy(); From 026e98ad500c5382de51fc8eaa348062bbb9965c Mon Sep 17 00:00:00 2001 From: Den Date: Sun, 28 Jan 2024 17:05:31 +0200 Subject: [PATCH 2/3] test: add remote RPC for testing backward compatibility --- packages/accounts/test/config.js | 17 +++++++-- packages/near-api-js/test/config.js | 7 ++++ packages/providers/test/config.js | 59 ++++++++++++++++------------- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/packages/accounts/test/config.js b/packages/accounts/test/config.js index 23f994a1dd..4afbbc6ef5 100644 --- a/packages/accounts/test/config.js +++ b/packages/accounts/test/config.js @@ -37,16 +37,27 @@ module.exports = async function getConfig(env) { case 'test': case 'ci': { if (!worker) worker = await Worker.init(); - const keyFile = fs.readFileSync(`${worker.rootAccount.manager.config.homeDir}/validator_key.json`); + const keyFile = fs.readFileSync( + `${worker.rootAccount.manager.config.homeDir}/validator_key.json` + ); const keyPair = JSON.parse(keyFile.toString()); return { networkId: worker.config.network, nodeUrl: worker.manager.config.rpcAddr, masterAccount: worker.rootAccount._accountId, - secretKey: keyPair.secret_key || keyPair.private_key + secretKey: keyPair.secret_key || keyPair.private_key, + }; + } + case 'remote_ci': { + return { + networkId: 'shared-test', + nodeUrl: 'https://rpc.ci-testnet.near.org', + masterAccount: 'test.near', }; } default: - throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`); + throw Error( + `Unconfigured environment '${env}'. Can be configured in src/config.js.` + ); } }; diff --git a/packages/near-api-js/test/config.js b/packages/near-api-js/test/config.js index 7bbf1bfc0c..9bbfab99c2 100644 --- a/packages/near-api-js/test/config.js +++ b/packages/near-api-js/test/config.js @@ -45,6 +45,13 @@ module.exports = async function getConfig(env) { masterAccount: worker.rootAccount._accountId, secretKey: keyPair.secret_key || keyPair.private_key }; + case 'remote_ci': { + return { + networkId: 'shared-test', + nodeUrl: 'https://rpc.ci-testnet.near.org', + masterAccount: 'test.near', + }; + } default: throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`); } diff --git a/packages/providers/test/config.js b/packages/providers/test/config.js index 2d3e574260..4afbbc6ef5 100644 --- a/packages/providers/test/config.js +++ b/packages/providers/test/config.js @@ -1,41 +1,41 @@ -const { Worker } = require("near-workspaces"); -const fs = require("fs"); +const { Worker } = require('near-workspaces'); +const fs = require('fs'); let worker; module.exports = async function getConfig(env) { switch (env) { - case "production": - case "mainnet": + case 'production': + case 'mainnet': return { - networkId: "mainnet", - nodeUrl: "https://rpc.mainnet.near.org", - walletUrl: "https://wallet.near.org", - helperUrl: "https://helper.mainnet.near.org", + networkId: 'mainnet', + nodeUrl: 'https://rpc.mainnet.near.org', + walletUrl: 'https://wallet.near.org', + helperUrl: 'https://helper.mainnet.near.org', }; - case "development": - case "testnet": + case 'development': + case 'testnet': return { - networkId: "default", - nodeUrl: "https://rpc.testnet.near.org", - walletUrl: "https://wallet.testnet.near.org", - helperUrl: "https://helper.testnet.near.org", - masterAccount: "test.near", + networkId: 'default', + nodeUrl: 'https://rpc.testnet.near.org', + walletUrl: 'https://wallet.testnet.near.org', + helperUrl: 'https://helper.testnet.near.org', + masterAccount: 'test.near', }; - case "betanet": + case 'betanet': return { - networkId: "betanet", - nodeUrl: "https://rpc.betanet.near.org", - walletUrl: "https://wallet.betanet.near.org", - helperUrl: "https://helper.betanet.near.org", + networkId: 'betanet', + nodeUrl: 'https://rpc.betanet.near.org', + walletUrl: 'https://wallet.betanet.near.org', + helperUrl: 'https://helper.betanet.near.org', }; - case "local": + case 'local': return { - networkId: "local", - nodeUrl: "http://localhost:3030", + networkId: 'local', + nodeUrl: 'http://localhost:3030', keyPath: `${process.env.HOME}/.near/validator_key.json`, - walletUrl: "http://localhost:4000/wallet", + walletUrl: 'http://localhost:4000/wallet', }; - case "test": - case "ci": { + case 'test': + case 'ci': { if (!worker) worker = await Worker.init(); const keyFile = fs.readFileSync( `${worker.rootAccount.manager.config.homeDir}/validator_key.json` @@ -48,6 +48,13 @@ module.exports = async function getConfig(env) { secretKey: keyPair.secret_key || keyPair.private_key, }; } + case 'remote_ci': { + return { + networkId: 'shared-test', + nodeUrl: 'https://rpc.ci-testnet.near.org', + masterAccount: 'test.near', + }; + } default: throw Error( `Unconfigured environment '${env}'. Can be configured in src/config.js.` From 2c08668a13501974aa5c084a6bcc78c3bd3e5398 Mon Sep 17 00:00:00 2001 From: Den Date: Sun, 18 Feb 2024 20:39:40 +0200 Subject: [PATCH 3/3] fix: generate lock file for `providers` package --- pnpm-lock.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e319d6e3f1..9a7c18f133 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -435,6 +435,9 @@ importers: specifier: 2.6.7 version: 2.6.7 devDependencies: + '@near-js/keystores': + specifier: workspace:* + version: link:../keystores '@types/node': specifier: 18.11.18 version: 18.11.18