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

fix(ui-ux): contact tab bug fixes #385

Merged
merged 16 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
StateMutability,
} from "@api/types";
import { DFI_TOKEN_SYMBOL } from "shared/constants";
import { useNetwork } from "@contexts/NetworkContext";
import ContractMethodTextInput from "./ContractMethodTextInput";
import ContractMethodResult from "./ContractMethodResult";
import SubmitButton from "./SubmitButton";
Expand All @@ -36,6 +37,7 @@ export default function ContractMethodForm({
const router = useRouter();
const contractId = router.query.aid as string;
const { isConnected } = useAccount();
const { connection } = useNetwork();

// eslint-disable-next-line @typescript-eslint/no-use-before-define
const defaultInputValues = getDefaultValues(method.inputs ?? []);
Expand All @@ -62,15 +64,43 @@ export default function ContractMethodForm({
}
}, [resetForm]);

// To handle user input values based on the type of method input
function convertUserInputs(
input: KeyValue,
inputTypes: SmartContractInputOutput[] | [],
) {
return Object.entries(input).map(([, value], i) => {
const inputType = inputTypes[i].type;

// Check if inputType matches is an array
if (inputType.includes("[]")) {
try {
const parsedValue = JSON.parse(value);
if (Array.isArray(parsedValue)) {
// parse the string into an array
return parsedValue;
}
} catch (e) {
// Intentionally empty - ignore JSON parsing errors
}
}
if (inputType === "bool") {
return value === "true";
}
return value;
});
}

const handleSubmit = async () => {
try {
const convertedValue = convertUserInputs(userInput, method.inputs);
setIsLoading(true);
const config = {
address: contractId as `0x${string}`,
abi: [method],
functionName: method.name,
args: method.inputs?.length > 0 ? [...Object.values(userInput)] : [],
...(dfiValue && { value: parseEther(`${Number(dfiValue)}`) }),
args: method.inputs?.length > 0 ? convertedValue : [],
...(dfiValue && { value: parseEther(`${Number(dfiValue)}`) }), // to specify the amount of Ether to send with the contract function call, if any
};
if (isWriteOrWriteProxy) {
// Write/WriteProxy
Expand All @@ -80,7 +110,10 @@ export default function ContractMethodForm({
// Read/ReadProxy
const data = (await readContract(config)) ?? [];
const results = method.outputs?.map((output, index) => {
const value = typeof data === "object" ? data[index] : data;
const value =
typeof data === "object" && !Array.isArray(data)
? data[index]
: data;
return {
type: output.type,
value,
Expand Down Expand Up @@ -139,7 +172,9 @@ export default function ContractMethodForm({
<SubmitButton
testId={`${method.name}-${type}-result-button`}
label="View your transaction"
onClick={() => window.open(`/tx/${writeResult}`, "_blank")}
onClick={() =>
window.open(`/tx/${writeResult}?network=${connection}`, "_blank")
}
/>
)}
</div>
Expand All @@ -156,7 +191,7 @@ export default function ContractMethodForm({
* Returns object with key-value pair based on the `inputs` length,
* wherein key is the index from array, initialized with empty string
*
* Eg: [{name:"amout", type:"uint256"}, {name:"to", type:"address"}] -> { '0': "", '1': "" }
* Eg: [{name:"amount", type:"uint256"}, {name:"to", type:"address"}] -> { '0': "", '1': "" }
* @param inputs SmartContractInputOutput[]
* @returns
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ const formatOutputValue = (output: SmartContractOutputWithValue): string => {
if (typeof output.value === "bigint") {
return BigInt(output.value).toString();
}

if (typeof output.value === "boolean") {
return JSON.stringify(output.value);
}
if (typeof output.value === "object") {
return JSON.stringify(output.value, (_, value) =>
typeof value === "bigint" ? value.toString() : value,
);
}
// for outputs like uint256[], int256[], etc
return output.value;
};

Expand Down
Loading