Commit cc56187a authored by tom's avatar tom

fix write contract

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