Commit cc56187a authored by tom's avatar tom

fix write contract

parent 8d9b3f49
......@@ -39,17 +39,17 @@ const ContractWrite = ({ addressHash, isProxy, isCustomAbi }: Props) => {
},
});
const { contract, proxy, custom } = useContractContext();
const _contract = (() => {
const { contractInfo, customInfo, proxyInfo } = useContractContext();
const abi = (() => {
if (isProxy) {
return proxy;
return proxyInfo?.abi;
}
if (isCustomAbi) {
return custom;
return customInfo?.abi;
}
return contract;
return contractInfo?.abi;
})();
const handleMethodFormSubmit = React.useCallback(async(item: SmartContractWriteMethod, args: Array<string | Array<unknown>>) => {
......@@ -61,34 +61,37 @@ const ContractWrite = ({ addressHash, isProxy, isCustomAbi }: Props) => {
await switchNetworkAsync?.(Number(config.network.id));
}
if (!_contract) {
if (!abi) {
throw new Error('Something went wrong. Try again later.');
}
if (item.type === 'receive') {
const value = args[0] ? getNativeCoinValue(args[0]) : '0';
if (item.type === 'receive' || item.type === 'fallback') {
const value = getNativeCoinValue(args[0]);
const hash = await walletClient?.sendTransaction({
to: addressHash as `0x${ string }` | undefined,
value: BigInt(value), // todo_tom simplify this
value,
});
return { hash };
}
const _args = 'stateMutability' in item && item.stateMutability === 'payable' ? args.slice(0, -1) : args;
const value = 'stateMutability' in item && item.stateMutability === 'payable' ? getNativeCoinValue(args[args.length - 1]) : undefined;
const methodName = item.type === 'fallback' ? 'fallback' : item.name;
const methodName = item.name;
if (!methodName) {
throw new Error('Method name is not defined');
}
const hash = await _contract.write[methodName](..._args, {
gasLimit: 100_000,
value,
const hash = await walletClient?.writeContract({
args: _args,
abi: abi,
functionName: methodName,
address: addressHash as `0x${ string }`,
value: value as undefined,
});
return { hash };
}, [ _contract, addressHash, chain, isConnected, walletClient, switchNetworkAsync ]);
}, [ isConnected, chain, abi, walletClient, addressHash, switchNetworkAsync ]);
const renderContent = React.useCallback((item: SmartContractWriteMethod, index: number, id: number) => {
return (
......
import { useQueryClient } from '@tanstack/react-query';
import type { Abi } from 'abitype';
import React from 'react';
import type { WalletClient } from 'wagmi';
import { useWalletClient } from 'wagmi';
import type { GetContractResult } from 'wagmi/actions';
import { getContract } from 'wagmi/actions';
import type { Address } from 'types/api/address';
import type { SmartContract } from 'types/api/contract';
import useApiQuery, { getResourceKey } from 'lib/api/useApiQuery';
......@@ -16,19 +12,18 @@ type ProviderProps = {
}
type TContractContext = {
contract: GetContractResult<Abi, WalletClient> | undefined;
proxy: GetContractResult<Abi, WalletClient> | undefined;
custom: GetContractResult<Abi, WalletClient> | undefined;
contractInfo: SmartContract | undefined;
proxyInfo: SmartContract | undefined;
customInfo: SmartContract | undefined;
};
const ContractContext = React.createContext<TContractContext>({
contract: undefined,
proxy: undefined,
custom: undefined,
proxyInfo: undefined,
contractInfo: undefined,
customInfo: undefined,
});
export function ContractContextProvider({ addressHash, children }: ProviderProps) {
const { data: walletClient } = useWalletClient();
const queryClient = useQueryClient();
const { data: contractInfo } = useApiQuery('contract', {
......@@ -60,30 +55,11 @@ export function ContractContextProvider({ addressHash, children }: ProviderProps
},
});
const contract = addressHash && contractInfo?.abi ? getContract({
address: addressHash as `0x${ string }`,
abi: contractInfo?.abi ?? undefined,
walletClient: walletClient ?? undefined,
}) : undefined;
const proxy = addressInfo?.implementation_address && proxyInfo?.abi ? getContract({
address: addressInfo?.implementation_address as `0x${ string }`,
abi: proxyInfo?.abi,
walletClient: walletClient ?? undefined,
}) : undefined;
const custom = addressHash && customInfo ? getContract({
address: addressHash as `0x${ string }`,
abi: customInfo,
walletClient: walletClient ?? undefined,
}) : undefined;
const value = React.useMemo(() => ({
contract,
proxy,
custom,
// todo_tom fix this
} as TContractContext), [ contract, proxy, custom ]);
proxyInfo,
contractInfo,
customInfo,
} as TContractContext), [ proxyInfo, contractInfo, customInfo ]);
return (
<ContractContext.Provider value={ value }>
......
import BigNumber from 'bignumber.js';
import config from 'configs/app/config';
export const getNativeCoinValue = (value: string | Array<unknown>) => {
const _value = Array.isArray(value) ? value[0] : value;
if (typeof _value !== 'string') {
return '0';
return BigInt(0);
}
return BigNumber(_value).times(10 ** config.network.currency.decimals).toString();
return BigInt(Number(_value) * 10 ** config.network.currency.decimals);
};
export const addZeroesAllowed = (valueType: string) => {
......
......@@ -56,11 +56,12 @@ const getConfig = () => {
}
};
const { wagmiConfig, ethereumClient } = getConfig();
interface Props {
children: React.ReactNode;
fallback?: JSX.Element | (() => JSX.Element);
}
const { wagmiConfig, ethereumClient } = getConfig();
const Web3ModalProvider = ({ children, fallback }: Props) => {
const modalZIndex = useToken<string>('zIndices', 'modal');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment