Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 1x 2x 2x 2x 2x 1x 1x 1x 3x 3x 3x 1x 2x 1x 4x 4x 4x 2x 2x 1x 4x 1x 5x 5x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x 2x 2x | import { createPublicClient, formatUnits, http } from "viem";
import { CONSTANTS, EnvMode, getEnvConstant } from "./serverConstants";
import { ABIs } from "../abis";
import { handleError } from "./errorHandlerUtils";
import { configStore } from "./configStore";
import { USDT_DECIMAL } from "./constantVariables";
export function getPublicClient() {
const rpcUrl = configStore.getRpcUrl();
const envMode = configStore.getEnvMode() as EnvMode;
const POLYGON_CHAIN = CONSTANTS[envMode].POLYGON_CHAIN;
return createPublicClient({
chain: POLYGON_CHAIN,
transport: rpcUrl?.length > 0 ? http(rpcUrl) : http(),
});
}
interface PublicClientReadParams {
args: unknown[];
functionName: string;
contractType: ContractType;
}
export async function publicClientRead({
args,
contractType,
functionName,
}: PublicClientReadParams): Promise<any> {
try {
const { abi, address } = getContractDetails(contractType);
const publicClient = getPublicClient();
const response = await publicClient.readContract({
address: address as `0x${string}`,
abi: abi as any[],
functionName: functionName,
args: args,
});
return response;
} catch (error) {
throw {
success: false,
error: error,
};
}
}
interface PublicClientReadInsParams {
functionName: string;
args?: unknown[];
}
export async function publicClientReadIns({
args,
functionName,
}: PublicClientReadInsParams): Promise<any> {
try {
const publicClient = getPublicClient();
const response = await publicClient.readContract({
address: getEnvConstant("INSURANCE_POOL_ADDRESS") as `0x${string}`,
abi: ABIs.InsurancePoolAbi,
functionName: functionName,
args: args || [],
});
return response;
} catch (error) {
throw {
success: false,
error: error,
};
}
}
interface publicEstimateGasInsParams {
args?: unknown[];
functionName: string;
walletClient: any;
}
export async function publicEstimateGasIns({
args,
functionName,
walletClient,
}: publicEstimateGasInsParams): Promise<any> {
try {
const publicClient = getPublicClient();
const gasEstimate = await publicClient.estimateContractGas({
address: getEnvConstant("INSURANCE_POOL_ADDRESS") as `0x${string}`,
abi: ABIs.InsurancePoolAbi,
functionName: functionName,
args: args || [],
account: walletClient.account,
});
return {
success: true,
gas: BigInt(gasEstimate.toString()),
};
} catch (gasError) {
return handleError(gasError, functionName);
}
}
export const formatNumber = (response: BigInt, decimals: number) => {
return Number(formatUnits(response as bigint, decimals));
};
export const parseString = (val: string): string => {
const result = val?.replace(/"/g, "");
return result;
};
export const convertToBigInt = (amount: number) => {
const MULTIPLIER = 10 ** USDT_DECIMAL;
return BigInt(amount * MULTIPLIER);
};
interface ResolvedContract {
abi: unknown[];
address: string;
}
export enum ContractType {
Policy = "Policy",
Single = "Single",
Combo = "Combo",
Token = "Token",
InsurancePool = "InsurancePool",
}
export function getContractDetails(
contractType: ContractType
): ResolvedContract {
const envAddresses = {
[ContractType.Policy]: getEnvConstant("POLICY_MANAGER_ADDRESS"),
[ContractType.Combo]: getEnvConstant("COMBO_PREMIUM_CALCULATOR_ADDRESS"),
[ContractType.Single]: getEnvConstant("SINGLE_PREMIUM_CALCULATOR_ADDRESS"),
[ContractType.Token]: getEnvConstant("TOKEN_ADDRESS"),
[ContractType.InsurancePool]: getEnvConstant("INSURANCE_POOL_ADDRESS"),
};
const address = envAddresses[contractType];
if (typeof address !== "string") {
throw new Error(`${contractType} address must be a string`);
}
const abis = {
[ContractType.Policy]: ABIs.PolicyManagerAbi,
[ContractType.Combo]: ABIs.ComboPremiumCalculatorAbi,
[ContractType.Single]: ABIs.SinglePremiumCalculatorAbi,
[ContractType.Token]: ABIs.TokenAbi,
[ContractType.InsurancePool]: ABIs.InsurancePoolAbi,
};
return { abi: abis[contractType], address };
}
|