-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffe1876
commit b54cdb4
Showing
5 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Balances } from '../../types'; | ||
import { b64decode } from '../utils'; | ||
import { CosmosEvent } from '@subql/types-cosmos'; | ||
|
||
interface Attribute { | ||
key: string; | ||
value: string; | ||
} | ||
|
||
export interface DecodedEvent { | ||
type: string; | ||
attributes: Attribute[]; | ||
} | ||
export const balancesEventKit = () => { | ||
function decodeEvent(cosmosEvent: CosmosEvent): DecodedEvent { | ||
const { event } = cosmosEvent; | ||
|
||
const decodedData: DecodedEvent = { | ||
type: event.type, | ||
attributes: [], | ||
}; | ||
|
||
event.attributes.forEach((attribute) => { | ||
const decodedKey = b64decode(attribute.key); | ||
const decodedValue = b64decode(attribute.value); | ||
|
||
decodedData.attributes.push({ | ||
key: decodedKey, | ||
value: decodedValue, | ||
}); | ||
}); | ||
|
||
return decodedData; | ||
} | ||
|
||
async function addressExists(address: string): Promise<boolean> { | ||
const balance = await Balances.getByAddress(address); | ||
if (!balance) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
async function createBalancesEntry(address: string) { | ||
const newBalance = new Balances(address); | ||
newBalance.address = address; | ||
newBalance.balance = BigInt(0); | ||
newBalance.denom = ''; | ||
|
||
await newBalance.save(); | ||
|
||
logger.info(`Created new entry for address: ${address}`); | ||
} | ||
|
||
async function updateBalances( | ||
senderAddress: string, | ||
recipientAddress: string, | ||
amount: bigint | ||
): Promise<void> { | ||
const senderBalances = await Balances.getByAddress(senderAddress); | ||
const senderBalance = senderBalances ? senderBalances[0] : undefined; | ||
|
||
const recipientBalances = await Balances.getByAddress(recipientAddress); | ||
const recipientBalance = recipientBalances | ||
? recipientBalances[0] | ||
: undefined; | ||
|
||
if (senderBalance && recipientBalance) { | ||
senderBalance.balance = (senderBalance.balance || BigInt(0)) - amount; | ||
recipientBalance.balance = | ||
(recipientBalance.balance || BigInt(0)) + amount; | ||
|
||
await senderBalance.save(); | ||
await recipientBalance.save(); | ||
|
||
logger.info( | ||
`Updated balances: Sender (${senderAddress}) balance: ${senderBalance.balance}, Recipient (${recipientAddress}) balance: ${recipientBalance.balance}` | ||
); | ||
} else { | ||
if (!senderBalance) { | ||
logger.error(`Sender balance not found for address: ${senderAddress}`); | ||
} | ||
if (!recipientBalance) { | ||
logger.error( | ||
`Recipient balance not found for address: ${recipientAddress}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
decodeEvent, | ||
addressExists, | ||
createBalancesEntry, | ||
updateBalances, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters