Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2073 support type ticket deprecated in michelson encoder #2094

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
47cf160
Merge branch 'master' of github.com:ecadlabs/taquito
hui-an-yang Aug 8, 2022
53bf5c5
Merge branch 'master' of github.com:ecadlabs/taquito
hui-an-yang Aug 11, 2022
eddeb1d
Merge branch 'master' of github.com:ecadlabs/taquito
hui-an-yang Aug 23, 2022
ac9bd70
Merge branch 'master' of github.com:ecadlabs/taquito
hui-an-yang Sep 13, 2022
ca9577d
Merge branch 'master' of github.com:ecadlabs/taquito
hui-an-yang Sep 28, 2022
8c1643d
Merge branch 'master' of github.com:ecadlabs/taquito
hui-an-yang Oct 4, 2022
d34128b
Merge branch 'master' of github.com:ecadlabs/taquito
hui-an-yang Oct 5, 2022
ad53532
Merge branch 'master' of github.com:ecadlabs/taquito
hui-an-yang Oct 20, 2022
3280638
feat: exposed setParserProvider configuration through TezosToolkit
hui-an-yang Oct 25, 2022
a82f23a
test: added integration tests using setParserProvider with TezosToolkit
hui-an-yang Oct 25, 2022
7f7c813
docs: updated starting production server commands with taquito-test-d…
hui-an-yang Oct 25, 2022
77d79f4
test: removed unnecessory console.log
hui-an-yang Oct 25, 2022
c03f145
Merge branch 'master' of github.com:ecadlabs/taquito into 660-support…
hui-an-yang Oct 26, 2022
1a12a83
chore: update vite to latest version
hui-an-yang Oct 27, 2022
ef0ca04
docs: fixed some typos in doc to address comments
hui-an-yang Oct 28, 2022
5579da2
Merge branch 'master' of github.com:ecadlabs/taquito into 660-support…
hui-an-yang Oct 28, 2022
04c0a87
1630 ballot operation support in contract API (#2050)
dsawali Oct 30, 2022
6be3114
660 support customize parser options (#2061)
hui-an-yang Oct 31, 2022
1359969
feat(n): support type ticket_deprecated in michelson encoder
hui-an-yang Oct 31, 2022
59fccf4
contract-security-non-existent-KT-address: use rpc (#2091)
arvidj Nov 1, 2022
910b295
Merge pull request #2092 from arvidj/arvid@2091-use-rpc-in-contract-s…
roxaneletourneau Nov 1, 2022
0d6dd3e
Merge branch 'master' of github.com:ecadlabs/taquito into 2073-suppor…
hui-an-yang Nov 1, 2022
4158036
fix: address comment with TicketDeprecatedEncodeError and fix unit tests
hui-an-yang Nov 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});


})
11 changes: 10 additions & 1 deletion packages/taquito-michelson-encoder/src/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export type TicketTokenSchema = {
amount: { __michelsonType: 'int'; schema: 'int' };
};
};
export type TicketDeprecatedTokenSchema = {
__michelsonType: 'ticket_deprecated';
schema: {
value: TokenSchema;
ticketer: { __michelsonType: 'contract'; schema: 'contract' };
amount: { __michelsonType: 'int'; schema: 'int' };
};
};

export type TokenSchema =
| BaseTokenSchema
Expand All @@ -83,4 +91,5 @@ export type TokenSchema =
| SaplingStateTokenSchema
| SaplingTransactionTokenSchema
| SaplingTransactionDeprecatedTokenSchema
| TicketTokenSchema;
| TicketTokenSchema
| TicketDeprecatedTokenSchema;
106 changes: 106 additions & 0 deletions packages/taquito-michelson-encoder/src/tokens/ticket-deprecated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { TicketDeprecatedTokenSchema } from '../schema/types';
import { IntToken } from './comparable/int';
import { ContractToken } from './contract';
import { Token, TokenFactory, Semantic, SemanticEncoding } from './token';

/**
* @category Error
* @description Error that indicates a failure when encoding and sending a ticket to the blockchain
*/
export class EncodeTicketError extends Error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we need a TicketDeprecatedEncodeError instead? In essence, it seems to do the same job as TicketEncodeError just more descriptive to be ticket-deprecated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think TicketDeprecatedEncodeError would be more descriptive

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, comment addressed in the new commit :)

name = 'TicketEncodeError';

constructor() {
super('Tickets cannot be sent to the blockchain; they are created on-chain');
}
}

const ticketerType = { prim: 'contract' };
const amountType = { prim: 'int' };

export class TicketDeprecatedToken extends Token {
static prim: 'ticket_deprecated' = 'ticket_deprecated' as const;

constructor(
protected val: { prim: string; args: any[]; annots: any[] },
protected idx: number,
protected fac: TokenFactory
) {
super(val, idx, fac);
}

get valueToken() {
return this.createToken(this.val.args[0], this.idx);
}

public Encode(_args: any[]): any {
throw new EncodeTicketError();
}

public EncodeObject(args: any, semantic?: SemanticEncoding): any {
if (semantic && semantic[TicketDeprecatedToken.prim]) {
return semantic[TicketDeprecatedToken.prim](args, this.val);
}
throw new EncodeTicketError();
}

public Execute(val: any, semantics?: Semantic) {
if (semantics && semantics[TicketDeprecatedToken.prim]) {
return semantics[TicketDeprecatedToken.prim](val, this.val);
}
const ticketer = this.createToken(ticketerType, this.idx);
const value = this.valueToken;
const amount = this.createToken(amountType, this.idx);

if (undefined === val.args[2] && undefined !== val.args[1].args) {
return {
ticketer: ticketer.Execute(val.args[0], semantics),
value: value.Execute(val.args[1].args[0], semantics),
amount: amount.Execute(val.args[1].args[1], semantics),
};
}

return {
ticketer: ticketer.Execute(val.args[0], semantics),
value: value.Execute(val.args[1], semantics),
amount: amount.Execute(val.args[2], semantics),
};
}

/**
* @deprecated ExtractSchema has been deprecated in favor of generateSchema
*
*/
public ExtractSchema() {
return {
ticketer: ContractToken.prim,
value: this.valueToken.ExtractSchema(),
amount: IntToken.prim,
};
}

generateSchema(): TicketDeprecatedTokenSchema {
return {
__michelsonType: TicketDeprecatedToken.prim,
schema: {
value: this.valueToken.generateSchema(),
ticketer: {
__michelsonType: ContractToken.prim,
schema: ContractToken.prim,
},
amount: {
__michelsonType: IntToken.prim,
schema: IntToken.prim,
},
},
};
}

findAndReturnTokens(tokenToFind: string, tokens: Token[]) {
if (TicketDeprecatedToken.prim === tokenToFind) {
tokens.push(this);
}
this.valueToken.findAndReturnTokens(tokenToFind, tokens);
return tokens;
}
}
2 changes: 2 additions & 0 deletions packages/taquito-michelson-encoder/src/tokens/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { OperationToken } from './operation';
import { SetToken } from './set';
import { ChainIDToken } from './chain-id';
import { TicketToken } from './ticket';
import { TicketDeprecatedToken } from './ticket-deprecated';
import { NeverToken } from './never';
import { SaplingStateToken } from './sapling-state';
import { SaplingTransactionToken } from './sapling-transaction';
Expand Down Expand Up @@ -70,6 +71,7 @@ export const tokens = [
SetToken,
ChainIDToken,
TicketToken,
TicketDeprecatedToken,
NeverToken,
SaplingStateToken,
SaplingTransactionToken,
Expand Down
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