Skip to content

Commit

Permalink
feat 新增配置
Browse files Browse the repository at this point in the history
  • Loading branch information
webkubor authored and webkubor committed Jan 8, 2025
1 parent 71c5fa0 commit 17bf916
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@arco-design/web-vue": "^2.56.1",
"@web3modal/ethers": "^5.1.7",
"ethers": "^6.13.2",
"tronweb": "^6.0.0",
"vue": "^3.5.4"
},
"devDependencies": {
Expand Down
147 changes: 145 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 57 additions & 21 deletions src/services/monitorService.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ref } from 'vue'
import { USDT_CONTRACTS } from '@/utils/networks'
import { USDT_CONTRACTS } from '../utils/networks'
import {
JsonRpcProvider,
Contract,
formatUnits
} from 'ethers'
import ERC20_ABI from '@/abis/ERC20.json'
import TronWeb from 'tronweb'
import ERC20_ABI from '../abis/ERC20.json'

interface Transaction {
hash: string
Expand All @@ -27,16 +28,17 @@ interface TransferEvent {

const RPC_URLS: Record<string, string> = {
eth: 'https://rpc.ankr.com/eth',
bsc: 'https://bsc-dataseed1.defibit.io/'
bsc: 'https://bsc-dataseed1.defibit.io/',
tron: 'https://api.trongrid.io'
}

export const useMonitorService = () => {
const loading = ref(false)
const error = ref<string | null>(null)

const watchTransferEvents = (
const watchTransferEvents = async (
address: string,
chain: keyof typeof USDT_CONTRACTS,
chain: 'ETH' | 'BSC' | 'TRON',
callback: (event: {
hash: string
from: string
Expand All @@ -49,27 +51,61 @@ export const useMonitorService = () => {
throw new Error('不支持的链类型')
}

const provider = new JsonRpcProvider(RPC_URLS[chain])
const contract = new Contract(contractAddress, ERC20_ABI, provider)

const filter = contract.filters.Transfer(null, address)
const handler = (from: string, to: string, value: bigint, event: { transactionHash: string }) => {
callback({
hash: event.transactionHash,
from,
to,
amount: formatUnits(value, 6).toString()
if (chain === 'TRON') {
// TRON-specific event monitoring
const tronWeb = new TronWeb({
fullHost: RPC_URLS.tron
})
}

contract.on(filter, handler)

const contract = await tronWeb.contract().at(contractAddress)
const filter = {
eventName: 'Transfer',
filters: {
to: address
}
}

const watch = contract[filter.eventName](filter.filters)
.watch((err: any, event: any) => {
if (err) {
console.error('TRON event error:', err)
return
}
callback({
hash: event.transaction,
from: event.result.from,
to: event.result.to,
amount: (event.result.value / 1e6).toString()
})
})

return () => {
watch.stop()
}
} else {
// EVM chains (ETH, BSC)
const provider = new JsonRpcProvider(RPC_URLS[chain])
const contract = new Contract(contractAddress, ERC20_ABI, provider)

const filter = contract.filters.Transfer(null, address)
const handler = (from: string, to: string, value: bigint, event: { transactionHash: string }) => {
callback({
hash: event.transactionHash,
from,
to,
amount: formatUnits(value, 6).toString()
})
}

contract.on(filter, handler)

return () => {
contract.off(filter, handler)
return () => {
contract.off(filter, handler)
}
}
}

const getTransactions = async (address: string, chain: keyof typeof USDT_CONTRACTS): Promise<Transaction[]> => {
const getTransactions = async (address: string, chain: 'ETH' | 'BSC' | 'TRON'): Promise<Transaction[]> => {
try {
loading.value = true
error.value = null
Expand Down
Loading

0 comments on commit 17bf916

Please sign in to comment.