Skip to content

Commit faa999c

Browse files
authored
fix: fix referral address extraction (#195)
* fix: fix referral address extraction * Fix tests * Handle specific error * Handle case when error is undefined * Remove decoding and encoding data * Unde new line deletion
1 parent 472ae6d commit faa999c

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/modules/referral/services/service.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ describe("ReferralService", () => {
88
const moduleRef = await Test.createTestingModule({
99
providers: [ReferralService],
1010
})
11-
.useMocker((token) => {
12-
if (token === "DepositRepository") {
13-
return {};
14-
}
11+
.useMocker(() => {
12+
return {};
1513
})
1614
.compile();
1715

@@ -45,4 +43,11 @@ describe("ReferralService", () => {
4543
);
4644
expect(address).toEqual(undefined);
4745
});
46+
47+
it("should not extract referral address if calldata too short", () => {
48+
const address = service.extractReferralAddressUsingDelimiter(
49+
"0xb1a8f7c700000000000000000000000000000000000000000000000000000000000000ca00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000022000100000000000000000000000000000000000000000000000000000000000c3500000000000000000000000000000000000000000000000000000000000000",
50+
);
51+
expect(address).toEqual(undefined);
52+
});
4853
});

src/modules/referral/services/service.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,15 @@ export class ReferralService {
170170
return { referralRate: 0.4, tier: 1 };
171171
}
172172

173+
/**
174+
* Returns the calldata string without the arguments of the deposit function
175+
* @param data
176+
*/
173177
public subtractFunctionArgsFromCallData(data: string) {
174-
const coder = new ethers.utils.AbiCoder();
175-
// strip hex method identifier
176-
const dataNoMethod = ethers.utils.hexDataSlice(data, 4);
177-
// keep method hex identifier
178-
const methodHex = data.replace(dataNoMethod.replace("0x", ""), "");
179-
const decodedData = coder.decode(
180-
["address", "address", "uint256", "uint256", "uint64", "uint32"],
181-
ethers.utils.hexDataSlice(data, 4),
182-
);
183-
const encoded = coder.encode(["address", "address", "uint256", "uint256", "uint64", "uint32"], decodedData);
184-
const fullEncoded = methodHex + encoded.replace("0x", "");
185-
return data.replace(fullEncoded, "");
178+
// the length of the string including the method identifier and
179+
// the deposit function args ["address", "address", "uint256", "uint256", "uint64", "uint32"]
180+
const methodIdAndArgsLength = 395;
181+
return data.slice(methodIdAndArgsLength - 1);
186182
}
187183

188184
public extractReferralAddressUsingDelimiter(data: string) {

0 commit comments

Comments
 (0)