336 lines
12 KiB
JavaScript
336 lines
12 KiB
JavaScript
import { useReadContract, useReadContracts, useToken, useBalance as useInnerBalance } from "wagmi";
|
|
import { simulateContract, writeContract, waitForTransactionReceipt } from "@wagmi/core";
|
|
import toast from "react-hot-toast";
|
|
|
|
import { getTokenAbi, getTokenAddress, getTokenDecimals } from "../helpers";
|
|
|
|
import { isNetworkLegacyType } from "../../constants";
|
|
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
|
import { shorten } from "../../helpers";
|
|
import { tokenNameConverter } from "../../helpers/tokenConverter";
|
|
import { config } from "../../config";
|
|
import { WETH_ADDRESSES } from "../../constants/addresses";
|
|
|
|
export const usePastVotes = (chainId, name, timepoint, address) => {
|
|
const decimals = getTokenDecimals(name);
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
|
|
const { data, refetch, error } = useReadContract({
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "getPastVotes",
|
|
args: [address, timepoint],
|
|
scopeKey: `getPastVotes-${timepoint}-${chainId}`,
|
|
chainId: chainId,
|
|
});
|
|
|
|
const pastVotes = new DecimalBigNumber(data ? data : 0n, decimals);
|
|
return { pastVotes }
|
|
}
|
|
|
|
export const usePastTotalSupply = (chainId, name, timepoint) => {
|
|
const decimals = getTokenDecimals(name);
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
|
|
const { data, refetch, error } = useReadContract({
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "getPastTotalSupply",
|
|
args: [timepoint],
|
|
scopeKey: `getPastTotalSupply-${timepoint}-${chainId}`,
|
|
chainId: chainId,
|
|
});
|
|
|
|
const pastTotalSupply = new DecimalBigNumber(data ? data : 0n, decimals);
|
|
return { pastTotalSupply, refetch };
|
|
}
|
|
|
|
export const useTotalSupply = (chainId, name) => {
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
const { data, refetch } = useToken({
|
|
address: contractAddress,
|
|
scopeKey: `token-${contractAddress}-${chainId}`,
|
|
chainId
|
|
});
|
|
|
|
const totalSupplyPrepared = data ? data.totalSupply.value : 0n;
|
|
const decimals = data ? data.decimals : 0;
|
|
const totalSupply = new DecimalBigNumber(totalSupplyPrepared, decimals);
|
|
|
|
return { totalSupply, refetch };
|
|
};
|
|
|
|
export const useBalance = (chainId, name, address) => {
|
|
let contractAddress = getTokenAddress(chainId, name);
|
|
let isNative = false;
|
|
|
|
let requestObj = {
|
|
address,
|
|
chainId,
|
|
scopeKey: `balance-${contractAddress}-${address}-${chainId}`,
|
|
};
|
|
|
|
if (contractAddress !== undefined) {
|
|
requestObj.token = contractAddress;
|
|
} else {
|
|
contractAddress = WETH_ADDRESSES[chainId];
|
|
isNative = true;
|
|
}
|
|
|
|
const { data, refetch, error } = useInnerBalance(requestObj);
|
|
const balancePrepared = data ? data.value : 0n;
|
|
const decimals = data ? data.decimals : getTokenDecimals(name);
|
|
|
|
const balance = new DecimalBigNumber(balancePrepared, decimals);
|
|
|
|
return { balance, refetch, contractAddress, isNative };
|
|
}
|
|
|
|
export const useAllowance = (chainId, name, owner, spender, decimals) => {
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
const { data, refetch } = useReadContract({
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "allowance",
|
|
args: [owner, spender],
|
|
scopeKey: `allowance-${contractAddress}-${owner}-${spender}-${chainId}`,
|
|
chainId: chainId,
|
|
});
|
|
|
|
const allowancePrepared = data ? data : 0n;
|
|
decimals = decimals ? decimals : getTokenDecimals(name);
|
|
const allowance = new DecimalBigNumber(allowancePrepared, decimals);
|
|
|
|
return { allowance, refetch };
|
|
}
|
|
|
|
export const useTokenSymbol = (chainId, name) => {
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
|
|
const { data, refetch } = useReadContract({
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "symbol",
|
|
scopeKey: `symbol-${contractAddress}-${chainId}`,
|
|
chainId: chainId,
|
|
});
|
|
|
|
let symbol = data ? data : "";
|
|
symbol = tokenNameConverter(chainId, symbol);
|
|
|
|
return { symbol, refetch };
|
|
}
|
|
|
|
export const useTokenSymbols = (chainId, names) => {
|
|
const { data: symbols } = useReadContracts({
|
|
contracts: names?.map((name) => {
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
return {
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "symbol",
|
|
scopeKey: `symbol-${contractAddress}-${chainId}`,
|
|
chainId: chainId,
|
|
}
|
|
})
|
|
});
|
|
|
|
return { symbols };
|
|
}
|
|
|
|
export const useConversionRate = (chainId, name) => {
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
const { data: rateRaw } = useReadContract({
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "conversionRate",
|
|
scopeKey: `conversionRate-${contractAddress}-${chainId}`,
|
|
chainId: chainId,
|
|
});
|
|
|
|
const ratePrepared = rateRaw ? rateRaw : 0n;
|
|
const rate = new DecimalBigNumber(ratePrepared, 0);
|
|
|
|
return rate;
|
|
}
|
|
|
|
export const useAccumulatedDonation = (chainId, name) => {
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
const { data: donationRaw } = useReadContract({
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "accumulatedDonation",
|
|
scopeKey: `accumulatedDonation-${contractAddress}-${chainId}`,
|
|
chainId: chainId,
|
|
});
|
|
|
|
const preparedDonationRaw = donationRaw ? donationRaw : 0n;
|
|
const accumulatedDonation = new DecimalBigNumber(preparedDonationRaw, 0);
|
|
|
|
return accumulatedDonation;
|
|
}
|
|
|
|
export const useCirculatingSupply = (chainId, name) => {
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
const { data: circulatingSupplyRaw } = useReadContract({
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "circulatingSupply",
|
|
scopeKey: `circulatingSupply-${contractAddress}-${chainId}`,
|
|
chainId: chainId,
|
|
});
|
|
|
|
const circulatingSupplyPrepared = circulatingSupplyRaw ? circulatingSupplyRaw : 0n;
|
|
const circulatingSupply = new DecimalBigNumber(circulatingSupplyPrepared, 9);
|
|
|
|
return circulatingSupply;
|
|
};
|
|
|
|
export const useBalanceForShares = (chainId, name, amount) => {
|
|
const contractAddress = getTokenAddress(chainId, name);
|
|
const { data: balanceForSharesRaw } = useReadContract({
|
|
abi: getTokenAbi(name),
|
|
address: contractAddress,
|
|
functionName: "balanceForShares",
|
|
args: [amount],
|
|
scopeKey: `balanceForShares-${chainId}`,
|
|
chainId: chainId,
|
|
});
|
|
|
|
const balanceForSharesPrepared = balanceForSharesRaw ? balanceForSharesRaw : 0n;
|
|
const balanceForShares = new DecimalBigNumber(balanceForSharesPrepared, 9);
|
|
|
|
return { balanceForShares };
|
|
};
|
|
|
|
export const approveTokens = async (chainId, name, owner, spender, value) => {
|
|
try {
|
|
const { request } = await simulateContract(config, {
|
|
abi: getTokenAbi(name),
|
|
address: getTokenAddress(chainId, name),
|
|
functionName: 'approve',
|
|
args: [spender, value],
|
|
account: owner,
|
|
chainId: chainId,
|
|
type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559',
|
|
});
|
|
|
|
const txHash = await writeContract(config, request);
|
|
await waitForTransactionReceipt(config, {
|
|
hash: txHash,
|
|
onReplaced: () => toast("Approve transaction was replaced. Wait for inclusion please."),
|
|
chainId
|
|
});
|
|
|
|
toast.success(name + " tokens successfully approved to " + shorten(spender));
|
|
} catch (err) {
|
|
console.error(err);
|
|
toast.error(name + " tokens approval failed. Check logs for error detalization.")
|
|
}
|
|
}
|
|
|
|
export const mintDai = async (chainId, account, value) => {
|
|
try {
|
|
const { request } = await simulateContract(config, {
|
|
abi: getTokenAbi("GDAI"),
|
|
address: getTokenAddress(chainId, "GDAI"),
|
|
functionName: 'mint',
|
|
args: [account],
|
|
account: account,
|
|
value: value,
|
|
chainId: chainId,
|
|
type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559',
|
|
});
|
|
|
|
const txHash = await writeContract(config, request);
|
|
await waitForTransactionReceipt(config, {
|
|
hash: txHash,
|
|
onReplaced: () => toast("Mint transaction was replaced. Wait for inclusion please."),
|
|
chainId
|
|
});
|
|
|
|
toast.success("gDAI successfully minted to your wallet! Funds will be used on Ghost Faucet.");
|
|
} catch (err) {
|
|
console.error(err);
|
|
toast.error("Minting gDAI from the faucet failed. Check logs for error detalization.")
|
|
}
|
|
}
|
|
|
|
export const burnDai = async (chainId, account, value) => {
|
|
try {
|
|
const { request } = await simulateContract(config, {
|
|
abi: getTokenAbi("GDAI"),
|
|
address: getTokenAddress(chainId, "GDAI"),
|
|
functionName: 'burn',
|
|
args: [value],
|
|
account: account,
|
|
chainId: chainId,
|
|
type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559',
|
|
});
|
|
|
|
const txHash = await writeContract(config, request);
|
|
await waitForTransactionReceipt(config, {
|
|
hash: txHash,
|
|
onReplaced: () => toast("Burn transaction was replaced. Wait for inclusion please."),
|
|
chainId
|
|
});
|
|
|
|
toast.success("gDAI successfully burned for native coins! Check your wallet balance.");
|
|
} catch (err) {
|
|
console.error(err);
|
|
toast.error("Burning gDAI from the faucet failed. Check logs for error detalization.")
|
|
}
|
|
}
|
|
|
|
export const depositNative = async (chainId, account, value) => {
|
|
try {
|
|
const { request } = await simulateContract(config, {
|
|
abi: getTokenAbi("WETH"),
|
|
address: getTokenAddress(chainId, "WETH"),
|
|
functionName: 'deposit',
|
|
account: account,
|
|
chainId: chainId,
|
|
value: value,
|
|
type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559',
|
|
});
|
|
const txHash = await writeContract(config, request);
|
|
|
|
await waitForTransactionReceipt(config, {
|
|
hash: txHash,
|
|
onReplaced: () => toast("WETH9 deposit transaction was replaced. Wait for inclusion please."),
|
|
chainId
|
|
});
|
|
|
|
toast.success("WETH9 successfully minted to your wallet! Check your wallet balance.");
|
|
} catch (err) {
|
|
console.error(err);
|
|
toast.error("WETH9 wrapping failed. Check logs for error detalization.")
|
|
}
|
|
}
|
|
|
|
export const withdrawWeth = async (chainId, account, value) => {
|
|
try {
|
|
const { request } = await simulateContract(config, {
|
|
abi: getTokenAbi("WETH"),
|
|
address: getTokenAddress(chainId, "WETH"),
|
|
functionName: 'withdraw',
|
|
args: [value],
|
|
account: account,
|
|
chainId: chainId,
|
|
type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559',
|
|
});
|
|
|
|
const txHash = await writeContract(config, request);
|
|
await waitForTransactionReceipt(config, {
|
|
hash: txHash,
|
|
onReplaced: () => toast("WETH9 withdraw transaction was replaced. Wait for inclusion please."),
|
|
chainId
|
|
});
|
|
|
|
toast.success("WETH9 successfully burned for native coins! Check your wallet balance.");
|
|
} catch (err) {
|
|
console.error(err);
|
|
toast.error("WETH9 unwrapping failed. Check logs for error detalization.")
|
|
}
|
|
}
|