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 8 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,40 @@ 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 input type is an array of uint256 values
if (inputType.endsWith("[]")) {
// parse the string into an array
return JSON.parse(value);
}

if (inputType === "bool") {
return value === "true";
}
if (inputType === "uint256") {
return parseEther(value);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets remove this,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

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 Down Expand Up @@ -139,7 +166,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 +185,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,11 @@ 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);
}

// for outputs like uint256[], int256[], etc
return output.value;
};

Expand Down
Loading