-
Notifications
You must be signed in to change notification settings - Fork 102
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
return type === "vector<u8>" && arg.trim().startsWith("0x") | ||
? Array.from(new HexString(arg).toUint8Array()).map((x) => | ||
x.toString(), | ||
|
@@ -287,6 +288,7 @@ function RunContractForm({ | |
} else return arg; | ||
}), | ||
}; | ||
console.log(payload); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logss |
||
|
||
await submitTransaction(payload); | ||
if (transactionResponse?.transactionSubmitted) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Will get to:
Then
Then do that recursively which will return two items
|
||
} | ||
return result.split(","); | ||
|
||
return inside.split(","); | ||
} | ||
|
||
function isArrayStr(str: string): boolean { | ||
return str.startsWith("[") && str.endsWith("]"); | ||
} | ||
|
||
function encodeVectorForViewRequest(type: string, value: string) { | ||
|
@@ -201,7 +221,7 @@ | |
return ( | ||
HexString.fromUint8Array( | ||
new Uint8Array( | ||
rawVector.map((v) => { | ||
const result = ensureNumber(v.trim()); | ||
if (result < 0 || result > 255) | ||
throw new Error(`Invalid u8 value: ${result}`); | ||
|
@@ -211,13 +231,13 @@ | |
) as any | ||
).hexString; | ||
} else if (["u16", "u32"].includes(match[1])) { | ||
return rawVector.map((v) => ensureNumber(v.trim())); | ||
} else if (["u64", "u128", "u256"].includes(match[1])) { | ||
// For bigint, not need to convert, only validation | ||
rawVector.forEach((v) => ensureBigInt(v.trim())); | ||
return rawVector; | ||
} else if (match[1] === "bool") { | ||
return rawVector.map((v) => ensureBoolean(v.trim())); | ||
} else { | ||
// 1. Address type no need to convert | ||
// 2. Other complex types like Struct is not support yet. We just pass what user input. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove console.log?