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" ;
39import { type ChainClient } from "./anvil.js" ;
410import { addressToBytes32 , type Deposit } from "../../src/index.js" ;
11+ import { spokePoolAbiV3_5 } from "../../src/abis/SpokePool/v3_5.js" ;
512
613export function isAddressDefined ( address ?: Address ) : address is Address {
714 return address && address !== "0x" && address !== zeroAddress ? true : false ;
@@ -44,10 +51,19 @@ export const transferAmount = 1_000_000_000n;
4451
4552export async function getUsdcBalance (
4653 walletAddress : Address ,
47- publicClient : PublicClient ,
54+ publicClient : PublicClient | ChainClient ,
4855) {
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+
4965 const data = await publicClient . readContract ( {
50- address : USDC_MAINNET ,
66+ address : tokenAddress ,
5167 abi : balanceOfAbi ,
5268 functionName : "balanceOf" ,
5369 args : [ walletAddress ] ,
@@ -59,24 +75,49 @@ export async function getUsdcBalance(
5975export async function fundUsdc (
6076 testClient : ChainClient ,
6177 walletAddress : Address ,
78+ chainId : number ,
6279 amount = transferAmount ,
6380) {
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+
6492 await testClient . impersonateAccount ( {
65- address : USDC_WHALE ,
93+ address : sender ,
6694 } ) ;
6795
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+
68106 const { request } = await testClient . simulateContract ( {
69- address : USDC_MAINNET ,
107+ address : tokenAddress ,
70108 abi : transferAbi ,
71109 functionName : "transfer" ,
72- args : [ walletAddress , amount ] ,
73- account : USDC_WHALE ,
110+ args : [ walletAddress , amountWithPadding ] ,
111+ account : sender ,
74112 } ) ;
75113
76114 const hash = await testClient . writeContract ( request ) ;
77115 const receipt = await testClient . waitForTransactionReceipt ( { hash } ) ;
78116
79117 console . log ( "USDC funded!" , receipt ) ;
118+ await testClient . stopImpersonatingAccount ( {
119+ address : sender ,
120+ } ) ;
80121
81122 return receipt ;
82123}
@@ -174,3 +215,21 @@ export function checkFields(
174215
175216 return true ;
176217}
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