Skip to content

Commit 8a29382

Browse files
authored
fix(approval): add tokenId condition (#31)
1 parent ab5ec7a commit 8a29382

File tree

4 files changed

+159
-63
lines changed

4 files changed

+159
-63
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"anonymous": false,
4+
"inputs": [
5+
{
6+
"indexed": true,
7+
"name": "owner",
8+
"type": "address"
9+
},
10+
{
11+
"indexed": true,
12+
"name": "spender",
13+
"type": "address"
14+
},
15+
{
16+
"indexed": true,
17+
"name": "tokenId",
18+
"type": "uint256"
19+
}
20+
],
21+
"name": "Approval",
22+
"type": "event"
23+
}
24+
]
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import { GoldRushDecoder } from "../../decoder";
2-
import {
3-
type EventDetails,
4-
type EventTokens,
5-
type EventType,
6-
} from "../../decoder.types";
2+
import { type EventDetails, type EventType } from "../../decoder.types";
73
import {
84
DECODED_ACTION,
95
DECODED_EVENT_CATEGORY,
106
} from "../../decoder.constants";
117
import { decodeEventLog, type Abi } from "viem";
12-
import ABI from "./abis/approval.abi.json";
8+
import ERC20ABI from "./abis/approval-erc20.abi.json";
9+
import ERC721ABI from "./abis/approval-erc721.abi.json";
1310
import { TimestampParser } from "../../../../utils/functions";
1411
import { prettifyCurrency } from "@covalenthq/client-sdk";
1512

