292 lines
9.6 KiB
JavaScript
292 lines
9.6 KiB
JavaScript
import { useReadContract, useReadContracts, useToken, useBalance as useInnerBalance } from "wagmi";
|
|
import toast from "react-hot-toast";
|
|
|
|
import {
|
|
getTokenAbi,
|
|
getTokenAddress,
|
|
getTokenDecimals,
|
|
executeOnChainTransaction
|
|
} from "../helpers";
|
|
|
|
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
|
import { shorten } from "../../helpers";
|
|
import { tokenNameConverter } from "../../helpers/tokenConverter";
|
|
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) => {
|
|
const messages = {
|
|
replacedMsg: "Approve transaction was replaced. Wait for inclusion please.",
|
|
successMsg: `${name} tokens successfully approved to ${shorten(spender)}`,
|
|
errorMsg: `${name} tokens approval failed. Check logs for error detalization.`
|
|
};
|
|
await executeOnChainTransaction({
|
|
chainId,
|
|
args: [spender, value],
|
|
abi: getTokenAbi(name),
|
|
address: getTokenAddress(chainId, name),
|
|
functionName: "approve",
|
|
account: owner,
|
|
messages,
|
|
});
|
|
}
|
|
|
|
export const mintDai = async (chainId, account, value) => {
|
|
const messages = {
|
|
replacedMsg: "Mint transaction was replaced. Wait for inclusion please.",
|
|
successMsg: "gDAI successfully minted to your wallet! Funds will be used on Ghost Faucet.",
|
|
errorMsg: "Minting gDAI from the faucet failed. Check logs for error detalization."
|
|
};
|
|
await executeOnChainTransaction({
|
|
chainId,
|
|
args: [account],
|
|
abi: getTokenAbi("GDAI"),
|
|
address: getTokenAddress(chainId, "GDAI"),
|
|
functionName: "mint",
|
|
account,
|
|
messages,
|
|
});
|
|
}
|
|
|
|
export const burnDai = async (chainId, account, value) => {
|
|
const messages = {
|
|
replacedMsg: "Burn transaction was replaced. Wait for inclusion please.",
|
|
successMsg: "gDAI successfully burned for native coins! Check your wallet balance.",
|
|
errorMsg: "Burning gDAI from the faucet failed. Check logs for error detalization."
|
|
};
|
|
await executeOnChainTransaction({
|
|
chainId,
|
|
abi: getTokenAbi("GDAI"),
|
|
address: getTokenAddress(chainId, "GDAI"),
|
|
functionName: 'burn',
|
|
args: [value],
|
|
account,
|
|
messages,
|
|
});
|
|
}
|
|
|
|
export const depositNative = async (chainId, account, value) => {
|
|
const messages = {
|
|
replacedMsg: "WETH9 deposit transaction was replaced. Wait for inclusion please.",
|
|
successMsg: "WETH9 successfully minted to your wallet! Check your wallet balance.",
|
|
errorMsg: "WETH9 wrapping failed. Check logs for error detalization."
|
|
};
|
|
await executeOnChainTransaction({
|
|
chainId,
|
|
abi: getTokenAbi("WETH"),
|
|
address: getTokenAddress(chainId, "WETH"),
|
|
functionName: 'deposit',
|
|
value,
|
|
account,
|
|
messages,
|
|
});
|
|
}
|
|
|
|
export const withdrawWeth = async (chainId, account, value) => {
|
|
const messages = {
|
|
replacedMsg: "WETH9 withdraw transaction was replaced. Wait for inclusion please.",
|
|
successMsg: "WETH9 successfully burned for native coins! Check your wallet balance.",
|
|
errorMsg: "WETH9 unwrapping failed. Check logs for error detalization."
|
|
};
|
|
await executeOnChainTransaction({
|
|
chainId,
|
|
abi: getTokenAbi("WETH"),
|
|
address: getTokenAddress(chainId, "WETH"),
|
|
functionName: 'withdraw',
|
|
args: [value],
|
|
account,
|
|
messages,
|
|
});
|
|
}
|