From e8115cbe6592c58ccd0ab3cfe1d425867cfa8045 Mon Sep 17 00:00:00 2001 From: Alireza Haghshenas Date: Wed, 26 Jun 2024 15:58:04 -0700 Subject: [PATCH] docs: use a smaller example, enable live code blocks for the example --- docs/michelson_encoder.md | 588 +++--------------- website/src/theme/CodeBlock/index.js | 4 +- .../version-20.0.0/michelson_encoder.md | 588 +++--------------- 3 files changed, 163 insertions(+), 1017 deletions(-) diff --git a/docs/michelson_encoder.md b/docs/michelson_encoder.md index 6ac085652e..9e9f69c391 100644 --- a/docs/michelson_encoder.md +++ b/docs/michelson_encoder.md @@ -548,526 +548,98 @@ When having nested `pair`s or unions (`or`), Taquito assigns numbers to fields w In previous versions of Taquito, the nested object's fields were numbered were a continuation of the parent object's fields. For example, the following schema: -```js -{ - prim: 'or', - args: [ - { - prim: 'pair', - args: [{ prim: 'address' }, { prim: 'nat' }], - annots: ['%transfer'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [{ prim: 'address' }, { prim: 'nat' }], - annots: ['%approve'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { - prim: 'pair', - args: [{ prim: 'address' }, { prim: 'nat' }] - } - ], - annots: ['%transferFrom'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { - prim: 'contract', - args: [{ prim: 'nat' }], - annots: [':NatContract'] - } - ], - annots: ['%balanceOf'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { - prim: 'pair', - args: [ - { prim: 'address' }, - { - prim: 'contract', - args: [ - { - prim: 'pair', - args: [ - { prim: 'nat' }, - { prim: 'nat' } - ] - } - ], - annots: [':NatNatContract'] - } - ] - } - ], - annots: ['%allowance'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { prim: 'nat' } - ], - annots: ['%createAccount'] - }, - { - prim: 'list', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { prim: 'nat' } - ] - } - ], - annots: ['%createAccounts'] - } - ] - } - ] - } - ] - } - ] - } - ] - } - ], - annots: [':_entries'] -} +```js live noInline +const param = { + prim: 'or', + args: [ + { + prim: 'pair', + args: [{ prim: 'address' }, { prim: 'nat' }], + annots: ['%transfer'] + }, + { + prim: 'or', + args: [ + { + prim: 'pair', + args: [{ prim: 'address' }, { prim: 'nat' }], + annots: ['%approve'] + }, + { + prim: 'pair', + args: [{ prim: 'address' }, { prim: 'nat' }], + annots: ['%mint'] + } + ] + } + ], + annots: [':_entries'] +}; +const parameterSchema = new ParameterSchema(param); +Token.fieldNumberingStrategy = "Legacy"; //To bring back the old behavior +const value = parameterSchema.generateSchema(); +console.log(JSON.stringify(value, null, 2)); +Token.fieldNumberingStrategy = 'Latest'; //To restore the default (new) behavior ``` -The function `generateSchema` would return the following object: - -```js -{ - __michelsonType: 'or', - schema: { - allowance: { - __michelsonType: 'pair', - schema: { - '4': { - __michelsonType: 'address', - schema: 'address', - }, - '5': { - __michelsonType: 'address', - schema: 'address', - }, - NatNatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'nat', - schema: 'nat', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - }, - }, - approve: { - __michelsonType: 'pair', - schema: { - '1': { - __michelsonType: 'address', - schema: 'address', - }, - '2': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - balanceOf: { - __michelsonType: 'pair', - schema: { - '3': { - __michelsonType: 'address', - schema: 'address', - }, - NatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - createAccount: { - __michelsonType: 'pair', - schema: { - '5': { - __michelsonType: 'address', - schema: 'address', - }, - '6': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - createAccounts: { - __michelsonType: 'list', - schema: { - __michelsonType: 'pair', - schema: { - '6': { - __michelsonType: 'address', - schema: 'address', - }, - '7': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - transfer: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - transferFrom: { - __michelsonType: 'pair', - schema: { - '2': { - __michelsonType: 'address', - schema: 'address', - }, - '3': { - __michelsonType: 'address', - schema: 'address', - }, - '4': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, -} -``` +Please run the code above and check the output. Please note how nested field numbers are not predictable. The field numbers are assigned in the order their parent were encountered during the traversal of the tree. While this behavior is not an error, it is prone to unexpected changes when the schema is modified. Also, predicting the field number of a specific field is not straightforward. With the release of Taquito version 20.0.0, we have made a breaking change in the Michelson Encoder package. The field numbering is now predictable and consistent. -The field numbers for each nested object (`Or`/`Pair`) are now reset from zero: - -```js -{ - __michelsonType: 'or', - schema: { - allowance: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'address', - schema: 'address', - }, - NatNatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'nat', - schema: 'nat', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - }, - }, - approve: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - balanceOf: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - NatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - createAccount: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - createAccounts: { - __michelsonType: 'list', - schema: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - transfer: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - transferFrom: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'address', - schema: 'address', - }, - '2': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, -} -``` +The field numbers for each nested object (`Or`/`Pair`) are now reset from zero. You can see that by commenting out the line: `Token.fieldNumberingStrategy = "Legacy";` and running the code again. Below you can see a diff of the new versus old behavior: ```diff { - __michelsonType: 'or', - schema: { - allowance: { - __michelsonType: 'pair', - schema: { -- '4': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '5': { -+ '1': { - __michelsonType: 'address', - schema: 'address', - }, - NatNatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'nat', - schema: 'nat', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - }, - }, - approve: { - __michelsonType: 'pair', - schema: { -- '1': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '2': { -+ '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - balanceOf: { - __michelsonType: 'pair', - schema: { -- '3': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, - NatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - createAccount: { - __michelsonType: 'pair', - schema: { -- '5': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '6': { -+ '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - createAccounts: { - __michelsonType: 'list', - schema: { - __michelsonType: 'pair', - schema: { -- '6': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '7': { -+ '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - transfer: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - transferFrom: { - __michelsonType: 'pair', - schema: { -- '2': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '3': { -+ '1': { - __michelsonType: 'address', - schema: 'address', - }, -- '4': { -+ '2': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, + "__michelsonType": "or", + "schema": { + "transfer": { + "__michelsonType": "pair", + "schema": { + "0": { + "__michelsonType": "address", + "schema": "address" + }, + "1": { + "__michelsonType": "nat", + "schema": "nat" + } + } + }, + "approve": { + "__michelsonType": "pair", + "schema": { +- "1": { ++ "0": { + "__michelsonType": "address", + "schema": "address" + }, +- "2": { ++ "1": { + "__michelsonType": "nat", + "schema": "nat" + } + } + }, + "mint": { + "__michelsonType": "pair", + "schema": { +- "2": { ++ "0": { + "__michelsonType": "address", + "schema": "address" + }, +- "3": { ++ "1": { + "__michelsonType": "nat", + "schema": "nat" + } + } + } + } } ``` You can enable the old behavior by setting the `Token.fieldNumberingStrategy = 'Legacy'`. Please not that this value should stay the same for the whole application. diff --git a/website/src/theme/CodeBlock/index.js b/website/src/theme/CodeBlock/index.js index cd93b05172..9d0a6b9cb6 100755 --- a/website/src/theme/CodeBlock/index.js +++ b/website/src/theme/CodeBlock/index.js @@ -83,7 +83,7 @@ export default ({ const { LedgerSigner, DerivationType } = await import('@taquito/ledger-signer'); const { Tzip16Module, tzip16, MichelsonStorageView } = await import('@taquito/tzip16') const { Tzip12Module, tzip12 } = await import("@taquito/tzip12"); - const { Schema, ParameterSchema } = await import("@taquito/michelson-encoder"); + const { Schema, ParameterSchema, Token } = await import("@taquito/michelson-encoder"); const { Parser, packDataBytes, emitMicheline } = await import('@taquito/michel-codec'); const { RpcClient } = await import('@taquito/rpc'); const TransportWebHID = (await import("@ledgerhq/hw-transport-webhid")).default; @@ -128,6 +128,7 @@ export default ({ compose, Schema, ParameterSchema, + Token, getRevealFee, verifySignature, Parser, @@ -185,6 +186,7 @@ export default ({ compose: dependencies?.compose, Schema: dependencies?.Schema, ParameterSchema: dependencies?.ParameterSchema, + Token: dependencies?.Token, getRevealFee: dependencies?.getRevealFee, verifySignature: dependencies?.verifySignature, Parser: dependencies?.Parser, diff --git a/website/versioned_docs/version-20.0.0/michelson_encoder.md b/website/versioned_docs/version-20.0.0/michelson_encoder.md index 6ac085652e..9e9f69c391 100644 --- a/website/versioned_docs/version-20.0.0/michelson_encoder.md +++ b/website/versioned_docs/version-20.0.0/michelson_encoder.md @@ -548,526 +548,98 @@ When having nested `pair`s or unions (`or`), Taquito assigns numbers to fields w In previous versions of Taquito, the nested object's fields were numbered were a continuation of the parent object's fields. For example, the following schema: -```js -{ - prim: 'or', - args: [ - { - prim: 'pair', - args: [{ prim: 'address' }, { prim: 'nat' }], - annots: ['%transfer'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [{ prim: 'address' }, { prim: 'nat' }], - annots: ['%approve'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { - prim: 'pair', - args: [{ prim: 'address' }, { prim: 'nat' }] - } - ], - annots: ['%transferFrom'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { - prim: 'contract', - args: [{ prim: 'nat' }], - annots: [':NatContract'] - } - ], - annots: ['%balanceOf'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { - prim: 'pair', - args: [ - { prim: 'address' }, - { - prim: 'contract', - args: [ - { - prim: 'pair', - args: [ - { prim: 'nat' }, - { prim: 'nat' } - ] - } - ], - annots: [':NatNatContract'] - } - ] - } - ], - annots: ['%allowance'] - }, - { - prim: 'or', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { prim: 'nat' } - ], - annots: ['%createAccount'] - }, - { - prim: 'list', - args: [ - { - prim: 'pair', - args: [ - { prim: 'address' }, - { prim: 'nat' } - ] - } - ], - annots: ['%createAccounts'] - } - ] - } - ] - } - ] - } - ] - } - ] - } - ], - annots: [':_entries'] -} +```js live noInline +const param = { + prim: 'or', + args: [ + { + prim: 'pair', + args: [{ prim: 'address' }, { prim: 'nat' }], + annots: ['%transfer'] + }, + { + prim: 'or', + args: [ + { + prim: 'pair', + args: [{ prim: 'address' }, { prim: 'nat' }], + annots: ['%approve'] + }, + { + prim: 'pair', + args: [{ prim: 'address' }, { prim: 'nat' }], + annots: ['%mint'] + } + ] + } + ], + annots: [':_entries'] +}; +const parameterSchema = new ParameterSchema(param); +Token.fieldNumberingStrategy = "Legacy"; //To bring back the old behavior +const value = parameterSchema.generateSchema(); +console.log(JSON.stringify(value, null, 2)); +Token.fieldNumberingStrategy = 'Latest'; //To restore the default (new) behavior ``` -The function `generateSchema` would return the following object: - -```js -{ - __michelsonType: 'or', - schema: { - allowance: { - __michelsonType: 'pair', - schema: { - '4': { - __michelsonType: 'address', - schema: 'address', - }, - '5': { - __michelsonType: 'address', - schema: 'address', - }, - NatNatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'nat', - schema: 'nat', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - }, - }, - approve: { - __michelsonType: 'pair', - schema: { - '1': { - __michelsonType: 'address', - schema: 'address', - }, - '2': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - balanceOf: { - __michelsonType: 'pair', - schema: { - '3': { - __michelsonType: 'address', - schema: 'address', - }, - NatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - createAccount: { - __michelsonType: 'pair', - schema: { - '5': { - __michelsonType: 'address', - schema: 'address', - }, - '6': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - createAccounts: { - __michelsonType: 'list', - schema: { - __michelsonType: 'pair', - schema: { - '6': { - __michelsonType: 'address', - schema: 'address', - }, - '7': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - transfer: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - transferFrom: { - __michelsonType: 'pair', - schema: { - '2': { - __michelsonType: 'address', - schema: 'address', - }, - '3': { - __michelsonType: 'address', - schema: 'address', - }, - '4': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, -} -``` +Please run the code above and check the output. Please note how nested field numbers are not predictable. The field numbers are assigned in the order their parent were encountered during the traversal of the tree. While this behavior is not an error, it is prone to unexpected changes when the schema is modified. Also, predicting the field number of a specific field is not straightforward. With the release of Taquito version 20.0.0, we have made a breaking change in the Michelson Encoder package. The field numbering is now predictable and consistent. -The field numbers for each nested object (`Or`/`Pair`) are now reset from zero: - -```js -{ - __michelsonType: 'or', - schema: { - allowance: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'address', - schema: 'address', - }, - NatNatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'nat', - schema: 'nat', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - }, - }, - approve: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - balanceOf: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - NatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - createAccount: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - createAccounts: { - __michelsonType: 'list', - schema: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - transfer: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - transferFrom: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'address', - schema: 'address', - }, - '2': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, -} -``` +The field numbers for each nested object (`Or`/`Pair`) are now reset from zero. You can see that by commenting out the line: `Token.fieldNumberingStrategy = "Legacy";` and running the code again. Below you can see a diff of the new versus old behavior: ```diff { - __michelsonType: 'or', - schema: { - allowance: { - __michelsonType: 'pair', - schema: { -- '4': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '5': { -+ '1': { - __michelsonType: 'address', - schema: 'address', - }, - NatNatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'nat', - schema: 'nat', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - }, - }, - approve: { - __michelsonType: 'pair', - schema: { -- '1': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '2': { -+ '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - balanceOf: { - __michelsonType: 'pair', - schema: { -- '3': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, - NatContract: { - __michelsonType: 'contract', - schema: { - parameter: { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - }, - createAccount: { - __michelsonType: 'pair', - schema: { -- '5': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '6': { -+ '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - createAccounts: { - __michelsonType: 'list', - schema: { - __michelsonType: 'pair', - schema: { -- '6': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '7': { -+ '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, - transfer: { - __michelsonType: 'pair', - schema: { - '0': { - __michelsonType: 'address', - schema: 'address', - }, - '1': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - transferFrom: { - __michelsonType: 'pair', - schema: { -- '2': { -+ '0': { - __michelsonType: 'address', - schema: 'address', - }, -- '3': { -+ '1': { - __michelsonType: 'address', - schema: 'address', - }, -- '4': { -+ '2': { - __michelsonType: 'nat', - schema: 'nat', - }, - }, - }, - }, + "__michelsonType": "or", + "schema": { + "transfer": { + "__michelsonType": "pair", + "schema": { + "0": { + "__michelsonType": "address", + "schema": "address" + }, + "1": { + "__michelsonType": "nat", + "schema": "nat" + } + } + }, + "approve": { + "__michelsonType": "pair", + "schema": { +- "1": { ++ "0": { + "__michelsonType": "address", + "schema": "address" + }, +- "2": { ++ "1": { + "__michelsonType": "nat", + "schema": "nat" + } + } + }, + "mint": { + "__michelsonType": "pair", + "schema": { +- "2": { ++ "0": { + "__michelsonType": "address", + "schema": "address" + }, +- "3": { ++ "1": { + "__michelsonType": "nat", + "schema": "nat" + } + } + } + } } ``` You can enable the old behavior by setting the `Token.fieldNumberingStrategy = 'Legacy'`. Please not that this value should stay the same for the whole application.