@@ -142,12 +142,17 @@ describe('Centrifuge', () => {
142142 } as any )
143143 )
144144
145- const estimate = await centrifuge . _estimate ( 1 , 2 , [ { type : MessageType . NotifyPool , poolId } , { type : MessageType . NotifyShareClass , poolId } ] )
145+ const estimate = await centrifuge . _estimate ( 1 , 2 , [
146+ { type : MessageType . NotifyPool , poolId } ,
147+ { type : MessageType . NotifyShareClass , poolId } ,
148+ ] )
146149 expect ( estimate ) . to . equal ( 1165n )
147150 expect (
148151 readContract . calledWithMatch ( {
149152 functionName : 'estimate' ,
150- args : [ 2 , '0x0' , 240n ] ,
153+ args : sinon . match ( ( args : unknown ) => {
154+ return Array . isArray ( args ) && args [ 0 ] === 2 && typeof args [ 1 ] === 'string' && args [ 2 ] === 240n
155+ } ) ,
151156 } )
152157 ) . to . equal ( true )
153158 } )
@@ -172,7 +177,10 @@ describe('Centrifuge', () => {
172177 )
173178
174179 try {
175- await centrifuge . _estimate ( 1 , 2 , [ { type : MessageType . NotifyPool , poolId } , { type : MessageType . NotifyShareClass , poolId } ] )
180+ await centrifuge . _estimate ( 1 , 2 , [
181+ { type : MessageType . NotifyPool , poolId } ,
182+ { type : MessageType . NotifyShareClass , poolId } ,
183+ ] )
176184 expect . fail ( 'Expected _estimate to throw when maxBatchGasLimit is exceeded' )
177185 } catch ( error ) {
178186 expect ( ( error as Error ) . message ) . to . equal ( 'Batch gas 400 exceeds limit 300 for chain 2' )
@@ -554,6 +562,53 @@ describe('Centrifuge', () => {
554562 } )
555563
556564 describe ( 'Transactions' , ( ) => {
565+ it ( 'should repay an underpaid batch using the returned gasLimit and buffered estimate' , async ( ) => {
566+ const centrifuge = new Centrifuge ( { environment : 'testnet' } )
567+ const gateway = randomAddress ( )
568+ const multiAdapter = randomAddress ( )
569+ const signingAddress = randomAddress ( )
570+ const batch = '0x1234'
571+ const readContract = sinon . stub ( )
572+ const writeContract = sinon . stub ( ) . resolves ( '0x1' )
573+
574+ readContract . withArgs ( sinon . match ( { functionName : 'underpaid' } ) ) . resolves ( [ 1200n , 2n ] )
575+ readContract . withArgs ( sinon . match ( { functionName : 'estimate' } ) ) . resolves ( 100n )
576+
577+ sinon . stub ( centrifuge as any , '_protocolAddresses' ) . resolves ( {
578+ gateway,
579+ multiAdapter,
580+ } )
581+ sinon . stub ( centrifuge as any , 'getClient' ) . resolves ( {
582+ readContract,
583+ } )
584+ sinon . stub ( centrifuge as any , '_transact' ) . callsFake ( ( callback : any ) =>
585+ callback ( {
586+ signingAddress,
587+ walletClient : { writeContract } ,
588+ publicClient : {
589+ getCode : sinon . stub ( ) . resolves ( undefined ) ,
590+ waitForTransactionReceipt : sinon . stub ( ) . resolves ( { status : 'success' } ) ,
591+ } ,
592+ } )
593+ )
594+
595+ expect (
596+ readContract . calledWithMatch ( {
597+ address : multiAdapter ,
598+ functionName : 'estimate' ,
599+ args : [ 2 , batch , 1200n ] ,
600+ } )
601+ ) . to . equal ( true )
602+ expect (
603+ writeContract . calledWithMatch ( {
604+ address : gateway ,
605+ functionName : 'repay' ,
606+ args : [ 2 , batch , signingAddress ] ,
607+ value : 155n ,
608+ } )
609+ ) . to . equal ( true )
610+ } )
611+
557612 it ( 'should register an asset' , async ( ) => {
558613 const centrifuge = new Centrifuge ( {
559614 environment : 'testnet' ,
0 commit comments