From 5bd13bbecea5a7b37d01c7ccd32187b4f7d91554 Mon Sep 17 00:00:00 2001 From: Brian Li Date: Thu, 24 Aug 2023 11:21:14 -0400 Subject: [PATCH] Support Option in contract --- .../Account/Tabs/ModulesTab/Contract.tsx | 43 +++++++++++-------- src/utils.ts | 6 ++- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/pages/Account/Tabs/ModulesTab/Contract.tsx b/src/pages/Account/Tabs/ModulesTab/Contract.tsx index ef772413..1f61af40 100644 --- a/src/pages/Account/Tabs/ModulesTab/Contract.tsx +++ b/src/pages/Account/Tabs/ModulesTab/Contract.tsx @@ -106,24 +106,27 @@ function Contract({address, isRead}: {address: string; isRead: boolean}) { return ; } - const moduleAndFnsGroup = modules.reduce((acc, module) => { - if (module.abi === undefined) { - return acc; - } + const moduleAndFnsGroup = modules.reduce( + (acc, module) => { + if (module.abi === undefined) { + return acc; + } - const fns = module.abi.exposed_functions.filter((fn) => - isRead ? fn.is_view : fn.is_entry, - ); - if (fns.length === 0) { - return acc; - } + const fns = module.abi.exposed_functions.filter((fn) => + isRead ? fn.is_view : fn.is_entry, + ); + if (fns.length === 0) { + return acc; + } - const moduleName = module.abi.name; - return { - ...acc, - [moduleName]: fns, - } as Record; - }, {} as Record); + const moduleName = module.abi.name; + return { + ...acc, + [moduleName]: fns, + } as Record; + }, + {} as Record, + ); const module = modules.find((m) => m.abi?.name === selectedModuleName)?.abi; const fn = selectedModuleName @@ -309,6 +312,8 @@ function RunContractForm({ x.toString(), ) : deserializeVector(arg); + } else if (type.startsWith("0x1::option::Option")) { + arg ? {vec: [arg]} : undefined; } else return arg; }), }; @@ -683,16 +688,18 @@ function ContractForm({ ))} {fnParams.map((param, i) => { + // TODO: Need a nice way to differenciate between option and empty string + const isOption = param.startsWith("0x1::option::Option"); return ( ( diff --git a/src/utils.ts b/src/utils.ts index a3ac8ab9..ada7fde4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -12,8 +12,8 @@ export function assertNever(x: never): never { } /* -If the transaction doesn't have a version property, -that means it's a pending transaction (and thus it's expected version will be higher than any existing versions). +If the transaction doesn't have a version property, +that means it's a pending transaction (and thus it's expected version will be higher than any existing versions). We can consider the version to be Infinity for this case. */ export function sortTransactions( @@ -174,6 +174,8 @@ export function encodeInputArgsForViewRequest(type: string, value: string) { return value === "true" ? true : false; } else if (["u8", "u16", "u32"].includes(type)) { return ensureNumber(value); + } else if (type.startsWith("0x1::option::Option")) { + return {vec: [...(value ? [value] : [])]}; } else return value; }