-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathConnext.ts
115 lines (102 loc) · 2.86 KB
/
Connext.ts
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
import { getDeployedMultisendContract } from '@connext/nxtp-txservice'
import {
type ChainData,
MultisendAbi,
chainIdToDomain,
domainToChainId,
getChainData,
} from '@connext/nxtp-utils'
import {
type BridgeActionParams,
type TransactionFilter,
compressJson,
} from '@rabbitholegg/questdk'
import { type Address, zeroAddress } from 'viem'
import { XCALL_ABI_FRAGMENTS } from './abi'
import { ConnextContract } from './contract-addresses'
let _chainDataCache: Map<string, ChainData> | null = null
const ETH_TOKEN_ADDRESS = zeroAddress
const _getChainData = async () => {
if (!_chainDataCache) {
const chainData = await getChainData()
_chainDataCache = chainData
}
return _chainDataCache
}
export const bridge = async (
bridge: BridgeActionParams,
): Promise<TransactionFilter> => {
const { sourceChainId, destinationChainId, tokenAddress, amount, recipient } =
bridge
const xcallContractAddress = ConnextContract[sourceChainId]
const destinationDomain = destinationChainId
? chainIdToDomain(destinationChainId)
: undefined
const multiSendContractAddress =
getDeployedMultisendContract(sourceChainId)?.address
const ethUsedIn = tokenAddress === ETH_TOKEN_ADDRESS
if (!xcallContractAddress) {
throw new Error(`No xcall contract deployed on chain ${sourceChainId}`)
}
if (!multiSendContractAddress) {
throw new Error(`No multisend contract deployed on chain ${sourceChainId}`)
}
return compressJson({
chainId: sourceChainId,
to: {
$or: [
xcallContractAddress.toLowerCase(),
multiSendContractAddress.toLowerCase(),
],
},
from: recipient,
value: ethUsedIn ? amount : undefined,
input: {
$or: [
{
$abi: MultisendAbi,
transactions: {
$regex: recipient?.toLowerCase().slice(2),
},
},
{
$abi: XCALL_ABI_FRAGMENTS,
_destination: destinationDomain
? Number(destinationDomain)
: undefined,
_asset: tokenAddress,
_amount: amount,
_delegate: recipient,
},
],
},
})
}
export const getSupportedTokenAddresses = async (_chainId: number) => {
const chains = await _getChainData()
try {
const domainId = chainIdToDomain(_chainId)
const chainData = chains?.get(String(domainId))
if (!chainData) {
return []
}
return Object.keys(chainData.assetId).filter((addr) => !!addr) as Address[]
} catch (_e) {
return []
}
}
export const getSupportedChainIds = async () => {
const chains = await _getChainData()
if (!chains || !(chains instanceof Map)) {
return []
}
return Array.from(chains.keys())
.map((domainId) => {
try {
return domainToChainId(Number(domainId))
} catch (_e) {
return undefined
}
})
.filter((chain) => chain !== undefined) as number[]
}