Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix token supply on mint #160

Merged
2 commits merged into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

32 changes: 14 additions & 18 deletions backend/Application/Api/GraphQL/Import/EventLogs/EventLogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,31 @@ public List<CisAccountUpdate> HandleLogs(TransactionPair[] transactions)

//Select Cis Events
var cisEvents = events
.Select(e => ParseCisEvent(e.TxId, e.Address, e.ContractEvent.Bytes))
.Select(e => ParseCisEvent(e.Address, e.ContractEvent.Bytes))
.Where(e => e != null)
.Cast<CisEvent>();
.Cast<CisEvent>()
.ToList();

//Handle Smart Contract Cis Events Logs
var updates = cisEvents.Select(e => new
{
TokenUpdate = GetTokenUpdates(e),
AccountUpdates = GetAccountUpdates(e)
});
}).ToList();

IEnumerable<CisEventTokenUpdate> tokenUpdates = updates
var tokenUpdates = updates
.Where(u => u.TokenUpdate != null)
.Select(u => u.TokenUpdate)
.Cast<CisEventTokenUpdate>();
.Cast<CisEventTokenUpdate>()
.ToList();
var accountUpdates = updates.SelectMany(a => a.AccountUpdates).ToList();

if (tokenUpdates.Count() > 0)
if (tokenUpdates.Any())
{
writer.ApplyTokenUpdates(tokenUpdates);
}

