Skip to content
Snippets Groups Projects

add balance to accounts

Open poka requested to merge account-balance into main
Compare and Show latest version
3 files
+ 261
8
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 78
7
@@ -94,11 +94,87 @@ export class DataHandler {
ctx.log.info(
`New transaction: ${transfer.from} to ${transfer.to} of ${transfer.amount} tokens`
);
// should never fail because source of transfer must be an existing account
const fromAccount = await this.getAccountByAddressOrFail(ctx, transfer.from);
// shoud never fail because destination of transfer must be existing account or raise System.NewAccount
const toAccount = await this.getAccountByAddressOrFail(ctx, transfer.to);
const newTransfer = new Transfer({
id: transfer.id,
blockNumber: transfer.blockNumber,
timestamp: transfer.timestamp,
from: fromAccount,
to: toAccount,
amount: transfer.amount,
});
this.data.transfers.set(transfer.id, newTransfer);
// Update balances
await this.updateAccountBalance(ctx, transfer.from, -transfer.amount);
await this.updateAccountBalance(ctx, transfer.to, transfer.amount);
}
// Process deposits
for (const deposit of newData.deposits) {
ctx.log.info(
`New deposit: ${deposit.address} received ${deposit.amount} tokens`
);
await this.updateAccountBalance(ctx, deposit.address, deposit.amount);
}
// Process withdraws
for (const withdraw of newData.withdraws) {
ctx.log.info(
`New withdraw: ${withdraw.address} withdrew ${withdraw.amount} tokens`
);
await this.updateAccountBalance(ctx, withdraw.address, -withdraw.amount);
}
// Process reserved amounts
for (const reserved of newData.reserved) {
ctx.log.info(
`New reserve: ${reserved.address} reserved ${reserved.amount} tokens`
);
await this.updateAccountBalance(ctx, reserved.address, -reserved.amount);
}
// Process unreserved amounts
for (const unreserved of newData.unreserved) {
ctx.log.info(
`New unreserve: ${unreserved.address} unreserved ${unreserved.amount} tokens`
);
await this.updateAccountBalance(ctx, unreserved.address, unreserved.amount);
}
// Process endowed accounts
for (const endowed of newData.endowed) {
ctx.log.info(
`New endowed account: ${endowed.address} received initial balance of ${endowed.amount} tokens`
);
const account = await this.getOrCreateAccount(ctx, endowed.address);
account.createdOn = endowed.blockNumber;
account.balance = endowed.amount;
this.data.accounts.set(endowed.address, account);
}
// Process dust lost accounts
for (const dustLost of newData.dustLost) {
ctx.log.info(
`Dust lost: ${dustLost.address} lost ${dustLost.amount} tokens due to falling below existential deposit`
);
const account = await this.getAccountByAddressOrFail(ctx, dustLost.address);
account.balance = 0n;
account.isActive = false;
this.data.accounts.set(dustLost.address, account);
}
// Process slashed accounts
for (const slashed of newData.slashed) {
ctx.log.info(
`Account slashed: ${slashed.address} lost ${slashed.amount} tokens due to misbehavior`
);
await this.updateAccountBalance(ctx, slashed.address, -slashed.amount);
}
// Process comments
for (const commentEvt of newData.comments) {
const sender = await this.getAccountByAddressOrFail(ctx, commentEvt.sender);
@@ -543,7 +619,7 @@ export class DataHandler {
// Distribute UD to each member
for (const identity of memberIdentities) {
await this.handleUniversalDividend(ctx, await ctx.store.getOrFail(Event, event.id), identity, BigInt(amount));
await this.updateAccountBalance(ctx, identity.account!.id, amount); // we are sure that a non-removed identity has non-null account
}
this.data.universalDividend.push(new UniversalDividend({
@@ -673,6 +749,7 @@ export class DataHandler {
// we need to create it
account = new Account({
id,
createdOn: ctx.blocks[0].header.height,
isActive: true,
balance: 0n
});
@@ -741,10 +818,4 @@ export class DataHandler {
account.balance = (account.balance || 0n) + amount;
this.data.accounts.set(accountId, account);
}
async handleUniversalDividend(ctx: Ctx, event: Event, identity: Identity, amount: bigint): Promise<void> {
if (identity.account) {
await this.updateAccountBalance(ctx, identity.account.id, amount);
}
}
}
Loading