Skip to content

Commit 5216345

Browse files
authored
improve(relayerFeeCalculator): Add logging around relayer fee calculations (#66)
1 parent 13833ca commit 5216345

File tree

7 files changed

+23
-16
lines changed

7 files changed

+23
-16
lines changed

src/relayFeeCalculator/chain-queries/arbitrum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ export class ArbitrumQueries implements QueryInterface {
2424
return gasPrice.mul(gasEstimate).toString();
2525
}
2626

27-
async getTokenPrice(tokenSymbol: string): Promise<string | number> {
27+
async getTokenPrice(tokenSymbol: string): Promise<number> {
2828
if (!this.symbolMapping[tokenSymbol]) throw new Error(`${tokenSymbol} does not exist in mapping`);
2929
const [, price] = await Coingecko.get().getCurrentPriceByContract(this.symbolMapping[tokenSymbol].address, "eth");
3030
return price;
3131
}
3232

33-
async getTokenDecimals(tokenSymbol: string): Promise<number> {
33+
getTokenDecimals(tokenSymbol: string): number {
3434
if (!this.symbolMapping[tokenSymbol]) throw new Error(`${tokenSymbol} does not exist in mapping`);
3535
return this.symbolMapping[tokenSymbol].decimals;
3636
}

src/relayFeeCalculator/chain-queries/boba.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class BobaQueries implements QueryInterface {
4848
return price;
4949
}
5050

51-
async getTokenDecimals(tokenSymbol: string): Promise<number> {
51+
getTokenDecimals(tokenSymbol: string): number {
5252
if (!this.symbolMapping[tokenSymbol]) throw new Error(`${tokenSymbol} does not exist in mapping`);
5353
return this.symbolMapping[tokenSymbol].decimals;
5454
}

src/relayFeeCalculator/chain-queries/ethereum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class EthereumQueries implements QueryInterface {
8787
return price;
8888
}
8989

90-
async getTokenDecimals(tokenSymbol: string): Promise<number> {
90+
getTokenDecimals(tokenSymbol: string): number {
9191
if (!this.symbolMapping[tokenSymbol]) throw new Error(`${tokenSymbol} does not exist in mapping`);
9292
return this.symbolMapping[tokenSymbol].decimals;
9393
}

src/relayFeeCalculator/chain-queries/optimism.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class OptimismQueries implements QueryInterface {
4848
return price;
4949
}
5050

51-
async getTokenDecimals(tokenSymbol: string): Promise<number> {
51+
getTokenDecimals(tokenSymbol: string): number {
5252
if (!this.symbolMapping[tokenSymbol]) throw new Error(`${tokenSymbol} does not exist in mapping`);
5353
return this.symbolMapping[tokenSymbol].decimals;
5454
}

src/relayFeeCalculator/chain-queries/polygon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class PolygonQueries implements QueryInterface {
1717
.toString();
1818
}
1919

20-
async getTokenPrice(tokenSymbol: string): Promise<string | number> {
20+
async getTokenPrice(tokenSymbol: string): Promise<number> {
2121
if (!this.symbolMapping[tokenSymbol]) throw new Error(`${tokenSymbol} does not exist in mapping`);
2222
const [, tokenPrice] = await Coingecko.get().getCurrentPriceByContract(
2323
this.symbolMapping[tokenSymbol].address,
@@ -28,7 +28,7 @@ export class PolygonQueries implements QueryInterface {
2828
return Number((tokenPrice / maticPrice).toFixed(this.symbolMapping["MATIC"].decimals));
2929
}
3030

31-
async getTokenDecimals(tokenSymbol: string): Promise<number> {
31+
getTokenDecimals(tokenSymbol: string): number {
3232
if (!this.symbolMapping[tokenSymbol]) throw new Error(`${tokenSymbol} does not exist in mapping`);
3333
return this.symbolMapping[tokenSymbol].decimals;
3434
}

src/relayFeeCalculator/relayFeeCalculator.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class ExampleQueries implements QueryInterface {
2626
async getGasCosts(): Promise<BigNumberish> {
2727
return gasCost(this.defaultGas, "100");
2828
}
29-
async getTokenPrice(): Promise<number | string> {
29+
async getTokenPrice(): Promise<number> {
3030
return 1;
3131
}
32-
async getTokenDecimals(): Promise<number> {
32+
getTokenDecimals(): number {
3333
return 18;
3434
}
3535
}

src/relayFeeCalculator/relayFeeCalculator.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const { percent, fixedPointAdjustment } = uma.across.utils;
77
// This needs to be implemented for every chain and passed into RelayFeeCalculator
88
export interface QueryInterface {
99
getGasCosts: (tokenSymbol: string) => Promise<BigNumberish>;
10-
getTokenPrice: (tokenSymbol: string) => Promise<number | string>;
11-
getTokenDecimals: (tokenSymbol: string) => Promise<number>;
10+
getTokenPrice: (tokenSymbol: string) => Promise<number>;
11+
getTokenDecimals: (tokenSymbol: string) => number;
1212
}
1313

1414
export const expectedCapitalCostsKeys = ["lowerBound", "upperBound", "cutoff", "decimals"];
@@ -72,11 +72,16 @@ export class RelayFeeCalculator {
7272
}
7373

7474
async gasFeePercent(amountToRelay: BigNumberish, tokenSymbol: string): Promise<BigNumber> {
75-
const [gasCosts, tokenPrice, decimals] = await Promise.all([
76-
this.queries.getGasCosts(tokenSymbol),
77-
this.queries.getTokenPrice(tokenSymbol),
78-
this.queries.getTokenDecimals(tokenSymbol),
79-
]);
75+
const getGasCosts = this.queries.getGasCosts(tokenSymbol).catch((error) => {
76+
console.error(`ERROR(gasFeePercent): Error while fetching gas costs ${error}`);
77+
throw error;
78+
});
79+
const getTokenPrice = this.queries.getTokenPrice(tokenSymbol).catch((error) => {
80+
console.error(`ERROR(gasFeePercent): Error while fetching token price ${error}`);
81+
throw error;
82+
});
83+
const [gasCosts, tokenPrice] = await Promise.all([getGasCosts, getTokenPrice]);
84+
const decimals = this.queries.getTokenDecimals(tokenSymbol);
8085
const gasFeesInToken = nativeToToken(gasCosts, tokenPrice, decimals, this.nativeTokenDecimals);
8186
return percent(gasFeesInToken, amountToRelay);
8287
}
@@ -121,8 +126,10 @@ export class RelayFeeCalculator {
121126
async relayerFeeDetails(amountToRelay: BigNumberish, tokenSymbol: string) {
122127
let isAmountTooLow = false;
123128
const gasFeePercent = await this.gasFeePercent(amountToRelay, tokenSymbol);
129+
console.log(`INFO(relayerFeeDetails): Computed gasFeePercent ${gasFeePercent}`);
124130
const gasFeeTotal = gasFeePercent.mul(amountToRelay).div(fixedPointAdjustment);
125131
const capitalFeePercent = await this.capitalFeePercent(amountToRelay, tokenSymbol);
132+
console.log(`INFO(relayerFeeDetails): Computed capitalFeePercent ${capitalFeePercent}`);
126133
const capitalFeeTotal = capitalFeePercent.mul(amountToRelay).div(fixedPointAdjustment);
127134
const relayFeePercent = gasFeePercent.add(capitalFeePercent);
128135
const relayFeeTotal = gasFeeTotal.add(capitalFeeTotal);

0 commit comments

Comments
 (0)