Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DustLost amount #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 46 additions & 45 deletions server/src/helpers/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,41 @@ export async function getOperations(
}
}

var transactionFeeFromAddress = null
var transactionFeeAmount = null
let transactionFeeFromAddress = null;
let transactionFeeAmount = null;

for (const event of events) {
const { event: { data } } = event;
const {
event: { data },
} = event;

if (isTransactionFeePaidEvent(event)) {
transactionFeeFromAddress = data[0].toString()
transactionFeeAmount = (data[1] as u128)
transactionFeeFromAddress = data[0].toString();
transactionFeeAmount = data[1] as u128;

const transactionFeeDebitOperation = Operation.constructFromObject({
operation_identifier: new OperationIdentifier(operations.length),
type: OpType.TRANSACTION_FEE_PAID,
status: OperationStatus.SUCCESS,
account: new AccountIdentifier(transactionFeeFromAddress),
amount: new Amount(transactionFeeAmount.toBn().clone().neg().toString(), currency),
})
operation_identifier: new OperationIdentifier(operations.length),
type: OpType.TRANSACTION_FEE_PAID,
status: OperationStatus.SUCCESS,
account: new AccountIdentifier(transactionFeeFromAddress),
amount: new Amount(transactionFeeAmount.toBn().clone().neg().toString(), currency),
});

operations.push(transactionFeeDebitOperation);
break;
}
}

var transactionFeeWithdrawSkipped = false
let transactionFeeWithdrawSkipped = false;
for (const event of events) {
const {
event: { data },
} = event;

if (isTransferEvent(event)) {
const debitAccount = data[0].toString()
const creditAccount = data[1].toString()
const amount = (data[2] as u128).toBn()
const debitAccount = data[0].toString();
const creditAccount = data[1].toString();
const amount = (data[2] as u128).toBn();

operations.push(
Operation.constructFromObject({
Expand All @@ -102,7 +104,6 @@ export async function getOperations(
status: opStatus,
account: new AccountIdentifier(debitAccount),
amount: new Amount(amount.clone().neg().toString(), currency),

}),
);

Expand All @@ -116,70 +117,70 @@ export async function getOperations(
related_operations: [new OperationIdentifier(operations.length - 1)],
}),
);

continue;
}

if (isWithdrawEvent(event)) {
const account = data[0].toString()
const amount = (data[1] as u128)
const account = data[0].toString();
const amount = data[1] as u128;

const withdrawOperation = Operation.constructFromObject({
operation_identifier: new OperationIdentifier(operations.length),
type: OpType.WITHDRAW,
status: OperationStatus.SUCCESS,
account: new AccountIdentifier(account),
amount: new Amount(amount.toBn().clone().neg().toString(), currency),
})
});

if (!transactionFeeWithdrawSkipped) {
const accountsMatch = account === transactionFeeFromAddress;
const amountsMatch = amount.eq(transactionFeeAmount);

if(!transactionFeeWithdrawSkipped) {
const accountsMatch = account === transactionFeeFromAddress
const amountsMatch = amount.eq(transactionFeeAmount)

if (accountsMatch && amountsMatch) {
transactionFeeWithdrawSkipped = true
transactionFeeWithdrawSkipped = true;
continue;
}
}

operations.push(withdrawOperation);

continue;
}

if (isDepositEvent(event)) {
const account = data[0].toString()
const amount = data[1] as u128
const account = data[0].toString();
const amount = data[1] as u128;

const depositOperation = Operation.constructFromObject({
operation_identifier: new OperationIdentifier(operations.length),
type: OpType.DEPOSIT,
status: OperationStatus.SUCCESS,
account: new AccountIdentifier(account),
amount: new Amount(amount.toString(), currency),
})
});

operations.push(depositOperation);
}

if (isDustLostEvent(event)) {
const account = data[0].toString()
const amount = data[1] as u128
const account = data[0].toString();
const amount = data[1] as u128;

const dustLostOperation = Operation.constructFromObject({
operation_identifier: new OperationIdentifier(operations.length),
type: OpType.DUST_LOST,
status: OperationStatus.SUCCESS,
account: new AccountIdentifier(account),
amount: new Amount(amount.toString(), currency),
})
amount: new Amount(amount.toBn().clone().neg().toString(), currency),
});

operations.push(dustLostOperation);
}

if (isReservedEvent(event)) {
const account = data[0].toString()
const amount = (data[1] as u128).toBn()
const account = data[0].toString();
const amount = (data[1] as u128).toBn();

operations.push(
Operation.constructFromObject({
Expand All @@ -194,8 +195,8 @@ export async function getOperations(
}

if (isUnreservedEvent(event)) {
const account = data[0].toString()
const amount = (data[1] as u128).toBn()
const account = data[0].toString();
const amount = (data[1] as u128).toBn();

operations.push(
Operation.constructFromObject({
Expand All @@ -210,8 +211,8 @@ export async function getOperations(
}

if (isReserveRepatrEvent(event)) {
const account = data[1].toString()
const amount = (data[2] as u128).toBn()
const account = data[1].toString();
const amount = (data[2] as u128).toBn();

operations.push(
Operation.constructFromObject({
Expand All @@ -230,7 +231,7 @@ export async function getOperations(
const balance = new BN(await api.getBalanceAtBlock(acc, parentBlockHash)['balance']);
const setBalanceAmount = (data[1] as u128).toBn();
const amount = setBalanceAmount.sub(balance).toString();

operations.push(
Operation.constructFromObject({
operation_identifier: new OperationIdentifier(operations.length),
Expand Down Expand Up @@ -268,7 +269,7 @@ export function getTxsAndEvents(
for (const e of events) {
if (e.phase.isApplyExtrinsic) {
const txIndex = e.phase.asApplyExtrinsic.toNumber();

if (isBalanceEvent(e.event.section) || isTransactionPaymentEvent(e.event.section)) {
if (txIndex in txIndexEvents) {
txIndexEvents[txIndex].events.push(e);
Expand Down
Loading