Skip to content

Commit

Permalink
fix(approval): add tokenId condition
Browse files Browse the repository at this point in the history
  • Loading branch information
jagnani73 committed Feb 28, 2024
1 parent e5faf91 commit b909e14
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 63 deletions.
24 changes: 24 additions & 0 deletions services/decoder/fallbacks/approval/abis/approval-erc721.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": true,
"name": "tokenId",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
}
]
195 changes: 132 additions & 63 deletions services/decoder/fallbacks/approval/approval.fallback.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { GoldRushDecoder } from "../../decoder";
import {
type EventDetails,
type EventTokens,
type EventType,
} from "../../decoder.types";
import { type EventDetails, type EventType } from "../../decoder.types";
import {
DECODED_ACTION,
DECODED_EVENT_CATEGORY,
} from "../../decoder.constants";
import { decodeEventLog, type Abi } from "viem";
import ABI from "./abis/approval.abi.json";
import ERC20ABI from "./abis/approval-erc20.abi.json";
import ERC721ABI from "./abis/approval-erc721.abi.json";
import { TimestampParser } from "../../../../utils/functions";
import { prettifyCurrency } from "@covalenthq/client-sdk";

GoldRushDecoder.fallback(
"Approval",
ABI as Abi,
ERC20ABI as Abi,
async (log_event, tx, chain_name, covalent_client): Promise<EventType> => {
const {
block_signed_at,
Expand All @@ -28,25 +25,52 @@ GoldRushDecoder.fallback(
sender_contract_decimals,
} = log_event;

const { args: decoded } = decodeEventLog({
abi: ABI,
topics: raw_log_topics as [],
data: raw_log_data as `0x${string}`,
eventName: "Approval",
}) as {
eventName: "Approval";
args: {
owner: string;
spender: string;
value: bigint;
};
};
let decoded:
| {
owner: string;
spender: string;
value: bigint;
tokenId?: never;
}
| {
owner: string;
spender: string;
tokenId: bigint;
value?: never;
};

const unlimitedValue: boolean =
decoded.value.toString() ===
"115792089237316195423570985008687907853269984665640564039457584007913129639935";
try {
const { args } = decodeEventLog({
abi: ERC20ABI,
topics: raw_log_topics as [],
data: raw_log_data as `0x${string}`,
eventName: "Approval",
}) as {
eventName: "Approval";
args: {
owner: string;
spender: string;
value: bigint;
};
};
decoded = args;
} catch (error) {
const { args } = decodeEventLog({
abi: ERC721ABI,
topics: raw_log_topics as [],
data: raw_log_data as `0x${string}`,
eventName: "Approval",
}) as {
eventName: "Approval";
args: {
owner: string;
spender: string;
tokenId: bigint;
};
};
decoded = args;
}

const tokens: EventTokens = [];
const details: EventDetails = [
{
heading: "Owner",
Expand All @@ -60,44 +84,7 @@ GoldRushDecoder.fallback(
},
];

if (unlimitedValue) {
details.push({
heading: "Value",
value: "Unlimited",
type: "text",
});
} else {
const date = TimestampParser(block_signed_at, "YYYY-MM-DD");
const { data } =
await covalent_client.PricingService.getTokenPrices(
chain_name,
"USD",
sender_address,
{
from: date,
to: date,
}
);

tokens.push({
heading: "Value",
value: decoded.value.toString(),
ticker_symbol: sender_contract_ticker_symbol,
ticker_logo: sender_logo_url,
decimals: sender_contract_decimals ?? 18,
pretty_quote: prettifyCurrency(
data?.[0]?.items?.[0]?.price *
(Number(decoded.value) /
Math.pow(
10,
data?.[0]?.items?.[0]?.contract_metadata
?.contract_decimals ?? 18
))
),
});
}

return {
const parsedData: EventType = {
action: DECODED_ACTION.SWAPPED,
category: DECODED_EVENT_CATEGORY.DEX,
name: "Approval",
Expand All @@ -106,7 +93,89 @@ GoldRushDecoder.fallback(
name: sender_name as string,
},
details: details,
tokens: tokens,
};

if (decoded.value) {
const unlimitedValue: boolean =
decoded.value.toString() ===
"115792089237316195423570985008687907853269984665640564039457584007913129639935";

if (unlimitedValue) {
details.push({
heading: "Value",
value: "Unlimited",
type: "text",
});
} else {
const date = TimestampParser(block_signed_at, "YYYY-MM-DD");
const { data } =
await covalent_client.PricingService.getTokenPrices(
chain_name,
"USD",
sender_address,
{
from: date,
to: date,
}
);

parsedData.tokens = [
{
heading: "Value",
value: decoded.value.toString(),
ticker_symbol: sender_contract_ticker_symbol,
ticker_logo: sender_logo_url,
decimals: sender_contract_decimals ?? 18,
pretty_quote: prettifyCurrency(
data?.[0]?.items?.[0]?.price *
(Number(decoded.value) /
Math.pow(
10,
data?.[0]?.items?.[0]?.contract_metadata
?.contract_decimals ?? 18
))
),
},
];
}
} else if (decoded.tokenId) {
const { data } =
await covalent_client.NftService.getNftMetadataForGivenTokenIdForContract(
chain_name,
sender_address,
decoded.tokenId.toString(),
{
withUncached: true,
}
);

parsedData.nfts = [
{
heading: "NFT Transferred",
collection_address: data?.items?.[0]?.contract_address,
collection_name:
data?.items?.[0]?.nft_data?.external_data?.name || null,
token_identifier:
data?.items?.[0]?.nft_data?.token_id?.toString() ||
null,
images: {
"1024":
data?.items?.[0]?.nft_data?.external_data
?.image_1024 || null,
"512":
data?.items?.[0]?.nft_data?.external_data
?.image_512 || null,
"256":
data?.items?.[0]?.nft_data?.external_data
?.image_256 || null,
default:
data?.items?.[0]?.nft_data?.external_data?.image ||
null,
},
},
];
}

return parsedData;
}
);
3 changes: 3 additions & 0 deletions services/decoder/fallbacks/approval/approval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ describe("fallback", () => {
if (event.tokens?.length) {
expect(event.tokens?.length).toEqual(1);
expect(event.details?.length).toEqual(2);
} else if (event.nfts?.length) {
expect(event.nfts?.length).toEqual(1);
expect(event.details?.length).toEqual(2);
} else {
expect(event.details?.length).toEqual(3);
}
Expand Down

0 comments on commit b909e14

Please sign in to comment.