From f547446e581223ca2eb2a487c804d190043270a8 Mon Sep 17 00:00:00 2001 From: Alireza Haghshenas Date: Fri, 18 Aug 2023 16:26:25 -0700 Subject: [PATCH] fix: fixed idx calculation in pairs with mix of annotated and unannotated children --- .../src/tokens/pair.ts | 6 +-- .../test/tokens/constant.spec.ts | 2 +- .../test/tokens/pair.spec.ts | 48 ++++++++++++++++++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/packages/taquito-michelson-encoder/src/tokens/pair.ts b/packages/taquito-michelson-encoder/src/tokens/pair.ts index 30cba99f26..1bf8c02656 100644 --- a/packages/taquito-michelson-encoder/src/tokens/pair.ts +++ b/packages/taquito-michelson-encoder/src/tokens/pair.ts @@ -169,11 +169,11 @@ export class PairToken extends ComparableToken { const leftToken = this.createToken(args[0], this.idx); let keyCount = 1; let leftValue; - if (leftToken instanceof PairToken) { - keyCount = Object.keys(leftToken.ExtractSchema()).length; - } if (leftToken instanceof PairToken && !leftToken.hasAnnotations()) { leftValue = getLeftValue(leftToken); + if (leftToken instanceof PairToken) { + keyCount = Object.keys(leftToken.ExtractSchema()).length; + } } else { leftValue = { [leftToken.annot()]: getLeftValue(leftToken) }; } diff --git a/packages/taquito-michelson-encoder/test/tokens/constant.spec.ts b/packages/taquito-michelson-encoder/test/tokens/constant.spec.ts index 3f120bc6b7..1ab7481338 100644 --- a/packages/taquito-michelson-encoder/test/tokens/constant.spec.ts +++ b/packages/taquito-michelson-encoder/test/tokens/constant.spec.ts @@ -79,7 +79,7 @@ describe('Global constant token', () => { } catch (e: any) { expect(e).toBeInstanceOf(GlobalConstantDecodingError); expect(e.message).toEqual( - `[2] Unable to decode a value represented by a global constants. Please provide an expanded script to the Michelson-Encoder or semantics for the decoding. The following global constant hash was encountered: expru5X5fvCer8tbRkSAtwyVCs9FUCq46JQG7QCAkhZSumjbZBUGzb.` + `[1] Unable to decode a value represented by a global constants. Please provide an expanded script to the Michelson-Encoder or semantics for the decoding. The following global constant hash was encountered: expru5X5fvCer8tbRkSAtwyVCs9FUCq46JQG7QCAkhZSumjbZBUGzb.` ); } }); diff --git a/packages/taquito-michelson-encoder/test/tokens/pair.spec.ts b/packages/taquito-michelson-encoder/test/tokens/pair.spec.ts index d356392b31..1fb1a2aa81 100644 --- a/packages/taquito-michelson-encoder/test/tokens/pair.spec.ts +++ b/packages/taquito-michelson-encoder/test/tokens/pair.spec.ts @@ -1,5 +1,7 @@ +import { Schema } from '../../src/taquito-michelson-encoder'; import { createToken } from '../../src/tokens/createToken'; import { PairToken } from '../../src/tokens/pair'; +import BigNumber from 'bignumber.js'; describe('Pair token', () => { const token = createToken( @@ -162,7 +164,7 @@ describe('Complexe pair token', () => { optional: { __michelsonType: 'pair', schema: { - '3': { + '2': { __michelsonType: 'or', schema: { int: { @@ -175,7 +177,7 @@ describe('Complexe pair token', () => { }, }, }, - '4': { + '3': { __michelsonType: 'or', schema: { int: { @@ -312,3 +314,45 @@ describe('Complexe pair token', () => { }); }); }); + +describe('PairToken mixed with and without annotations', () => { + const schema = { + prim: 'pair', + args: [ + { + prim: 'pair', + args: [ + { + prim: 'pair', + args: [{ prim: 'int' }, { prim: 'int' }], + annots: ['%A3'], + }, + { prim: 'int' }, + ], + }, + { prim: 'bool' }, + ], + }; + + const michelineJson = { + prim: 'Pair', + args: [ + { + prim: 'Pair', + args: [{ prim: 'Pair', args: [{ int: '11' }, { int: '22' }] }, { int: '33' }], + }, + { prim: 'True' }, + ], + }; + + const javaScriptObject = new Schema(schema).Execute(michelineJson); + + expect(javaScriptObject).toEqual({ + 1: new BigNumber(33), + 2: true, + A3: { + 0: new BigNumber(11), + 1: new BigNumber(22), + }, + }); +});