forked from decentraland/marketplace
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: index sales (decentraland#451)
- Loading branch information
Showing
12 changed files
with
15,731 additions
and
35,766 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
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
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
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
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
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
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
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,21 @@ | ||
import { BigInt, Address } from '@graphprotocol/graph-ts' | ||
import { Account } from '../../entities/schema' | ||
|
||
export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' | ||
|
||
export function createOrLoadAccount(id: Address): Account { | ||
let account = Account.load(id.toHex()) | ||
|
||
if (account == null) { | ||
account = new Account(id.toHex()) | ||
account.address = id | ||
account.sales = 0 | ||
account.purchases = 0 | ||
account.earned = BigInt.fromI32(0) | ||
account.spent = BigInt.fromI32(0) | ||
} | ||
|
||
account.save() | ||
|
||
return account! | ||
} |
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,57 @@ | ||
import { Address, BigInt, Bytes, log } from '@graphprotocol/graph-ts' | ||
import { NFT, Sale } from '../../entities/schema' | ||
import { createOrLoadAccount } from '../account' | ||
import { buildCountFromSale } from '../count' | ||
|
||
export let BID_SALE_TYPE = 'bid' | ||
export let ORDER_SALE_TYPE = 'order' | ||
|
||
export function trackSale( | ||
type: string, | ||
buyer: Address, | ||
seller: Address, | ||
nftId: string, | ||
price: BigInt, | ||
timestamp: BigInt, | ||
txHash: Bytes | ||
): void { | ||
// count sale | ||
let count = buildCountFromSale(price) | ||
count.save() | ||
|
||
// load entities | ||
let nft = NFT.load(nftId) | ||
|
||
// save sale | ||
let saleId = BigInt.fromI32(count.salesTotal).toString() | ||
let sale = new Sale(saleId) | ||
sale.type = type | ||
sale.buyer = buyer | ||
sale.seller = seller | ||
sale.price = price | ||
sale.nft = nftId | ||
sale.timestamp = timestamp | ||
sale.txHash = txHash | ||
sale.searchTokenId = nft.tokenId | ||
sale.searchContractAddress = nft.contractAddress | ||
sale.save() | ||
|
||
// update buyer account | ||
let buyerAccount = createOrLoadAccount(buyer) | ||
buyerAccount.purchases += 1 | ||
buyerAccount.spent = buyerAccount.spent.plus(price) | ||
buyerAccount.save() | ||
|
||
// update seller account | ||
let sellerAccount = createOrLoadAccount(seller) | ||
sellerAccount.sales += 1 | ||
sellerAccount.earned = sellerAccount.earned.plus(price) | ||
sellerAccount.save() | ||
|
||
// update nft | ||
nft.soldAt = timestamp | ||
nft.sales += 1 | ||
nft.volume = nft.volume.plus(price) | ||
nft.updatedAt = timestamp | ||
nft.save() | ||
} |
Oops, something went wrong.