From 4166bafcb2be5dc6c55c3b82d696f2abd9c09da7 Mon Sep 17 00:00:00 2001 From: Sergey Kintsel Date: Thu, 6 Jun 2024 11:40:55 +0100 Subject: [PATCH] Fix contract origination with Michelson storage --- src/utils/tezos/helpers.test.ts | 93 +++++++++++++++++++++++++-------- src/utils/tezos/helpers.ts | 21 +++++++- 2 files changed, 91 insertions(+), 23 deletions(-) diff --git a/src/utils/tezos/helpers.test.ts b/src/utils/tezos/helpers.test.ts index 20a0227d04..d182f20e94 100644 --- a/src/utils/tezos/helpers.test.ts +++ b/src/utils/tezos/helpers.test.ts @@ -160,30 +160,79 @@ describe("tezos utils helpers", () => { }); }); - test("contract_origination", () => { - const operation: ContractOrigination = { - type: "contract_origination", - sender: mockImplicitAddress(0), - code: [{ prim: "unit" }], - storage: { some: { nested: "storage" } }, - }; - expect(operationToTaquitoOperation(operation)).toEqual({ - code: [ - { - prim: "unit", + describe("contract_origination", () => { + it("converts an origination with a plain JS storage object", () => { + const operation: ContractOrigination = { + type: "contract_origination", + sender: mockImplicitAddress(0), + code: [{ prim: "unit" }], + storage: { some: { nested: "storage" } }, + }; + + expect(operationToTaquitoOperation(operation)).toEqual({ + code: [ + { + prim: "unit", + }, + ], + kind: "origination", + storage: { + some: { + nested: "storage", + }, }, - ], - kind: "origination", - sender: { - pkh: "tz1gUNyn3hmnEWqkusWPzxRaon1cs7ndWh7h", - type: "implicit", - }, - storage: { - some: { - nested: "storage", + }); + }); + + it("converts an origination with a valid Michelson storage object", () => { + const operation: ContractOrigination = { + type: "contract_origination", + sender: mockImplicitAddress(0), + code: [{ prim: "unit" }], + storage: { + prim: "Pair", + args: [ + { + prim: "Pair", + args: [ + [], + [ + { + prim: "Elt", + args: [ + { + string: "tz1UNer1ijeE9ndjzSszRduR3CzX49hoBUB3", + }, + { + int: "100000", + }, + ], + }, + ], + ], + }, + { + prim: "Pair", + args: [ + { + string: "tz1UNer1ijeE9ndjzSszRduR3CzX49hoBUB3", + }, + [], + ], + }, + ], }, - }, - type: "contract_origination", + }; + + expect(operationToTaquitoOperation(operation)).toEqual({ + code: [ + { + prim: "unit", + }, + ], + kind: "origination", + init: operation.storage, + }); }); }); diff --git a/src/utils/tezos/helpers.ts b/src/utils/tezos/helpers.ts index ddfae79adc..f692aebb99 100644 --- a/src/utils/tezos/helpers.ts +++ b/src/utils/tezos/helpers.ts @@ -1,5 +1,6 @@ import TransportWebUSB from "@ledgerhq/hw-transport-webusb"; import { DerivationType, LedgerSigner } from "@taquito/ledger-signer"; +import { Parser } from "@taquito/michel-codec"; import { OpKind } from "@taquito/rpc"; import { Curves, InMemorySigner } from "@taquito/signer"; import { ParamsWithKind, TezosToolkit, WalletParamsWithKind } from "@taquito/taquito"; @@ -174,14 +175,32 @@ export const operationToTaquitoOperation = (operation: Operation): ParamsWithKin parameter: makeFA2TransactionParameter(operation), }; case "contract_origination": { + // if storage is a valid Michelson we need to pass it in as init, not the storage + if (isValidMichelson(operation.storage)) { + return { + kind: OpKind.ORIGINATION, + code: operation.code, + init: operation.storage, + }; + } return { kind: OpKind.ORIGINATION, - ...operation, + code: operation.code, + storage: operation.storage, }; } } }; +const isValidMichelson = (rawStorage: any): boolean => { + try { + new Parser().parseJSON(rawStorage); + return true; + } catch (_) { + return false; + } +}; + export const operationsToBatchParams = ({ type: operationsType, operations: originalOperations,