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 nested vector in explorer #681

Merged
merged 5 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/pages/Account/Tabs/ModulesTab/Contract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ function RunContractForm({
const type = fnParams[i];
if (type.includes("vector")) {
// when it's a vector<u8>, we support both hex and javascript array format
console.log(type, arg);
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove console.log?

return type === "vector<u8>" && arg.trim().startsWith("0x")
? Array.from(new HexString(arg).toUint8Array()).map((x) =>
x.toString(),
Expand All @@ -287,6 +288,7 @@ function RunContractForm({
} else return arg;
}),
};
console.log(payload);
Copy link
Collaborator

Choose a reason for hiding this comment

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

logss


await submitTransaction(payload);
if (transactionResponse?.transactionSubmitted) {
Expand Down
32 changes: 26 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,36 @@
}

// Deserialize "[1,2,3]" or "1,2,3" to ["1", "2", "3"]
export function deserializeVector(vectorString: string): string[] {
let result = vectorString.trim();
if (result[0] === "[" && result[result.length - 1] === "]") {
result = result.slice(1, -1);
// Also works nested [[1, 2], [3, 4]] to [["1", "2"], ["3", "4"]]
// TODO: Need to type this recursively
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this recursive now?

export function deserializeVector(vectorString: string): any {
const trimmed = vectorString.trim();
let inside = "";
if (isArrayStr(trimmed)) {
inside = trimmed.slice(1, -1);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this doing?

Can we put a comment above it?

// There's a tradeoff here between empty string, and empty array. We're going with empty array.
if (result.length == 0) {
if (inside.length == 0) {
return [];
} else if (isArrayStr(inside)) {
const innerArrayStrs = [];
let build = "";
for (let i = 0; i < inside.length; i++) {
if (inside[i] === "]") {
innerArrayStrs.push(build);
build = "";
} else {
build += inside[i];
}
}
return innerArrayStrs.map(deserializeVector);
Copy link
Contributor

Choose a reason for hiding this comment

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

This code will have the bracket at the beginning?

But, I guess you call it recursively?

I think this misses, can we make a unit test, I suspect that:

[[blah,blah],[blah,blah]]

Will get to:

[blah, blah], [blah, blah]

Then

innerArrayStrs.push('[blah, blah')
innerArrayStrs.push(',[blah, blah')

Then do that recursively which will return two items

'[blah', 'blah'
'','[blah', 'blah'

}
return result.split(",");

return inside.split(",");
}

function isArrayStr(str: string): boolean {
return str.startsWith("[") && str.endsWith("]");
}

function encodeVectorForViewRequest(type: string, value: string) {
Expand All @@ -201,7 +221,7 @@
return (
HexString.fromUint8Array(
new Uint8Array(
rawVector.map((v) => {

Check failure on line 224 in src/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'v' implicitly has an 'any' type.
const result = ensureNumber(v.trim());
if (result < 0 || result > 255)
throw new Error(`Invalid u8 value: ${result}`);
Expand All @@ -211,13 +231,13 @@
) as any
).hexString;
} else if (["u16", "u32"].includes(match[1])) {
return rawVector.map((v) => ensureNumber(v.trim()));

Check failure on line 234 in src/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'v' implicitly has an 'any' type.
} else if (["u64", "u128", "u256"].includes(match[1])) {
// For bigint, not need to convert, only validation
rawVector.forEach((v) => ensureBigInt(v.trim()));

Check failure on line 237 in src/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'v' implicitly has an 'any' type.
return rawVector;
} else if (match[1] === "bool") {
return rawVector.map((v) => ensureBoolean(v.trim()));

Check failure on line 240 in src/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'v' implicitly has an 'any' type.
} else {
// 1. Address type no need to convert
// 2. Other complex types like Struct is not support yet. We just pass what user input.
Expand Down
Loading