From f8a0ad7e7a734737747219d30b71befc5a8b5a0d Mon Sep 17 00:00:00 2001 From: TheDude Date: Fri, 12 May 2023 11:04:38 +0100 Subject: [PATCH] Added handling of new Smart contracts (#189) * Added mark as used for NFTs * Added readers for methods that can not be indexed --------- Co-authored-by: TheDude --- .../Models/QueryAddressAsset.cs | 2 ++ .../Storage/Mongo/CirrusMongoData.cs | 1 + .../NonFungibleToken/GetMethodsLogReader.cs | 15 +++++++++ .../NonFungibleToken/MarkAsUsedLogReader.cs | 31 +++++++++++++++++++ .../NonFungibleToken/SafeMintLogReader.cs | 6 ++-- .../NonFungibleToken/SetRoyaltiesLogReader.cs | 21 +++++++++++++ .../Mongo/Types/NonFungibleTokenTable.cs | 2 +- 7 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/GetMethodsLogReader.cs create mode 100644 src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/MarkAsUsedLogReader.cs create mode 100644 src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/SetRoyaltiesLogReader.cs diff --git a/src/Blockcore.Indexer.Cirrus/Models/QueryAddressAsset.cs b/src/Blockcore.Indexer.Cirrus/Models/QueryAddressAsset.cs index d75ee2e8..9acef1f7 100644 --- a/src/Blockcore.Indexer.Cirrus/Models/QueryAddressAsset.cs +++ b/src/Blockcore.Indexer.Cirrus/Models/QueryAddressAsset.cs @@ -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; } diff --git a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/CirrusMongoData.cs b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/CirrusMongoData.cs index 16ab73a9..4f6abc2c 100644 --- a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/CirrusMongoData.cs +++ b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/CirrusMongoData.cs @@ -407,6 +407,7 @@ public async Task> GetNonFungibleTokensForAddress Id = _.Id.TokenId, Uri = _.Uri, IsBurned = _.IsBurned, + IsUsed = _.IsUsed, TransactionId = _.SalesHistory.LastOrDefault()?.TransactionId, PricePaid = _.SalesHistory.LastOrDefault() switch { diff --git a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/GetMethodsLogReader.cs b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/GetMethodsLogReader.cs new file mode 100644 index 00000000..1981b509 --- /dev/null +++ b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/GetMethodsLogReader.cs @@ -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 +{ + public bool CanReadLogForMethodType(string methodType) => methodType is "TokenURI" or "RoyaltyInfo"; + + public bool IsTransactionLogComplete(LogResponse[] logs) => true; + + public WriteModel[] UpdateContractFromTransactionLog(CirrusContractTable contractTransaction, + NonFungibleTokenContractTable computedTable) => new WriteModel[]{}; +} diff --git a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/MarkAsUsedLogReader.cs b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/MarkAsUsedLogReader.cs new file mode 100644 index 00000000..4a1890d2 --- /dev/null +++ b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/MarkAsUsedLogReader.cs @@ -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 +{ + public bool CanReadLogForMethodType(string methodType) => methodType.Equals("MarkAsUsed"); + + public bool IsTransactionLogComplete(LogResponse[] logs) => logs?.Any(_ => _.Log.Event == "MarkAsUsedLog") ?? false; + + public WriteModel[] 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(Builders.Filter + .Where(_ => _.Id.TokenId == tokenId && _.Id.ContractAddress == computedTable.ContractAddress), + Builders.Update.Set(_ => _.IsUsed, true)) + }; + } +} diff --git a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/SafeMintLogReader.cs b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/SafeMintLogReader.cs index 37e1a9c8..6faace0b 100644 --- a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/SafeMintLogReader.cs +++ b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/SafeMintLogReader.cs @@ -16,9 +16,9 @@ public class SafeMintLogReader : ILogReader[] 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); diff --git a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/SetRoyaltiesLogReader.cs b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/SetRoyaltiesLogReader.cs new file mode 100644 index 00000000..aad04130 --- /dev/null +++ b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/SmartContracts/NonFungibleToken/SetRoyaltiesLogReader.cs @@ -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 +{ + public bool CanReadLogForMethodType(string methodType) => methodType is "SetRoyalties"; + + public bool IsTransactionLogComplete(LogResponse[] logs) => logs.Length > 0; + + public WriteModel[] 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[]{}; + } +} diff --git a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/Types/NonFungibleTokenTable.cs b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/Types/NonFungibleTokenTable.cs index 555d8863..d327e9e1 100644 --- a/src/Blockcore.Indexer.Cirrus/Storage/Mongo/Types/NonFungibleTokenTable.cs +++ b/src/Blockcore.Indexer.Cirrus/Storage/Mongo/Types/NonFungibleTokenTable.cs @@ -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 SalesHistory { get; set; } = new(); }