1+ import type {
2+ AbiParametersToPrimitiveTypes ,
3+ ExtractAbiFunction ,
4+ } from "abitype" ;
15import {
26 type Account ,
37 type Address ,
@@ -6,15 +10,28 @@ import {
610 type WalletClient ,
711} from "viem" ;
812import { readContract } from "viem/actions" ;
9- import { formatBN , type ILogger } from "../sdk/index.js" ;
13+ import { formatBN , type GearboxSDK , type ILogger } from "../sdk/index.js" ;
14+
15+ interface TokenClaim {
16+ token : Address ;
17+ amount : bigint ;
18+ }
1019
1120interface ClaimFromFaucetOptions {
21+ sdk : GearboxSDK ;
1222 publicClient : PublicClient ;
1323 wallet : WalletClient ;
1424 faucet : Address ;
1525 claimer : Account ;
1626 role ?: string ;
17- amountUSD ?: bigint | ( ( minAmountUSD : bigint ) => bigint ) ;
27+ /**
28+ * Either:
29+ * - List of tokens with exact amounts to claim
30+ * - Amount in USD
31+ * - Function that returns amount in USD
32+ * - Undefined (will claim default amount)
33+ */
34+ amount ?: TokenClaim [ ] | bigint | ( ( minAmountUSD : bigint ) => bigint ) ;
1835 gasMultiplier ?: bigint ;
1936 logger ?: ILogger ;
2037}
@@ -23,60 +40,77 @@ const faucetAbi = parseAbi([
2340 "function minAmountUSD() external view returns (uint256)" ,
2441 "function claim() external" ,
2542 "function claim(uint256 amountUSD) external" ,
43+ "function claim((address token, uint256 amount)[] claims) external" ,
2644] ) ;
2745
2846export async function claimFromFaucet (
2947 opts : ClaimFromFaucetOptions ,
3048) : Promise < void > {
3149 const {
50+ sdk,
3251 publicClient,
3352 wallet,
3453 faucet,
3554 claimer,
3655 role,
37- amountUSD ,
56+ amount ,
3857 logger,
3958 gasMultiplier = 10n ,
4059 } = opts ;
4160
42- let toClaimUSD : bigint | undefined ;
43- if ( typeof amountUSD === "bigint" ) {
44- toClaimUSD = amountUSD ;
45- } else if ( typeof amountUSD === "function" ) {
46- toClaimUSD = await readContract ( publicClient , {
61+ let amnt = "default amount" ;
62+ let args : AbiParametersToPrimitiveTypes <
63+ ExtractAbiFunction < typeof faucetAbi , "claim" > [ "inputs" ]
64+ > ;
65+ if ( Array . isArray ( amount ) ) {
66+ args = [ amount ] ;
67+ amnt = amount
68+ . map ( v => {
69+ try {
70+ return sdk . tokensMeta . formatBN ( v . token , v . amount , { symbol : true } ) ;
71+ } catch {
72+ return `${ v . amount } of ${ v . token } ` ;
73+ }
74+ } )
75+ . join ( ", " ) ;
76+ } else if ( typeof amount === "bigint" ) {
77+ args = [ amount ] ;
78+ amnt = `${ formatBN ( args [ 0 ] , 8 ) } USD` ;
79+ } else if ( typeof amount === "function" ) {
80+ const minAmountUSD = await readContract ( publicClient , {
4781 address : faucet ,
4882 abi : faucetAbi ,
4983 functionName : "minAmountUSD" ,
5084 } ) ;
51- logger ?. debug ( `faucet min amount USD: ${ toClaimUSD } ` ) ;
52- toClaimUSD = amountUSD ( toClaimUSD ) ;
85+ logger ?. debug ( `faucet min amount USD: ${ minAmountUSD } ` ) ;
86+ args = [ amount ( minAmountUSD ) ] ;
87+ amnt = `${ formatBN ( args [ 0 ] , 8 ) } USD` ;
88+ } else {
89+ args = [ ] ;
5390 }
5491
55- if ( toClaimUSD === 0n ) {
92+ if ( args [ 0 ] === 0n ) {
5693 logger ?. debug ( "amount is 0, skipping claim" ) ;
5794 return ;
5895 }
5996
60- const [ usr , amnt ] = [
61- [ role , claimer . address ] . filter ( Boolean ) . join ( " " ) ,
62- toClaimUSD ? formatBN ( toClaimUSD , 8 ) : "default amount" ,
63- ] ;
97+ const usr = [ role , claimer . address ] . filter ( Boolean ) . join ( " " ) ;
6498
65- logger ?. debug ( `${ usr } claiming ${ amnt } USD from faucet` ) ;
99+ logger ?. debug ( `${ usr } claiming ${ amnt } from faucet` ) ;
66100
67101 const gas = await publicClient . estimateContractGas ( {
68102 account : claimer ,
69103 address : faucet ,
70104 abi : faucetAbi ,
71105 functionName : "claim" ,
72- args : toClaimUSD ? [ toClaimUSD ] : [ ] ,
106+ args,
73107 } ) ;
74108 const hash = await wallet . writeContract ( {
75109 account : claimer ,
76110 address : faucet ,
77111 abi : faucetAbi ,
78112 functionName : "claim" ,
79- args : toClaimUSD ? [ toClaimUSD ] : [ ] ,
113+ args,
80114 chain : wallet . chain ,
81115 gas : gas * gasMultiplier ,
82116 } ) ;
@@ -85,10 +119,8 @@ export async function claimFromFaucet(
85119 } ) ;
86120 if ( receipt . status === "reverted" ) {
87121 throw new Error (
88- `${ usr } failed to claimed equivalent of ${ amnt } USD from faucet, tx: ${ hash } ` ,
122+ `${ usr } failed to claimed ${ amnt } from faucet, tx: ${ hash } ` ,
89123 ) ;
90124 }
91- logger ?. debug (
92- `${ usr } claimed equivalent of ${ amnt } USD from faucet, tx: ${ hash } ` ,
93- ) ;
125+ logger ?. debug ( `${ usr } claimed ${ amnt } from faucet, tx: ${ hash } ` ) ;
94126}
0 commit comments