Skip to content

Commit

Permalink
Adds support for multiple ABIs for ERC20 Transfer events.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrés Elizondo committed May 29, 2024
1 parent 7530f96 commit ae7981c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ export const STAKING_REWARDS_PAID_ABI = {
type: 'event',
};

export const ERC20_TRANSFER_ABI = {
export const STANDARD_ERC20_TRANSFER_ABI = {
anonymous: false,
inputs: [
{
Expand Down
26 changes: 16 additions & 10 deletions src/parsers/events/erc20_transfer_events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERC20_TRANSFER_ABI } from '../../constants';
import { STANDARD_ERC20_TRANSFER_ABI } from '../../constants';
import { ERC20TransferEvent } from '../../entities';
import { parseEvent } from './parse_event';
import { BigNumber } from '@0x/utils';
Expand All @@ -13,17 +13,23 @@ export function parseERC20TransferEvent(eventLog: LogEntry): ERC20TransferEvent

parseEvent(eventLog, eRC20TransferEvent);

// ERC20 Transfers have 3 topics and 32 bytes of data
// ERC721 Transfers have 4 topics and no data
if (eventLog.topics.length !== 3) {
if (eventLog.topics.length === 4 && eventLog.data === '0x') {
// ERC721 Transfer - 4 topics, no data
return null;
} else if (eventLog.topics.length === 3 && eventLog.data !== '0x') {
// Standard ERC20 Transfer - 3 topics, 32 bytes of data
const decodedLog = abiCoder.decodeLog(
STANDARD_ERC20_TRANSFER_ABI.inputs,
eventLog.data,
eventLog.topics.slice(1),
);

eRC20TransferEvent.from = decodedLog.from.toLowerCase();
eRC20TransferEvent.to = decodedLog.to.toLowerCase();
eRC20TransferEvent.value = new BigNumber(decodedLog.value);
} else {
return null;
}

const decodedLog = abiCoder.decodeLog(ERC20_TRANSFER_ABI.inputs, eventLog.data, eventLog.topics.slice(1));

eRC20TransferEvent.from = decodedLog.from.toLowerCase();
eRC20TransferEvent.to = decodedLog.to.toLowerCase();
eRC20TransferEvent.value = new BigNumber(decodedLog.value);

return eRC20TransferEvent;
}

0 comments on commit ae7981c

Please sign in to comment.