make interaction for accountBalances

This commit is contained in:
Einhornimmond 2025-12-19 08:48:16 +01:00
parent b1214f8b6c
commit 5c753a3b32
13 changed files with 130 additions and 32 deletions

View File

@ -9,8 +9,9 @@ import { addCommunityRootTransaction } from './blockchain'
import { Context } from './Context'
import { communityDbToCommunity } from './convert'
import { loadAdminUsersCache, loadCommunities, loadContributionLinkModeratorCache } from './database'
import { generateKeyPairCommunity } from './keyPair'
import { generateKeyPairCommunity } from './data/keyPair'
import { CommunityContext } from './valibot.schema'
import { Balance } from './data/Balance'
export async function bootstrap(): Promise<Context> {
const context = await Context.create()
@ -47,6 +48,8 @@ async function bootstrapCommunities(context: Context): Promise<Map<string, Commu
blockchain,
topicId,
folder: communityDb.uniqueAlias.replace(/[^a-zA-Z0-9]/g, '_'),
gmwBalance: new Balance(),
aufBalance: new Balance(),
})
generateKeyPairCommunity(communityDb, context.cache, topicId)

View File

@ -14,7 +14,7 @@ import {
memoSchema,
timeoutDurationSchema,
} from '../../schemas/typeGuard.schema'
import { TransactionTypeId } from './TransactionTypeId'
import { TransactionTypeId } from './data/TransactionTypeId'
import { CommunityDb, CreatedUserDb, TransactionDb, TransactionLinkDb } from './valibot.schema'
export function getInputTransactionTypeFromTypeId(typeId: TransactionTypeId): InputTransactionType {

View File

@ -0,0 +1,25 @@
import Decimal from 'decimal.js-light'
import { GradidoUnit } from 'gradido-blockchain-js'
import { legacyCalculateDecay } from '../utils'
export class Balance {
private balance: GradidoUnit
private date: Date
public constructor()
{
this.balance = new GradidoUnit(0)
this.date = new Date()
}
public update(amount: Decimal, date: Date) {
if (this.balance.equal(GradidoUnit.zero())) {
this.balance = GradidoUnit.fromString(amount.toString())
this.date = date
} else {
const decayedBalance = legacyCalculateDecay(new Decimal(this.balance.toString()), this.date, date )
const newBalance = decayedBalance.add(amount)
this.balance = GradidoUnit.fromString(newBalance.toString())
this.date = date
}
}
}

View File

@ -1,13 +1,13 @@
import { KeyPairEd25519, MemoryBlock, MemoryBlockPtr } from 'gradido-blockchain-js'
import { getLogger } from 'log4js'
import { KeyPairCacheManager } from '../../cache/KeyPairCacheManager'
import { CONFIG } from '../../config'
import { LOG4JS_BASE_CATEGORY } from '../../config/const'
import { KeyPairIdentifierLogic } from '../../data/KeyPairIdentifier.logic'
import { AccountKeyPairRole } from '../../interactions/resolveKeyPair/AccountKeyPair.role'
import { UserKeyPairRole } from '../../interactions/resolveKeyPair/UserKeyPair.role'
import { HieroId } from '../../schemas/typeGuard.schema'
import { CommunityDb, UserDb } from './valibot.schema'
import { KeyPairCacheManager } from '../../../cache/KeyPairCacheManager'
import { CONFIG } from '../../../config'
import { LOG4JS_BASE_CATEGORY } from '../../../config/const'
import { KeyPairIdentifierLogic } from '../../../data/KeyPairIdentifier.logic'
import { AccountKeyPairRole } from '../../../interactions/resolveKeyPair/AccountKeyPair.role'
import { UserKeyPairRole } from '../../../interactions/resolveKeyPair/UserKeyPair.role'
import { HieroId } from '../../../schemas/typeGuard.schema'
import { CommunityDb, UserDb } from '../valibot.schema'
const logger = getLogger(`${LOG4JS_BASE_CATEGORY}.migrations.db-v2.7.0_to_blockchain-v3.6.keyPair`)

View File

@ -17,7 +17,7 @@ import {
userSelectSchema,
usersTable
} from './drizzle.schema'
import { TransactionTypeId } from './TransactionTypeId'
import { TransactionTypeId } from './data/TransactionTypeId'
import {
CommunityDb,
CreatedUserDb,

View File

@ -0,0 +1,10 @@
import { AccountBalances } from 'gradido-blockchain-js'
export class AbstractBalancesRole {
public accountBalances: AccountBalances
public constructor() {
this.accountBalances = new AccountBalances()
}
}

View File

@ -0,0 +1,5 @@
import { AbstractBalancesRole } from './AbstractBalances.role'
export class CreationBalancesRole extends AbstractBalancesRole {
}

View File

@ -0,0 +1,70 @@
import { InputTransactionType } from '../../../../data/InputTransactionType.enum'
import { Transaction } from '../../../../schemas/transaction.schema'
import { Context } from '../../Context'
import { TransactionDb } from '../../valibot.schema'
import { AbstractBalancesRole } from './AbstractBalances.role'
import { CreationBalancesRole } from './CreationBalances.role'
export function accountBalancesContext(transaction: Transaction, dbTransaction: TransactionDb, context: Context) {
let role: AbstractBalancesRole | null = null
if (InputTransactionType.GRADIDO_CREATION === transaction.type) {
role = new CreationBalancesRole()
}
}
/*
const senderCommunityContext = this.context.getCommunityContextByUuid(item.user.communityUuid)
const recipientCommunityContext = this.context.getCommunityContextByUuid(
item.linkedUser.communityUuid,
)
this.context.cache.setHomeCommunityTopicId(senderCommunityContext.topicId)
const transaction = transactionDbToTransaction(
item,
senderCommunityContext.topicId,
recipientCommunityContext.topicId,
)
const accountBalances = new AccountBalances()
if (InputTransactionType.GRADIDO_CREATION === transaction.type) {
const recipientKeyPair = await ResolveKeyPair(
new KeyPairIdentifierLogic(transaction.linkedUser!),
)
accountBalances.add(new AccountBalance(recipientKeyPair.getPublicKey(), item.balance, ''))
// update gmw and auf
this.updateGmwAuf(new Decimal(item.amount.toString(4)), item.balanceDate)
const communityKeyPair = await ResolveKeyPair(new KeyPairIdentifierLogic({ communityTopicId: senderCommunityContext.topicId }))
const gmwKeyPair = communityKeyPair.deriveChild(
hardenDerivationIndex(GMW_ACCOUNT_DERIVATION_INDEX),
)
if (!gmwKeyPair) {
throw new GradidoBlockchainCryptoError(
`KeyPairEd25519 child derivation failed, has private key: ${communityKeyPair.hasPrivateKey()} for community: ${senderCommunityContext.communityId}`,
)
}
const aufKeyPair = communityKeyPair.deriveChild(
hardenDerivationIndex(AUF_ACCOUNT_DERIVATION_INDEX),
)
if (!aufKeyPair) {
throw new GradidoBlockchainCryptoError(
`KeyPairEd25519 child derivation failed, has private key: ${communityKeyPair.hasPrivateKey()} for community: ${senderCommunityContext.communityId}`,
)
}
accountBalances.add(new AccountBalance(gmwKeyPair.getPublicKey(), GradidoUnit.fromString(
TransactionsSyncRole.gmwBalance!.balance.toString()), ''))
accountBalances.add(new AccountBalance(aufKeyPair.getPublicKey(), GradidoUnit.fromString(
TransactionsSyncRole.aufBalance!.balance.toString()), ''))
} else if (InputTransactionType.REGISTER_ADDRESS === transaction.type) {
const recipientKeyPair = await ResolveKeyPair(
new KeyPairIdentifierLogic(transaction.user),
)
accountBalances.add(new AccountBalance(recipientKeyPair.getPublicKey(), GradidoUnit.zero(), ''))
} else {
// I use the receiving part of transaction pair, so the user is the recipient and the linked user the sender
const senderKeyPair = await ResolveKeyPair(
new KeyPairIdentifierLogic(transaction.linkedUser!),
)
const recipientKeyPair = await ResolveKeyPair(
new KeyPairIdentifierLogic(transaction.user),
)
accountBalances.add(new AccountBalance(senderKeyPair.getPublicKey(), item.linkedUserBalance, ''))
accountBalances.add(new AccountBalance(recipientKeyPair.getPublicKey(), item.balance, ''))
*/

View File

@ -20,8 +20,6 @@ type BalanceDate = {
export class TransactionsSyncRole extends AbstractSyncRole<TransactionDb> {
private static transactionLinkCodes = new Set<string>()
static doubleTransactionLinkCodes: string[] = []
static gmwBalance: BalanceDate | undefined = undefined
static aufBalance: BalanceDate | undefined = undefined
getDate(): Date {
return this.peek().balanceDate
@ -45,23 +43,6 @@ export class TransactionsSyncRole extends AbstractSyncRole<TransactionDb> {
})
}
updateGmwAuf(amount: Decimal, date: Date) {
if(!TransactionsSyncRole.gmwBalance) {
TransactionsSyncRole.gmwBalance = { balance: amount, date }
} else {
const oldGmwBalanceDate = TransactionsSyncRole.gmwBalance
const newBalance = legacyCalculateDecay(oldGmwBalanceDate.balance, oldGmwBalanceDate.date, date )
TransactionsSyncRole.gmwBalance = { balance: newBalance, date }
}
if(!TransactionsSyncRole.aufBalance) {
TransactionsSyncRole.aufBalance = { balance: amount, date }
} else {
const oldAufBalanceDate = TransactionsSyncRole.aufBalance
const newBalance = legacyCalculateDecay(oldAufBalanceDate.balance, oldAufBalanceDate.date, date )
TransactionsSyncRole.aufBalance = { balance: newBalance, date }
}
}
async pushToBlockchain(item: TransactionDb): Promise<void> {
const senderCommunityContext = this.context.getCommunityContextByUuid(item.user.communityUuid)
const recipientCommunityContext = this.context.getCommunityContextByUuid(

View File

@ -1,7 +1,7 @@
import { addTransaction } from '../../blockchain'
import { userDbToTransaction } from '../../convert'
import { loadUsers } from '../../database'
import { generateKeyPairUserAccount } from '../../keyPair'
import { generateKeyPairUserAccount } from '../../data/keyPair'
import { CreatedUserDb } from '../../valibot.schema'
import { AbstractSyncRole } from './AbstractSync.role'

View File

@ -8,7 +8,8 @@ import {
memoSchema,
uuidv4Schema,
} from '../../schemas/typeGuard.schema'
import { TransactionTypeId } from './TransactionTypeId'
import { TransactionTypeId } from './data/TransactionTypeId'
import { Balance } from './data/Balance'
export const createdUserDbSchema = v.object({
id: v.pipe(v.number(), v.minValue(1)),
@ -81,6 +82,8 @@ export const communityContextSchema = v.object({
v.maxLength(512, 'expect string length <= 512'),
v.regex(/^[a-zA-Z0-9-_]+$/, 'expect string to be a valid (alphanumeric, _, -) folder name'),
),
gmwBalance: v.instance(Balance),
aufBalance: v.instance(Balance),
})
export type TransactionDb = v.InferOutput<typeof transactionDbSchema>
@ -88,4 +91,5 @@ export type UserDb = v.InferOutput<typeof userDbSchema>
export type CreatedUserDb = v.InferOutput<typeof createdUserDbSchema>
export type TransactionLinkDb = v.InferOutput<typeof transactionLinkDbSchema>
export type CommunityDb = v.InferOutput<typeof communityDbSchema>
export type Balance = v.InferOutput<typeof balanceSchema>
export type CommunityContext = v.InferOutput<typeof communityContextSchema>