Skip to content

Commit

Permalink
revert: implementation and test of nested options with encode (due to…
Browse files Browse the repository at this point in the history
… limitation)

re #2344
  • Loading branch information
hui-an-yang committed Apr 19, 2023
1 parent 8a87d3d commit 7c0aa75
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 31 deletions.
26 changes: 7 additions & 19 deletions packages/taquito-michelson-encoder/src/tokens/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,18 @@ export class OptionToken extends ComparableToken {
}

public Encode(args: any): any {
let value = args;
const value = args;
if (value === undefined || value === null) {
return { prim: 'None' };
}
if (!Array.isArray(value)) {
return { prim: 'Some', args: [this.schema().Encode(value)] };
}
if (Array.isArray(value[value.length - 1])) {
value = value[value.length - 1];
}
if (value[0] === 'Some') {
value.shift();
return { prim: 'Some', args: [this.schema().Encode(value)] };
}
if (
value[value.length - 1] === 'None' ||
value[value.length - 1] === null ||
value[value.length - 1] === undefined
} else if (
Array.isArray(value) &&
(value[value.length - 1] === undefined || value[value.length - 1] === null)
) {
value.pop();
return { prim: 'None' };
}
return { prim: 'Some', args: [this.schema().Encode(value)] };

return { prim: 'Some', args: [this.schema().Encode(args)] };
}

public EncodeObject(args: any, semantic?: SemanticEncoding): any {
Expand Down Expand Up @@ -86,8 +75,7 @@ export class OptionToken extends ComparableToken {
}

public ExtractSignature() {
// return [...this.schema().ExtractSignature(), []];
return ['Some' as any, ...this.schema().ExtractSignature()];
return [...this.schema().ExtractSignature()];
}

get KeySchema(): ComparableToken {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,5 @@ describe('Exchange contract test', () => {
it('Should extract signature properly', () => {
const schema = new ParameterSchema(parameter.args[0]);
expect(schema.ExtractSignatures()).toContainEqual(['key_hash']);
expect(schema.ExtractSignatures()).toContainEqual('Some');
});
});
11 changes: 0 additions & 11 deletions packages/taquito-michelson-encoder/test/tokens/option.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ describe('Option token', () => {
prim: 'Some',
args: [{ prim: 'Some', args: [{ int: '10' }] }],
});
expect(nestedToken.Encode(['Some', 10])).toEqual({
prim: 'Some',
args: [{ prim: 'Some', args: [{ int: '10' }] }],
});
});

it('Should encode to None when null', () => {
Expand All @@ -84,13 +80,6 @@ describe('Option token', () => {
expect(unitToken.Encode([undefined])).toEqual({ prim: 'None' });
expect(nestedToken.Encode([undefined])).toEqual({ prim: 'None' });
});

it('Should encode to Some(None) when ["Some", null]', () => {
expect(nestedToken.Encode(['Some', null])).toEqual({
prim: 'Some',
args: [{ prim: 'None' }],
});
});
});

describe('Execute', () => {
Expand Down

0 comments on commit 7c0aa75

Please sign in to comment.