Skip to content

Commit

Permalink
Merge pull request #3 from Web3Camp-Labs/dev
Browse files Browse the repository at this point in the history
add value format before submit
  • Loading branch information
xrdavies authored Jan 27, 2024
2 parents 9849d2f + f74814b commit ae8b925
Showing 1 changed file with 30 additions and 8 deletions.
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

0 comments on commit ae8b925

Please sign in to comment.