-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from OriumNetwork/aavegotchi-update
Aavegotchi update
- Loading branch information
Showing
8 changed files
with
10,349 additions
and
7,718 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
73
src/aavegotchi/gotchi/gotchi-lending-cancelled2-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!, | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.