Skip to content

Commit

Permalink
feat: include new markdown on bridged transfers (#884)
Browse files Browse the repository at this point in the history
* docs: add documentation to fn

* chore: cleaned up return type

* chore: defined function instead of variable

* feat: fn to resolve decimals from a token symbol

* feat: include new markdown

* improve: set specific chain id

Co-authored-by: Paul <108695806+pxrl@users.noreply.github.com>

---------

Co-authored-by: Paul <108695806+pxrl@users.noreply.github.com>
  • Loading branch information
james-a-morris and pxrl committed Aug 16, 2023
1 parent 6f95f59 commit f83e9ee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/clients/bridges/BaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ZERO_ADDRESS,
assert,
compareAddressesSimple,
formatUnitsForToken,
} from "../../utils";
import { etherscanLink, getNetworkName, MAX_UINT_VAL, runTransaction } from "../../utils";

Expand Down Expand Up @@ -279,7 +280,7 @@ export abstract class BaseAdapter {
amount: BigNumberish,
contract: Contract,
method: string,
args: any[],
args: unknown[],
gasLimitMultiplier: number,
msgValue: BigNumber,
simMode: boolean
Expand All @@ -301,6 +302,7 @@ export abstract class BaseAdapter {
throw new Error(`${message} (${reason})`);
}

const tokenSymbol = matchTokenSymbol(l1Token, this.hubChainId)[0];
const message = `💌⭐️ Bridging tokens from ${this.hubChainId} to ${this.chainId}`;
this.logger.debug({
at: `${this.getName()}#_sendTokenToTargetChain`,
Expand All @@ -310,6 +312,7 @@ export abstract class BaseAdapter {
amount,
contract: contract.address,
txnRequestData,
mrkdwn: `Sent ${formatUnitsForToken(tokenSymbol, amount)} ${tokenSymbol} to chain ${this.chainId}`,
});
if (simMode) {
this.logger.debug({
Expand Down
6 changes: 6 additions & 0 deletions src/utils/AddressUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export function matchTokenSymbol(tokenAddress: string, chainId: number): string[
.map(({ symbol }) => symbol);
}

/**
* Resolves a list of token symbols for a list of token addresses and a chain ID.
* @param tokenAddresses The token addresses to resolve the symbols for.
* @param chainId The chain ID to resolve the symbols for.
* @returns The token symbols for the given token addresses and chain ID. Undefined symbols are filtered out.
*/
export function resolveTokenSymbols(tokenAddresses: string[], chainId: number): string[] {
const tokenSymbols = Object.values(TOKEN_SYMBOLS_MAP);
return tokenAddresses
Expand Down
20 changes: 16 additions & 4 deletions src/utils/TokenUtils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { constants, utils } from "@across-protocol/sdk-v2";
import { CONTRACT_ADDRESSES } from "../common";
import { BigNumberish, utils as ethersUtils } from "ethers";
const { TOKEN_SYMBOLS_MAP, CHAIN_IDs, ZERO_ADDRESS } = constants;

export const { fetchTokenInfo } = utils;

export const getL2TokenAddresses = (l1TokenAddress: string): { [chainId: number]: string } => {
export function getL2TokenAddresses(l1TokenAddress: string): { [chainId: number]: string } {
return Object.values(TOKEN_SYMBOLS_MAP).find((details) => {
return details.addresses[CHAIN_IDs.MAINNET] === l1TokenAddress;
})?.addresses;
};
}

export const getEthAddressForChain = (chainId: number): string => {
export function getEthAddressForChain(chainId: number): string {
return CONTRACT_ADDRESSES[chainId]?.eth?.address ?? ZERO_ADDRESS;
};
}

/**
* Format the given amount of tokens to the correct number of decimals for the given token symbol.
* @param symbol The token symbol to format the amount for.
* @param amount The amount to format.
* @returns The formatted amount as a decimal-inclusive string.
*/
export function formatUnitsForToken(symbol: string, amount: BigNumberish): string {
const decimals = (TOKEN_SYMBOLS_MAP[symbol]?.decimals as number) ?? 18;
return ethersUtils.formatUnits(amount, decimals);
}

0 comments on commit f83e9ee

Please sign in to comment.