145 lines
4.1 KiB
TypeScript
145 lines
4.1 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import Decimal from 'decimal.js';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
console.log('Seeding mining-wallet-service database...');
|
|
|
|
// 1. 初始化核心系统账户(总部、运营、手续费)
|
|
const systemAccounts = [
|
|
{ accountType: 'HEADQUARTERS', name: '总部账户', code: 'HQ' },
|
|
{ accountType: 'OPERATION', name: '运营账户', code: 'OP' },
|
|
{ accountType: 'FEE', name: '手续费账户', code: 'FEE' },
|
|
];
|
|
|
|
for (const account of systemAccounts) {
|
|
const existing = await prisma.systemAccount.findFirst({
|
|
where: { code: account.code },
|
|
});
|
|
|
|
if (!existing) {
|
|
const created = await prisma.systemAccount.create({
|
|
data: {
|
|
accountType: account.accountType as any,
|
|
name: account.name,
|
|
code: account.code,
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
// 发布系统账户创建事件到 Outbox
|
|
await prisma.outboxEvent.create({
|
|
data: {
|
|
aggregateType: 'SystemAccount',
|
|
aggregateId: created.id,
|
|
eventType: 'WalletSystemAccountCreated',
|
|
topic: 'mining-wallet.system-account.created',
|
|
key: created.code,
|
|
payload: {
|
|
id: created.id,
|
|
accountType: created.accountType,
|
|
name: created.name,
|
|
code: created.code,
|
|
provinceId: null,
|
|
cityId: null,
|
|
shareBalance: 0,
|
|
usdtBalance: 0,
|
|
greenPointBalance: 0,
|
|
frozenShare: 0,
|
|
frozenUsdt: 0,
|
|
totalInflow: 0,
|
|
totalOutflow: 0,
|
|
blockchainAddress: null,
|
|
isActive: created.isActive,
|
|
},
|
|
},
|
|
});
|
|
|
|
console.log(`Created system account: ${account.code}`);
|
|
} else {
|
|
console.log(`System account already exists: ${account.code}`);
|
|
}
|
|
}
|
|
|
|
// 2. 初始化池账户(积分股池、黑洞池、流通池)
|
|
const poolAccounts = [
|
|
{
|
|
poolType: 'SHARE_POOL',
|
|
name: '积分股池',
|
|
balance: new Decimal('100000000'), // 1亿初始发行量
|
|
description: '挖矿奖励的来源池,总发行量',
|
|
},
|
|
{
|
|
poolType: 'BLACK_HOLE_POOL',
|
|
name: '黑洞池',
|
|
balance: new Decimal('0'),
|
|
targetBurn: new Decimal('50000000'), // 目标销毁5000万
|
|
description: '销毁的积分股,用于减少流通量',
|
|
},
|
|
{
|
|
poolType: 'CIRCULATION_POOL',
|
|
name: '流通池',
|
|
balance: new Decimal('0'),
|
|
description: '市场流通的积分股',
|
|
},
|
|
];
|
|
|
|
for (const pool of poolAccounts) {
|
|
const existing = await prisma.poolAccount.findFirst({
|
|
where: { poolType: pool.poolType as any },
|
|
});
|
|
|
|
if (!existing) {
|
|
const created = await prisma.poolAccount.create({
|
|
data: {
|
|
poolType: pool.poolType as any,
|
|
name: pool.name,
|
|
balance: pool.balance,
|
|
targetBurn: pool.targetBurn,
|
|
remainingBurn: pool.targetBurn,
|
|
description: pool.description,
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
// 发布池账户创建事件到 Outbox
|
|
await prisma.outboxEvent.create({
|
|
data: {
|
|
aggregateType: 'PoolAccount',
|
|
aggregateId: created.id,
|
|
eventType: 'WalletPoolAccountCreated',
|
|
topic: 'mining-wallet.pool-account.created',
|
|
key: created.poolType,
|
|
payload: {
|
|
id: created.id,
|
|
poolType: created.poolType,
|
|
name: created.name,
|
|
balance: created.balance.toString(),
|
|
totalInflow: 0,
|
|
totalOutflow: 0,
|
|
targetBurn: created.targetBurn?.toString() || null,
|
|
remainingBurn: created.remainingBurn?.toString() || null,
|
|
isActive: created.isActive,
|
|
},
|
|
},
|
|
});
|
|
|
|
console.log(`Created pool account: ${pool.poolType}`);
|
|
} else {
|
|
console.log(`Pool account already exists: ${pool.poolType}`);
|
|
}
|
|
}
|
|
|
|
console.log('Seeding completed!');
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error('Seeding failed:', e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|