Skip to content

Commit

Permalink
feat: support new chain, scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
hzhu committed Sep 24, 2024
1 parent 80e2a59 commit 5f5bed4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
6 changes: 3 additions & 3 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"/> |
| :----------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------: |
| Arbitrum | Avalanche | Base | BNB Chain | Ethereum | Optimism | Polygon |
| <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 |

## Overview

Expand Down
40 changes: 39 additions & 1 deletion src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import {
type Transport,
type Chain,
} from "viem";
import { arbitrum, base, mainnet, optimism, polygon } from "viem/chains";
import {
base,
scroll,
polygon,
mainnet,
arbitrum,
optimism,
} from "viem/chains";
import { test, expect } from "vitest";
import { parseSwap } from "../index";
import { NATIVE_TOKEN_ADDRESS } from "../constants";
Expand Down Expand Up @@ -951,3 +958,34 @@ test("gracefully handles a revert in a erc-4337 transaction", async () => {

expect(result).toEqual(null);
});

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

const transactionHash = `0x84b07445a1a868b4338df8aed67c9ea330e771596bf603dbef8c12b3cb9970e5`;

const result = await parseSwap({
publicClient,
transactionHash,
smartContractWallet: "0x3F6dAB60Cc16377Df9684959e20962f44De20988",
});

expect(result).toEqual({
tokenIn: {
symbol: "USDC",
amount: "1",
address: "0x06eFdBFf2a14a7c8E15944D1F4A48F9F95F663A4",
},
tokenOut: {
symbol: "USDT",
amount: "0.998168",
address: "0xf55BEC9cafDbE8730f096Aa55dad6D22d44099Df",
},
});
});
22 changes: 21 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { fromHex, erc20Abi, getAddress, formatUnits, formatEther } from "viem";
import {
bsc,
base,
scroll,
mainnet,
polygon,
arbitrum,
optimism,
avalanche,
} from "viem/chains";
import { NATIVE_SYMBOL_BY_CHAIN_ID, NATIVE_TOKEN_ADDRESS } from "../constants";
import type { Address } from "viem";
import type {
Expand All @@ -11,7 +21,17 @@ import type {
export function isChainIdSupported(
chainId: number
): chainId is SupportedChainId {
return [1, 10, 56, 137, 8453, 42161, 43114].includes(chainId);
const supportedChainIds: number[] = [
bsc.id,
base.id,
scroll.id,
polygon.id,
mainnet.id,
arbitrum.id,
optimism.id,
avalanche.id,
];
return supportedChainIds.includes(chainId);
}

export function calculateNativeTransfer(
Expand Down

0 comments on commit 5f5bed4

Please sign in to comment.