From 5bd13bbecea5a7b37d01c7ccd32187b4f7d91554 Mon Sep 17 00:00:00 2001 From: Brian Li Date: Thu, 24 Aug 2023 11:21:14 -0400 Subject: [PATCH 1/2] 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; } From 40ccb3db47c694b4d2f576ad4c43acc495f8a461 Mon Sep 17 00:00:00 2001 From: Brian Li Date: Thu, 24 Aug 2023 11:22:33 -0400 Subject: [PATCH 2/2] Spelling --- src/pages/Account/Tabs/ModulesTab/Contract.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/Account/Tabs/ModulesTab/Contract.tsx b/src/pages/Account/Tabs/ModulesTab/Contract.tsx index 1f61af40..1ab2f532 100644 --- a/src/pages/Account/Tabs/ModulesTab/Contract.tsx +++ b/src/pages/Account/Tabs/ModulesTab/Contract.tsx @@ -688,7 +688,7 @@ function ContractForm({ ))} {fnParams.map((param, i) => { - // TODO: Need a nice way to differenciate between option and empty string + // TODO: Need a nice way to differentiate between option and empty string const isOption = param.startsWith("0x1::option::Option"); return (