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

use transferAllowDeath instead of transfer method #155

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
],
"private": true,
"dependencies": {
"@polkadot/api": "10.11.2",
"@polkadot/types": "10.11.2",
"@polkadot/types-codec": "10.11.2",
"@polkadot/api": "11.0.2",
"@polkadot/types": "11.0.2",
"@polkadot/types-codec": "11.0.2",
"@polkadot/util": "12.6.2",
"@substrate/txwrapper-core": "7.2.0",
"@substrate/txwrapper-polkadot": "7.2.0",
"@substrate/txwrapper-core": "7.4.0",
"@substrate/txwrapper-polkadot": "7.4.0",
"body-parser": "1.20.2",
"camelcase": "6.3.0",
"commander": "^11.0.0",
Expand Down
42 changes: 21 additions & 21 deletions server/src/helpers/construct-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface TxParams {
tip: number;
specVersion: number;
transactionVersion: number;
disableKeepAlive: boolean,
disableKeepAlive: boolean;
networkIdent: GearNetworkIdentifier;
}

Expand All @@ -36,28 +36,28 @@ export function constructTx({
}: TxParams) {
const args = { dest: { id: toAddress }, value };
const info = {
address: deriveAddress(fromAddress, ss58Format),
blockHash,
blockNumber,
eraPeriod,
nonce,
tip,
genesisHash: genesis,
metadataRpc: metadataRpc,
specVersion,
transactionVersion,
};
address: deriveAddress(fromAddress, ss58Format),
blockHash,
blockNumber,
eraPeriod,
nonce,
tip,
genesisHash: genesis,
metadataRpc: metadataRpc,
specVersion,
transactionVersion,
};
const options = {
metadataRpc: metadataRpc,
registry,
};
metadataRpc: metadataRpc,
registry,
};
const unsigned = disableKeepAlive
? methods.balances.transfer(args, info, options)
? methods.balances.transferAllowDeath(args, info, options)
: methods.balances.transferKeepAlive(args, info, options);

const loggedUnsignedTx = unsigned;
loggedUnsignedTx.metadataRpc = "0x...truncated...";
logger.info('[constructTx] Generated unsigned tx', { tx: loggedUnsignedTx })
loggedUnsignedTx.metadataRpc = '0x...truncated...';
logger.info('[constructTx] Generated unsigned tx', { tx: loggedUnsignedTx });

const { method, version, address } = unsigned;
const unsignedTx = stringToHex(JSON.stringify({ method, version, address, nonce, era: unsigned.era }));
Expand All @@ -78,7 +78,7 @@ export function constructSignedTx(unsigned: any, signature: string, { registry }
header[0] = 0;
const sigWithHeader = u8aConcat(header, sigU8a);
const tx = JSON.parse(hexToString(unsigned));

const extrinsic = registry.createType('Extrinsic', tx, { version: tx.version });
extrinsic.addSignature(tx.address, sigWithHeader, tx);
return extrinsic.toHex();
Expand All @@ -93,12 +93,12 @@ export function parseTransaction(
? decode(transaction, { registry, metadataRpc })
: decode(JSON.parse(hexToString(transaction)), { registry, metadataRpc });

tx.metadataRpc = "0x...truncated...";
tx.metadataRpc = '0x...truncated...';

logger.info(`[parseTransaction] Decoded transaction`, {
signed: signed,
encoded_tx: transaction,
decoded_tx: tx
decoded_tx: tx,
});

const source = tx.address;
Expand Down
10 changes: 8 additions & 2 deletions server/src/helpers/extrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { AnyTuple } from '@polkadot/types-codec/types';

import { TxMethodLC, TxSectionLC } from '../types';

const TRANSFER_METHODS: string[] = [
TxMethodLC.TRANSFER,
TxMethodLC.TRANSFER_KEEP_ALIVE,
TxMethodLC.TRANSFER_ALLOW_DEATH,
TxMethodLC.TRANSFER_ALL
];

export const isTransferTx = ({ method: { method, section } }: GenericExtrinsic<AnyTuple>) =>
section.toLowerCase() === TxSectionLC.BALANCES &&
(method.toLowerCase() === TxMethodLC.TRANSFER || method.toLowerCase() === TxMethodLC.TRANSFER_KEEP_ALIVE);
section.toLowerCase() === TxSectionLC.BALANCES && TRANSFER_METHODS.includes(method.toLocaleLowerCase());
98 changes: 52 additions & 46 deletions server/src/helpers/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AccountIdentifier, Amount, Operation, OperationIdentifier } from 'roset
import { EventRecord } from '@polkadot/types/interfaces';
import { BN } from '@polkadot/util';

import { OperationsParams, OperationStatus, OpType } from '../types';
import { OperationsParams, OperationStatus, OpType, TxMethodLC } from '../types';
import { u128, Vec } from '@polkadot/types-codec';
import { GenericExtrinsic } from '@polkadot/types';
import { AnyTuple } from '@polkadot/types-codec/types';
Expand Down Expand Up @@ -38,7 +38,12 @@ export async function getOperations(
const dest = tx.args[0].toJSON()['id'];

if (src && dest) {
const amount = new BN(tx.args[1].toString());
let amount = null;

if (tx.method.method.toLocaleLowerCase() !== TxMethodLC.TRANSFER_ALL) {
amount = new BN(tx.args[1].toString());
}

operations.push(
Operation.constructFromObject({
operation_identifier: new OperationIdentifier(operations.length),
Expand All @@ -61,39 +66,41 @@ export async function getOperations(
}
}

var transactionFeeFromAddress = null
var transactionFeeAmount = null
var transactionFeeFromAddress = null;
var 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
var 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 +109,6 @@ export async function getOperations(
status: opStatus,
account: new AccountIdentifier(debitAccount),
amount: new Amount(amount.clone().neg().toString(), currency),

}),
);

Expand All @@ -116,70 +122,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),
})
});

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 +200,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 +216,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 +236,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 +274,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
6 changes: 4 additions & 2 deletions server/src/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export enum EventMethodsLC {
TRANSFER = 'transfer',
BALANCE_SET = 'balanceset',
TRANSACTION_FEE_PAID = 'transactionfeepaid',
DUST_LOST = 'dustlost'
DUST_LOST = 'dustlost',
}

export enum EventSectionLC {
Expand All @@ -38,7 +38,7 @@ export enum OpType {
RESERVE_REPATRIATED = 'ReserveRepatriated',
BALANCE_SET = 'BalanceSet',
TRANSACTION_FEE_PAID = 'TransactionFeePaid',
DUST_LOST = 'DustLost'
DUST_LOST = 'DustLost',
}

export const opTypes = Object.values(OpType);
Expand All @@ -50,4 +50,6 @@ export enum TxSectionLC {
export enum TxMethodLC {
TRANSFER = 'transfer',
TRANSFER_KEEP_ALIVE = 'transferkeepalive',
TRANSFER_ALLOW_DEATH = 'transferallowdeath',
TRANSFER_ALL = 'transferall',
}
Loading
Loading