Skip to content

Use batch processor for balances #129

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

Open
wants to merge 3 commits into
base: feature/firesquid
Choose a base branch
from
Open
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
152 changes: 152 additions & 0 deletions prawns/balances/src/handleBalanceChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import {
SubstrateBalanceAccount,
SubstrateBalanceChangeEvent,
SubstrateBalanceChangeEventType,
SubstrateBalanceTransfer,
SubstrateNetwork,
} from './model';
import { EventProcessorParams } from './types/custom';
import { decodeAddress, encodeAddress, getRegistry } from './utils';

export default function handleBalanceChange({
eventType,
processorParams: { blockNumber, date, network, item, models },
eventParams: { account, amount, from },
}: {
eventType: SubstrateBalanceChangeEventType;
processorParams: EventProcessorParams;
eventParams: {
account: Uint8Array;
amount: bigint;
from?: Uint8Array; // presence indicates it's a transfer
};
}) {
const symbol = getRegistry(network).symbols[0];
const decimals = getRegistry(network).decimals[0];
const id = encodeAddress(network, account);
const publicKey = decodeAddress(account);
const fromId = from ? encodeAddress(network, from) : null;

const accountModel = upsertAccount(
models.accounts,
{
id,
publicKey,
blockNumber,
date,
network,
symbol,
decimals,
},
!!from
);

models.changeEvents.push(
new SubstrateBalanceChangeEvent({
id: `${network}:${blockNumber.toString()}:${item.event.pos}`,
network,
account: accountModel,
type: eventType,
symbol,
decimals,
amount,
blockNumber,
date,
})
);

if (fromId) {
const fromAccount = upsertAccount(
models.accounts,
{
id: fromId,
publicKey: decodeAddress(fromId),
blockNumber,
date,
network,
symbol,
decimals,
},
true
);

models.changeEvents.push(
new SubstrateBalanceChangeEvent({
id: `${network}:${blockNumber.toString()}:${item.event.pos}.from`,
network,
account: fromAccount,
type: eventType,
symbol,
decimals,
amount: -amount,
blockNumber,
date,
})
);

models.transfers.push(
new SubstrateBalanceTransfer({
id: `${network}:${blockNumber.toString()}:${item.event.pos}`,
network,
from: fromAccount,
to: accountModel,
symbol,
decimals,
amount,
blockNumber,
date,
})
);
}
}

function upsertAccount(
models: SubstrateBalanceAccount[],
{
id,
publicKey,
blockNumber,
date,
network,
symbol,
decimals,
}: {
id: string;
publicKey: string;
blockNumber: bigint;
date: Date;
network: SubstrateNetwork;
decimals: number;
symbol: string;
},
isTransfer = false
): SubstrateBalanceAccount {
let model = models.find((acc) => acc.id === id);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit picky... but feel like this would be easier to follow if the condition between creating a model and changing it were separated. Something like

let model = models[existingAccountIndex];
if (!model) {
    model = new SubstrateBalanceAccount({
        id,
        publicKey,
        network,
        symbol,
        decimals,
        firstBalanceChangeEventDate: date,
        firstBalanceChangeEventBlockNumber: blockNumber,
        lastBalanceChangeEventDate: date,
        lastBalanceChangeEventBlockNumber: blockNumber,
        totalTransfers: 0,
        totalBalanceChangeEvents: 0,
      });
     models.push(model);
}

model.lastBalanceChangeEventDate = date;
model.lastBalanceChangeEventBlockNumber =
      blockNumber;
model.totalBalanceChangeEvents += 1;
if (isTransfer) {
      model.totalTransfers += 1;
    }
      

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's easier to read, updated

if (!model) {
model = new SubstrateBalanceAccount({
id,
publicKey,
network,
symbol,
decimals,
firstBalanceChangeEventDate: date,
firstBalanceChangeEventBlockNumber: blockNumber,
lastBalanceChangeEventDate: date,
lastBalanceChangeEventBlockNumber: blockNumber,
totalTransfers: 0,
totalBalanceChangeEvents: 0,
});
models.push(model);
}

model.lastBalanceChangeEventDate = date;
model.lastBalanceChangeEventBlockNumber = blockNumber;
model.totalBalanceChangeEvents += 1;

if (isTransfer) {
model.totalTransfers += 1;
}

return model;
}
19 changes: 0 additions & 19 deletions prawns/balances/src/handlers/balances.balanceset.event.ts

This file was deleted.

19 changes: 0 additions & 19 deletions prawns/balances/src/handlers/balances.deposit.event.ts

This file was deleted.

19 changes: 0 additions & 19 deletions prawns/balances/src/handlers/balances.endowed.event.ts

This file was deleted.

20 changes: 0 additions & 20 deletions prawns/balances/src/handlers/balances.transfer.event.ts

This file was deleted.

125 changes: 0 additions & 125 deletions prawns/balances/src/handlers/handleBalanceChange.ts

This file was deleted.

19 changes: 0 additions & 19 deletions prawns/balances/src/handlers/treasury.awarded.event.ts

This file was deleted.

Loading