1613
GoldRushDecoder.fallback(
1714
"Approval",
18-
ABI as Abi,
15+
ERC20ABI as Abi,
1916
async (log_event, tx, chain_name, covalent_client): Promise<EventType> => {
2017
const {
2118
block_signed_at,
@@ -28,25 +25,52 @@ GoldRushDecoder.fallback(
2825
sender_contract_decimals,
2926
} = log_event;
3027

31-
const { args: decoded } = decodeEventLog({
32-
abi: ABI,
33-
topics: raw_log_topics as [],
34-
data: raw_log_data as `0x${string}`,
35-
eventName: "Approval",
36-
}) as {
37-
eventName: "Approval";
38-
args: {
39-
owner: string;
40-
spender: string;
41-
value: bigint;
42-
};
43-
};
28+
let decoded:
29+
| {
30+
owner: string;
31+
spender: string;
32+
value: bigint;
33+
tokenId?: never;
34+
}
35+
| {
36+
owner: string;
37+
spender: string;
38+
tokenId: bigint;
39+
value?: never;
40+
};
4441

45-
const unlimitedValue: boolean =
46-
decoded.value.toString() ===
47-
"115792089237316195423570985008687907853269984665640564039457584007913129639935";
42+
try {
43+
const { args } = decodeEventLog({
44+
abi: ERC20ABI,
45+
topics: raw_log_topics as [],
46+
data: raw_log_data as `0x${string}`,
47+
eventName: "Approval",
48+
}) as {
49+
eventName: "Approval";
50+
args: {
51+
owner: string;
52+
spender: string;
53+
value: bigint;
54+
};
55+
};
56+
decoded = args;
57+
} catch (error) {
58+
const { args } = decodeEventLog({
59+
abi: ERC721ABI,
60+
topics: raw_log_topics as [],
61+
data: raw_log_data as `0x${string}`,
62+
eventName: "Approval",
63+
}) as {
64+
eventName: "Approval";
65+
args: {
66+
owner: string;
67+
spender: string;
68+
tokenId: bigint;
69+
};
70+
};
71+
decoded = args;
72+
}
4873

49-
const tokens: EventTokens = [];
5074
const details: EventDetails = [
5175
{
5276
heading: "Owner",
@@ -60,44 +84,7 @@ GoldRushDecoder.fallback(
6084
},
6185
];
6286

63-
if (unlimitedValue) {
64-
details.push({
65-
heading: "Value",
66-
value: "Unlimited",
67-
type: "text",
68-
});
69-
} else {
70-
const date = TimestampParser(block_signed_at, "YYYY-MM-DD");
71-
const { data } =
72-
await covalent_client.PricingService.getTokenPrices(
73-
chain_name,
74-
"USD",
75-
sender_address,
76-
{
77-
from: date,
78-
to: date,
79-
}
80-
);
81-
82-
tokens.push({
83-
heading: "Value",
84-
value: decoded.value.toString(),
85-
ticker_symbol: sender_contract_ticker_symbol,
86-
ticker_logo: sender_logo_url,
87-
decimals: sender_contract_decimals ?? 18,
88-
pretty_quote: prettifyCurrency(
89-
data?.[0]?.items?.[0]?.price *
90-
(Number(decoded.value) /
91-
Math.pow(
92-
10,
93-
data?.[0]?.items?.[0]?.contract_metadata
94-
?.contract_decimals ?? 18
95-
))
96-
),
97-
});
98-
}
99-
100-
return {
87+
const parsedData: EventType = {
10188
action: DECODED_ACTION.SWAPPED,
10289
category: DECODED_EVENT_CATEGORY.DEX,
10390
name: "Approval",
@@ -106,7 +93,89 @@ GoldRushDecoder.fallback(
10693
name: sender_name as string,
10794
},
10895
details: details,
109-
tokens: tokens,
11096
};
97+
98+
if (decoded.value) {
99+
const unlimitedValue: boolean =
100+
decoded.value.toString() ===
101+
"115792089237316195423570985008687907853269984665640564039457584007913129639935";
102+
103+
if (unlimitedValue) {
104+
details.push({
105+
heading: "Value",
106+
value: "Unlimited",
107+
type: "text",
108+
});
109+
} else {
110+
const date = TimestampParser(block_signed_at, "YYYY-MM-DD");
111+
const { data } =
112+
await covalent_client.PricingService.getTokenPrices(
113+
chain_name,
114+
"USD",
115+
sender_address,
116+
{
117+
from: date,
118+
to: date,
119+
}
120+
);
121+
122+
parsedData.tokens = [
123+
{
124+
heading: "Value",
125+
value: decoded.value.toString(),
126+
ticker_symbol: sender_contract_ticker_symbol,
127+
ticker_logo: sender_logo_url,
128+
decimals: sender_contract_decimals ?? 18,
129+
pretty_quote: prettifyCurrency(
130+
data?.[0]?.items?.[0]?.price *
131+
(Number(decoded.value) /
132+
Math.pow(
133+
10,
134+
data?.[0]?.items?.[0]?.contract_metadata
135+
?.contract_decimals ?? 18
136+
))
137+
),
138+
},
139+
];
140+
}
141+
} else if (decoded.tokenId) {
142+
const { data } =
143+
await covalent_client.NftService.getNftMetadataForGivenTokenIdForContract(
144+
chain_name,
145+
sender_address,
146+
decoded.tokenId.toString(),
147+
{
148+
withUncached: true,
149+
}
150+
);
151+
152+
parsedData.nfts = [
153+
{
154+
heading: "NFT Transferred",
155+
collection_address: data?.items?.[0]?.contract_address,
156+
collection_name:
157+
data?.items?.[0]?.nft_data?.external_data?.name || null,
158+
token_identifier:
159+
data?.items?.[0]?.nft_data?.token_id?.toString() ||
160+
null,
161+
images: {
162+
"1024":
163+
data?.items?.[0]?.nft_data?.external_data
164+
?.image_1024 || null,
165+
"512":
166+
data?.items?.[0]?.nft_data?.external_data
167+
?.image_512 || null,
168+
"256":
169+
data?.items?.[0]?.nft_data?.external_data
170+
?.image_256 || null,
171+
default:
172+
data?.items?.[0]?.nft_data?.external_data?.image ||
173+
null,
174+
},
175+
},
176+
];
177+
}
178+
179+
return parsedData;
111180
}
112181
);

services/decoder/fallbacks/approval/approval.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ describe("fallback", () => {
2020
if (event.tokens?.length) {
2121
expect(event.tokens?.length).toEqual(1);
2222
expect(event.details?.length).toEqual(2);
23+
} else if (event.nfts?.length) {
24+
expect(event.nfts?.length).toEqual(1);
25+
expect(event.details?.length).toEqual(2);
2326
} else {
2427
expect(event.details?.length).toEqual(3);
2528
}

0 commit comments

Comments
 (0)