if (accountUpdates.Count() > 0)
if (accountUpdates.Any())
{
writer.ApplyAccountUpdates(accountUpdates);
}
Expand All @@ -85,7 +87,7 @@ private CisAccountUpdate[] GetAccountUpdates(CisEvent log)
case CisBurnEvent e:
if (e.FromAddress is CisEventAddressAccount accntAddress)
{
return new CisAccountUpdate[] {
return new[] {
new CisAccountUpdate()
{
ContractIndex = e.ContractIndex,
Expand Down Expand Up @@ -128,7 +130,7 @@ private CisAccountUpdate[] GetAccountUpdates(CisEvent log)
case CisMintEvent e:
if (e.ToAddress is CisEventAddressAccount accntAddress4)
{
return new CisAccountUpdate[] {new CisAccountUpdate()
return new[] {new CisAccountUpdate()
{
ContractIndex = e.ContractIndex,
ContractSubIndex = e.ContractSubIndex,
Expand Down Expand Up @@ -162,7 +164,7 @@ private CisAccountUpdate[] GetAccountUpdates(CisEvent log)
AmountDelta = log.TokenAmount * -1
};
case CisMintEvent log:
return new CisEventTokenAddedUpdate()
return new CisEventTokenAmountUpdate()
{
ContractIndex = log.ContractIndex,
ContractSubIndex = log.ContractSubIndex,
Expand Down Expand Up @@ -190,16 +192,10 @@ private CisAccountUpdate[] GetAccountUpdates(CisEvent log)
/// <param name="address">CIS Contract Address</param>
/// <param name="bytes">Input bytes</param>
/// <returns></returns>
private CisEvent? ParseCisEvent(
long txnId,
private static CisEvent? ParseCisEvent(
Concordium.Sdk.Types.ContractAddress address,
byte[] bytes)
{
if (!CisEvent.IsCisEvent(bytes))
{
return null;
}

CisEvent cisEvent;
if (!CisEvent.TryParse(bytes, address, out cisEvent))
{
Expand All @@ -209,4 +205,4 @@ private CisAccountUpdate[] GetAccountUpdates(CisEvent log)
return cisEvent;
}
}
}
}
35 changes: 8 additions & 27 deletions backend/Application/Api/GraphQL/Import/EventLogs/EventLogWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public int ApplyTokenUpdates(IEnumerable<CisEventTokenUpdate> tokenUpdates)
case CisEventTokenAmountUpdate e:
batch.BatchCommands.Add(CreateTokenAmountUpdateCmd(cmd, e));
break;
case CisEventTokenAddedUpdate e:
batch.BatchCommands.Add(CreateTokenAddedCmd(cmd, e));
break;
case CisEventTokenMetadataUpdate e:
batch.BatchCommands.Add(CreateTokenMetadataUpdatedCmd(cmd, e));
break;
Expand All @@ -70,39 +67,23 @@ public int ApplyTokenUpdates(IEnumerable<CisEventTokenUpdate> tokenUpdates)
return updates;
}

private DbBatchCommand CreateTokenAmountUpdateCmd(DbBatchCommand cmd, CisEventTokenAmountUpdate e)
{
cmd.CommandText = @"
UPDATE graphql_tokens
SET total_supply = total_supply + @AmountDelta
WHERE contract_index = @ContractIndex
AND contract_sub_index = @ContractSubIndex
AND token_id = @TokenId";
cmd.Parameters.Add(new NpgsqlParameter<long>("ContractIndex", Convert.ToInt64(e.ContractIndex)));
cmd.Parameters.Add(new NpgsqlParameter<long>("ContractSubIndex", Convert.ToInt64(e.ContractSubIndex)));
cmd.Parameters.Add(new NpgsqlParameter<string>("TokenId", e.TokenId));
cmd.Parameters.Add(new NpgsqlParameter<BigInteger>("AmountDelta", e.AmountDelta));

return cmd;
}

private DbBatchCommand CreateTokenAddedCmd(DbBatchCommand cmd, CisEventTokenAddedUpdate e)
private static DbBatchCommand CreateTokenAmountUpdateCmd(DbBatchCommand cmd, CisEventTokenAmountUpdate e)
{
cmd.CommandText = @"
INSERT INTO graphql_tokens(contract_index, contract_sub_index, token_id, total_supply)
VALUES (@ContractIndex, @ContractSubIndex, @TokenId, @TotalSupply)
VALUES (@ContractIndex, @ContractSubIndex, @TokenId, @AmountDelta)
ON CONFLICT ON CONSTRAINT graphql_tokens_pkey
DO NOTHING";

DO UPDATE SET total_supply = graphql_tokens.total_supply + @AmountDelta";
cmd.Parameters.Add(new NpgsqlParameter<long>("ContractIndex", Convert.ToInt64(e.ContractIndex)));
cmd.Parameters.Add(new NpgsqlParameter<long>("ContractSubIndex", Convert.ToInt64(e.ContractSubIndex)));
cmd.Parameters.Add(new NpgsqlParameter<string>("TokenId", e.TokenId));
cmd.Parameters.Add(new NpgsqlParameter<BigInteger>("TotalSupply", e.AmountDelta));
cmd.Parameters.Add(new NpgsqlParameter<BigInteger>("AmountDelta", e.AmountDelta));

return cmd;
}

private DbBatchCommand CreateTokenMetadataUpdatedCmd(DbBatchCommand cmd, CisEventTokenMetadataUpdate e)
private static DbBatchCommand CreateTokenMetadataUpdatedCmd(DbBatchCommand cmd, CisEventTokenMetadataUpdate e)
{
cmd.CommandText = @"
UPDATE graphql_tokens
Expand All @@ -128,7 +109,7 @@ public int ApplyAccountUpdates(IEnumerable<CisAccountUpdate> accountUpdates)
{
IEnumerable<string> accountBaseAddresses = accountUpdates.Select(u => u.Address.GetBaseAddress().ToString()).Distinct();
var accountsMap = this._accountLookup.GetAccountIdsFromBaseAddresses(accountBaseAddresses);
using var counter = _metrics.MeasureDuration(nameof(EventLogWriter), nameof(ApplyTokenUpdates));
using var counter = _metrics.MeasureDuration(nameof(EventLogWriter), nameof(ApplyAccountUpdates));

using var context = _dbContextFactory.CreateDbContext();
var connection = context.Database.GetDbConnection();
Expand Down Expand Up @@ -170,4 +151,4 @@ ON CONFLICT ON CONSTRAINT graphql_account_tokens_pk
return updates;
}
}
}
}
2 changes: 2 additions & 0 deletions backend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## Unreleased changes
- Bugfix
- Fix total token supply

## 1.8.1
- Added mapping for chain update events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public EventLogWriterTest(DatabaseFixture dbFixture)
public void ShouldHandleTokenUpdates()
{
var updatesCount = writer.ApplyTokenUpdates(new List<CisEventTokenUpdate>() {
new CisEventTokenAddedUpdate() { TokenId = TOKEN_1_ID, AmountDelta = 0, ContractIndex = 1, ContractSubIndex = 0 },
new CisEventTokenAddedUpdate() { TokenId = TOKEN_2_ID, AmountDelta = 2, ContractIndex = 2, ContractSubIndex = 0 },
new CisEventTokenAmountUpdate() { TokenId = TOKEN_1_ID, AmountDelta = 0, ContractIndex = 1, ContractSubIndex = 0 },
new CisEventTokenAmountUpdate() { TokenId = TOKEN_2_ID, AmountDelta = 2, ContractIndex = 2, ContractSubIndex = 0 },
new CisEventTokenAmountUpdate() { TokenId = TOKEN_1_ID, AmountDelta = 1, ContractIndex= 1, ContractSubIndex = 0 },
new CisEventTokenAmountUpdate() { TokenId = TOKEN_2_ID, AmountDelta = -1, ContractIndex= 2, ContractSubIndex = 0 },
new CisEventTokenMetadataUpdate() {
Expand Down Expand Up @@ -100,4 +100,4 @@ public void ShouldHandleAccountUpdates()
accntToken.Balance.Should().Be(2);
}
}
}
}
Loading