1
- import { Hex , zeroAddress , type Address , type PublicClient } from "viem" ;
2
- import { USDC_MAINNET , USDC_WHALE } from "./constants.js" ;
1
+ import {
2
+ Hex ,
3
+ parseEther ,
4
+ zeroAddress ,
5
+ type Address ,
6
+ type PublicClient ,
7
+ } from "viem" ;
8
+ import { USDC_ADDRESS , USDC_WHALES } from "./constants.js" ;
3
9
import { type ChainClient } from "./anvil.js" ;
4
10
import { addressToBytes32 , type Deposit } from "../../src/index.js" ;
11
+ import { spokePoolAbiV3_5 } from "../../src/abis/SpokePool/v3_5.js" ;
5
12
6
13
export function isAddressDefined ( address ?: Address ) : address is Address {
7
14
return address && address !== "0x" && address !== zeroAddress ? true : false ;
@@ -44,10 +51,19 @@ export const transferAmount = 1_000_000_000n;
44
51
45
52
export async function getUsdcBalance (
46
53
walletAddress : Address ,
47
- publicClient : PublicClient ,
54
+ publicClient : PublicClient | ChainClient ,
48
55
) {
56
+ const chainId = publicClient . chain ?. id ;
57
+ const tokenAddress = chainId ? USDC_ADDRESS ?. [ chainId ] : undefined ;
58
+
59
+ if ( ! tokenAddress ) {
60
+ throw new Error (
61
+ `Unable to fetch USDC address on chain: ${ publicClient ?. chain ?. name } ` ,
62
+ ) ;
63
+ }
64
+
49
65
const data = await publicClient . readContract ( {
50
- address : USDC_MAINNET ,
66
+ address : tokenAddress ,
51
67
abi : balanceOfAbi ,
52
68
functionName : "balanceOf" ,
53
69
args : [ walletAddress ] ,
@@ -59,24 +75,49 @@ export async function getUsdcBalance(
59
75
export async function fundUsdc (
60
76
testClient : ChainClient ,
61
77
walletAddress : Address ,
78
+ chainId : number ,
62
79
amount = transferAmount ,
63
80
) {
81
+ const sender = USDC_WHALES ?. [ chainId ] ;
82
+ const tokenAddress = USDC_ADDRESS ?. [ chainId ] ;
83
+
84
+ if ( ! sender || ! tokenAddress ) {
85
+ throw new Error ( `USDC config missing for chainId ${ chainId } ` ) ;
86
+ }
87
+ await testClient . setBalance ( {
88
+ address : sender ,
89
+ value : parseEther ( "1" ) ,
90
+ } ) ;
91
+
64
92
await testClient . impersonateAccount ( {
65
- address : USDC_WHALE ,
93
+ address : sender ,
66
94
} ) ;
67
95
96
+ const amountWithPadding = amount * 2n ;
97
+
98
+ const sponsorBalance = await getUsdcBalance ( sender , testClient ) ;
99
+
100
+ if ( sponsorBalance <= amountWithPadding ) {
101
+ throw new Error (
102
+ `Sponsor with address ${ testClient . account . address } does not have enough balance. Sponsor balance: ${ sponsorBalance } ` ,
103
+ ) ;
104
+ }
105
+
68
106
const { request } = await testClient . simulateContract ( {
69
- address : USDC_MAINNET ,
107
+ address : tokenAddress ,
70
108
abi : transferAbi ,
71
109
functionName : "transfer" ,
72
- args : [ walletAddress , amount ] ,
73
- account : USDC_WHALE ,
110
+ args : [ walletAddress , amountWithPadding ] ,
111
+ account : sender ,
74
112
} ) ;
75
113
76
114
const hash = await testClient . writeContract ( request ) ;
77
115
const receipt = await testClient . waitForTransactionReceipt ( { hash } ) ;
78
116
79
117
console . log ( "USDC funded!" , receipt ) ;
118
+ await testClient . stopImpersonatingAccount ( {
119
+ address : sender ,
120
+ } ) ;
80
121
81
122
return receipt ;
82
123
}
@@ -174,3 +215,21 @@ export function checkFields(
174
215
175
216
return true ;
176
217
}
218
+
219
+ export async function getFillDeadline ( {
220
+ publicClient,
221
+ spokePoolAddress,
222
+ buffer = 3600 , // 1 hour
223
+ } : {
224
+ publicClient : PublicClient ;
225
+ spokePoolAddress : Address ;
226
+ buffer ?: number ;
227
+ } ) : Promise < number > {
228
+ const currentTime = await publicClient . readContract ( {
229
+ address : spokePoolAddress ,
230
+ abi : spokePoolAbiV3_5 ,
231
+ functionName : "getCurrentTime" ,
232
+ } ) ;
233
+
234
+ return Number ( currentTime ) + buffer ;
235
+ }
0 commit comments