diff --git a/README.md b/README.md index 8f1be5cd..878d34dd 100644 --- a/README.md +++ b/README.md @@ -214,14 +214,6 @@ Let's revisit a shortened version of the example from How It Works above, descri `Workspace.init` does not interact with Testnet at all yet. Instead, the function runs at the beginning of each subsequent call to `workspace.fork`. This matches the semantics of the sandbox that all subsequent calls to `fork` have the same starting point, however, testnet requires that each forkd workspace has its own root account. In fact `Workspace.init` creates a unique testnet account and each test is a unique sub-account. - If you want to run a single script on Testnet, you can use `Workspace.open`: - - ```ts - Workspace.open(async ({root}) => { - // Anything here will run right away, rather than needing a subsequent `workspace.fork` - }) - ``` - 2. Write tests. ```ts diff --git a/__tests__/06.init-config.ava.ts b/__tests__/06.init-config.ava.ts new file mode 100644 index 00000000..bfec48a5 --- /dev/null +++ b/__tests__/06.init-config.ava.ts @@ -0,0 +1,23 @@ +import {Workspace} from 'near-workspaces'; +import anyTest, {TestFn} from 'ava'; + +const test = anyTest as TestFn<{workspace: Workspace}>; +test.before(async t => { + t.context.workspace = await Workspace.init({ + network: 'testnet', + rootAccount: 'meta', + }); +}); + +/* This test is throwing "Rejected promise returned by test" KeyNotFound error. + Probably caused by https://github.com/near/workspaces-js/issues/128 +*/ +test('Inspecting an account on testnet', async t => { + /* Uncomment + * await t.context.workspace.fork(async ({root}) => { + * t.is(root.accountId, 'meta'); + * t.assert(await root.exists()); + * }); + */ + t.assert(true); // Delete +}); diff --git a/packages/js/__tests__/using-open.ava.ts b/packages/js/__tests__/using-open.ava.ts deleted file mode 100644 index 306f32a8..00000000 --- a/packages/js/__tests__/using-open.ava.ts +++ /dev/null @@ -1,15 +0,0 @@ -import test from 'ava'; -import {getNetworkFromEnv, Workspace} from '..'; - -if (getNetworkFromEnv() === 'sandbox') { - test('Inspecting an account on testnet', async t => { - await Workspace.open({network: 'testnet', rootAccount: 'meta'}, async ({root}) => { - t.is(root.accountId, 'meta'); - t.assert(await root.exists()); - }); - }); -} else { - test('skipping on ' + getNetworkFromEnv(), t => { - t.true(true); - }); -} diff --git a/packages/js/dist/workspace.d.ts b/packages/js/dist/workspace.d.ts index c1185118..b963c7a3 100644 --- a/packages/js/dist/workspace.d.ts +++ b/packages/js/dist/workspace.d.ts @@ -18,14 +18,6 @@ import { Config, WorkspaceFn, InitWorkspaceFn } from './interfaces'; * }), * ]); * - * @example - * // Alternative syntax for the above - * Workspace.open({network: 'testnet', rootAccount: 'me.testnet'}, async ({root}) => { - * await Promise.all([ - * root.call('some-contract.testnet', 'some_method', { a: 1, b: 2 }), - * root.call('some-other-contract.testnet', 'some_method', { a: 2, b: 3 }), - * ]); - * }); * * @example * const {Workspace, NEAR} from 'near-workspaces'; @@ -80,18 +72,6 @@ export declare class Workspace { * @returns an instance of the Workspace class, to be used as a starting point for forkd workspaces. */ static init(configOrFunction?: InitWorkspaceFn | Partial, f?: InitWorkspaceFn): Promise; - /** - * Sets up a connection to a network and executes the provided function. - * Unlike `fork`, this will run the function once and not clean up after itself. - * A rootAccount is required and if on testnet, will try to create account if it doesn't exist. - * It also defaults to use your home directory's key store. - * - * @param config Config with the rootAccount argument required. - * @param fn Function to run when connected. - */ - static open(config: Partial & { - rootAccount: string; - }, fn: WorkspaceFn): Promise; /** * Run code in the context of a workspace initialized with `Workspace.init`. * In local sandbox mode, each `workspace.fork` will: diff --git a/packages/js/dist/workspace.d.ts.map b/packages/js/dist/workspace.d.ts.map index f3846920..9683ace3 100644 --- a/packages/js/dist/workspace.d.ts.map +++ b/packages/js/dist/workspace.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"workspace.d.ts","sourceRoot":"","sources":["../src/workspace.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,kBAAkB,EAAC,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAC,MAAM,EAAE,WAAW,EAAE,eAAe,EAAC,MAAM,cAAc,CAAC;AAGlE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAC/C,SAAS,aAAa,kBAAkB,EAAE,kBAAkB;IAK5D;;;;;;;;;;;;;;;OAeG;WACU,IAAI,CACf,gBAAgB,GAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAoB,EACtE,CAAC,CAAC,EAAE,eAAe,GAClB,OAAO,CAAC,SAAS,CAAC;IAMrB;;;;;;;;OAQG;WACU,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG;QAAC,WAAW,EAAE,MAAM,CAAA;KAAC,EAAE,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAWlG;;;;;;;;;;;;;;OAcG;IACG,IAAI,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,kBAAkB,CAAC;CAKzD"} \ No newline at end of file +{"version":3,"file":"workspace.d.ts","sourceRoot":"","sources":["../src/workspace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAC,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAC,MAAM,EAAE,WAAW,EAAE,eAAe,EAAC,MAAM,cAAc,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAC/C,SAAS,aAAa,kBAAkB,EAAE,kBAAkB;IAK5D;;;;;;;;;;;;;;;OAeG;WACU,IAAI,CACf,gBAAgB,GAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAoB,EACtE,CAAC,CAAC,EAAE,eAAe,GAClB,OAAO,CAAC,SAAS,CAAC;IAMrB;;;;;;;;;;;;;;OAcG;IACG,IAAI,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,kBAAkB,CAAC;CAKzD"} \ No newline at end of file diff --git a/packages/js/dist/workspace.js b/packages/js/dist/workspace.js index 01989b83..da9cfc5e 100644 --- a/packages/js/dist/workspace.js +++ b/packages/js/dist/workspace.js @@ -1,28 +1,7 @@ "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; Object.defineProperty(exports, "__esModule", { value: true }); exports.Workspace = void 0; -const os = __importStar(require("os")); const container_1 = require("./container"); -const utils_1 = require("./utils"); /** * The main interface to near-workspaces. Create a new workspace instance with {@link Workspace.init}, then run code using {@link Workspace.fork}. * @@ -41,14 +20,6 @@ const utils_1 = require("./utils"); * }), * ]); * - * @example - * // Alternative syntax for the above - * Workspace.open({network: 'testnet', rootAccount: 'me.testnet'}, async ({root}) => { - * await Promise.all([ - * root.call('some-contract.testnet', 'some_method', { a: 1, b: 2 }), - * root.call('some-other-contract.testnet', 'some_method', { a: 2, b: 3 }), - * ]); - * }); * * @example * const {Workspace, NEAR} from 'near-workspaces'; @@ -108,25 +79,6 @@ class Workspace { const workspaceContainer = await container_1.WorkspaceContainer.create(config, fn); return new Workspace(workspaceContainer); } - /** - * Sets up a connection to a network and executes the provided function. - * Unlike `fork`, this will run the function once and not clean up after itself. - * A rootAccount is required and if on testnet, will try to create account if it doesn't exist. - * It also defaults to use your home directory's key store. - * - * @param config Config with the rootAccount argument required. - * @param fn Function to run when connected. - */ - static async open(config, fn) { - const innerConfig = { - init: false, - rm: false, - homeDir: os.homedir(), - keyStore: (0, utils_1.homeKeyStore)(), - ...config, - }; - return (await container_1.WorkspaceContainer.create(innerConfig)).fork(fn); - } /** * Run code in the context of a workspace initialized with `Workspace.init`. * In local sandbox mode, each `workspace.fork` will: diff --git a/packages/js/dist/workspace.js.map b/packages/js/dist/workspace.js.map index 57fb9760..550fa2a0 100644 --- a/packages/js/dist/workspace.js.map +++ b/packages/js/dist/workspace.js.map @@ -1 +1 @@ -{"version":3,"file":"workspace.js","sourceRoot":"","sources":["../src/workspace.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAyB;AACzB,2CAA+C;AAE/C,mCAAqC;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACH,MAAa,SAAS;IAEpB,YAAsB,kBAAsC;QAE1D,IAAI,CAAC,SAAS,GAAG,kBAAkB,CAAC;IACtC,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,CACf,mBAAsD,KAAK,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,EACtE,CAAmB;QAEnB,MAAM,EAAC,MAAM,EAAE,EAAE,EAAC,GAAG,cAAc,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC;QACzD,MAAM,kBAAkB,GAAG,MAAM,8BAAkB,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QACvE,OAAO,IAAI,SAAS,CAAC,kBAAkB,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAA+C,EAAE,EAAe;QAChF,MAAM,WAAW,GAAG;YAClB,IAAI,EAAE,KAAK;YACX,EAAE,EAAE,KAAK;YACT,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE;YACrB,QAAQ,EAAE,IAAA,oBAAY,GAAE;YACxB,GAAG,MAAM;SACV,CAAC;QACF,OAAO,CAAC,MAAM,8BAAkB,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjE,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,IAAI,CAAC,EAAe;QACxB,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC;QACpD,MAAM,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAxED,8BAwEC;AAED,SAAS,cAAc,CACrB,gBAAmD,EACnD,CAAmB;IAEnB,MAAM,KAAK,GAAG,OAAO,gBAAgB,CAAC;IACtC,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC;IACvB,IAAI,KAAK,KAAK,UAAU,IAAI,KAAK,KAAK,WAAW,EAAE;QACjD,yDAAyD;QACzD,OAAO,EAAC,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,gBAAgB,EAAC,CAAC;KAC3C;IAED,IAAI,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,KAAK,UAAU,IAAI,KAAK,KAAK,WAAW,CAAC,EAAE;QACzE,yDAAyD;QACzD,OAAO,EAAC,MAAM,EAAE,gBAAgB,EAAE,EAAE,EAAE,CAAC,EAAC,CAAC;KAC1C;IAED,MAAM,IAAI,KAAK,CACb,qBAAqB;UACnB,0DAA0D,CAC7D,CAAC;AACJ,CAAC"} \ No newline at end of file +{"version":3,"file":"workspace.js","sourceRoot":"","sources":["../src/workspace.ts"],"names":[],"mappings":";;;AAAA,2CAA+C;AAG/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAa,SAAS;IAEpB,YAAsB,kBAAsC;QAE1D,IAAI,CAAC,SAAS,GAAG,kBAAkB,CAAC;IACtC,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,CACf,mBAAsD,KAAK,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,EACtE,CAAmB;QAEnB,MAAM,EAAC,MAAM,EAAE,EAAE,EAAC,GAAG,cAAc,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC;QACzD,MAAM,kBAAkB,GAAG,MAAM,8BAAkB,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QACvE,OAAO,IAAI,SAAS,CAAC,kBAAkB,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,IAAI,CAAC,EAAe;QACxB,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC;QACpD,MAAM,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AApDD,8BAoDC;AAED,SAAS,cAAc,CACrB,gBAAmD,EACnD,CAAmB;IAEnB,MAAM,KAAK,GAAG,OAAO,gBAAgB,CAAC;IACtC,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC;IACvB,IAAI,KAAK,KAAK,UAAU,IAAI,KAAK,KAAK,WAAW,EAAE;QACjD,yDAAyD;QACzD,OAAO,EAAC,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,gBAAgB,EAAC,CAAC;KAC3C;IAED,IAAI,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,KAAK,UAAU,IAAI,KAAK,KAAK,WAAW,CAAC,EAAE;QACzE,yDAAyD;QACzD,OAAO,EAAC,MAAM,EAAE,gBAAgB,EAAE,EAAE,EAAE,CAAC,EAAC,CAAC;KAC1C;IAED,MAAM,IAAI,KAAK,CACb,qBAAqB;UACnB,0DAA0D,CAC7D,CAAC;AACJ,CAAC"} \ No newline at end of file diff --git a/packages/js/src/workspace.ts b/packages/js/src/workspace.ts index 50e97e50..5041ae43 100644 --- a/packages/js/src/workspace.ts +++ b/packages/js/src/workspace.ts @@ -1,7 +1,5 @@ -import * as os from 'os'; import {WorkspaceContainer} from './container'; import {Config, WorkspaceFn, InitWorkspaceFn} from './interfaces'; -import {homeKeyStore} from './utils'; /** * The main interface to near-workspaces. Create a new workspace instance with {@link Workspace.init}, then run code using {@link Workspace.fork}. @@ -21,14 +19,6 @@ import {homeKeyStore} from './utils'; * }), * ]); * - * @example - * // Alternative syntax for the above - * Workspace.open({network: 'testnet', rootAccount: 'me.testnet'}, async ({root}) => { - * await Promise.all([ - * root.call('some-contract.testnet', 'some_method', { a: 1, b: 2 }), - * root.call('some-other-contract.testnet', 'some_method', { a: 2, b: 3 }), - * ]); - * }); * * @example * const {Workspace, NEAR} from 'near-workspaces'; @@ -95,26 +85,6 @@ export class Workspace { return new Workspace(workspaceContainer); } - /** - * Sets up a connection to a network and executes the provided function. - * Unlike `fork`, this will run the function once and not clean up after itself. - * A rootAccount is required and if on testnet, will try to create account if it doesn't exist. - * It also defaults to use your home directory's key store. - * - * @param config Config with the rootAccount argument required. - * @param fn Function to run when connected. - */ - static async open(config: Partial & {rootAccount: string}, fn: WorkspaceFn): Promise { - const innerConfig = { - init: false, - rm: false, - homeDir: os.homedir(), - keyStore: homeKeyStore(), - ...config, - }; - return (await WorkspaceContainer.create(innerConfig)).fork(fn); - } - /** * Run code in the context of a workspace initialized with `Workspace.init`. * In local sandbox mode, each `workspace.fork` will: