diff --git a/abis/AavegotchiDiamond.json b/abis/AavegotchiDiamond.json index 7966371..524d65e 100644 --- a/abis/AavegotchiDiamond.json +++ b/abis/AavegotchiDiamond.json @@ -9857,5 +9857,510 @@ ], "name": "ERC1155ListingPriceUpdate", "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "components": [ + { + "internalType": "uint32", + "name": "listingId", + "type": "uint32" + }, + { + "internalType": "address", + "name": "lender", + "type": "address" + }, + { + "internalType": "uint32", + "name": "tokenId", + "type": "uint32" + }, + { + "internalType": "uint96", + "name": "initialCost", + "type": "uint96" + }, + { + "internalType": "uint32", + "name": "period", + "type": "uint32" + }, + { + "internalType": "uint8[3]", + "name": "revenueSplit", + "type": "uint8[3]" + }, + { + "internalType": "address", + "name": "originalOwner", + "type": "address" + }, + { + "internalType": "address", + "name": "thirdParty", + "type": "address" + }, + { + "internalType": "uint32", + "name": "whitelistId", + "type": "uint32" + }, + { + "internalType": "address[]", + "name": "revenueTokens", + "type": "address[]" + }, + { + "internalType": "uint256", + "name": "timeCreated", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "permissions", + "type": "uint256" + } + ], + "indexed": false, + "internalType": "struct LibEventStructContainers.GotchiLendingAdd", + "name": "", + "type": "tuple" + } + ], + "name": "GotchiLendingAdded", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "components": [ + { + "internalType": "uint32", + "name": "listingId", + "type": "uint32" + }, + { + "internalType": "address", + "name": "lender", + "type": "address" + }, + { + "internalType": "address", + "name": "borrower", + "type": "address" + }, + { + "internalType": "uint32", + "name": "tokenId", + "type": "uint32" + }, + { + "internalType": "uint96", + "name": "initialCost", + "type": "uint96" + }, + { + "internalType": "uint32", + "name": "period", + "type": "uint32" + }, + { + "internalType": "uint8[3]", + "name": "revenueSplit", + "type": "uint8[3]" + }, + { + "internalType": "address", + "name": "originalOwner", + "type": "address" + }, + { + "internalType": "address", + "name": "thirdParty", + "type": "address" + }, + { + "internalType": "uint32", + "name": "whitelistId", + "type": "uint32" + }, + { + "internalType": "address[]", + "name": "revenueTokens", + "type": "address[]" + }, + { + "internalType": "uint256", + "name": "timeAgreed", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "permissions", + "type": "uint256" + } + ], + "indexed": false, + "internalType": "struct LibEventStructContainers.GotchiLendingExecution", + "name": "", + "type": "tuple" + } + ], + "name": "GotchiLendingExecuted", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "components": [ + { + "internalType": "uint32", + "name": "listingId", + "type": "uint32" + }, + { + "internalType": "address", + "name": "lender", + "type": "address" + }, + { + "internalType": "uint32", + "name": "tokenId", + "type": "uint32" + }, + { + "internalType": "uint96", + "name": "initialCost", + "type": "uint96" + }, + { + "internalType": "uint32", + "name": "period", + "type": "uint32" + }, + { + "internalType": "uint8[3]", + "name": "revenueSplit", + "type": "uint8[3]" + }, + { + "internalType": "address", + "name": "originalOwner", + "type": "address" + }, + { + "internalType": "address", + "name": "thirdParty", + "type": "address" + }, + { + "internalType": "uint32", + "name": "whitelistId", + "type": "uint32" + }, + { + "internalType": "address[]", + "name": "revenueTokens", + "type": "address[]" + }, + { + "internalType": "uint256", + "name": "timeCancelled", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "permissions", + "type": "uint256" + } + ], + "indexed": false, + "internalType": "struct LibEventStructContainers.GotchiLendingCancellation", + "name": "", + "type": "tuple" + } + ], + "name": "GotchiLendingCancelled", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "components": [ + { + "internalType": "uint32", + "name": "listingId", + "type": "uint32" + }, + { + "internalType": "address", + "name": "lender", + "type": "address" + }, + { + "internalType": "address", + "name": "borrower", + "type": "address" + }, + { + "internalType": "uint32", + "name": "tokenId", + "type": "uint32" + }, + { + "internalType": "uint96", + "name": "initialCost", + "type": "uint96" + }, + { + "internalType": "uint32", + "name": "period", + "type": "uint32" + }, + { + "internalType": "uint8[3]", + "name": "revenueSplit", + "type": "uint8[3]" + }, + { + "internalType": "address", + "name": "originalOwner", + "type": "address" + }, + { + "internalType": "address", + "name": "thirdParty", + "type": "address" + }, + { + "internalType": "uint32", + "name": "whitelistId", + "type": "uint32" + }, + { + "internalType": "address[]", + "name": "revenueTokens", + "type": "address[]" + }, + { + "internalType": "uint256[]", + "name": "amounts", + "type": "uint256[]" + }, + { + "internalType": "uint256", + "name": "timeClaimed", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "permissions", + "type": "uint256" + } + ], + "indexed": false, + "internalType": "struct LibEventStructContainers.GotchiLendingClaim", + "name": "", + "type": "tuple" + } + ], + "name": "GotchiLendingClaimed", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "components": [ + { + "internalType": "uint32", + "name": "listingId", + "type": "uint32" + }, + { + "internalType": "address", + "name": "lender", + "type": "address" + }, + { + "internalType": "address", + "name": "borrower", + "type": "address" + }, + { + "internalType": "uint32", + "name": "tokenId", + "type": "uint32" + }, + { + "internalType": "uint96", + "name": "initialCost", + "type": "uint96" + }, + { + "internalType": "uint32", + "name": "period", + "type": "uint32" + }, + { + "internalType": "uint8[3]", + "name": "revenueSplit", + "type": "uint8[3]" + }, + { + "internalType": "address", + "name": "originalOwner", + "type": "address" + }, + { + "internalType": "address", + "name": "thirdParty", + "type": "address" + }, + { + "internalType": "uint32", + "name": "whitelistId", + "type": "uint32" + }, + { + "internalType": "address[]", + "name": "revenueTokens", + "type": "address[]" + }, + { + "internalType": "uint256", + "name": "timeEnded", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "permissions", + "type": "uint256" + } + ], + "indexed": false, + "internalType": "struct LibEventStructContainers.GotchiLendingEnd", + "name": "", + "type": "tuple" + } + ], + "name": "GotchiLendingEnded", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "buyOrderId", + "type": "uint256" + }, + { + "indexed": true, + "internalType": "address", + "name": "buyer", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "erc721TokenAddress", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "erc721TokenId", + "type": "uint256" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "category", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "priceInWei", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "duration", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "bytes32", + "name": "validationHash", + "type": "bytes32" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "time", + "type": "uint256" + } + ], + "name": "ERC721BuyOrderAdded", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "buyOrderId", + "type": "uint256" + }, + { + "indexed": true, + "internalType": "address", + "name": "buyer", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "seller", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "erc721TokenAddress", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "erc721TokenId", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "priceInWei", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "time", + "type": "uint256" + } + ], + "name": "ERC721BuyOrderExecuted", + "type": "event" } ] diff --git a/abis/RealmDiamond.json b/abis/RealmDiamond.json index d4a2e59..076c3c7 100644 --- a/abis/RealmDiamond.json +++ b/abis/RealmDiamond.json @@ -1,4 +1,23 @@ [ + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "_tokenId", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "_value", + "type": "uint256" + } + ], + "name": "KinshipBurned", + "type": "event" + }, { "anonymous": false, "inputs": [ diff --git a/schema.graphql b/schema.graphql index 93654fe..834fb71 100644 --- a/schema.graphql +++ b/schema.graphql @@ -368,6 +368,8 @@ type GotchiLending @entity { timeEnded: BigInt claimed: [ClaimedToken!]! @derivedFrom(field: "lending") + + channellingAllowed: Boolean! } type ClaimedToken @entity { @@ -392,3 +394,17 @@ type Whitelist @entity { # id: ID! # calldata: #} +## buy order +type ERC721BuyOrder @entity { + id: ID! + buyer: Bytes! + seller: Bytes + erc721TokenAddress: Bytes! + erc721TokenId: BigInt! + category: BigInt! + priceInWei: BigInt! + duration: BigInt! + validationHash: Bytes! + createdAt: BigInt! + executedAt: BigInt +} diff --git a/src/mappings/diamond.ts b/src/mappings/diamond.ts index 66bf532..33edc70 100644 --- a/src/mappings/diamond.ts +++ b/src/mappings/diamond.ts @@ -48,6 +48,10 @@ import { GotchiLendingExecuted, GotchiLendingCanceled, GotchiLendingClaimed, + GotchiLendingAdded1, + GotchiLendingExecuted1, + GotchiLendingCancelled, + GotchiLendingClaimed1, GotchiLendingAdded, WhitelistAccessRightSet, WhitelistOwnershipTransferred, @@ -56,6 +60,9 @@ import { ERC1155ListingWhitelistSet, ERC721ListingPriceUpdate, ERC1155ListingPriceUpdate, + GotchiLendingEnded1, + ERC721BuyOrderAdded, + ERC721BuyOrderExecuted, } from "../../generated/AavegotchiDiamond/AavegotchiDiamond"; import { getOrCreateUser, @@ -80,6 +87,8 @@ import { createOrUpdateWhitelist, getOrCreateClaimedToken, getOrCreateWhitelist, + itemMaxQuantityToRarity, + getOrCreateERC721BuyOrder, } from "../utils/helpers/diamond"; import { BIGINT_ONE, @@ -97,7 +106,9 @@ import { RealmDiamond, MintParcel, ResyncParcel, + KinshipBurned, } from "../../generated/RealmDiamond/RealmDiamond"; +import { updatePermissionsFromBitmap } from "../utils/decimals"; export function handleBuyPortals(event: BuyPortals): void { let contract = AavegotchiDiamond.bind(event.address); @@ -1144,6 +1155,43 @@ export function handleERC1155ExecutedToRecipient( purchase.buyer = event.params.buyer; purchase.recipient = event.params.recipient; + purchase.listingID = event.params.listingId; + + // listing + let listing = getOrCreateERC1155Listing(event.params.listingId.toString()); + purchase.category = listing.category; + purchase.erc1155TokenAddress = listing.erc1155TokenAddress; + purchase.erc1155TypeId = listing.erc1155TypeId; + purchase.quantity = BIGINT_ONE; + purchase.priceInWei = listing.priceInWei; + purchase.timeLastPurchased = event.block.timestamp; + purchase.category = listing.category; + purchase.erc1155TokenAddress = listing.erc1155TokenAddress; + purchase.erc1155TypeId = listing.erc1155TypeId; + purchase.seller = listing.seller; + purchase.timeLastPurchased = event.block.timestamp; + purchase.priceInWei = listing.priceInWei; + purchase.quantity = purchase.quantity + ? purchase.quantity.plus(BIGINT_ONE) + : BIGINT_ONE; + + //tickets + if (listing.category.equals(BigInt.fromI32(3))) { + let rarityLevel = listing.erc1155TypeId; + purchase.rarityLevel = rarityLevel; + + //items + } else { + let itemType = getOrCreateItemType( + purchase.erc1155TypeId.toString(), + false + ); + + if (itemType) { + listing.rarityLevel = itemMaxQuantityToRarity(itemType.maxQuantity); + } + } + purchase.save(); } @@ -1453,3 +1501,303 @@ export function handleERC721ListingPriceUpdate( listing.priceUpdatedAt = event.params.time; listing.save(); } + +export function handleGotchiLendingAdded2(event: GotchiLendingAdded1): void { + let lending = getOrCreateGotchiLending(event.params.param0.listingId); + lending = updatePermissionsFromBitmap( + lending, + event.params.param0.permissions + ); + lending.upfrontCost = event.params.param0.initialCost; + lending.rentDuration = event.params.param0.period; + lending.lender = event.params.param0.lender; + lending.originalOwner = event.params.param0.originalOwner; + lending.period = event.params.param0.period; + lending.splitOwner = BigInt.fromI32(event.params.param0.revenueSplit[0]); + lending.splitBorrower = BigInt.fromI32(event.params.param0.revenueSplit[1]); + lending.splitOther = BigInt.fromI32(event.params.param0.revenueSplit[2]); + lending.tokensToShare = event.params.param0.revenueTokens.map( + (e) => e + ); + lending.thirdPartyAddress = event.params.param0.thirdParty; + lending.timeCreated = event.params.param0.timeCreated; + lending.cancelled = false; + lending.completed = false; + if (event.params.param0.whitelistId != BIGINT_ZERO) { + let whitelist = getOrCreateWhitelist( + event.params.param0.whitelistId, + event + ); + if (whitelist) { + lending.whitelist = whitelist.id; + lending.whitelistMembers = whitelist.members; + lending.whitelistId = event.params.param0.whitelistId; + } + } + let gotchi = getOrCreateAavegotchi( + event.params.param0.tokenId.toString(), + event + )!; + lending.gotchi = gotchi.id; + lending.gotchiTokenId = event.params.param0.tokenId; + lending.gotchiKinship = gotchi.kinship; + lending.gotchiBRS = gotchi.withSetsRarityScore; + + lending.save(); +} + +export function handleGotchiLendingExecuted2( + event: GotchiLendingExecuted1 +): void { + let lending = getOrCreateGotchiLending(event.params.param0.listingId); + lending = updatePermissionsFromBitmap( + lending, + event.params.param0.permissions + ); + lending.upfrontCost = event.params.param0.initialCost; + lending.lender = event.params.param0.lender; + lending.originalOwner = event.params.param0.originalOwner; + lending.period = event.params.param0.period; + lending.splitOwner = BigInt.fromI32(event.params.param0.revenueSplit[0]); + lending.splitBorrower = BigInt.fromI32(event.params.param0.revenueSplit[1]); + lending.splitOther = BigInt.fromI32(event.params.param0.revenueSplit[2]); + lending.tokensToShare = event.params.param0.revenueTokens.map( + (e) => e + ); + lending.thirdPartyAddress = event.params.param0.thirdParty; + lending.gotchiTokenId = event.params.param0.tokenId; + lending.timeAgreed = event.params.param0.timeAgreed; + lending.cancelled = false; + lending.completed = false; + lending.borrower = event.params.param0.borrower; + if (event.params.param0.whitelistId != BIGINT_ZERO) { + let whitelist = getOrCreateWhitelist( + event.params.param0.whitelistId, + event + ); + if (whitelist) { + lending.whitelist = whitelist.id; + lending.whitelistMembers = whitelist.members; + lending.whitelistId = event.params.param0.whitelistId; + } + } + lending.save(); + + // update originalOwner to lender + let gotchi = getOrCreateAavegotchi(lending.gotchi, event)!; + let lender = getOrCreateUser(lending.lender!.toHexString()); + gotchi.originalOwner = lender.id; + lender.save(); + gotchi.save(); + + let originalOwner = getOrCreateUser(lending.lender!.toHexString()); + let gotchisLentOut = originalOwner.gotchisLentOut; + gotchisLentOut.push(lending.gotchiTokenId); + originalOwner.gotchisLentOut = gotchisLentOut; + originalOwner.save(); + + let borrower = getOrCreateUser(lending.borrower!.toHexString()); + let gotchisBorrowed = borrower.gotchisBorrowed; + gotchisBorrowed.push(lending.gotchiTokenId); + borrower.gotchisBorrowed = gotchisBorrowed; + borrower.save(); + + // update stats + let stats = getStatisticEntity(); + stats.aavegotchisBorrowed = stats.aavegotchisBorrowed.plus(BIGINT_ONE); + stats.save(); +} + +export function handleGotchiLendingCancelled2( + event: GotchiLendingCancelled +): void { + let lending = getOrCreateGotchiLending(event.params.param0.listingId); + lending = updatePermissionsFromBitmap( + lending, + event.params.param0.permissions + ); + lending.upfrontCost = event.params.param0.initialCost; + lending.lender = event.params.param0.lender; + lending.originalOwner = event.params.param0.originalOwner; + lending.period = event.params.param0.period; + lending.splitOwner = BigInt.fromI32(event.params.param0.revenueSplit[0]); + lending.splitBorrower = BigInt.fromI32(event.params.param0.revenueSplit[1]); + lending.splitOther = BigInt.fromI32(event.params.param0.revenueSplit[2]); + lending.tokensToShare = event.params.param0.revenueTokens.map( + (e) => e + ); + lending.thirdPartyAddress = event.params.param0.thirdParty; + lending.gotchiTokenId = event.params.param0.tokenId; + lending.cancelled = true; + lending.completed = false; + if (event.params.param0.whitelistId != BIGINT_ZERO) { + let whitelist = getOrCreateWhitelist( + event.params.param0.whitelistId, + event + ); + if (whitelist) { + lending.whitelist = whitelist.id; + lending.whitelistMembers = whitelist.members; + lending.whitelistId = event.params.param0.whitelistId; + } + } + lending.save(); +} + +export function handleGotchiLendingClaimed2( + event: GotchiLendingClaimed1 +): void { + let lending = getOrCreateGotchiLending(event.params.param0.listingId); + lending = updatePermissionsFromBitmap( + lending, + event.params.param0.permissions + ); + for (let i = 0; i < event.params.param0.revenueTokens.length; i++) { + let ctoken = getOrCreateClaimedToken( + event.params.param0.revenueTokens[i], + lending + ); + ctoken.amount = ctoken.amount.plus(event.params.param0.amounts[i]); + ctoken.save(); + } + lending.upfrontCost = event.params.param0.initialCost; + lending.lender = event.params.param0.lender; + lending.originalOwner = event.params.param0.originalOwner; + lending.period = event.params.param0.period; + lending.splitOwner = BigInt.fromI32(event.params.param0.revenueSplit[0]); + lending.splitBorrower = BigInt.fromI32(event.params.param0.revenueSplit[1]); + lending.splitOther = BigInt.fromI32(event.params.param0.revenueSplit[2]); + lending.tokensToShare = event.params.param0.revenueTokens.map( + (e) => e + ); + lending.thirdPartyAddress = event.params.param0.thirdParty; + lending.lastClaimed = event.params.param0.timeClaimed; + lending.gotchiTokenId = event.params.param0.tokenId; + lending.borrower = event.params.param0.borrower; + lending.cancelled = false; + lending.completed = false; + if (event.params.param0.whitelistId != BIGINT_ZERO) { + let whitelist = getOrCreateWhitelist( + event.params.param0.whitelistId, + event + ); + if (whitelist) { + lending.whitelist = whitelist.id; + lending.whitelistMembers = whitelist.members; + lending.whitelistId = event.params.param0.whitelistId; + } + } + lending.save(); +} + +export function handleGotchiLendingEnded2(event: GotchiLendingEnded1): void { + let lending = getOrCreateGotchiLending(event.params.param0.listingId); + lending = updatePermissionsFromBitmap( + lending, + event.params.param0.permissions + ); + lending.upfrontCost = event.params.param0.initialCost; + lending.lender = event.params.param0.lender; + lending.originalOwner = event.params.param0.originalOwner; + lending.period = event.params.param0.period; + lending.splitOwner = BigInt.fromI32(event.params.param0.revenueSplit[0]); + lending.splitBorrower = BigInt.fromI32(event.params.param0.revenueSplit[1]); + lending.splitOther = BigInt.fromI32(event.params.param0.revenueSplit[2]); + lending.tokensToShare = event.params.param0.revenueTokens.map( + (e) => e + ); + lending.thirdPartyAddress = event.params.param0.thirdParty; + lending.gotchiTokenId = event.params.param0.tokenId; + lending.completed = true; + lending.timeEnded = event.block.timestamp; + if (event.params.param0.whitelistId != BIGINT_ZERO) { + let whitelist = getOrCreateWhitelist( + event.params.param0.whitelistId, + event + ); + if (whitelist) { + lending.whitelist = whitelist.id; + lending.whitelistMembers = whitelist.members; + lending.whitelistId = event.params.param0.whitelistId; + } + } + lending.save(); + + // remove gotchi from originalOwner gotchisLentout + let originalOwner = getOrCreateUser(lending.lender!.toHexString()); + if (originalOwner.gotchisLentOut.length > 0) { + let newGotchiLentOut = new Array(); + + for (let i = 0; i < originalOwner.gotchisLentOut.length; i++) { + let gotchiId = originalOwner.gotchisLentOut[i]; + if (!gotchiId.equals(lending.gotchiTokenId)) { + newGotchiLentOut.push(gotchiId); + } + } + originalOwner.gotchisLentOut = newGotchiLentOut; + originalOwner.save(); + } + + // remove gotchi from borrower gotchis borrowed + let borrower = getOrCreateUser(lending.borrower!.toHexString()); + if (borrower.gotchisBorrowed.length > 0) { + let newGotchiLentOut = new Array(); + + for (let i = 0; i < borrower.gotchisBorrowed.length; i++) { + let gotchiId = borrower.gotchisBorrowed[i]; + if (!gotchiId.equals(lending.gotchiTokenId)) { + newGotchiLentOut.push(gotchiId); + } + } + borrower.gotchisBorrowed = newGotchiLentOut; + borrower.save(); + } + + let gotchi = getOrCreateAavegotchi( + lending.gotchiTokenId.toString(), + event + )!; + gotchi.lending = null; + gotchi.originalOwner = originalOwner.id; + gotchi.save(); + + // update Stats + let stats = getStatisticEntity(); + stats.aavegotchisBorrowed = stats.aavegotchisBorrowed.minus(BIGINT_ONE); + stats.save(); +} + +export function handleERC721BuyOrderAdded(event: ERC721BuyOrderAdded): void { + // instantiate entity on subgraph + let entity = getOrCreateERC721BuyOrder(event.params.buyOrderId.toString()); + entity.buyer = event.params.buyer; + entity.category = event.params.category; + entity.createdAt = event.params.time; + entity.duration = event.params.duration; + entity.erc721TokenAddress = event.params.erc721TokenAddress; + entity.erc721TokenId = event.params.erc721TokenId; + entity.priceInWei = event.params.priceInWei; + entity.validationHash = event.params.validationHash; + entity.save(); +} + +export function handleERC721BuyOrderExecuted( + event: ERC721BuyOrderExecuted +): void { + // update buy order + let entity = getOrCreateERC721BuyOrder(event.params.buyOrderId.toString()); + entity.seller = event.params.seller; + entity.erc721TokenAddress = event.params.erc721TokenAddress; + entity.erc721TokenId = event.params.erc721TokenId; + entity.priceInWei = event.params.priceInWei; + entity.buyer = event.params.buyer; + entity.executedAt = event.params.time; + entity.save(); +} + +export function handleKinshipBurned(event: KinshipBurned): void { + let gotchi = getOrCreateAavegotchi(event.params._tokenId.toString(), event); + if (!gotchi) return; + gotchi.kinship = event.params._value; + gotchi.save(); +} diff --git a/src/utils/decimals.ts b/src/utils/decimals.ts index aebbc8f..b2bd3bf 100644 --- a/src/utils/decimals.ts +++ b/src/utils/decimals.ts @@ -1,4 +1,6 @@ -import { BigDecimal, BigInt } from "@graphprotocol/graph-ts"; +import { BigDecimal, BigInt, log, ByteArray } from "@graphprotocol/graph-ts"; +import { GotchiLending } from "../../generated/schema"; +import { BIGINT_ZERO } from "./constants"; export const DEFAULT_DECIMALS = 18; @@ -26,3 +28,21 @@ export function toDecimal( return value.divDecimal(precision); } + +export function updatePermissionsFromBitmap( + lending: GotchiLending, + bitmap: BigInt +): GotchiLending { + const permissions = bitmap.bitAnd(BigInt.fromI32(0xff)); + const channelling = bitmap.rightShift(8).bitAnd(BigInt.fromI32(0xff)); + + log.info("permissions: {}, channelling: {}", [ + permissions.toString(), + channelling.toString(), + ]); + lending.channellingAllowed = !( + permissions == BIGINT_ZERO || channelling == BIGINT_ZERO + ); + + return lending; +} diff --git a/src/utils/helpers/diamond.ts b/src/utils/helpers/diamond.ts index f78b7c7..38ed8f6 100644 --- a/src/utils/helpers/diamond.ts +++ b/src/utils/helpers/diamond.ts @@ -20,6 +20,7 @@ import { GotchiLending, Whitelist, ClaimedToken, + ERC721BuyOrder, } from "../../../generated/schema"; import { BIGINT_ZERO, STATUS_AAVEGOTCHI } from "../constants"; import { Address, BigInt, Bytes, ethereum, log } from "@graphprotocol/graph-ts"; @@ -254,7 +255,7 @@ export function updateERC721ListingInfo( } //@ts-ignore -function itemMaxQuantityToRarity(bigInt: BigInt): BigInt { +export function itemMaxQuantityToRarity(bigInt: BigInt): BigInt { let quantity = bigInt.toI32(); if (quantity >= 1000) return BigInt.fromI32(0); if (quantity >= 500) return BigInt.fromI32(1); @@ -768,6 +769,7 @@ export function getOrCreateGotchiLending(listingId: BigInt): GotchiLending { lending.whitelist = null; lending.whitelistMembers = []; lending.whitelistId = null; + lending.channellingAllowed = false; } return lending; @@ -879,3 +881,12 @@ export function getOrCreateWhitelist( return whitelist; } + +export function getOrCreateERC721BuyOrder(id: string): ERC721BuyOrder { + let entity = ERC721BuyOrder.load(id); + if (!entity) { + entity = new ERC721BuyOrder(id); + } + + return entity; +} diff --git a/subgraph.mumbai.yaml b/subgraph.mumbai.yaml index c007bef..e205d4b 100644 --- a/subgraph.mumbai.yaml +++ b/subgraph.mumbai.yaml @@ -13,9 +13,9 @@ dataSources: name: AavegotchiDiamond network: mumbai source: - address: "0x14B55C7862023c9f2aEfCA1EB5a606465dA034B0" + address: "0x83e73d9cf22dfc3a767ea1ce0611f7f50306622e" abi: AavegotchiDiamond - startBlock: 26304071 + startBlock: 34467860 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -141,6 +141,20 @@ dataSources: handler: handleERC1155ListingWhitelistSet - event: ERC1155ListingPriceUpdate(indexed uint256,uint256,uint256) handler: handleERC1155ListingPriceUpdate + - event: GotchiLendingAdded((uint32,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256,uint256)) + handler: handleGotchiLendingAdded2 + - event: GotchiLendingExecuted((uint32,address,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256,uint256)) + handler: handleGotchiLendingExecuted2 + - event: GotchiLendingCancelled((uint32,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256,uint256)) + handler: handleGotchiLendingCancelled2 + - event: GotchiLendingClaimed((uint32,address,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256[],uint256,uint256)) + handler: handleGotchiLendingClaimed2 + - event: GotchiLendingEnded((uint32,address,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256,uint256)) + handler: handleGotchiLendingEnded2 + - event: ERC721BuyOrderAdded(indexed uint256,indexed address,address,uint256,indexed uint256,uint256,uint256,bytes32,uint256) + handler: handleERC721BuyOrderAdded + - event: ERC721BuyOrderExecuted(indexed uint256,indexed address,address,address,uint256,uint256,uint256) + handler: handleERC721BuyOrderExecuted # - event: DiamondCut((address,uint8,bytes4[])[],address,bytes) # handler: handleDiamondCut # - AavegotchiInteract(indexed uint256,uint256) @@ -224,4 +238,6 @@ dataSources: handler: handleTransferParcel - event: ResyncParcel(uint256) handler: handleResyncParcel + - event: KinshipBurned(uint256,uint256) + handler: handleKinshipBurned file: ./src/mappings/diamond.ts diff --git a/subgraph.yaml b/subgraph.yaml index fb2a3b0..9b20211 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -141,6 +141,20 @@ dataSources: handler: handleERC1155ListingWhitelistSet - event: ERC1155ListingPriceUpdate(indexed uint256,uint256,uint256) handler: handleERC1155ListingPriceUpdate + - event: GotchiLendingAdded((uint32,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256,uint256)) + handler: handleGotchiLendingAdded2 + - event: GotchiLendingExecuted((uint32,address,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256,uint256)) + handler: handleGotchiLendingExecuted2 + - event: GotchiLendingCancelled((uint32,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256,uint256)) + handler: handleGotchiLendingCancelled2 + - event: GotchiLendingClaimed((uint32,address,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256[],uint256,uint256)) + handler: handleGotchiLendingClaimed2 + - event: GotchiLendingEnded((uint32,address,address,uint32,uint96,uint32,uint8[3],address,address,uint32,address[],uint256,uint256)) + handler: handleGotchiLendingEnded2 + - event: ERC721BuyOrderAdded(indexed uint256,indexed address,address,uint256,indexed uint256,uint256,uint256,bytes32,uint256) + handler: handleERC721BuyOrderAdded + - event: ERC721BuyOrderExecuted(indexed uint256,indexed address,address,address,uint256,uint256,uint256) + handler: handleERC721BuyOrderExecuted # - event: DiamondCut((address,uint8,bytes4[])[],address,bytes) # handler: handleDiamondCut # - AavegotchiInteract(indexed uint256,uint256) @@ -224,4 +238,6 @@ dataSources: handler: handleTransferParcel - event: ResyncParcel(uint256) handler: handleResyncParcel + - event: KinshipBurned(uint256,uint256) + handler: handleKinshipBurned file: ./src/mappings/diamond.ts diff --git a/tests/erc721BuyOrder.test.ts b/tests/erc721BuyOrder.test.ts new file mode 100644 index 0000000..0fafca1 --- /dev/null +++ b/tests/erc721BuyOrder.test.ts @@ -0,0 +1,119 @@ +import { test, assert } from "matchstick-as/assembly/index"; +import { + handleERC721BuyOrderAdded, + handleERC721BuyOrderExecuted, +} from "../src/mappings/diamond"; +import { + erc721BuyOrderAddedMockEvent, + erc721BuyOrderExecutedMockEvent, +} from "./mocks"; + +test("erc721buyOrderAdded should create erc721buyorder entity", () => { + let event = erc721BuyOrderAddedMockEvent(); + + handleERC721BuyOrderAdded(event); + + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "id", + event.params.buyOrderId.toString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "buyer", + event.params.buyer.toHexString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "erc721TokenAddress", + event.params.erc721TokenAddress.toHexString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "erc721TokenId", + event.params.erc721TokenId.toString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "category", + event.params.category.toString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "priceInWei", + event.params.priceInWei.toString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "duration", + event.params.duration.toString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "validationHash", + event.params.validationHash.toHexString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "createdAt", + event.params.time.toString() + ); +}); + +test("erc721buyOrderExecuted should update erc721buyorder entity", () => { + let event = erc721BuyOrderExecutedMockEvent(); + + handleERC721BuyOrderExecuted(event); + + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "id", + event.params.buyOrderId.toString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "buyer", + event.params.buyer.toHexString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "seller", + event.params.seller.toHexString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "erc721TokenAddress", + event.params.erc721TokenAddress.toHexString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "erc721TokenId", + event.params.erc721TokenId.toString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "priceInWei", + event.params.priceInWei.toString() + ); + assert.fieldEquals( + "ERC721BuyOrder", + "1", + "executedAt", + event.params.time.toString() + ); +}); diff --git a/tests/gotchiLending.test.ts b/tests/gotchiLending.test.ts new file mode 100644 index 0000000..1365be6 --- /dev/null +++ b/tests/gotchiLending.test.ts @@ -0,0 +1,491 @@ +import { Address, BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts"; +import { + test, + assert, + createMockedFunction, + newMockEvent, +} from "matchstick-as/assembly/index"; +import { Aavegotchi, Whitelist } from "../generated/schema"; +import { + handleGotchiLendingAdded2, + handleGotchiLendingCancelled2, + handleGotchiLendingClaimed2, + handleGotchiLendingEnded2, + handleGotchiLendingExecuted2, +} from "../src/mappings/diamond"; +import { BIGINT_ONE, BIGINT_ZERO } from "../src/utils/constants"; +import { getAavegotchiMock } from "./mocks"; +import { + GotchiLendingAdded1, + GotchiLendingCancelled, + GotchiLendingClaimed1, + GotchiLendingEnded1, + GotchiLendingExecuted1, +} from "../generated/AavegotchiDiamond/AavegotchiDiamond"; + +const address = Address.fromString( + "0x1AD3d72e54Fb0eB46e87F82f77B284FC8a66b16C" +); + +const borrower = Address.fromString( + "0x5C647cA5e2Bc5BD17c40410FcB8003D38879Ae50" +); + +test("add gotchi lending", () => { + const lendingTuple: ethereum.Tuple = changetype([ + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddress(address), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromI32Array([1, 2, 3]), + ethereum.Value.fromAddress(address), + ethereum.Value.fromAddress(address), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddressArray([address]), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(257)), + ]); + + let gotchi = new Aavegotchi("1"); + gotchi.withSetsRarityScore = BIGINT_ONE; + gotchi.kinship = BIGINT_ONE; + gotchi.gotchiId = BIGINT_ONE; + gotchi.hauntId = BIGINT_ONE; + gotchi.name = "Test Gotchi"; + gotchi.nameLowerCase = "test gotchi"; + gotchi.randomNumber = BIGINT_ONE; + gotchi.status = BIGINT_ONE; + gotchi.numericTraits = [1, 1, 1]; + gotchi.modifiedNumericTraits = [1, 1, 1]; + gotchi.equippedWearables = [1]; + gotchi.collateral = address; + gotchi.escrow = address; + gotchi.stakedAmount = BIGINT_ONE; + gotchi.minimumStake = BIGINT_ONE; + gotchi.lastInteracted = BIGINT_ONE; + gotchi.experience = BIGINT_ONE; + gotchi.toNextLevel = BIGINT_ONE; + gotchi.usedSkillPoints = BIGINT_ONE; + gotchi.level = BIGINT_ONE; + gotchi.baseRarityScore = BIGINT_ONE; + gotchi.modifiedRarityScore = BIGINT_ONE; + gotchi.locked = false; + gotchi.originalOwner = address.toHexString(); + gotchi.owner = address.toHexString(); + gotchi.timesTraded = BIGINT_ONE; + gotchi.save(); + + let newMockevent = newMockEvent(); + let event = new GotchiLendingAdded1( + newMockevent.address, + newMockevent.logIndex, + newMockevent.transactionLogIndex, + newMockevent.logType, + newMockevent.block, + newMockevent.transaction, + newMockevent.parameters, + null + ); + event.parameters = new Array(); + event.block.number = BIGINT_ONE; + + event.parameters.push( + new ethereum.EventParam( + "lending", + ethereum.Value.fromTuple(lendingTuple) + ) + ); + + let contractAddress = Address.fromString( + "0x86935F11C86623deC8a25696E1C19a8659CbF95d" + ); + event.address = contractAddress; + + // init + let whitelist = new Whitelist("1"); + whitelist.name = "Test Whitelist"; + let members = new Array(); + members.push(address); + + whitelist.members = members; + whitelist.owner = address.toHexString(); + whitelist.ownerAddress = address; + whitelist.save(); + + // create mock for updateAavegotchi and getAavegotchi + createMockedFunction( + event.address, + "getAavegotchi", + "getAavegotchi(uint256):((uint256,string,address,uint256,uint256,int16[6],int16[6],uint16[16],address,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,bool,(uint256,uint256,(string,string,string,int8[6],bool[16],uint8[],(uint8,uint8,uint8,uint8),uint256,uint256,uint256,uint32,uint8,bool,uint16,bool,uint8,int16,uint32))[]))" + ) + .withArgs([ethereum.Value.fromUnsignedBigInt(BIGINT_ONE)]) + .returns(getAavegotchiMock(event)); + + handleGotchiLendingAdded2(event); + + // check gotchi lending entity + assert.fieldEquals("GotchiLending", "1", "cancelled", "false"); + assert.fieldEquals("GotchiLending", "1", "completed", "false"); + assert.fieldEquals("GotchiLending", "1", "gotchi", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiTokenId", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiBRS", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiKinship", "1"); + assert.fieldEquals("GotchiLending", "1", "upfrontCost", "1"); + assert.fieldEquals("GotchiLending", "1", "rentDuration", "1"); + assert.fieldEquals("GotchiLending", "1", "lender", address.toHexString()); + assert.fieldEquals( + "GotchiLending", + "1", + "originalOwner", + address.toHexString() + ); + assert.fieldEquals("GotchiLending", "1", "period", "1"); + assert.fieldEquals("GotchiLending", "1", "splitOwner", "1"); + assert.fieldEquals("GotchiLending", "1", "splitBorrower", "2"); + assert.fieldEquals("GotchiLending", "1", "splitOther", "3"); + assert.fieldEquals( + "GotchiLending", + "1", + "thirdPartyAddress", + address.toHexString() + ); + assert.fieldEquals("GotchiLending", "1", "timeCreated", "1"); + assert.fieldEquals("GotchiLending", "1", "channellingAllowed", "true"); + assert.fieldEquals("GotchiLending", "1", "whitelist", "1"); + assert.fieldEquals("GotchiLending", "1", "whitelistId", "1"); + assert.fieldEquals( + "GotchiLending", + "1", + "whitelistMembers", + "[0x1ad3d72e54fb0eb46e87f82f77b284fc8a66b16c]" + ); +}); + +test("execute gotchi lending", () => { + const lendingTuple: ethereum.Tuple = changetype([ + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddress(address), + ethereum.Value.fromAddress(borrower), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromI32Array([1, 2, 3]), + ethereum.Value.fromAddress(address), + ethereum.Value.fromAddress(address), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddressArray([address]), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(256)), + ]); + let newMockevent = newMockEvent(); + let event = new GotchiLendingExecuted1( + newMockevent.address, + newMockevent.logIndex, + newMockevent.transactionLogIndex, + newMockevent.logType, + newMockevent.block, + newMockevent.transaction, + newMockevent.parameters, + null + ); + event.parameters = new Array(); + event.block.number = BIGINT_ONE; + + event.parameters.push( + new ethereum.EventParam( + "lending", + ethereum.Value.fromTuple(lendingTuple) + ) + ); + + let contractAddress = Address.fromString( + "0x86935F11C86623deC8a25696E1C19a8659CbF95d" + ); + event.address = contractAddress; + + // create mock for updateAavegotchi and getAavegotchi + createMockedFunction( + event.address, + "getAavegotchi", + "getAavegotchi(uint256):((uint256,string,address,uint256,uint256,int16[6],int16[6],uint16[16],address,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,bool,(uint256,uint256,(string,string,string,int8[6],bool[16],uint8[],(uint8,uint8,uint8,uint8),uint256,uint256,uint256,uint32,uint8,bool,uint16,bool,uint8,int16,uint32))[]))" + ) + .withArgs([ethereum.Value.fromUnsignedBigInt(BIGINT_ONE)]) + .returns(getAavegotchiMock(event)); + + handleGotchiLendingExecuted2(event); + + // check gotchi lending entity + assert.fieldEquals("GotchiLending", "1", "cancelled", "false"); + assert.fieldEquals("GotchiLending", "1", "completed", "false"); + assert.fieldEquals("GotchiLending", "1", "gotchi", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiTokenId", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiBRS", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiKinship", "1"); + assert.fieldEquals("GotchiLending", "1", "upfrontCost", "1"); + assert.fieldEquals("GotchiLending", "1", "rentDuration", "1"); + assert.fieldEquals("GotchiLending", "1", "timeAgreed", "1"); + assert.fieldEquals("GotchiLending", "1", "channellingAllowed", "false"); + assert.fieldEquals( + "GotchiLending", + "1", + "borrower", + borrower.toHexString() + ); + assert.fieldEquals("GotchiLending", "1", "lender", address.toHexString()); + assert.fieldEquals( + "GotchiLending", + "1", + "originalOwner", + address.toHexString() + ); + assert.fieldEquals("GotchiLending", "1", "period", "1"); + assert.fieldEquals("GotchiLending", "1", "splitOwner", "1"); + assert.fieldEquals("GotchiLending", "1", "splitBorrower", "2"); + assert.fieldEquals("GotchiLending", "1", "splitOther", "3"); + assert.fieldEquals( + "GotchiLending", + "1", + "thirdPartyAddress", + address.toHexString() + ); + assert.fieldEquals("GotchiLending", "1", "timeCreated", "1"); + assert.fieldEquals("GotchiLending", "1", "whitelist", "1"); + assert.fieldEquals("GotchiLending", "1", "whitelistId", "1"); + assert.fieldEquals( + "GotchiLending", + "1", + "whitelistMembers", + "[0x1ad3d72e54fb0eb46e87f82f77b284fc8a66b16c]" + ); + + assert.fieldEquals("Aavegotchi", "1", "owner", address.toHexString()); + assert.fieldEquals( + "Aavegotchi", + "1", + "originalOwner", + address.toHexString() + ); + + assert.fieldEquals("User", address.toHexString(), "gotchisLentOut", "[1]"); + assert.fieldEquals( + "User", + borrower.toHexString(), + "gotchisBorrowed", + "[1]" + ); + + assert.fieldEquals("Statistic", "0", "aavegotchisBorrowed", "1"); +}); + +test("claimed gotchi lending", () => { + const lendingTuple: ethereum.Tuple = changetype([ + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddress(address), + ethereum.Value.fromAddress(borrower), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromI32Array([1, 2, 3]), + ethereum.Value.fromAddress(address), + ethereum.Value.fromAddress(address), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddressArray([address]), + ethereum.Value.fromUnsignedBigIntArray([BIGINT_ONE]), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(255)), + ]); + let newMockevent = newMockEvent(); + let event = new GotchiLendingClaimed1( + newMockevent.address, + newMockevent.logIndex, + newMockevent.transactionLogIndex, + newMockevent.logType, + newMockevent.block, + newMockevent.transaction, + newMockevent.parameters, + null + ); + event.parameters = new Array(); + event.block.number = BIGINT_ONE; + + event.parameters.push( + new ethereum.EventParam( + "lending", + ethereum.Value.fromTuple(lendingTuple) + ) + ); + + let contractAddress = Address.fromString( + "0x86935F11C86623deC8a25696E1C19a8659CbF95d" + ); + event.address = contractAddress; + + // create mock for updateAavegotchi and getAavegotchi + createMockedFunction( + event.address, + "getAavegotchi", + "getAavegotchi(uint256):((uint256,string,address,uint256,uint256,int16[6],int16[6],uint16[16],address,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,bool,(uint256,uint256,(string,string,string,int8[6],bool[16],uint8[],(uint8,uint8,uint8,uint8),uint256,uint256,uint256,uint32,uint8,bool,uint16,bool,uint8,int16,uint32))[]))" + ) + .withArgs([ethereum.Value.fromUnsignedBigInt(BIGINT_ONE)]) + .returns(getAavegotchiMock(event)); + + handleGotchiLendingClaimed2(event); + + assert.fieldEquals("GotchiLending", "1", "cancelled", "false"); + assert.fieldEquals("GotchiLending", "1", "completed", "false"); + assert.fieldEquals("GotchiLending", "1", "gotchi", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiTokenId", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiBRS", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiKinship", "1"); + + assert.fieldEquals("GotchiLending", "1", "channellingAllowed", "false"); + + assert.fieldEquals( + "ClaimedToken", + `1_${address.toHexString()}`, + "amount", + "1" + ); +}); + +test("cancel gotchi lending", () => { + const lendingTuple: ethereum.Tuple = changetype([ + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddress(address), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromI32Array([1, 2, 3]), + ethereum.Value.fromAddress(address), + ethereum.Value.fromAddress(address), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddressArray([address]), + ethereum.Value.fromUnsignedBigIntArray([BIGINT_ONE]), + ethereum.Value.fromUnsignedBigInt(BIGINT_ZERO), + ]); + let newMockevent = newMockEvent(); + let event = new GotchiLendingCancelled( + newMockevent.address, + newMockevent.logIndex, + newMockevent.transactionLogIndex, + newMockevent.logType, + newMockevent.block, + newMockevent.transaction, + newMockevent.parameters, + null + ); + event.parameters = new Array(); + event.block.number = BIGINT_ONE; + + event.parameters.push( + new ethereum.EventParam( + "lending", + ethereum.Value.fromTuple(lendingTuple) + ) + ); + + let contractAddress = Address.fromString( + "0x86935F11C86623deC8a25696E1C19a8659CbF95d" + ); + event.address = contractAddress; + + // create mock for updateAavegotchi and getAavegotchi + createMockedFunction( + event.address, + "getAavegotchi", + "getAavegotchi(uint256):((uint256,string,address,uint256,uint256,int16[6],int16[6],uint16[16],address,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,bool,(uint256,uint256,(string,string,string,int8[6],bool[16],uint8[],(uint8,uint8,uint8,uint8),uint256,uint256,uint256,uint32,uint8,bool,uint16,bool,uint8,int16,uint32))[]))" + ) + .withArgs([ethereum.Value.fromUnsignedBigInt(BIGINT_ONE)]) + .returns(getAavegotchiMock(event)); + + handleGotchiLendingCancelled2(event); + + assert.fieldEquals("GotchiLending", "1", "cancelled", "true"); + assert.fieldEquals("GotchiLending", "1", "completed", "false"); + assert.fieldEquals("GotchiLending", "1", "gotchi", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiTokenId", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiBRS", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiKinship", "1"); + assert.fieldEquals("GotchiLending", "1", "channellingAllowed", "false"); +}); + +test("end gotchi lending", () => { + const lendingTuple: ethereum.Tuple = changetype([ + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddress(address), + ethereum.Value.fromAddress(borrower), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromI32Array([1, 2, 3]), + ethereum.Value.fromAddress(address), + ethereum.Value.fromAddress(address), + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE), + ethereum.Value.fromAddressArray([address]), + ethereum.Value.fromUnsignedBigIntArray([BIGINT_ONE]), + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(257)), + ]); + let newMockevent = newMockEvent(); + let event = new GotchiLendingEnded1( + newMockevent.address, + newMockevent.logIndex, + newMockevent.transactionLogIndex, + newMockevent.logType, + newMockevent.block, + newMockevent.transaction, + newMockevent.parameters, + null + ); + event.parameters = new Array(); + event.block.number = BIGINT_ONE; + + event.parameters.push( + new ethereum.EventParam( + "lending", + ethereum.Value.fromTuple(lendingTuple) + ) + ); + + let contractAddress = Address.fromString( + "0x86935F11C86623deC8a25696E1C19a8659CbF95d" + ); + event.address = contractAddress; + + // create mock for updateAavegotchi and getAavegotchi + createMockedFunction( + event.address, + "getAavegotchi", + "getAavegotchi(uint256):((uint256,string,address,uint256,uint256,int16[6],int16[6],uint16[16],address,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,bool,(uint256,uint256,(string,string,string,int8[6],bool[16],uint8[],(uint8,uint8,uint8,uint8),uint256,uint256,uint256,uint32,uint8,bool,uint16,bool,uint8,int16,uint32))[]))" + ) + .withArgs([ethereum.Value.fromUnsignedBigInt(BIGINT_ONE)]) + .returns(getAavegotchiMock(event)); + + handleGotchiLendingEnded2(event); + + assert.fieldEquals("GotchiLending", "1", "completed", "true"); + assert.fieldEquals( + "GotchiLending", + "1", + "timeEnded", + event.block.timestamp.toString() + ); + assert.fieldEquals("GotchiLending", "1", "gotchi", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiTokenId", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiBRS", "1"); + assert.fieldEquals("GotchiLending", "1", "gotchiKinship", "1"); + assert.fieldEquals("GotchiLending", "1", "completed", "true"); + assert.fieldEquals("GotchiLending", "1", "channellingAllowed", "true"); + + assert.fieldEquals("User", borrower.toHexString(), "gotchisBorrowed", "[]"); + assert.fieldEquals("User", address.toHexString(), "gotchisLentOut", "[]"); + + assert.fieldEquals("Aavegotchi", "1", "owner", address.toHexString()); + assert.fieldEquals( + "Aavegotchi", + "1", + "originalOwner", + address.toHexString() + ); + assert.fieldEquals("Aavegotchi", "1", "lending", "null"); +}); diff --git a/tests/handleInteract.test.ts b/tests/handleInteract.test.ts index c27dfae..d4068a5 100644 --- a/tests/handleInteract.test.ts +++ b/tests/handleInteract.test.ts @@ -16,6 +16,7 @@ test("Count as interacted if gotchi is claimed", () => { // Initialise let gotchi = new Aavegotchi("1"); gotchi.locked = false; + gotchi.gotchiId = BIGINT_ONE; gotchi.kinship = BIGINT_ZERO; gotchi.save(); diff --git a/tests/handleKinshipBurned.test.ts b/tests/handleKinshipBurned.test.ts new file mode 100644 index 0000000..a979d2d --- /dev/null +++ b/tests/handleKinshipBurned.test.ts @@ -0,0 +1,74 @@ +import { + test, + assert, + clearStore, + newMockEvent, +} from "matchstick-as/assembly/index"; +import { Bytes, ethereum } from "@graphprotocol/graph-ts"; +import { handleKinshipBurned } from "../src/mappings/diamond"; + +import { Aavegotchi } from "../generated/schema"; +import { BIGINT_ONE } from "../src/utils/constants"; +import { KinshipBurned } from "../generated/RealmDiamond/RealmDiamond"; + +test("should update kinship of gotchi", () => { + // Initialise + let gotchi = new Aavegotchi("1"); + gotchi.locked = false; + gotchi.kinship = BIGINT_ONE; + gotchi.gotchiId = BIGINT_ONE; + gotchi.hauntId = BIGINT_ONE; + gotchi.name = "Test 123"; + gotchi.nameLowerCase = "test 123"; + gotchi.randomNumber = BIGINT_ONE; + gotchi.status = BIGINT_ONE; + gotchi.numericTraits = [1, 2, 3, 4]; + gotchi.modifiedNumericTraits = [1, 2, 3, 4]; + gotchi.equippedWearables = [1, 2, 3, 4]; + gotchi.baseRarityScore = BIGINT_ONE; + gotchi.collateral = new Bytes(0); + gotchi.escrow = new Bytes(0); + gotchi.stakedAmount = BIGINT_ONE; + gotchi.minimumStake = BIGINT_ONE; + gotchi.kinship = BIGINT_ONE; + gotchi.lastInteracted = BIGINT_ONE; + gotchi.experience = BIGINT_ONE; + gotchi.toNextLevel = BIGINT_ONE; + gotchi.usedSkillPoints = BIGINT_ONE; + gotchi.level = BIGINT_ONE; + gotchi.baseRarityScore = BIGINT_ONE; + gotchi.modifiedRarityScore = BIGINT_ONE; + gotchi.locked = false; + gotchi.timesTraded = BIGINT_ONE; + gotchi.save(); + + // prepare event + let newMockevent = newMockEvent(); + let event = new KinshipBurned( + newMockevent.address, + newMockevent.logIndex, + newMockevent.transactionLogIndex, + newMockevent.logType, + newMockevent.block, + newMockevent.transaction, + newMockevent.parameters, + null + ); + event.parameters = new Array(); + + let _tokenId = new ethereum.EventParam( + "_tokenId", + ethereum.Value.fromI32(1) + ); + event.parameters.push(_tokenId); + + let _value = new ethereum.EventParam("_value", ethereum.Value.fromI32(0)); + event.parameters.push(_value); + + // execute handler with event + handleKinshipBurned(event); + + // assert and clear store + assert.fieldEquals("Aavegotchi", "1", "kinship", "0"); + clearStore(); +}); diff --git a/tests/mocks.ts b/tests/mocks.ts index e3d4510..6e7b7d9 100644 --- a/tests/mocks.ts +++ b/tests/mocks.ts @@ -1,6 +1,8 @@ -import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts"; +import { Address, BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts"; import { ClaimAavegotchi, + ERC721BuyOrderAdded, + ERC721BuyOrderExecuted, ERC721ExecutedListing, ERC721ListingAdd, ERC721ListingCancelled, @@ -459,3 +461,166 @@ export function getClaimAavegotchiEvent(): ClaimAavegotchi { event.address = contractAddress; return event; } + +export function erc721BuyOrderAddedMockEvent(): ERC721BuyOrderAdded { + let newMockevent = newMockEvent(); + let event = new ERC721BuyOrderAdded( + newMockevent.address, + newMockevent.logIndex, + newMockevent.transactionLogIndex, + newMockevent.logType, + newMockevent.block, + newMockevent.transaction, + newMockevent.parameters, + null + ); + event.parameters = new Array(); + event.block.number = BIGINT_ONE; + + // Add dummy data for each parameter + event.parameters.push( + new ethereum.EventParam( + "indexed uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "indexed address", + ethereum.Value.fromAddress( + Address.fromString("0x1234567890abcdef1234567890abcdef12345678") + ) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "address", + ethereum.Value.fromAddress( + Address.fromString("0x9876543210fedcba9876543210fedcba98765432") + ) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "indexed uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "bytes32", + ethereum.Value.fromBytes( + Bytes.fromHexString( + "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + ) + ) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + + let contractAddress = Address.fromString( + "0x86935F11C86623deC8a25696E1C19a8659CbF95d" + ); + event.address = contractAddress; + return event; +} + +export function erc721BuyOrderExecutedMockEvent(): ERC721BuyOrderExecuted { + let mockEvent = newMockEvent(); + let event = new ERC721BuyOrderExecuted( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + mockEvent.parameters, + null + ); + event.parameters = new Array(); + event.block.number = BIGINT_ONE; + + // Add dummy data for each parameter + event.parameters.push( + new ethereum.EventParam( + "indexed uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "indexed address", + ethereum.Value.fromAddress( + Address.fromString("0x1234567890abcdef1234567890abcdef12345678") + ) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "address", + ethereum.Value.fromAddress( + Address.fromString("0x9876543210fedcba9876543210fedcba98765432") + ) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "address", + ethereum.Value.fromAddress( + Address.fromString("0xabcdef1234567890abcdef1234567890abcdef12") + ) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + event.parameters.push( + new ethereum.EventParam( + "uint256", + ethereum.Value.fromUnsignedBigInt(BIGINT_ONE) + ) + ); + let contractAddress = Address.fromString( + "0x86935F11C86623deC8a25696E1C19a8659CbF95d" + ); + event.address = contractAddress; + return event; +}