/* eslint-disable prefer-const */ import { BigInt, BigDecimal, Address } from "@graphprotocol/graph-ts"; import { BEP20 } from "../../../generated/Factory/BEP20"; import { BEP20SymbolBytes } from "../../../generated/Factory/BEP20SymbolBytes"; import { BEP20NameBytes } from "../../../generated/Factory/BEP20NameBytes"; import { Factory as FactoryContract } from "../../../generated/templates/Pair/Factory"; export const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000"; export const FACTORY_ADDRESS = "0x877fe7f4e22e21be397cd9364fafd4af4e15edb6"; export let ZERO_BI = BigInt.fromI32(0); export let ONE_BI = BigInt.fromI32(1); export let ZERO_BD = BigDecimal.fromString("0"); export let ONE_BD = BigDecimal.fromString("1"); export let BI_18 = BigInt.fromI32(18); export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)); export function exponentToBigDecimal(decimals: BigInt): BigDecimal { let bd = BigDecimal.fromString("1"); for (let i = ZERO_BI; i.lt(decimals as BigInt); i = i.plus(ONE_BI)) { bd = bd.times(BigDecimal.fromString("10")); } return bd; } export function convertTokenToDecimal(tokenAmount: BigInt, exchangeDecimals: BigInt): BigDecimal { if (exchangeDecimals == ZERO_BI) { return tokenAmount.toBigDecimal(); } return tokenAmount.toBigDecimal().div(exponentToBigDecimal(exchangeDecimals)); } export function isNullBnbValue(value: string): boolean { return value == "0x0000000000000000000000000000000000000000000000000000000000000001"; } export function fetchTokenSymbol(tokenAddress: Address): string { let contract = BEP20.bind(tokenAddress); let contractSymbolBytes = BEP20SymbolBytes.bind(tokenAddress); // try types string and bytes32 for symbol let symbolValue = "unknown"; let symbolResult = contract.try_symbol(); if (symbolResult.reverted) { let symbolResultBytes = contractSymbolBytes.try_symbol(); if (!symbolResultBytes.reverted) { // for broken pairs that have no symbol function exposed if (!isNullBnbValue(symbolResultBytes.value.toHexString())) { symbolValue = symbolResultBytes.value.toString(); } } } else { symbolValue = symbolResult.value; } return symbolValue; } export function fetchTokenName(tokenAddress: Address): string { let contract = BEP20.bind(tokenAddress); let contractNameBytes = BEP20NameBytes.bind(tokenAddress); // try types string and bytes32 for name let nameValue = "unknown"; let nameResult = contract.try_name(); if (nameResult.reverted) { let nameResultBytes = contractNameBytes.try_name(); if (!nameResultBytes.reverted) { // for broken exchanges that have no name function exposed if (!isNullBnbValue(nameResultBytes.value.toHexString())) { nameValue = nameResultBytes.value.toString(); } } } else { nameValue = nameResult.value; } return nameValue; } export function fetchTokenTotalSupply(tokenAddress: Address): BigInt { let contract = BEP20.bind(tokenAddress); let totalSupplyValue = null; let totalSupplyResult = contract.try_totalSupply(); if (!totalSupplyResult.reverted) { totalSupplyValue = totalSupplyResult as i32; } return BigInt.fromI32(totalSupplyValue as i32); } export function fetchTokenDecimals(tokenAddress: Address): BigInt { let contract = BEP20.bind(tokenAddress); // try types uint8 for decimals let decimalValue = null; let decimalResult = contract.try_decimals(); if (!decimalResult.reverted) { decimalValue = decimalResult.value; } return BigInt.fromI32(decimalValue as i32); }