Skip to content

Commit b9060fa

Browse files
authored
Allow Repayer to initiate Repay to remote self (#243)
1 parent 0737ad9 commit b9060fa

3 files changed

Lines changed: 68 additions & 3 deletions

File tree

contracts/Repayer.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ contract Repayer is
214214
}
215215

216216
if (provider == Provider.LOCAL) {
217-
// This should always pass because isRouteAllowed check will fail earlier.
218-
// It is put here for explicitness.
217+
require(destinationPool != address(this), RouteDenied());
219218
require(destinationDomain == DOMAIN, UnsupportedDomain());
220219
// For local we proceed to the process right away.
221220
_processRepayLOCAL(token, amount, destinationPool);
@@ -416,6 +415,11 @@ contract Repayer is
416415
}
417416

418417
function isRouteAllowed(address pool, Domain domain, Provider provider) public view returns (bool) {
418+
// As long as we deploy the Repayer contract on the same address on every supported domain this will work.
419+
// If we are to ever deploy it to new address, we will need to change this logic.
420+
if (pool == address(this)) {
421+
return true;
422+
}
419423
return _getStorage().allowedRoutes[pool].get(_toIndex(domain, provider));
420424
}
421425

coverage-baseline.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"lines": "99.66",
33
"functions": "99.64",
4-
"branches": "92.39",
4+
"branches": "92.56",
55
"statements": "99.66"
66
}

test/Repayer.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4307,4 +4307,65 @@ describe("Repayer", function () {
43074307
}
43084308
);
43094309
});
4310+
4311+
it("Should not allow initiating local repay to repayer's own address", async function () {
4312+
const {repayer, usdc, USDC_DEC, repayUser} = await loadFixture(deployAll);
4313+
4314+
await usdc.transfer(repayer, 10n * USDC_DEC);
4315+
await expect(repayer.connect(repayUser).initiateRepay(
4316+
usdc,
4317+
4n * USDC_DEC,
4318+
repayer,
4319+
Domain.BASE,
4320+
Provider.LOCAL,
4321+
"0x"
4322+
)).to.be.revertedWithCustomError(repayer, "RouteDenied()");
4323+
});
4324+
4325+
it("Should allow initiating remote repay to repayer's own address without an explicit route", async function () {
4326+
const {repayer, usdc, USDC_DEC, repayUser, cctpTokenMessenger} = await loadFixture(deployAll);
4327+
4328+
await usdc.transfer(repayer, 10n * USDC_DEC);
4329+
const tx = repayer.connect(repayUser).initiateRepay(
4330+
usdc,
4331+
4n * USDC_DEC,
4332+
repayer,
4333+
Domain.ETHEREUM,
4334+
Provider.CCTP,
4335+
"0x"
4336+
);
4337+
await expect(tx)
4338+
.to.emit(repayer, "InitiateRepay")
4339+
.withArgs(usdc.target, 4n * USDC_DEC, repayer.target, Domain.ETHEREUM, Provider.CCTP);
4340+
await expect(tx)
4341+
.to.emit(usdc, "Transfer")
4342+
.withArgs(repayer.target, cctpTokenMessenger.target, 4n * USDC_DEC);
4343+
await expect(tx)
4344+
.to.emit(usdc, "Transfer")
4345+
.withArgs(cctpTokenMessenger.target, ZERO_ADDRESS, 4n * USDC_DEC);
4346+
4347+
expect(await usdc.balanceOf(repayer)).to.equal(6n * USDC_DEC);
4348+
});
4349+
4350+
it("Should allow processing repay to repayer's own address without an explicit route", async function () {
4351+
const {repayer, usdc, USDC_DEC, repayUser} = await loadFixture(deployAll);
4352+
4353+
expect(await repayer.isRouteAllowed(repayer, Domain.BASE, Provider.LOCAL)).to.be.true;
4354+
4355+
const message = AbiCoder.defaultAbiCoder().encode(
4356+
["address", "address", "uint256"],
4357+
[usdc.target, repayer.target, 4n * USDC_DEC]
4358+
);
4359+
const signature = AbiCoder.defaultAbiCoder().encode(["bool", "bool"], [true, true]);
4360+
const extraData = AbiCoder.defaultAbiCoder().encode(["bytes", "bytes"], [message, signature]);
4361+
const tx = repayer.connect(repayUser).processRepay(repayer, Provider.CCTP, extraData);
4362+
await expect(tx)
4363+
.to.emit(repayer, "ProcessRepay")
4364+
.withArgs(usdc.target, 4n * USDC_DEC, repayer.target, Provider.CCTP);
4365+
await expect(tx)
4366+
.to.emit(usdc, "Transfer")
4367+
.withArgs(ZERO_ADDRESS, repayer.target, 4n * USDC_DEC);
4368+
4369+
expect(await usdc.balanceOf(repayer)).to.equal(4n * USDC_DEC);
4370+
});
43104371
});

0 commit comments

Comments
 (0)