Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,20 @@
<br>Plasma
</div>
</td>
<td style="width:100px; text-align:center;">
<div align="center">
<img alt="Abstract" src="https://www.abs.xyz/favicon.ico" width="24"/>
<br>Abstract
</div>
</td>
</tr>
<tr>
<td style="width:100px; text-align:center;">
<div align="center">
<img alt="coming soon" src="https://i.imgur.com/CexTjqF.png" width="22"/>
<br>🔜
</div>
</td>
</tr>
<tr>
<td style="width:100px; text-align:center;">
<div align="center">
<img alt="coming soon" src="https://cdn.prod.website-files.com/63692bf32544bee8b1836ea6/637b01428c7bd8e16af26756_favicon-32.png" width="22"/>
Expand Down
5 changes: 4 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
avalanche,
berachain,
worldchain,
monad
monad,
abstract,
} from "viem/chains";
import type { SupportedChainId } from "./types";

Expand Down Expand Up @@ -132,6 +133,7 @@ export const SUPPORTED_CHAINS = [
berachain,
worldchain,
monad,
abstract,
];

export const NATIVE_SYMBOL_BY_CHAIN_ID: { [key in SupportedChainId]: string } =
Expand All @@ -153,6 +155,7 @@ export const NATIVE_SYMBOL_BY_CHAIN_ID: { [key in SupportedChainId]: string } =
[avalanche.id]: avalanche.nativeCurrency.symbol,
[berachain.id]: berachain.nativeCurrency.symbol,
[worldchain.id]: worldchain.nativeCurrency.symbol,
[abstract.id]: abstract.nativeCurrency.symbol,
};

export const NATIVE_TOKEN_ADDRESS = `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`;
Expand Down
32 changes: 32 additions & 0 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
berachain,
bsc,
celo,
abstract,
} from "viem/chains";
import { vi, test, expect } from "vitest";
import { parseSwap } from "../index";
Expand Down Expand Up @@ -1533,3 +1534,34 @@ test("parse a swap on Mainnet (TRG for SHITCOIN)", async () => {
},
});
});

// https://abscan.org/tx/0x89d0b2806d53d06a9b7f8105c041ec2d114f6b75ce3ea85a8c7bdb4ce8ae3d5a
test("parse a swap on Abstract (PENGU for USDC.e)", async () => {
const publicClient = createPublicClient({
chain: abstract,
transport: http(
`https://abstract-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`
),
}) as PublicClient<Transport, Chain>;

const transactionHash =
"0x89d0b2806d53d06a9b7f8105c041ec2d114f6b75ce3ea85a8c7bdb4ce8ae3d5a";

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

expect(result).toEqual({
tokenIn: {
symbol: "PENGU",
amount: "313.635629165192342644",
address: "0x9eBe3A824Ca958e4b3Da772D2065518F009CBa62",
},
tokenOut: {
symbol: "USDC.e",
amount: "2.98552",
address: "0x84A71ccD554Cc1b02749b35d22F684CC8ec987e1",
},
});
});
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
worldchain,
berachain,
unichain,
monad
monad,
abstract,
} from "viem/chains";

import type {
Expand Down Expand Up @@ -45,7 +46,8 @@ export type SupportedChainId =
| typeof optimism.id
| typeof avalanche.id
| typeof berachain.id
| typeof worldchain.id;
| typeof worldchain.id
| typeof abstract.id;

export interface EnrichLogsArgs {
transactionReceipt: TransactionReceipt;
Expand Down
17 changes: 13 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
avalanche,
berachain,
worldchain,
monad
monad,
abstract,
} from "viem/chains";
import { NATIVE_SYMBOL_BY_CHAIN_ID, NATIVE_TOKEN_ADDRESS } from "../constants";
import type { Address } from "viem";
Expand Down Expand Up @@ -48,6 +49,7 @@ export function isChainIdSupported(
avalanche.id,
berachain.id,
worldchain.id,
abstract.id,
];
return supportedChainIds.includes(chainId);
}
Expand Down Expand Up @@ -111,8 +113,15 @@ export async function transferLogs({
const midpoint = Math.floor(results.length / 2);
const enrichedLogs = transferLogsAddresses
.map((log, index) => {
const symbol = results[index].result as string;
const decimals = results[midpoint + index].result as number;
const symbol = results[index].result;
const decimals = results[midpoint + index].result;

// zkSync-style system contracts can emit Transfer-like logs for native accounting,
// but those addresses do not implement ERC20 metadata, so skip them here.
if (symbol == null || decimals == null || typeof decimals !== "number") {
return null;
}

const amount =
log.data === "0x" ? "0" : formatUnits(BigInt(log.data), decimals);
const amountRaw = log.data === "0x" ? 0n : BigInt(log.data);
Expand All @@ -123,7 +132,7 @@ export async function transferLogs({

return { to, from, symbol, amount, amountRaw, address, decimals };
})
.filter((log) => log.amount !== "0");
.filter((log): log is EnrichedLog => log != null && log.amount !== "0")

return enrichedLogs;
}
Expand Down