Skip to content

Commit 5be349d

Browse files
committed
feat: add swap purchase infos
1 parent 419026d commit 5be349d

File tree

5 files changed

+214
-1
lines changed

5 files changed

+214
-1
lines changed

abis/AavegotchiDiamond.json

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12498,5 +12498,122 @@
1249812498
],
1249912499
"name": "ClaimedAt",
1250012500
"type": "event"
12501+
},
12502+
{
12503+
"anonymous": false,
12504+
"inputs": [
12505+
{
12506+
"indexed": true,
12507+
"internalType": "address",
12508+
"name": "tokenIn",
12509+
"type": "address"
12510+
},
12511+
{
12512+
"indexed": true,
12513+
"internalType": "address",
12514+
"name": "tokenOut",
12515+
"type": "address"
12516+
},
12517+
{
12518+
"indexed": false,
12519+
"internalType": "uint256",
12520+
"name": "amountIn",
12521+
"type": "uint256"
12522+
},
12523+
{
12524+
"indexed": false,
12525+
"internalType": "uint256",
12526+
"name": "amountOut",
12527+
"type": "uint256"
12528+
},
12529+
{
12530+
"indexed": true,
12531+
"internalType": "address",
12532+
"name": "recipient",
12533+
"type": "address"
12534+
}
12535+
],
12536+
"name": "TokenSwapped",
12537+
"type": "event"
12538+
},
12539+
{
12540+
"anonymous": false,
12541+
"inputs": [
12542+
{
12543+
"indexed": true,
12544+
"internalType": "address",
12545+
"name": "recipient",
12546+
"type": "address"
12547+
},
12548+
{
12549+
"indexed": true,
12550+
"internalType": "address",
12551+
"name": "tokenIn",
12552+
"type": "address"
12553+
},
12554+
{
12555+
"indexed": false,
12556+
"internalType": "uint256",
12557+
"name": "swapAmount",
12558+
"type": "uint256"
12559+
},
12560+
{
12561+
"indexed": false,
12562+
"internalType": "uint256",
12563+
"name": "ghstReceived",
12564+
"type": "uint256"
12565+
},
12566+
{
12567+
"indexed": false,
12568+
"internalType": "uint256",
12569+
"name": "listingId",
12570+
"type": "uint256"
12571+
},
12572+
{
12573+
"indexed": false,
12574+
"internalType": "uint256",
12575+
"name": "tokenId",
12576+
"type": "uint256"
12577+
},
12578+
{
12579+
"indexed": true,
12580+
"internalType": "address",
12581+
"name": "nftContract",
12582+
"type": "address"
12583+
}
12584+
],
12585+
"name": "SwapAndPurchase",
12586+
"type": "event"
12587+
},
12588+
{
12589+
"anonymous": false,
12590+
"inputs": [
12591+
{
12592+
"indexed": true,
12593+
"internalType": "address",
12594+
"name": "buyer",
12595+
"type": "address"
12596+
},
12597+
{
12598+
"indexed": true,
12599+
"internalType": "address",
12600+
"name": "tokenIn",
12601+
"type": "address"
12602+
},
12603+
{
12604+
"indexed": false,
12605+
"internalType": "uint256",
12606+
"name": "ghstReceived",
12607+
"type": "uint256"
12608+
},
12609+
{
12610+
"indexed": true,
12611+
"internalType": "uint256",
12612+
"name": "listingId",
12613+
"type": "uint256"
12614+
}
12615+
],
12616+
"name": "SwapAndPurchaseERC1155",
12617+
"type": "event"
1250112618
}
1250212619
]

schema.graphql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ type ERC721Listing @entity {
249249
fakeGotchi_artist: User
250250
fakeGotchi_artistName: String
251251
fakeGotchi_editions: Int
252+
253+
# Swap metadata when purchased via swap-and-buy
254+
purchasedWithSwap: Boolean
255+
swapTokenIn: Bytes
256+
swapAmountIn: BigInt
257+
swapGhstReceived: BigInt
252258
}
253259

