Skip to content

Commit

Permalink
Fix contract origination with Michelson storage
Browse files Browse the repository at this point in the history
  • Loading branch information
serjonya-trili committed Jun 6, 2024
1 parent d2dbd17 commit 2faa531
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 23 deletions.
93 changes: 71 additions & 22 deletions src/utils/tezos/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
});

Expand Down
21 changes: 20 additions & 1 deletion src/utils/tezos/helpers.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2faa531

Please sign in to comment.