Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Option type in Contract Tab #596

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions src/pages/Account/Tabs/ModulesTab/Contract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,27 @@ function Contract({address, isRead}: {address: string; isRead: boolean}) {
return <EmptyTabContent />;
}

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<string, Types.MoveFunction[]>;
}, {} as Record<string, Types.MoveFunction[]>);
const moduleName = module.abi.name;
return {
...acc,
[moduleName]: fns,
} as Record<string, Types.MoveFunction[]>;
},
{} as Record<string, Types.MoveFunction[]>,
);

const module = modules.find((m) => m.abi?.name === selectedModuleName)?.abi;
const fn = selectedModuleName
Expand Down Expand Up @@ -309,6 +312,8 @@ function RunContractForm({
x.toString(),
)
: deserializeVector(arg);
} else if (type.startsWith("0x1::option::Option")) {
arg ? {vec: [arg]} : undefined;
} else return arg;
}),
};
Expand Down Expand Up @@ -683,16 +688,18 @@ function ContractForm({
<TextField label="signer" disabled fullWidth />
))}
{fnParams.map((param, i) => {
// TODO: Need a nice way to differentiate between option and empty string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talking with @BriungRi , we would probably want a dropdown between some and none

const isOption = param.startsWith("0x1::option::Option");
return (
<Controller
key={`args-${i}`}
name={`args.${i}`}
control={control}
rules={{required: true}}
rules={{required: !isOption}}
render={({field: {onChange, value}}) => (
<TextField
onChange={onChange}
value={value ?? ""}
value={isOption ? value : value ?? ""}
label={`arg${i}: ${param}`}
fullWidth
/>
Expand Down
6 changes: 4 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand Down
Loading