From 6be3114da40921c4d5a0547797ed98e718c4c59d Mon Sep 17 00:00:00 2001 From: hui-an-yang <106410553+hui-an-yang@users.noreply.github.com> Date: Mon, 31 Oct 2022 09:57:29 -0700 Subject: [PATCH] 660 support customize parser options (#2061) * feat: exposed setParserProvider configuration through TezosToolkit re #660 * test: added integration tests using setParserProvider with TezosToolkit re #660 * docs: updated starting production server commands with taquito-test-dapp readme.md * test: removed unnecessory console.log re #660 * chore: update vite to latest version * docs: fixed some typos in doc to address comments re #660 --- apps/taquito-test-dapp/README.md | 5 +- apps/taquito-test-dapp/package.json | 2 +- .../contract-michelson-origination.spec.ts | 54 ++++++++++++++++- .../wallet-michelson-origination.spec.ts | 59 ++++++++++++++++++- packages/taquito/src/taquito.ts | 26 ++++++++ 5 files changed, 142 insertions(+), 4 deletions(-) diff --git a/apps/taquito-test-dapp/README.md b/apps/taquito-test-dapp/README.md index e0a18a591b..2d178473b1 100644 --- a/apps/taquito-test-dapp/README.md +++ b/apps/taquito-test-dapp/README.md @@ -10,4 +10,7 @@ A minimal end-to-end testing setup for developing Tezos DApps with Taquito and B `npm install` 4. Start development server: `npm run dev` -5. Open http://localhost:3030 in your browser to see a sample application. \ No newline at end of file +5. Open http://localhost:3030 in your browser to see a sample application. +6. Start production server: + `npm run build && npm run preview` +7. Open http://localhost:4173 in your browser to see a preview application. diff --git a/apps/taquito-test-dapp/package.json b/apps/taquito-test-dapp/package.json index 6aaad08406..e1d9e1ff83 100644 --- a/apps/taquito-test-dapp/package.json +++ b/apps/taquito-test-dapp/package.json @@ -21,7 +21,7 @@ "svelte-preprocess": "^4.9.8", "tslib": "^2.3.1", "typescript": "^4.5.4", - "vite": "^2.9.7", + "vite": "^3.2.0", "vite-compatible-readable-stream": "^3.6.1" }, "dependencies": { diff --git a/integration-tests/contract-michelson-origination.spec.ts b/integration-tests/contract-michelson-origination.spec.ts index 98799fc734..5542ea50d1 100644 --- a/integration-tests/contract-michelson-origination.spec.ts +++ b/integration-tests/contract-michelson-origination.spec.ts @@ -1,9 +1,61 @@ -import { IntegerError } from "@taquito/taquito"; +import { IntegerError, MichelCodecParser, NoopParser, Context, InvalidCodeParameter } from '@taquito/taquito'; import { CONFIGS } from "./config"; import { idMichelsonCode, idInitData } from "./data/id-contract" CONFIGS().forEach(({ lib, rpc, setup }) => { const Tezos = lib; + + describe(`Test contract origination to configure parserProvider to parse plain Michelson`, () => { + + beforeEach(async (done) => { + await setup() + done() + }) + it('uses noopParser to originate Michelson code and fails', async (done) => { + // Configure the Tezostoolkit to use the NoopParser (the Michelson won't be parsed) + Tezos.setParserProvider(new NoopParser()); + + try { + const op = await Tezos.contract.originate({ + balance: "0", + code: idMichelsonCode, + init: idInitData + }); + await op.confirmation() + } catch (error: any) { + expect(error).toBeInstanceOf(InvalidCodeParameter); + expect(error.message).toEqual('Wrong code parameter type, expected an array'); + } + done(); + }); + + it('uses MichelCodecParser to originate Michelson code and succeeds', async (done) => { + // Configure the Tezostoolkit to use the MichelCodecParser (the Michelson will be parsed to JSONMichelson) + Tezos.setParserProvider(new MichelCodecParser(new Context(rpc))); + + const op = await Tezos.contract.originate({ + balance: "0", + code: idMichelsonCode, + init: idInitData + }); + await op.confirmation() + expect(op.status).toEqual('applied') + done(); + }); + + it('no parser configured will use MichelCodecParser by default to originate Michelson code and succeeds', async (done) => { + // No parserProvider configured will use MichelCodecParser by default (the Michelson will be parsed to JSONMichelson) + const op = await Tezos.contract.originate({ + balance: "0", + code: idMichelsonCode, + init: idInitData + }); + await op.confirmation() + expect(op.status).toEqual('applied') + done(); + }); + }); + describe(`Test contract origination in a plain Michelson through contract api using: ${rpc}`, () => { beforeEach(async (done) => { diff --git a/integration-tests/wallet-michelson-origination.spec.ts b/integration-tests/wallet-michelson-origination.spec.ts index 145c278024..caf05f7fb2 100644 --- a/integration-tests/wallet-michelson-origination.spec.ts +++ b/integration-tests/wallet-michelson-origination.spec.ts @@ -1,5 +1,7 @@ import { CONFIGS } from "./config"; -import { idMichelsonCode, idInitData } from "./data/id-contract" +import { idMichelsonCode, idInitData } from "./data/id-contract"; +import { MichelCodecParser, NoopParser, Context, InvalidCodeParameter } from '@taquito/taquito'; + CONFIGS().forEach(({ lib, rpc, setup }) => { const Tezos = lib; @@ -21,4 +23,59 @@ CONFIGS().forEach(({ lib, rpc, setup }) => { done(); }); }); + + describe(`Test contract origination to configure parserProvider to parse plain Michelson`, () => { + + beforeEach(async (done) => { + await setup() + done() + }) + it('uses noopParser to originate Michelson code and fails', async (done) => { + // Configure the Tezostoolkit to use the NoopParser (the Michelson won't be parsed) + Tezos.setParserProvider(new NoopParser()); + + try { + const op = await Tezos.wallet.originate({ + balance: "0", + code: idMichelsonCode, + init: idInitData + }).send(); + await op.confirmation() + } catch (error: any) { + expect(error).toBeInstanceOf(InvalidCodeParameter); + expect(error.message).toEqual('Wrong code parameter type, expected an array'); + } + done(); + }); + + it('uses MichelCodecParser to originate Michelson code and succeeds', async (done) => { + // Configure the Tezostoolkit to use the MichelCodecParser (the Michelson will be parsed to JSONMichelson) + Tezos.setParserProvider(new MichelCodecParser(new Context(rpc))); + + const op = await Tezos.wallet.originate({ + balance: "0", + code: idMichelsonCode, + init: idInitData + }).send(); + await op.confirmation() + expect(op.opHash).toBeDefined(); + expect(op.status).toBeDefined(); + done(); + }); + + it('no parser configured will use MichelCodecParser by default to originate Michelson code and succeeds', async (done) => { + // No parserProvider configured will use MichelCodecParser by default (the Michelson will be parsed to JSONMichelson) + const op = await Tezos.wallet.originate({ + balance: "0", + code: idMichelsonCode, + init: idInitData + }).send(); + await op.confirmation() + expect(op.opHash).toBeDefined(); + expect(op.status).toBeDefined(); + done(); + }); + }); + + }) diff --git a/packages/taquito/src/taquito.ts b/packages/taquito/src/taquito.ts index e39e4c034b..3d81ff46e5 100644 --- a/packages/taquito/src/taquito.ts +++ b/packages/taquito/src/taquito.ts @@ -27,6 +27,8 @@ import { LegacyWalletProvider, Wallet, WalletProvider } from './wallet'; import { OperationFactory } from './wallet/operation-factory'; import { TaquitoLocalForger } from './forger/taquito-local-forger'; import { EstimationProvider } from './estimate/estimate-provider-interface'; +import { ParserProvider } from './parser/interface'; +import { MichelCodecParser } from './parser/michel-codec-parser'; export { MichelsonMap, UnitValue } from '@taquito/michelson-encoder'; export { Forger, ForgeParams, ForgeResponse } from '@taquito/local-forging'; @@ -77,6 +79,7 @@ export interface SetProviderOptions { config?: Partial; packer?: Packer; globalConstantsProvider?: GlobalConstantsProvider; + parserProvider?: ParserProvider; } export interface VersionInfo { @@ -135,6 +138,7 @@ export class TezosToolkit { packer, globalConstantsProvider, readProvider, + parserProvider, }: SetProviderOptions) { this.setRpcProvider(rpc); this.setStreamProvider(stream); @@ -144,6 +148,7 @@ export class TezosToolkit { this.setPackerProvider(packer); this.setGlobalConstantsProvider(globalConstantsProvider); this.setReadProvider(readProvider); + this.setParserProvider(parserProvider); this._context.proto = protocol; if (config) { @@ -312,6 +317,27 @@ export class TezosToolkit { this._context.readProvider = readP; } + /** + * @description Sets parser provider on the Tezos Taquito instance + * + * @param options parserProvider to use to interact with the Tezos network + * + * @example + * ``` + * Tezos.setParserProvider(new MichelCodecParser(...)) + * ``` + */ + setParserProvider(parserProvider?: SetProviderOptions['parserProvider']) { + if (!this._options.parserProvider && typeof parserProvider === 'undefined') { + const p = new MichelCodecParser(this._context); + this._context.parser = p; + this._options.parserProvider = p; + } else if (typeof parserProvider !== 'undefined') { + this._context.parser = parserProvider; + this._options.parserProvider = parserProvider; + } + } + /** * @description Provide access to tezos account management */