Skip to content

Commit

Permalink
runtime: extract possible NFTs
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Sep 27, 2023
1 parent 849d755 commit 615e858
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,13 @@ var (
runtime = $1 AND
token_address = $2`

RuntimeEVMNFTInsert = `
INSERT INTO chain.evm_nfts
(runtime, token_address, nft_id, last_want_download_round)
VALUES
($1, $2, $3, $4)
ON CONFLICT (runtime, token_address, nft_id) DO NOTHING`

RuntimeEVMTokenBalanceAnalysisStale = fmt.Sprintf(`
WITH
max_processed_round AS (
Expand Down
16 changes: 16 additions & 0 deletions analyzer/runtime/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ type TokenChangeKey struct {
AccountAddress apiTypes.Address
}

type NFTKey struct {
TokenAddress apiTypes.Address
TokenID *big.Int
}

type BlockData struct {
Header nodeapi.RuntimeBlockHeader
NumTransactions int // Might be different from len(TransactionData) if some transactions are malformed.
Expand All @@ -118,6 +123,7 @@ type BlockData struct {
AddressPreimages map[apiTypes.Address]*AddressPreimageData
TokenBalanceChanges map[TokenChangeKey]*big.Int
PossibleTokens map[apiTypes.Address]*evm.EVMPossibleToken // key is oasis bech32 address
PossibleNFTs map[NFTKey]struct{}
}

// Function naming conventions in this file:
Expand Down Expand Up @@ -235,6 +241,13 @@ func registerRelatedEthAddress(addressPreimages map[apiTypes.Address]*AddressPre
return addr, nil
}

func registerNFTExist(possibleNFTs map[NFTKey]struct{}, contractAddr apiTypes.Address, tokenID *big.Int) {
key := NFTKey{contractAddr, tokenID}
if _, ok := possibleNFTs[key]; !ok {
possibleNFTs[key] = struct{}{}
}
}

func findTokenChange(tokenChanges map[TokenChangeKey]*big.Int, contractAddr apiTypes.Address, accountAddr apiTypes.Address) *big.Int {
key := TokenChangeKey{contractAddr, accountAddr}
change, ok := tokenChanges[key]
Expand Down Expand Up @@ -264,6 +277,7 @@ func ExtractRound(blockHeader nodeapi.RuntimeBlockHeader, txrs []nodeapi.Runtime
AddressPreimages: map[apiTypes.Address]*AddressPreimageData{},
TokenBalanceChanges: map[TokenChangeKey]*big.Int{},
PossibleTokens: map[apiTypes.Address]*evm.EVMPossibleToken{},
PossibleNFTs: map[NFTKey]struct{}{},
}

// Extract info from non-tx events.
Expand Down Expand Up @@ -882,6 +896,7 @@ func extractEvents(blockData *BlockData, relatedAccountAddresses map[apiTypes.Ad
pt.TotalSupplyChange.Sub(&pt.TotalSupplyChange, big.NewInt(1))
pt.Mutated = true
}
registerNFTExist(blockData.PossibleNFTs, eventAddr, tokenID)
eventData.EvmLogName = evmabi.ERC721.Events["Transfer"].Name
eventData.EvmLogSignature = ethCommon.BytesToHash(event.Topics[0])
eventData.EvmLogParams = []*apiTypes.EvmEventParam{
Expand Down Expand Up @@ -925,6 +940,7 @@ func extractEvents(blockData *BlockData, relatedAccountAddresses map[apiTypes.Ad
}
tokenID := &big.Int{}
tokenID.SetBytes(tokenIDU256)
registerNFTExist(blockData.PossibleNFTs, eventAddr, tokenID)
eventData.EvmLogName = evmabi.ERC721.Events["Approval"].Name
eventData.EvmLogSignature = ethCommon.BytesToHash(event.Topics[0])
eventData.EvmLogParams = []*apiTypes.EvmEventParam{
Expand Down
5 changes: 5 additions & 0 deletions analyzer/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,9 @@ func (m *processor) queueDbUpdates(batch *storage.QueryBatch, data *BlockData) {
// verify the correct balance by querying the EVM.
batch.Queue(queries.RuntimeEVMTokenBalanceAnalysisInsert, m.runtime, key.TokenAddress, key.AccountAddress, data.Header.Round)
}

// Insert NFTs.
for key := range data.PossibleNFTs {
batch.Queue(queries.RuntimeEVMNFTInsert, m.runtime, key.TokenAddress, key.TokenID, data.Header.Round)
}
}

0 comments on commit 615e858

Please sign in to comment.