From 77f4d33edb6ad9ecdd021388ef7b51bde2088b36 Mon Sep 17 00:00:00 2001 From: CatFlow <22841916+Meow711@users.noreply.github.com> Date: Sat, 27 Jan 2024 15:16:46 +0800 Subject: [PATCH 1/2] add value format before submit --- src/components/AppMethod.jsx | 39 ++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/components/AppMethod.jsx b/src/components/AppMethod.jsx index ab0f597..22c943a 100644 --- a/src/components/AppMethod.jsx +++ b/src/components/AppMethod.jsx @@ -59,13 +59,30 @@ 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); + console.log("a", list); + 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); } @@ -77,9 +94,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); @@ -88,8 +111,8 @@ 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); @@ -97,7 +120,7 @@ export default function AppMethod({itemData, contract}) { } if (method.stateMutability === "payable") { - let result = await contract.functions[methodName](...methodValues); + let result = await contract.functions[methodName](...values); await result.wait(); setCallResult("Done") } From f74814bc2fb639dc7954cffb2a80e8d8a8a30ed8 Mon Sep 17 00:00:00 2001 From: CatFlow <22841916+Meow711@users.noreply.github.com> Date: Sat, 27 Jan 2024 15:20:34 +0800 Subject: [PATCH 2/2] remove test log --- src/components/AppMethod.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/AppMethod.jsx b/src/components/AppMethod.jsx index 22c943a..a441c44 100644 --- a/src/components/AppMethod.jsx +++ b/src/components/AppMethod.jsx @@ -65,7 +65,6 @@ export default function AppMethod({itemData, contract}) { } else if (type.endsWith("[]")) { try { const list = JSON.parse(value); - console.log("a", list); const itemType = type.replace("[]", ""); return list.map(item=>formatTypeValue(itemType, item)) } catch (error) {