Skip to content

Commit

Permalink
Merge pull request #38 from OriumNetwork/aavegotchi-update
Browse files Browse the repository at this point in the history
Aavegotchi update
  • Loading branch information
karacurt authored Aug 2, 2023
2 parents 72c2b2c + 17470c4 commit ba99565
Show file tree
Hide file tree
Showing 8 changed files with 10,349 additions and 7,718 deletions.
17,681 changes: 9,963 additions & 7,718 deletions abis/AavegotchiDiamond.json

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions src/aavegotchi/gotchi/gotchi-lending-add-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { BigInt, log } from "@graphprotocol/graph-ts";
import { GotchiLendingAdded } from "../../../generated/AavegotchiDiamond/AavegotchiDiamond";
import { Nft, RentalOffer } from "../../../generated/schema";
import { generateNftId, updateLastOfferExpirationAt } from "../../utils/misc";
import { GHST_TOKEN_ADDRESS } from "../../utils/addresses";
import { MAX_EXPIRATION_DATE, ONE_ETHER } from "../../utils/constants";

/**
* event GotchiLendingAdded(
* uint32 indexed listingId,
* address indexed lender,
* uint32 indexed tokenId,
* uint96 initialCost,
* uint32 period,
* uint8[3] revenueSplit,
* address originalOwner,
* address thirdParty,
* uint32 whitelistId,
* address[] revenueTokens,
* uint256 timeCreated
* );
*/
export function handleGotchiLendingAdded(event: GotchiLendingAdded): void {
const nftId = generateNftId("AAVEGOTCHI", event.params.tokenId);
const nft = Nft.load(nftId);

if (!nft) {
log.debug(
"[GotchiLendingAdded]: Aavegotchi {} does not exist, skiping...",
[event.params.tokenId.toString()]
);
return;
}

// create rental offer
const rentalOfferId = `${event.transaction.hash.toHex()}-${event.logIndex.toString()}`;
const rentalOffer = new RentalOffer(rentalOfferId);
rentalOffer.nfts = [nftId];
rentalOffer.lender = event.params.lender.toHexString().toLowerCase();
rentalOffer.createdAt = event.params.timeCreated;
rentalOffer.creationTxHash = event.transaction.hash.toHex();
rentalOffer.duration = [event.params.period];
rentalOffer.feeAmount = event.params.initialCost;
rentalOffer.feeToken = GHST_TOKEN_ADDRESS;
rentalOffer.expirationDate = MAX_EXPIRATION_DATE;

if (event.params.whitelistId != BigInt.fromI32(0)) {
rentalOffer.taker = event.params.whitelistId.toString();
}

rentalOffer.profitShareTokens = event.params.revenueTokens.map<string>((t) =>
t.toHex().toLowerCase()
);
rentalOffer.profitShareSplit = event.params.revenueSplit.map<BigInt>((s) =>
BigInt.fromI32(s).times(ONE_ETHER)
);

rentalOffer.save();

// link rental offer to nft
nft.currentRentalOffer = rentalOfferId;
nft.save();

updateLastOfferExpirationAt(nft, rentalOffer.expirationDate);

log.warning("[GotchiLendingAdded]: Gotchi {} added to rental offer {}", [
nftId,
rentalOffer.id,
]);
}
74 changes: 74 additions & 0 deletions src/aavegotchi/gotchi/gotchi-lending-added2-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { BigInt, log } from "@graphprotocol/graph-ts";
import { GotchiLendingAdded1 } from "../../../generated/AavegotchiDiamond/AavegotchiDiamond";
import { Nft, RentalOffer } from "../../../generated/schema";
import { generateNftId, updateLastOfferExpirationAt } from "../../utils/misc";
import { GHST_TOKEN_ADDRESS } from "../../utils/addresses";
import { MAX_EXPIRATION_DATE, ONE_ETHER } from "../../utils/constants";

/**
*
* struct GotchiLendingAdd {
uint32 listingId;
address lender;
uint32 tokenId;
uint96 initialCost;
uint32 period;
uint8[3] revenueSplit;
address originalOwner;
address thirdParty;
uint32 whitelistId;
address[] revenueTokens;
uint256 timeCreated;
uint256 permissions;
}
*
* event GotchiLendingAdded(GotchiLendingAdd);
*/
export function handleGotchiLendingAdded2(event: GotchiLendingAdded1): void {
const nftId = generateNftId("AAVEGOTCHI", event.params.param0.tokenId);
const nft = Nft.load(nftId);

if (!nft) {
log.debug(
"[GotchiLendingAdded]: Aavegotchi {} does not exist, skiping...",
[event.params.param0.tokenId.toString()]
);
return;
}

// create rental offer
const rentalOfferId = `${event.transaction.hash.toHex()}-${event.logIndex.toString()}`;
const rentalOffer = new RentalOffer(rentalOfferId);
rentalOffer.nfts = [nftId];
rentalOffer.lender = event.params.param0.lender.toHexString().toLowerCase();
rentalOffer.createdAt = event.params.param0.timeCreated;
rentalOffer.creationTxHash = event.transaction.hash.toHex();
rentalOffer.duration = [event.params.param0.period];
rentalOffer.feeAmount = event.params.param0.initialCost;
rentalOffer.feeToken = GHST_TOKEN_ADDRESS;
rentalOffer.expirationDate = MAX_EXPIRATION_DATE;

if (event.params.param0.whitelistId != BigInt.fromI32(0)) {
rentalOffer.taker = event.params.param0.whitelistId.toString();
}

rentalOffer.profitShareTokens = event.params.param0.revenueTokens.map<string>((t) =>
t.toHex().toLowerCase()
);
rentalOffer.profitShareSplit = event.params.param0.revenueSplit.map<BigInt>((s) =>
BigInt.fromI32(s).times(ONE_ETHER)
);

rentalOffer.save();

// link rental offer to nft
nft.currentRentalOffer = rentalOfferId;
nft.save();

updateLastOfferExpirationAt(nft, rentalOffer.expirationDate);

log.warning("[GotchiLendingAdded]: Gotchi {} added to rental offer {}", [
nftId,
rentalOffer.id,
]);
}
73 changes: 73 additions & 0 deletions src/aavegotchi/gotchi/gotchi-lending-cancelled2-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { BigInt, log } from "@graphprotocol/graph-ts";
import { GotchiLendingCancelled } from "../../../generated/AavegotchiDiamond/AavegotchiDiamond";
import { Nft, RentalOffer } from "../../../generated/schema";
import { generateNftId } from "../../utils/misc";
import { AAVEGOTCHI } from "../../utils/constants";

/**
* event GotchiLendingCancelled(
* uint32 indexed listingId,
* address indexed lender,
* uint32 tokenId,
* uint96 initialCost,
* uint32 period,
* uint8[3] revenueSplit,
* address originalOwner,
* address thirdParty,
* uint32 whitelistId,
* address[] revenueTokens,
* uint256 timeAgreed
* );
*/
export function handleGotchiLendingCancelled2(
event: GotchiLendingCancelled
): void {
const nftId = generateNftId(AAVEGOTCHI, event.params.param0.tokenId);

const nft = Nft.load(nftId);
if (!nft) {
log.debug(
"[handleGotchiLendingCancelled] Aavegotchi {} does not exist, tx: {}",
[event.params.param0.tokenId.toString(), event.transaction.hash.toHex()]
);
return;
}

const currentRentalOfferId = nft.currentRentalOffer;

if (!currentRentalOfferId) {
//probably it is a legacy rental offer not tracked before rental upgrade
log.warning(
"[handleGotchiLendingCancelled] NFT {} has no rental offer, skipping...",
[nft.id]
);
return;
}

const currentRentalOffer = RentalOffer.load(currentRentalOfferId!);

if (!currentRentalOffer) {
throw new Error(
"[handleGotchiLendingCancelled] RentalOffer " +
currentRentalOfferId! +
" does not exist, tx: " +
event.transaction.hash.toHex()
);
}

// update rental offer
currentRentalOffer.cancelledAt = event.block.timestamp;
currentRentalOffer.cancellationTxHash = event.transaction.hash.toHex();
currentRentalOffer.save();

// remove current rental offer from nft, because it has been executed, and link rental to nft
nft.currentRentalOffer = null;
// Since aavegotchi only allows one offer at a time, we can set the expiration date to zero
nft.lastOfferExpirationAt = BigInt.zero();
nft.save();

log.warning(
"[handleGotchiLendingCancelled] Rental Offer for NFT {} was cancelled. RentalOfferId: {}",
[nftId, currentRentalOfferId!]
);
}
61 changes: 61 additions & 0 deletions src/aavegotchi/gotchi/gotchi-lending-ended2-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { log } from '@graphprotocol/graph-ts'
import { GotchiLendingEnded1 } from '../../../generated/AavegotchiDiamond/AavegotchiDiamond'
import { Nft, Rental } from '../../../generated/schema'
import { generateNftId } from '../../utils/misc'
import { AAVEGOTCHI } from '../../utils/constants'

