Skip to content

Commit

Permalink
660 support customize parser options (#2061)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hui-an-yang authored Oct 31, 2022
1 parent 04c0a87 commit 6be3114
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 4 deletions.
5 changes: 4 additions & 1 deletion apps/taquito-test-dapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
2 changes: 1 addition & 1 deletion apps/taquito-test-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
54 changes: 53 additions & 1 deletion integration-tests/contract-michelson-origination.spec.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
59 changes: 58 additions & 1 deletion integration-tests/wallet-michelson-origination.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
});
});


})
26 changes: 26 additions & 0 deletions packages/taquito/src/taquito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -77,6 +79,7 @@ export interface SetProviderOptions {
config?: Partial<ConfigConfirmation>;
packer?: Packer;
globalConstantsProvider?: GlobalConstantsProvider;
parserProvider?: ParserProvider;
}

export interface VersionInfo {
Expand Down Expand Up @@ -135,6 +138,7 @@ export class TezosToolkit {
packer,
globalConstantsProvider,
readProvider,
parserProvider,
}: SetProviderOptions) {
this.setRpcProvider(rpc);
this.setStreamProvider(stream);
Expand All @@ -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) {
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 6be3114

Please sign in to comment.