|
| 1 | +<script setup lang="ts"> |
| 2 | +import { Contract, Interface, ethers, formatEther } from 'ethers' |
| 3 | +import { MulticallProvider } from '@ethers-ext/provider-multicall' |
| 4 | +import ConnectButton from '../button/ConnectButton.vue' |
| 5 | +import type { ConnWallet } from '@vue-dapp/core' |
| 6 | +import type { Header, Item } from 'vue3-easy-data-table' |
| 7 | +
|
| 8 | +const MAINNET_RPC = 'https://ethereum-rpc.publicnode.com' |
| 9 | +const ARBITRUM_RPC = 'https://arbitrum-one-rpc.publicnode.com' |
| 10 | +
|
| 11 | +const MAINNET_DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F' |
| 12 | +const MAINNET_USDC = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' |
| 13 | +const MAINNET_AUSDC = '0xBcca60bB61934080951369a648Fb03DF4F96263C' |
| 14 | +const ARBITRUM_DAI = '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1' |
| 15 | +const ARBITRUM_USDC = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831' |
| 16 | +const ARBITRUM_AUSDC = '0x724dc807b04555b71ed48a6896b6F41593b8C637' |
| 17 | +
|
| 18 | +const mainnetMulticaller = new MulticallProvider(new ethers.JsonRpcProvider(MAINNET_RPC)) |
| 19 | +const arbitrumMulticaller = new MulticallProvider(new ethers.JsonRpcProvider(ARBITRUM_RPC)) |
| 20 | +
|
| 21 | +const iface = new Interface([ |
| 22 | + 'function name() public view returns (string memory)', |
| 23 | + 'function symbol() view returns (string)', |
| 24 | + 'function decimals() view returns (uint8)', |
| 25 | + 'function balanceOf(address owner) view returns (uint256)', |
| 26 | +]) |
| 27 | +
|
| 28 | +const mainnetDai = new Contract(MAINNET_DAI, iface, mainnetMulticaller) |
| 29 | +const mainnetUsdc = new Contract(MAINNET_USDC, iface, mainnetMulticaller) |
| 30 | +const mainnetAusdc = new Contract(MAINNET_AUSDC, iface, mainnetMulticaller) |
| 31 | +const arbitrumDai = new Contract(ARBITRUM_DAI, iface, arbitrumMulticaller) |
| 32 | +const arbitrumUsdc = new Contract(ARBITRUM_USDC, iface, arbitrumMulticaller) |
| 33 | +const arbitrumAusdc = new Contract(ARBITRUM_AUSDC, iface, arbitrumMulticaller) |
| 34 | +
|
| 35 | +async function fetchMainnetData() { |
| 36 | + return await Promise.all([ |
| 37 | + mainnetDai.name(), |
| 38 | + mainnetDai.symbol(), |
| 39 | + mainnetDai.decimals(), |
| 40 | + mainnetUsdc.name(), |
| 41 | + mainnetUsdc.symbol(), |
| 42 | + mainnetUsdc.decimals(), |
| 43 | + mainnetAusdc.name(), |
| 44 | + mainnetAusdc.symbol(), |
| 45 | + mainnetAusdc.decimals(), |
| 46 | + ]) |
| 47 | +} |
| 48 | +
|
| 49 | +async function fetchArbitrumData() { |
| 50 | + return await Promise.all([ |
| 51 | + arbitrumDai.name(), |
| 52 | + arbitrumDai.symbol(), |
| 53 | + arbitrumDai.decimals(), |
| 54 | + arbitrumUsdc.name(), |
| 55 | + arbitrumUsdc.symbol(), |
| 56 | + arbitrumUsdc.decimals(), |
| 57 | + arbitrumAusdc.name(), |
| 58 | + arbitrumAusdc.symbol(), |
| 59 | + arbitrumAusdc.decimals(), |
| 60 | + ]) |
| 61 | +} |
| 62 | +
|
| 63 | +const { data: mainnetData } = useAsyncData('mainnet', () => fetchMainnetData()) |
| 64 | +const { data: arbitrumDaiData } = useAsyncData('arbitrum', () => fetchArbitrumData()) |
| 65 | +
|
| 66 | +const tokenList = computed(() => { |
| 67 | + function sliceData(data: any) { |
| 68 | + return [data.slice(0, 3), data.slice(3, 6), data.slice(6, 9)] |
| 69 | + } |
| 70 | +
|
| 71 | + return { |
| 72 | + mainnet: sliceData(mainnetData.value), |
| 73 | + arbitrum: sliceData(arbitrumDaiData.value), |
| 74 | + } |
| 75 | +}) |
| 76 | +
|
| 77 | +const balances = ref<{ mainnet: number[]; arbitrum: number[] }>({ mainnet: [], arbitrum: [] }) |
| 78 | +
|
| 79 | +const { onWalletUpdated, onDisconnected } = useVueDapp() |
| 80 | +
|
| 81 | +onWalletUpdated(async (wallet: ConnWallet) => { |
| 82 | + const mainnetBalance = await Promise.all([ |
| 83 | + mainnetDai.balanceOf(wallet.address), |
| 84 | + mainnetUsdc.balanceOf(wallet.address), |
| 85 | + mainnetAusdc.balanceOf(wallet.address), |
| 86 | + ]) |
| 87 | + const arbitrumBalance = await Promise.all([ |
| 88 | + arbitrumDai.balanceOf(wallet.address), |
| 89 | + arbitrumUsdc.balanceOf(wallet.address), |
| 90 | + arbitrumAusdc.balanceOf(wallet.address), |
| 91 | + ]) |
| 92 | + balances.value = { mainnet: mainnetBalance, arbitrum: arbitrumBalance } |
| 93 | +}) |
| 94 | +
|
| 95 | +onDisconnected(() => { |
| 96 | + balances.value = { mainnet: [], arbitrum: [] } |
| 97 | +}) |
| 98 | +
|
| 99 | +const headers: Header[] = [ |
| 100 | + { text: 'Network', value: 'network' }, |
| 101 | + { text: 'Name', value: 'name' }, |
| 102 | + { text: 'Symbol', value: 'symbol' }, |
| 103 | + { text: 'Decimals', value: 'decimals' }, |
| 104 | + { text: 'Balance', value: 'balance' }, |
| 105 | + // { text: 'Price', value: 'price' }, |
| 106 | +] |
| 107 | +
|
| 108 | +const items = computed<Item[]>(() => { |
| 109 | + const mainnetItems = tokenList.value.mainnet.map(([name, symbol, decimals], i) => ({ |
| 110 | + network: 'Mainnet', |
| 111 | + name, |
| 112 | + symbol, |
| 113 | + decimals: Number(decimals), |
| 114 | + balance: formatEther(balances.value.mainnet[i] ?? 0), |
| 115 | + })) |
| 116 | + const arbitrumItems = tokenList.value.arbitrum.map(([name, symbol, decimals], i) => ({ |
| 117 | + network: 'Arbitrum', |
| 118 | + name, |
| 119 | + symbol, |
| 120 | + decimals: Number(decimals), |
| 121 | + balance: formatEther(balances.value.arbitrum[i] ?? 0), |
| 122 | + })) |
| 123 | + return [...mainnetItems, ...arbitrumItems] |
| 124 | +}) |
| 125 | +</script> |
| 126 | + |
| 127 | +<template> |
| 128 | + <div class="flex flex-col gap-5"> |
| 129 | + <ConnectButton /> |
| 130 | + |
| 131 | + <ClientOnly> |
| 132 | + <Vue3EasyDataTable hide-rows-per-page hide-footer :headers="headers" :items="items"> </Vue3EasyDataTable> |
| 133 | + </ClientOnly> |
| 134 | + </div> |
| 135 | +</template> |
| 136 | + |
| 137 | +<style lang="scss"></style> |
0 commit comments