Skip to content

Commit

Permalink
Added handling of new Smart contracts (#189)
Browse files Browse the repository at this point in the history
* Added mark as used for NFTs

* Added readers for methods that can not be indexed

---------

Co-authored-by: TheDude <[email protected]>
  • Loading branch information
DavidGershony and TheDude authored May 12, 2023
1 parent 39cfbae commit f8a0ad7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Blockcore.Indexer.Cirrus/Models/QueryAddressAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class QueryAddressAsset
public string Creator { get; set; }
public string Uri { get; set; }
public bool IsBurned { get; set; }

public bool IsUsed { get; set; }
public long? PricePaid { get; set; }
public string TransactionId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ public async Task<QueryResult<QueryAddressAsset>> GetNonFungibleTokensForAddress
Id = _.Id.TokenId,
Uri = _.Uri,
IsBurned = _.IsBurned,
IsUsed = _.IsUsed,
TransactionId = _.SalesHistory.LastOrDefault()?.TransactionId,
PricePaid = _.SalesHistory.LastOrDefault() switch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Blockcore.Indexer.Cirrus.Client.Types;
using Blockcore.Indexer.Cirrus.Storage.Mongo.Types;
using MongoDB.Driver;

namespace Blockcore.Indexer.Cirrus.Storage.Mongo.SmartContracts.NonFungibleToken;

public class GetMethodsLogReader : ILogReader<NonFungibleTokenContractTable,NonFungibleTokenTable>
{
public bool CanReadLogForMethodType(string methodType) => methodType is "TokenURI" or "RoyaltyInfo";

public bool IsTransactionLogComplete(LogResponse[] logs) => true;

public WriteModel<NonFungibleTokenTable>[] UpdateContractFromTransactionLog(CirrusContractTable contractTransaction,
NonFungibleTokenContractTable computedTable) => new WriteModel<NonFungibleTokenTable>[]{};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;
using Blockcore.Indexer.Cirrus.Client.Types;
using Blockcore.Indexer.Cirrus.Storage.Mongo.Types;
using MongoDB.Driver;

namespace Blockcore.Indexer.Cirrus.Storage.Mongo.SmartContracts.NonFungibleToken;

public class MarkAsUsedLogReader : ILogReader<NonFungibleTokenContractTable,NonFungibleTokenTable>
{
public bool CanReadLogForMethodType(string methodType) => methodType.Equals("MarkAsUsed");

public bool IsTransactionLogComplete(LogResponse[] logs) => logs?.Any(_ => _.Log.Event == "MarkAsUsedLog") ?? false;

public WriteModel<NonFungibleTokenTable>[] UpdateContractFromTransactionLog(CirrusContractTable contractTransaction,
NonFungibleTokenContractTable computedTable)
{
var log = contractTransaction.Logs.FirstOrDefault(_ => _.Log.Event == "MarkAsUsedLog")?.Log
?? throw new InvalidOperationException($"missing MarkAsUsedLog in MarkAsRead transaction - {contractTransaction.TransactionId}");

object tokenId = log.Data["tokenId"];
string id = tokenId is string ? (string)tokenId : Convert.ToString(tokenId);

return new[]
{
new UpdateOneModel<NonFungibleTokenTable>(Builders<NonFungibleTokenTable>.Filter
.Where(_ => _.Id.TokenId == tokenId && _.Id.ContractAddress == computedTable.ContractAddress),
Builders<NonFungibleTokenTable>.Update.Set(_ => _.IsUsed, true))
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class SafeMintLogReader : ILogReader<NonFungibleTokenContractTable, Types
public WriteModel<Types.NonFungibleTokenTable>[] UpdateContractFromTransactionLog(CirrusContractTable contractTransaction,
NonFungibleTokenContractTable computedTable)
{
LogData log = contractTransaction.Logs[0].Log;
LogData saleLog = contractTransaction.Logs[1].Log;
LogData uriLog = contractTransaction.Logs[2].Log;
LogData log = contractTransaction.Logs.First(_ => _.Log.Event == "TransferLog").Log;
LogData saleLog = contractTransaction.Logs[1].Log; //TODO change this to get a list from sales event reader
LogData uriLog = contractTransaction.Logs.First(_ => _.Log.Event == "MintExtract").Log;

object tokenId = log.Data["tokenId"];
string id = tokenId is string ? (string)tokenId : Convert.ToString(tokenId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Blockcore.Indexer.Cirrus.Client.Types;
using Blockcore.Indexer.Cirrus.Storage.Mongo.Types;
using MongoDB.Driver;

namespace Blockcore.Indexer.Cirrus.Storage.Mongo.SmartContracts.NonFungibleToken;

public class SetRoyaltiesLogReader : ILogReader<NonFungibleTokenContractTable,NonFungibleTokenTable>
{
public bool CanReadLogForMethodType(string methodType) => methodType is "SetRoyalties";

public bool IsTransactionLogComplete(LogResponse[] logs) => logs.Length > 0;

public WriteModel<NonFungibleTokenTable>[] UpdateContractFromTransactionLog(CirrusContractTable contractTransaction,
NonFungibleTokenContractTable computedTable)
{
//TODO the logs are empty for this method so we need to find another way to get the data

return new WriteModel<NonFungibleTokenTable>[]{};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class NonFungibleTokenTable : SmartContractData
public string Owner { get; set; }
public string Uri { get; set; }
public bool IsBurned { get; set; }

public bool IsUsed { get; set; }
public List<TokenSaleEvent> SalesHistory { get; set; } = new();
}

0 comments on commit f8a0ad7

Please sign in to comment.