Skip to content

Commit

Permalink
fix: fixed idx calculation in pairs with mix of annotated and unannot…
Browse files Browse the repository at this point in the history
…ated children (#2609)
  • Loading branch information
ac10n authored Aug 25, 2023
1 parent 269c7ae commit 1c32e37
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/taquito-michelson-encoder/src/tokens/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
);
}
});
Expand Down
48 changes: 46 additions & 2 deletions packages/taquito-michelson-encoder/test/tokens/pair.spec.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -162,7 +164,7 @@ describe('Complexe pair token', () => {
optional: {
__michelsonType: 'pair',
schema: {
'3': {
'2': {
__michelsonType: 'or',
schema: {
int: {
Expand All @@ -175,7 +177,7 @@ describe('Complexe pair token', () => {
},
},
},
'4': {
'3': {
__michelsonType: 'or',
schema: {
int: {
Expand Down Expand Up @@ -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),
},
});
});

0 comments on commit 1c32e37

Please sign in to comment.