Skip to content

Commit 9fa71d8

Browse files
fix: swap params and add buffer (#396)
* swap params and add buffer * [bot] New pkg version: 1.6.3 * Trigger Build --------- Co-authored-by: GitHub Actions <actions@github.com>
1 parent 3cda9d2 commit 9fa71d8

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@centrifuge/sdk",
3-
"version": "1.6.2",
3+
"version": "1.6.3",
44
"description": "",
55
"homepage": "https://github.com/centrifuge/sdk/tree/main#readme",
66
"author": "",

src/Centrifuge.test.ts

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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',

src/Centrifuge.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import {
5757
type TransactionContext,
5858
} from './types/transaction.js'
5959
import { Balance } from './utils/BigInt.js'
60-
import { estimateBatchBridgeFee } from './utils/gas.js'
60+
import { addEstimateBuffer, estimateBatchBridgeFee } from './utils/gas.js'
6161
import { generateShareClassSalt, randomUint } from './utils/index.js'
6262
import { createPinning, getUrlFromHash } from './utils/ipfs.js'
6363
import { hashKey } from './utils/query.js'
@@ -675,7 +675,7 @@ export class Centrifuge {
675675
self.getClient(fromCentrifugeId),
676676
])
677677
const batchHash = keccak256(batch)
678-
const [counter, gasLimit] = await client.readContract({
678+
const [gasLimit, counter] = await client.readContract({
679679
address: addresses.gateway,
680680
abi: ABI.Gateway,
681681
functionName: 'underpaid',
@@ -690,14 +690,15 @@ export class Centrifuge {
690690
functionName: 'estimate',
691691
args: [toCentrifugeId, batch, gasLimit],
692692
})
693+
const bufferedEstimate = addEstimateBuffer(estimate)
693694

694695
yield* doTransaction('Repay', ctx, () =>
695696
ctx.walletClient.writeContract({
696697
address: addresses.gateway,
697698
abi: ABI.Gateway,
698699
functionName: 'repay',
699700
args: [toCentrifugeId, batch, ctx.signingAddress],
700-
value: estimate + extraPayment,
701+
value: bufferedEstimate + extraPayment,
701702
})
702703
)
703704
}, fromCentrifugeId)

src/utils/gas.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ function _assertBatchGasWithinLimit(batchGasLimit: bigint, maxBatchGasLimit: big
77
}
88
}
99

10+
export function addEstimateBuffer(estimate: bigint) {
11+
return (estimate * 3n) / 2n
12+
}
13+
1014
export async function estimateBatchBridgeFee(params: {
1115
publicClient: Client
1216
gasService: HexString
@@ -68,5 +72,5 @@ export async function estimateBatchBridgeFee(params: {
6872
})
6973

7074
// Add 50% buffer to the estimate, as the actual gas used can be higher than the estimate due dynamic message content
71-
return (estimate * 3n) / 2n
75+
return addEstimateBuffer(estimate)
7276
}

0 commit comments

Comments
 (0)