processor.addEventHandler('balances.Transfer', async ctx => {
let transfer = getTransferEvent(ctx)
let tip = ctx.extrinsic?.tip || 0n
let from = ss58.codec('kusama').encode(transfer.from)
let to = ss58.codec('kusama').encode(transfer.to)
let fromAcc = await getOrCreate(ctx.store, Account, from)
fromAcc.balance = fromAcc.balance || 0n
fromAcc.balance -= transfer.amount
await ctx.store.save(fromAcc)
const toAcc = await getOrCreate(ctx.store, Account, to)
toAcc.balance = toAcc.balance || 0n
toAcc.balance += transfer.amount
await ctx.store.save(toAcc)
await ctx.store.save(new HistoricalBalance({
id: ctx.event.id + '-to',
balance: fromAcc.balance,
date: new Date(ctx.block.timestamp)
await ctx.store.save(new HistoricalBalance({
id: ctx.event.id + '-from',
date: new Date(ctx.block.timestamp)
interface TransferEvent {
function getTransferEvent(ctx: EventHandlerContext): TransferEvent {
let event = new BalancesTransferEvent(ctx)
let [from, to, amount] = event.asV1020
return {from, to, amount}
} else if (event.isV1050) {
let [from, to, amount] = event.asV1050
return {from, to, amount}