/**
* event GotchiLendingEnded(
* uint32 indexed listingId,
* address indexed lender,
* address indexed borrower,
* uint32 tokenId,
* uint96 initialCost,
* uint32 period,
* uint8[3] revenueSplit,
* address originalOwner,
* address thirdParty,
* uint32 whitelistId,
* address[] revenueTokens,
* uint256 timeAgreed
* );
*/
export function handleGotchiLendingEnded2(event: GotchiLendingEnded1): void {
const nftId = generateNftId(AAVEGOTCHI, event.params.param0.tokenId)

const nft = Nft.load(nftId)
if (!nft) {
log.debug('[handleGotchiLendingEnded] Aavegotchi {} does not exist, tx: {}', [
event.params.param0.tokenId.toString(),
event.transaction.hash.toHex(),
])
return
}

const currentRentalId = nft.currentRental

if (!currentRentalId) {
//probably it is a legacy rental not tracked before rental upgrade
log.warning('[handleGotchiLendingEnded] NFT {} has no rental, skipping...', [nft.id])
return
}

const currentRental = Rental.load(currentRentalId!)

if (!currentRental) {
throw new Error(
'[handleGotchiLendingEnded] Rental ' + currentRentalId! + ' does not exist, tx: ' + event.transaction.hash.toHex()
)
}

// update rental
currentRental.endedAt = event.block.timestamp
currentRental.endedTxHash = event.transaction.hash.toHex()
currentRental.save()

// remove current rental from nft, because it ended
nft.currentRental = null
nft.save()

log.warning('[handleGotchiLendingEnded] NFT {} rental {} ended', [nftId, currentRentalId!])
}
96 changes: 96 additions & 0 deletions src/aavegotchi/gotchi/gotchi-lending-executed2-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { BigInt, log } from '@graphprotocol/graph-ts'
import { GotchiLendingExecuted1 } from '../../../generated/AavegotchiDiamond/AavegotchiDiamond'
import { Nft, Rental, RentalOffer } from '../../../generated/schema'
import { generateNftId } from '../../utils/misc'
import { AAVEGOTCHI } from '../../utils/constants'

/**
* event GotchiLendingExecuted(
* uint32 indexed listingId,
* address indexed lender,
* address indexed borrower,
* uint32 tokenId,
* uint96 initialCost,
* uint32 period,
* uint8[3] revenueSplit,
* address originalOwner,
* address thirdParty,
* uint32 whitelistId,
* address[] revenueTokens,
* uint256 timeAgreed
* );
*/
export function handleGotchiLendingExecuted2(event: GotchiLendingExecuted1): void {
const nftId = generateNftId(AAVEGOTCHI, event.params.param0.tokenId)

const nft = Nft.load(nftId)
if (!nft) {
log.debug('[handleGotchiLendingExecuted] Aavegotchi {} does not exist, tx: {}', [
event.params.param0.tokenId.toString(),
event.transaction.hash.toHex(),
])
return
}

const currentRentalOfferId = nft.currentRentalOffer

if (!currentRentalOfferId) {
//probably it is a legacy rental offer not tracked before rental upgrade
log.warning('[handleGotchiLendingExecuted] NFT {} has no rental offer, skipping...', [nft.id])
return
}

const rentalOffer = RentalOffer.load(currentRentalOfferId!)

if (!rentalOffer) {
throw new Error(
'[handleGotchiLendingExecuted] No rental offer with id ' +
currentRentalOfferId! +
' was found for token id: ' +
event.params.param0.tokenId.toString() +
', lender: ' +
event.params.param0.lender.toHexString() +
', tx: ' +
event.transaction.hash.toHex()
)
}

rentalOffer.executionTxHash = event.transaction.hash.toHex()
rentalOffer.save()

const previoustRental = nft.currentRental
if (previoustRental) {
throw new Error(
'[handleGotchiLendingExecuted] NFT ' +
nftId +
' already has a rental ' +
previoustRental +
', tx: ' +
event.transaction.hash.toHex()
)
}

const currentRental = new Rental(`${event.transaction.hash.toHex()}-${event.logIndex.toString()}`)
currentRental.nft = nftId
currentRental.lender = event.params.param0.lender.toHexString().toLowerCase()
currentRental.borrower = event.params.param0.borrower.toHexString().toLowerCase()
currentRental.startedAt = event.block.timestamp
currentRental.startedTxHash = event.transaction.hash.toHex()
currentRental.rentalOffer = currentRentalOfferId
currentRental.expirationDate = event.block.timestamp.plus(event.params.param0.period)
currentRental.beneficiaries = [currentRental.lender, currentRental.borrower, event.params.param0.thirdParty.toHexString().toLowerCase()]
currentRental.save()

// remove current rental offer from nft, because it has been executed, and link rental to nft
nft.currentRentalOffer = null
nft.currentRental = currentRental.id
// Since aavegotchi only allows one offer at a time, we can set the expiration date to zero
nft.lastOfferExpirationAt = BigInt.zero();
nft.save()

log.warning('[handleGotchiLendingExecuted] NFT {} has been rented, rentalId: {}, rentalOfferId: {}', [
nftId,
currentRental.id,
currentRentalOfferId!,
])
}
4 changes: 4 additions & 0 deletions src/aavegotchi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export { handleAlchemicaClaimed } from "./realm/alchemica-claimed-handler";
export { handleTransferTokensToGotchi } from "./realm/transfer-tokens-to-gotchi-handler";
export { handleParcelAccessRightSet } from "./realm/parcel-access-right-set-handler";
export { handleParcelWhitelistSet } from "./realm/parcel-whitelist-set-handler";
export { handleGotchiLendingAdded2 } from "./gotchi/gotchi-lending-added2-handler";
export { handleGotchiLendingExecuted2 } from "./gotchi/gotchi-lending-executed2-handler";
export { handleGotchiLendingEnded2 } from "./gotchi/gotchi-lending-ended2-handler";
export { handleGotchiLendingCancelled2 } from "./gotchi/gotchi-lending-cancelled2-handler";
Loading

0 comments on commit ba99565

Please sign in to comment.