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

add value format before submit #3

Merged
merged 2 commits into from
Jan 27, 2024
Merged
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
38 changes: 30 additions & 8 deletions src/components/AppMethod.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,29 @@ export default function AppMethod({itemData, contract}) {

}, [itemData]);

const formatTypeValue = (type, value) => {
if (type.startsWith("uint")) {
return ethers.BigNumber.from(value)
} else if (type.endsWith("[]")) {
try {
const list = JSON.parse(value);
const itemType = type.replace("[]", "");
return list.map(item=>formatTypeValue(itemType, item))
} catch (error) {
// TODO alert invalid array json
return value
}
} else if (type === "address") {
// TODO check address
return value
} else {
return value;
}
}

const onValueChange = async (e, index) => {
const values = [...methodValues];
let v = e.target.value
const inputDef = methodInputs[index];
if (inputDef.type.startsWith("uint")) {
v = ethers.BigNumber.from(v);
}
values[index] = v;
setMethodValues(values);
}
Expand All @@ -77,9 +93,15 @@ export default function AppMethod({itemData, contract}) {

//Interact with wallet.
const method = JSON.parse(appAbi).find(e => e.name === methodName)
const values = [];
for (let i = 0; i< method.inputs.length; i++) {
const inputDef = method.inputs[i];
values.push(formatTypeValue(inputDef.type, methodValues[i]))
}
console.log("values", values);
if (method?.type === "function") {
if (method.stateMutability === "view") {
let result = await contract.functions[methodName](...methodValues);
let result = await contract.functions[methodName](...values);
// TODO: handle result...
// console.log(ethers.utils.formatEther(result[0]));
console.log(result);
Expand All @@ -88,16 +110,16 @@ export default function AppMethod({itemData, contract}) {

if (method.stateMutability === "nonpayable") {

console.log(methodValues);
let result = await contract.functions[methodName](...methodValues);

let result = await contract.functions[methodName](...values);
console.log(result);
let receipt = await result.wait();
console.log(receipt);
setCallResult("Done")
}

if (method.stateMutability === "payable") {
let result = await contract.functions[methodName](...methodValues);
let result = await contract.functions[methodName](...values);
await result.wait();
setCallResult("Done")
}
Expand Down
Loading