|
| 1 | +import { Knex } from 'knex'; |
| 2 | +import { ethers, providers } from 'ethers'; |
| 3 | +import { ChainId, chainIdToChain } from '@wormhole-foundation/sdk-base'; |
| 4 | +import { Connection, PublicKey } from '@solana/web3.js'; |
| 5 | +import { TokenInfo } from 'src/fastTransfer/types'; |
| 6 | + |
| 7 | +const minABI = [ |
| 8 | + { |
| 9 | + inputs: [], |
| 10 | + name: 'name', |
| 11 | + outputs: [{ internalType: 'string', name: '', type: 'string' }], |
| 12 | + stateMutability: 'view', |
| 13 | + type: 'function', |
| 14 | + }, |
| 15 | + { |
| 16 | + inputs: [], |
| 17 | + name: 'symbol', |
| 18 | + outputs: [{ internalType: 'string', name: '', type: 'string' }], |
| 19 | + stateMutability: 'view', |
| 20 | + type: 'function', |
| 21 | + }, |
| 22 | + { |
| 23 | + inputs: [], |
| 24 | + name: 'decimals', |
| 25 | + outputs: [{ internalType: 'uint8', name: '', type: 'uint8' }], |
| 26 | + stateMutability: 'view', |
| 27 | + type: 'function', |
| 28 | + }, |
| 29 | +]; |
| 30 | +// TokenInfoManager class for managing token information across different chains |
| 31 | +// This class is to ensure that token info (e.g. decimal, name) for tokens that we see on Swap Layer is persisted for analytics purposes |
| 32 | +export class TokenInfoManager { |
| 33 | + private tokenInfoMap: Map<string, TokenInfo>; |
| 34 | + private db: Knex; |
| 35 | + private chainId: ChainId; |
| 36 | + private provider: providers.JsonRpcProvider | Connection; |
| 37 | + |
| 38 | + constructor(db: Knex, chainId: ChainId, provider: providers.JsonRpcProvider | Connection) { |
| 39 | + this.tokenInfoMap = new Map(); |
| 40 | + this.db = db; |
| 41 | + this.chainId = chainId; |
| 42 | + this.provider = provider; |
| 43 | + } |
| 44 | + |
| 45 | + // Retrieve token information from the database |
| 46 | + private async getTokenInfoFromDB(tokenAddress: string): Promise<TokenInfo | null> { |
| 47 | + return await this.db('token_infos') |
| 48 | + .select('token_address', 'name', 'symbol', 'decimals') |
| 49 | + .where('token_address', tokenAddress) |
| 50 | + .andWhere('chain_id', this.chainId) |
| 51 | + .first(); |
| 52 | + } |
| 53 | + |
| 54 | + private async saveTokenInfo(tokenAddress: string, tokenInfo: TokenInfo): Promise<void> { |
| 55 | + await this.db('token_infos') |
| 56 | + .insert({ |
| 57 | + token_address: tokenAddress, |
| 58 | + name: tokenInfo.name, |
| 59 | + symbol: tokenInfo.symbol, |
| 60 | + decimals: tokenInfo.decimals, |
| 61 | + chain_id: this.chainId, |
| 62 | + }) |
| 63 | + .onConflict(['token_address', 'chain_id']) |
| 64 | + .merge(); |
| 65 | + } |
| 66 | + |
| 67 | + // Save token information if it doesn't exist in the cache or database |
| 68 | + public async saveTokenInfoIfNotExist(tokenAddress: string): Promise<TokenInfo | null> { |
| 69 | + if (this.tokenInfoMap.has(tokenAddress)) { |
| 70 | + return this.tokenInfoMap.get(tokenAddress) || null; |
| 71 | + } |
| 72 | + // Check if token info is in the database |
| 73 | + const tokenInfo = await this.getTokenInfoFromDB(tokenAddress); |
| 74 | + if (tokenInfo) { |
| 75 | + this.tokenInfoMap.set(tokenAddress, tokenInfo); |
| 76 | + return tokenInfo; |
| 77 | + } |
| 78 | + // If not in database, fetch from RPC |
| 79 | + const fetchedTokenInfo = await this.fetchTokenInfoFromRPC(tokenAddress); |
| 80 | + if (fetchedTokenInfo) { |
| 81 | + await this.saveTokenInfo(tokenAddress, fetchedTokenInfo); |
| 82 | + this.tokenInfoMap.set(tokenAddress, fetchedTokenInfo); |
| 83 | + return fetchedTokenInfo; |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + // Fetch token information from RPC based on the chain ID |
| 89 | + private async fetchTokenInfoFromRPC(tokenAddress: string): Promise<TokenInfo | null> { |
| 90 | + if (chainIdToChain(this.chainId) === 'Solana') { |
| 91 | + return this.fetchSolanaTokenInfo(tokenAddress); |
| 92 | + } |
| 93 | + return this.fetchEVMTokenInfo(tokenAddress); |
| 94 | + } |
| 95 | + |
| 96 | + // Fetch Solana token information |
| 97 | + private async fetchSolanaTokenInfo(tokenAddress: string): Promise<TokenInfo | null> { |
| 98 | + try { |
| 99 | + const connection = this.provider as Connection; |
| 100 | + const tokenPublicKey = new PublicKey(tokenAddress); |
| 101 | + const accountInfo = await connection.getParsedAccountInfo(tokenPublicKey); |
| 102 | + |
| 103 | + if (accountInfo.value && accountInfo.value.data && 'parsed' in accountInfo.value.data) { |
| 104 | + const parsedData = accountInfo.value.data.parsed; |
| 105 | + if (parsedData.type === 'mint' && 'info' in parsedData) { |
| 106 | + const { name, symbol, decimals } = parsedData.info; |
| 107 | + if ( |
| 108 | + typeof name === 'string' && |
| 109 | + typeof symbol === 'string' && |
| 110 | + typeof decimals === 'number' |
| 111 | + ) { |
| 112 | + return { name, symbol, decimals, chain_id: this.chainId, token_address: tokenAddress }; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + throw new Error('Invalid token account'); |
| 117 | + } catch (error) { |
| 118 | + console.error('Error fetching Solana token info:', error); |
| 119 | + return null; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Fetch EVM token information |
| 124 | + private async fetchEVMTokenInfo(tokenAddress: string): Promise<TokenInfo | null> { |
| 125 | + // If it's null address, it's Ether or Wrapped Ether |
| 126 | + if (tokenAddress.toLowerCase() === '0x0000000000000000000000000000000000000000') { |
| 127 | + const { name, symbol } = this.getEtherInfo(); |
| 128 | + return { |
| 129 | + name, |
| 130 | + symbol, |
| 131 | + decimals: 18, |
| 132 | + chain_id: this.chainId, |
| 133 | + token_address: tokenAddress, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + const provider = this.provider as providers.JsonRpcProvider; |
| 138 | + const tokenContract = new ethers.Contract(tokenAddress, minABI, provider); |
| 139 | + try { |
| 140 | + const name = await tokenContract.name(); |
| 141 | + const symbol = await tokenContract.symbol(); |
| 142 | + const decimals = await tokenContract.decimals(); |
| 143 | + return { name, symbol, decimals, chain_id: this.chainId, token_address: tokenAddress }; |
| 144 | + } catch (error) { |
| 145 | + console.error('Error fetching EVM token info:', error, tokenAddress); |
| 146 | + return null; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // Helper function to get Ether or Wrapped Ether info based on chain ID |
| 151 | + private getEtherInfo(): { name: string; symbol: string } { |
| 152 | + switch (this.chainId) { |
| 153 | + case 2: |
| 154 | + case 5: |
| 155 | + return { name: 'Ether', symbol: 'ETH' }; |
| 156 | + default: |
| 157 | + return { name: 'Wrapped Ether', symbol: 'WETH' }; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments