Skip to content

Commit

Permalink
feat: support linea + update polygon native symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
hzhu committed Sep 26, 2024
1 parent ed6370d commit f2ff8c4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

## Blockchain Support

| <img alt="arbitrum" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/arbitrum/info/logo.png" width="23"/> | <img alt="avalanche" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/avalanchec/info/logo.png" width="20"/> | <img alt="base" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/base/info/logo.png" width="20"/> | <img alt="bnb chain" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/binance/info/logo.png" width="21"/> | <img alt="ethereum" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/ethereum/info/logo.png" width="21"/> | <img alt="optimism" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/optimism/info/logo.png" width="22"/> | <img alt="polygon" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/polygon/info/logo.png" width="22"/> | <img alt="scroll" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/scroll/info/logo.png" width="22"/> |
| :----------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------: |
| Arbitrum | Avalanche | Base | BNB Chain | Ethereum | Optimism | Polygon | Scroll |
| <img alt="arbitrum" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/arbitrum/info/logo.png" width="23"/> | <img alt="avalanche" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/avalanchec/info/logo.png" width="20"/> | <img alt="base" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/base/info/logo.png" width="20"/> | <img alt="bnb chain" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/binance/info/logo.png" width="21"/> | <img alt="ethereum" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/ethereum/info/logo.png" width="21"/> | <img alt="linea" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/linea/info/logo.png" width="22"/> | <img alt="optimism" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/optimism/info/logo.png" width="22"/> | <img alt="polygon" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/polygon/info/logo.png" width="22"/> | <img alt="scroll" src="https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/scroll/info/logo.png" width="22"/> |
| :----------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------: |
| Arbitrum | Avalanche | Base | BNB Chain | Ethereum | Linea | Optimism | Polygon | Scroll |

## Overview

Expand Down Expand Up @@ -57,8 +57,7 @@ async function main() {

// You can pass any transaction hash that you got after submitted a 0x transaction:
// https://etherscan.io/address/0x2fc205711fc933ef6e5bcc0bf6e6a9bfc220b2d8073aea4f41305882f485669d
const transactionHash =
"0x2fc205711fc933ef6e5bcc0bf6e6a9bfc220b2d8073aea4f41305882f485669d";
const transactionHash = `0x2fc205711fc933ef6e5bcc0bf6e6a9bfc220b2d8073aea4f41305882f485669d`;

const publicClient = createPublicClient({
chain: mainnet,
Expand Down
27 changes: 20 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import {
bsc,
base,
linea,
scroll,
mainnet,
polygon,
arbitrum,
optimism,
avalanche,
} from "viem/chains";
import type { SupportedChainId } from "./types";

export const SETTLER_META_TXN_ABI = [
Expand Down Expand Up @@ -33,13 +44,15 @@ export const FUNCTION_SELECTORS = { EXECUTE_META_TXN: "0xfd3ad6d4" };

export const NATIVE_SYMBOL_BY_CHAIN_ID: { [key in SupportedChainId]: string } =
{
1: "ETH", // Ethereum
10: "ETH", // Optimism
56: "BNB", // BNB Chain
137: "MATIC", // Polygon
8453: "ETH", // Base
42161: "ETH", // Arbitrum One
43114: "AVAX", // Avalanche
[bsc.id]: bsc.nativeCurrency.symbol,
[base.id]: base.nativeCurrency.symbol,
[linea.id]: linea.nativeCurrency.symbol,
[scroll.id]: scroll.nativeCurrency.symbol,
[mainnet.id]: mainnet.nativeCurrency.symbol,
[polygon.id]: polygon.nativeCurrency.symbol,
[optimism.id]: optimism.nativeCurrency.symbol,
[arbitrum.id]: arbitrum.nativeCurrency.symbol,
[avalanche.id]: avalanche.nativeCurrency.symbol,
};

export const NATIVE_TOKEN_ADDRESS = `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`;
Expand Down
43 changes: 37 additions & 6 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "viem";
import {
base,
linea,
scroll,
polygon,
mainnet,
Expand Down Expand Up @@ -391,7 +392,7 @@ test("parse a swap on Base (ETH for BRETT)", async () => {
});

// https://polygonscan.com/tx/0x438517b81f50858035f4b8e0870f5d797616509b5102c28814bcc378559c213d
test("parse a gasless approval + gasless swap on Polygon (USDC for MATIC)", async () => {
test("parse a gasless approval + gasless swap on Polygon (USDC for POL)", async () => {
const publicClient = createPublicClient({
chain: polygon,
transport: http(
Expand All @@ -414,7 +415,7 @@ test("parse a gasless approval + gasless swap on Polygon (USDC for MATIC)", asyn
address: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
},
tokenOut: {
symbol: "MATIC",
symbol: "POL",
amount: "15.513683571865599415",
address: NATIVE_TOKEN_ADDRESS,
},
Expand Down Expand Up @@ -816,7 +817,7 @@ test("parse a swap on Base (ETH for USDC) with smart contract wallet", async ()
});

// https://polygonscan.com/tx/0xc624eb3ea779d1645571b5a538683eee1fb1bd9afdb1d0cb470de2b8755353a9
test("parse a swap on Polygon (MATIC for USDC) with smart contract wallet", async () => {
test("parse a swap on Polygon (WPOL for USDC) with smart contract wallet", async () => {
const publicClient = createPublicClient({
chain: polygon,
transport: http(
Expand All @@ -834,7 +835,7 @@ test("parse a swap on Polygon (MATIC for USDC) with smart contract wallet", asyn

expect(result).toEqual({
tokenIn: {
symbol: "MATIC",
symbol: "POL",
amount: "5",
address: NATIVE_TOKEN_ADDRESS,
},
Expand All @@ -847,7 +848,7 @@ test("parse a swap on Polygon (MATIC for USDC) with smart contract wallet", asyn
});

// https://polygonscan.com/tx/0x6ec07b99b37695a60915b3886476a86efdd4c60420ddcd158efd62f9c3fba074
test("parse a swap on Polygon (USDC for WMATIC) with smart contract wallet", async () => {
test("parse a swap on Polygon (USDC for WPOL) with smart contract wallet", async () => {
const publicClient = createPublicClient({
chain: polygon,
transport: http(
Expand All @@ -870,7 +871,7 @@ test("parse a swap on Polygon (USDC for WMATIC) with smart contract wallet", asy
address: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
},
tokenOut: {
symbol: "WMATIC",
symbol: "WPOL",
amount: "4.996808166219061944",
address: "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270",
},
Expand Down Expand Up @@ -989,3 +990,33 @@ test("parse a swap on Scroll(USDC for USDT) with execute", async () => {
},
});
});

// https://lineascan.build/tx/0x3506d4cd4b434ec3e6fb2ec5473069471257a9436c4e8e576e0eca2a02816a75
test("parse a swap on Linear (USDC for WETH) with execute", async () => {
const publicClient = createPublicClient({
chain: linea,
transport: http(
`https://linea-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`
),
}) as PublicClient<Transport, Chain>;

const transactionHash = `0x3506d4cd4b434ec3e6fb2ec5473069471257a9436c4e8e576e0eca2a02816a75`;

const result = await parseSwap({
publicClient,
transactionHash,
});

expect(result).toEqual({
tokenIn: {
symbol: "USDC",
amount: "22.314511",
address: "0x176211869cA2b568f2A7D4EE941E073a821EE1ff",
},
tokenOut: {
symbol: "ETH",
amount: "0.008646502734906467",
address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
},
});
});
23 changes: 22 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import {
bsc,
base,
linea,
scroll,
mainnet,
polygon,
arbitrum,
optimism,
avalanche,
} from "viem/chains";

import type {
Hex,
Hash,
Expand All @@ -8,7 +20,16 @@ import type {
TransactionReceipt,
} from "viem";

export type SupportedChainId = 1 | 10 | 56 | 137 | 8453 | 42161 | 43114;
export type SupportedChainId =
| typeof bsc.id
| typeof base.id
| typeof linea.id
| typeof scroll.id
| typeof mainnet.id
| typeof polygon.id
| typeof arbitrum.id
| typeof optimism.id
| typeof avalanche.id;

export interface EnrichLogsArgs {
transactionReceipt: TransactionReceipt;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
arbitrum,
optimism,
avalanche,
linea,
} from "viem/chains";
import { NATIVE_SYMBOL_BY_CHAIN_ID, NATIVE_TOKEN_ADDRESS } from "../constants";
import type { Address } from "viem";
Expand All @@ -24,6 +25,7 @@ export function isChainIdSupported(
const supportedChainIds: number[] = [
bsc.id,
base.id,
linea.id,
scroll.id,
polygon.id,
mainnet.id,
Expand Down Expand Up @@ -165,7 +167,7 @@ export function parseSmartContractWalletTx({

if (!input && nativeAmountFromTaker !== "0") {
const wrappedNativeAsset =
chainId === 56 ? "WBNB" : chainId === 137 ? "WMATIC" : "WETH";
chainId === 56 ? "WBNB" : chainId === 137 ? "WPOL" : "WETH";

const inputLog = logs.find((log) => log.symbol === wrappedNativeAsset);

Expand Down

0 comments on commit f2ff8c4

Please sign in to comment.