Skip to content

Commit

Permalink
fix(bill): add unique key with index
Browse files Browse the repository at this point in the history
  • Loading branch information
Daryl-L committed Dec 8, 2022
1 parent b0a87e4 commit 2e881c7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
9 changes: 8 additions & 1 deletion offchain-modules/packages/app-monitor/src/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ export class Monitor {

async onSudtRecord(record: SudtRecord): Promise<void> {
logger.info(`Receive sudt:${JSON.stringify(record)}`);
await this.sudtDb.createSudtTransferRecord(record.txId, record.direction, record.lock, record.token, record.amount);
await this.sudtDb.createSudtTransferRecord(
record.txId,
record.direction,
record.lock,
record.token,
record.amount,
record.index,
);
}

async onCkbBurnRecord(burn: CkbBurnRecord): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions offchain-modules/packages/reconc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type SudtRecord = FromRecord & {
token: string;
lock: string;
direction: 'in' | 'out';
index: number;
};

export class Reconciliation {
Expand Down
6 changes: 5 additions & 1 deletion offchain-modules/packages/x/src/db/entity/sudt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';
import { Column, CreateDateColumn, Entity, Index, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';

@Entity()
@Index(['txHash', 'index', 'derection'], { unique: true })
export class Sudt {
@PrimaryGeneratedColumn('increment')
id: number;
Expand All @@ -17,6 +18,9 @@ export class Sudt {
@Column()
sudtArgs: string;

@Column()
index: number;

@Column()
amount: string;

Expand Down
2 changes: 2 additions & 0 deletions offchain-modules/packages/x/src/db/sudt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export class SudtDb {
address: string,
sudtArgs: string,
amount: string,
index: number,
): Promise<void> {
const record = this.sudtRepository.create({
txHash: hash,
direction: direction == 'in' ? 1 : -1,
address,
sudtArgs,
amount,
index,
});
await this.sudtRepository.save(record);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export class CKBRecordObservable {
try {
const records: SudtRecord[] = [];
const tx = await rpc.getTransaction(getTxResult.tx_hash);
for (const input of tx.transaction.inputs) {
const inputs = tx.transaction.inputs;
for (let i = 0; i < inputs.length; i++) {
const input = inputs[i];
if (input.previousOutput) {
const tx = await rpc.getTransaction(input.previousOutput.txHash);
const output = tx.transaction.outputs[parseInt(input.previousOutput.index)];
Expand All @@ -85,6 +87,7 @@ export class CKBRecordObservable {
output.type.hashType == searchKey.script.hash_type
) {
records.push({
index: i,
txId: tx.transaction.hash,
amount: tx.transaction.outputsData[parseInt(input.previousOutput.index)],
lock: this.provider.scriptToAddress(ScriptLike.from(output.lock)),
Expand All @@ -106,6 +109,7 @@ export class CKBRecordObservable {
v.type.hashType == searchKey.script.hash_type
) {
records.push({
index: k,
txId: tx.transaction.hash,
amount: tx.transaction.outputsData[k],
lock: this.provider.scriptToAddress(ScriptLike.from(v.lock)),
Expand Down

0 comments on commit 2e881c7

Please sign in to comment.