254260
type ERC1155Listing @entity {
@@ -293,6 +299,12 @@ type ERC1155Purchase @entity {
293299
timeLastPurchased: BigInt! # Time when the listing was purchased
294300
#sold: Boolean!
295301
rarityLevel: BigInt
302+
303+
# Swap metadata when purchased via swap-and-buy
304+
purchasedWithSwap: Boolean
305+
swapTokenIn: Bytes
306+
swapAmountIn: BigInt
307+
swapGhstReceived: BigInt
296308
}
297309

298310
type Statistic @entity {
@@ -539,6 +551,22 @@ type ERC1155BuyOrderExecution @entity {
539551
executedAt: BigInt
540552
}
541553

554+
# Swap audit trail
555+
type SwapAction @entity {
556+
id: ID!
557+
tokenIn: Bytes!
558+
tokenOut: Bytes!
559+
amountIn: BigInt!
560+
amountOut: BigInt!
561+
recipient: Bytes!
562+
createdAt: BigInt!
563+
txHash: Bytes!
564+
listingId: BigInt
565+
erc1155ListingId: BigInt
566+
tokenId: BigInt
567+
nftContract: Bytes
568+
}
569+
542570
# Cards
543571
type FakeGotchiCardBalance @entity {
544572
id: ID!

src/mappings/baseDiamond.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ import {
7171
ResyncAavegotchis,
7272
ClaimedAt,
7373
EscrowUpdated,
74+
SwapAndPurchase,
75+
SwapAndPurchaseERC1155,
76+
TokenSwapped,
7477
} from "../../generated/AavegotchiDiamond/AavegotchiDiamond";
7578
import {
7679
getOrCreateUser,
@@ -118,7 +121,13 @@ import {
118121
} from "../utils/constants";
119122
import { Address, BigInt, log, Bytes } from "@graphprotocol/graph-ts";
120123

121-
import { Parcel, TokenCommitment, ERC721Listing } from "../../generated/schema";
124+
import {
125+
Parcel,
126+
TokenCommitment,
127+
ERC721Listing,
128+
ERC1155Purchase,
129+
SwapAction,
130+
} from "../../generated/schema";
122131

123132
import { updatePermissionsFromBitmap } from "../utils/decimals";
124133
import * as erc7589 from "./erc-7589";
@@ -1308,6 +1317,53 @@ export function handleERC721ExecutedToRecipient(
13081317
listing.save();
13091318
}
13101319

1320+
// Swap-and-buy helpers
1321+
export function handleSwapAndPurchase(event: SwapAndPurchase): void {
1322+
let listing = getOrCreateERC721Listing(event.params.listingId.toString());
1323+
listing = updateERC721ListingInfo(listing, event.params.listingId, event);
1324+
1325+
listing.purchasedWithSwap = true;
1326+
listing.swapTokenIn = event.params.tokenIn;
1327+
listing.swapAmountIn = event.params.swapAmount;
1328+
listing.swapGhstReceived = event.params.ghstReceived;
1329+
1330+
listing.save();
1331+
}
1332+
1333+
export function handleSwapAndPurchaseERC1155(
1334+
event: SwapAndPurchaseERC1155
1335+
): void {
1336+
// Purchase id mirrors the id created in handleERC1155ExecutedListing
1337+
let purchaseID =
1338+
event.params.listingId.toString() +
1339+
"_" +
1340+
event.params.buyer.toHexString() +
1341+
"_" +
1342+
event.block.timestamp.toString();
1343+
1344+
let purchase = ERC1155Purchase.load(purchaseID);
1345+
if (purchase) {
1346+
purchase.purchasedWithSwap = true;
1347+
purchase.swapTokenIn = event.params.tokenIn;
1348+
// swapAmountIn not available in this event; leave null
1349+
purchase.swapGhstReceived = event.params.ghstReceived;
1350+
purchase.save();
1351+
}
1352+
}
1353+
1354+
export function handleTokenSwapped(event: TokenSwapped): void {
1355+
const id = event.transaction.hash.toHex() + "-" + event.logIndex.toString();
1356+
let action = new SwapAction(id);
1357+
action.tokenIn = event.params.tokenIn;
1358+
action.tokenOut = event.params.tokenOut;
1359+
action.amountIn = event.params.amountIn;
1360+
action.amountOut = event.params.amountOut;
1361+
action.recipient = event.params.recipient;
1362+
action.createdAt = event.block.timestamp;
1363+
action.txHash = event.transaction.hash;
1364+
action.save();
1365+
}
1366+
13111367
export function handleWhitelistAccessRightSet(
13121368
event: WhitelistAccessRightSet
13131369
): void {

subgraph.base.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ dataSources:
174174
handler: handleClaimedAt
175175
- event: EscrowUpdated(indexed uint256,address)
176176
handler: handleEscrowUpdated
177+
- event: SwapAndPurchase(indexed address,indexed address,uint256,uint256,uint256,uint256,indexed address)
178+
handler: handleSwapAndPurchase
179+
- event: SwapAndPurchaseERC1155(indexed address,indexed address,uint256,indexed uint256)
180+
handler: handleSwapAndPurchaseERC1155
181+
- event: TokenSwapped(indexed address,indexed address,uint256,uint256,indexed address)
182+
handler: handleTokenSwapped
177183

178184
- kind: ethereum/contract
179185
name: RealmDiamond

subgraph.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ dataSources:
174174
handler: handleClaimedAt
175175
- event: EscrowUpdated(indexed uint256,address)
176176
handler: handleEscrowUpdated
177+
- event: SwapAndPurchase(indexed address,indexed address,uint256,uint256,uint256,uint256,indexed address)
178+
handler: handleSwapAndPurchase
179+
- event: SwapAndPurchaseERC1155(indexed address,indexed address,uint256,indexed uint256)
180+
handler: handleSwapAndPurchaseERC1155
181+
- event: TokenSwapped(indexed address,indexed address,uint256,uint256,indexed address)
182+
handler: handleTokenSwapped
177183

178184
- kind: ethereum/contract
179185
name: RealmDiamond

0 commit comments

Comments
 (0)