@0x/0x-parser
Advanced tools
Comparing version 0.3.1 to 0.3.2
import type { ParseSwapArgs } from "./types"; | ||
export * from "./types"; | ||
export declare function parseSwap({ transactionHash, exchangeProxyAbi, rpcUrl, }: ParseSwapArgs): Promise<({ | ||
@@ -3,0 +4,0 @@ tokenIn: { |
@@ -1,2 +0,3 @@ | ||
import type { ProcessedLog } from "../types"; | ||
import type { TransactionReceipt } from "ethers"; | ||
import type { ProcessedLog, EnrichedTxReceipt } from "../types"; | ||
export declare function convertHexToAddress(hexString: string): string; | ||
@@ -9,2 +10,6 @@ export declare function parseHexDataToString(hexData: string): string; | ||
}; | ||
export declare function enrichTxReceipt({ txReceipt, rpcUrl, }: { | ||
txReceipt: TransactionReceipt; | ||
rpcUrl: string; | ||
}): Promise<EnrichedTxReceipt>; | ||
export declare function extractTokenInfo(inputLog: ProcessedLog, outputLog: ProcessedLog): { | ||
@@ -11,0 +16,0 @@ tokenIn: { |
{ | ||
"name": "@0x/0x-parser", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "🧾 Designed for 0x integrators: This parser library decodes swap details from 0x transactions on Ethereum.", | ||
@@ -26,3 +26,3 @@ "keywords": [ | ||
"module": "dist/index.esm.js", | ||
"types": "dist/types.d.ts", | ||
"types": "dist/index.d.ts", | ||
"sideEffects": false, | ||
@@ -29,0 +29,0 @@ "files": [ |
import { Contract, JsonRpcProvider } from "ethers"; | ||
import { abi as permitAndCallAbi } from "./abi/PermitAndCall.json"; | ||
import { CONTRACTS, EXCHANGE_PROXY_ABI_URL } from "./constants"; | ||
import { | ||
@@ -25,64 +26,13 @@ fillLimitOrder, | ||
} from "./parsers"; | ||
import { | ||
CONTRACTS, | ||
EVENT_SIGNATURES, | ||
EXCHANGE_PROXY_ABI_URL, | ||
} from "./constants"; | ||
import { erc20Rpc, formatUnits, convertHexToAddress } from "./utils"; | ||
import { enrichTxReceipt } from "./utils"; | ||
import { TransactionStatus } from "./types"; | ||
import type { TransactionReceipt } from "ethers"; | ||
import type { | ||
Mtx, | ||
Log, | ||
LogParsers, | ||
EnrichedTxReceipt, | ||
EnrichedLogWithoutAmount, | ||
ParseSwapArgs, | ||
ParseGaslessTxArgs, | ||
ParseSwapArgs, | ||
ProcessedLog, | ||
} from "./types"; | ||
async function enrichLog({ | ||
log, | ||
rpcUrl, | ||
}: { | ||
log: Log; | ||
rpcUrl: string; | ||
}): Promise<EnrichedLogWithoutAmount> { | ||
const [symbol, decimals] = await Promise.all([ | ||
erc20Rpc.getSymbol(log.address, rpcUrl), | ||
erc20Rpc.getDecimals(log.address, rpcUrl), | ||
]); | ||
export * from "./types"; | ||
return { ...log, symbol, decimals }; | ||
} | ||
function processLog(log: EnrichedLogWithoutAmount): ProcessedLog { | ||
const { topics, data, decimals, symbol, address } = log; | ||
const { 1: fromHex, 2: toHex } = topics; | ||
const from = convertHexToAddress(fromHex); | ||
const to = convertHexToAddress(toHex); | ||
const amount = formatUnits(data, decimals); | ||
return { to, from, symbol, amount, address, decimals }; | ||
} | ||
async function enrichTxReceipt({ | ||
txReceipt, | ||
rpcUrl, | ||
}: { | ||
txReceipt: TransactionReceipt; | ||
rpcUrl: string; | ||
}): Promise<EnrichedTxReceipt> { | ||
const isERC20TransferEvent = (log: Log) => | ||
log.topics[0] === EVENT_SIGNATURES.Transfer; | ||
const filteredLogs = txReceipt.logs.filter(isERC20TransferEvent); | ||
const enrichedLogs = await Promise.all( | ||
filteredLogs.map((log) => enrichLog({ log, rpcUrl })) | ||
); | ||
const logs = enrichedLogs.map(processLog); | ||
return { logs, from: txReceipt.from }; | ||
} | ||
export async function parseSwap({ | ||
@@ -97,3 +47,3 @@ transactionHash, | ||
throw new Error( | ||
`Missing 0x Exchange Proxy ABI, which can be found here: ${EXCHANGE_PROXY_ABI_URL}` | ||
`Missing 0x Exchange Proxy ABI: ${EXCHANGE_PROXY_ABI_URL}` | ||
); | ||
@@ -100,0 +50,0 @@ |
@@ -59,4 +59,4 @@ import { it, expect } from "vitest"; | ||
}).rejects.toThrowError( | ||
`Missing 0x Exchange Proxy ABI, which can be found here: ${EXCHANGE_PROXY_ABI_URL}` | ||
`Missing 0x Exchange Proxy ABI: ${EXCHANGE_PROXY_ABI_URL}` | ||
); | ||
}); |
@@ -1,2 +0,9 @@ | ||
import type { ProcessedLog } from "../types"; | ||
import { EVENT_SIGNATURES } from "../constants"; | ||
import type { TransactionReceipt } from "ethers"; | ||
import type { | ||
Log, | ||
ProcessedLog, | ||
EnrichedTxReceipt, | ||
EnrichedLogWithoutAmount, | ||
} from "../types"; | ||
@@ -78,2 +85,45 @@ export function convertHexToAddress(hexString: string): string { | ||
async function enrichLog({ | ||
log, | ||
rpcUrl, | ||
}: { | ||
log: Log; | ||
rpcUrl: string; | ||
}): Promise<EnrichedLogWithoutAmount> { | ||
const [symbol, decimals] = await Promise.all([ | ||
erc20Rpc.getSymbol(log.address, rpcUrl), | ||
erc20Rpc.getDecimals(log.address, rpcUrl), | ||
]); | ||
return { ...log, symbol, decimals }; | ||
} | ||
function processLog(log: EnrichedLogWithoutAmount): ProcessedLog { | ||
const { topics, data, decimals, symbol, address } = log; | ||
const { 1: fromHex, 2: toHex } = topics; | ||
const from = convertHexToAddress(fromHex); | ||
const to = convertHexToAddress(toHex); | ||
const amount = formatUnits(data, decimals); | ||
return { to, from, symbol, amount, address, decimals }; | ||
} | ||
export async function enrichTxReceipt({ | ||
txReceipt, | ||
rpcUrl, | ||
}: { | ||
txReceipt: TransactionReceipt; | ||
rpcUrl: string; | ||
}): Promise<EnrichedTxReceipt> { | ||
const isERC20TransferEvent = (log: Log) => | ||
log.topics[0] === EVENT_SIGNATURES.Transfer; | ||
const filteredLogs = txReceipt.logs.filter(isERC20TransferEvent); | ||
const enrichedLogs = await Promise.all( | ||
filteredLogs.map((log) => enrichLog({ log, rpcUrl })) | ||
); | ||
const logs = enrichedLogs.map(processLog); | ||
return { logs, from: txReceipt.from }; | ||
} | ||
export function extractTokenInfo( | ||
@@ -80,0 +130,0 @@ inputLog: ProcessedLog, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1137219
13319