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

Reform eth transactions #3386

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/bitcore-node/src/modules/ethereum/api/csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ export class ETHStateProvider extends InternalStateProvider implements IChainSta
await EthTransactionStorage.collection.updateOne({ _id: tx._id }, { $set: { receipt, fee } });
tx.receipt = receipt;
tx.fee = fee;
if (receipt && receipt.logs && receipt.logs.length > 0) {
const decodedLogs = await EthTransactionStorage.abiDecodeLogs(receipt.logs);
if (decodedLogs.length > 0) {
tx.logs = decodedLogs;
}
}
}
}
return tx;
Expand Down
67 changes: 21 additions & 46 deletions packages/bitcore-node/src/modules/ethereum/api/erc20Transform.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,37 @@
import { Transform } from 'stream';
import Web3 from 'web3';
import { MongoBound } from '../../../models/base';
import { IEthTransaction, IEthTransactionTransformed } from '../types';
import { IEthTransaction } from '../types';

export class Erc20RelatedFilterTransform extends Transform {
constructor(private web3: Web3, private tokenAddress: string) {
super({ objectMode: true });
}

async _transform(tx: MongoBound<IEthTransaction>, _, done) {
if (
tx.abiType &&
tx.abiType.type === 'ERC20' &&
tx.abiType.name === 'transfer' &&
tx.to.toLowerCase() === this.tokenAddress.toLowerCase()
) {
tx.value = tx.abiType!.params[1].value as any;
tx.to = this.web3.utils.toChecksumAddress(tx.abiType!.params[0].value);
} else if (
tx.abiType &&
tx.abiType.type === 'INVOICE' &&
tx.abiType.name === 'pay' &&
tx.abiType.params[8].value.toLowerCase() === this.tokenAddress.toLowerCase()
) {
tx.value = tx.abiType!.params[0].value as any;
} else if (tx.internal && tx.internal.length > 0) {
try {
const tokenRelatedIncomingInternalTxs = tx.internal.filter(
(internalTx: any) =>
internalTx.action.to && this.tokenAddress.toLowerCase() === internalTx.action.to.toLowerCase()
if (tx.logs && tx.logs.length > 0) {
const ERC20Log: any = tx.logs.find(
l =>
l.type == 'ERC20' &&
!l.logs.find(
i =>
i.name == 'Transfer' &&
i.address.toLowerCase() == this.tokenAddress.toLowerCase() &&
i.events[i.events.findIndex(j => j.name == '_from')].value.toLowerCase() == tx.from.toLowerCase()
)
);
if (ERC20Log) {
const log: any = ERC20Log.logs.find(
i =>
i.name == 'Transfer' &&
i.address.toLowerCase() == this.tokenAddress.toLowerCase() &&
i.events[i.events.findIndex(j => j.name == '_from')].value.toLowerCase() == tx.from.toLowerCase()
);
for (const internalTx of tokenRelatedIncomingInternalTxs) {
if (
internalTx.abiType &&
(internalTx.abiType.name === 'transfer' || internalTx.abiType.name === 'transferFrom')
) {
const _tx: IEthTransactionTransformed = Object.assign({}, tx);
for (const element of internalTx.abiType.params) {
if (element.name === '_value') _tx.value = element.value as any;
if (element.name === '_to') _tx.to = this.web3.utils.toChecksumAddress(element.value);
if (element.name === '_from') {
_tx.initialFrom = tx.from;
_tx.from = this.web3.utils.toChecksumAddress(element.value);
} else if (internalTx.action.from && internalTx.abiType && internalTx.abiType.name == 'transfer') {
_tx.initialFrom = tx.from;
_tx.from = this.web3.utils.toChecksumAddress(internalTx.action.from);
}
}
this.push(_tx);
}
if (log && log.events) {
tx.value = log.events.find(j => j.name == '_value').value;
tx.to = this.web3.utils.toChecksumAddress(log.events.find(j => j.name == '_to').value);
}
return done();
} catch (err) {
console.error(err);
return done();
}
} else {
return done();
}
this.push(tx);
return done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,54 @@ export class EthMultisigRelatedFilterTransform extends Transform {
this.push(_tx);
});
if (walletRelatedIncomingInternalTxs.length || walletRelatedOutgoingInternalTxs.length) return done();
} else if (
tx.abiType &&
tx.abiType.type === 'ERC20' &&
tx.abiType.name === 'transfer' &&
this.tokenAddress &&
tx.to.toLowerCase() === this.tokenAddress.toLowerCase()
) {
tx.value = tx.abiType!.params[1].value as any;
tx.to = this.web3.utils.toChecksumAddress(tx.abiType!.params[0].value);
} else if (
tx.internal &&
tx.internal.length > 0 &&
tx.internal[0].abiType &&
tx.internal[0].abiType.type === 'ERC20' &&
tx.internal[0].abiType.name === 'transfer' &&
tx.internal[0].action.to &&
tx.internal[0].action.from &&
tx.internal[0].action.to.toLowerCase() === this.tokenAddress.toLowerCase()
) {
tx.value = tx.internal[0].abiType!.params[1].value as any;
tx.to = this.web3.utils.toChecksumAddress(tx.internal[0].abiType!.params[0].value);
tx.from = this.web3.utils.toChecksumAddress(tx.internal[0].action.from);
} else if (tx.to !== this.multisigContractAddress || (tx.to === this.multisigContractAddress && tx.abiType)) {
} else if (tx.logs && tx.logs.length > 0) {
const ERC20Log: any = tx.logs.find(
l =>
l.type == 'ERC20' &&
!l.logs.find(
i =>
i.name == 'Transfer' &&
i.address.toLowerCase() == this.tokenAddress.toLowerCase() &&
i.events[i.events.findIndex(j => j.name == '_from')].value.toLowerCase() == tx.from.toLowerCase()
)
);
if (ERC20Log) {
const log: any = ERC20Log.logs.find(
i =>
i.name == 'Transfer' &&
i.address.toLowerCase() == this.tokenAddress.toLowerCase() &&
i.events[i.events.findIndex(j => j.name == '_from')].value.toLowerCase() == tx.from.toLowerCase()
);
if (log && log.events) {
tx.value = log.events.find(j => j.name == '_value').value;
tx.to = this.web3.utils.toChecksumAddress(log.events.find(j => j.name == '_to').value);
}
}
} else if (tx.logs && tx.logs.length > 0) {
const ERC20Log: any = tx.logs.find(
l =>
l.type == 'ERC20' &&
!l.logs.find(
i =>
i.name == 'Transfer' &&
i.address.toLowerCase() == this.tokenAddress.toLowerCase() &&
i.events[i.events.findIndex(j => j.name == '_from')].value.toLowerCase() == tx.from.toLowerCase()
)
);
if (ERC20Log) {
const log: any = ERC20Log.logs.find(
i =>
i.name == 'Transfer' &&
i.address.toLowerCase() == this.tokenAddress.toLowerCase() &&
i.events[i.events.findIndex(j => j.name == '_to')].value.toLowerCase() == tx.to.toLowerCase()
);
if (log && log.events) {
tx.value = log.events.find(j => j.name == '_value').value;
tx.to = this.web3.utils.toChecksumAddress(log.events.find(j => j.name == '_to').value);
tx.from = this.web3.utils.toChecksumAddress(log.events.find(j => j.name == '_from').value);
}
}
} else if (tx.to !== this.multisigContractAddress || tx.to === this.multisigContractAddress) {
return done();
}
this.push(tx);
Expand Down
3 changes: 3 additions & 0 deletions packages/bitcore-node/src/modules/ethereum/api/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class EthListTransactionsStream extends Transform {
receipt: transaction.receipt,
address: transaction.to,
blockTime: transaction.blockTimeNormalized,
logs: transaction.logs,
abiType: transaction.abiType,
error: transaction.error,
internal: transaction.internal,
Expand All @@ -53,6 +54,7 @@ export class EthListTransactionsStream extends Transform {
receipt: transaction.receipt,
address: transaction.to,
blockTime: transaction.blockTimeNormalized,
logs: transaction.logs,
abiType: transaction.abiType,
error: transaction.error,
internal: transaction.internal,
Expand Down Expand Up @@ -81,6 +83,7 @@ export class EthListTransactionsStream extends Transform {
receipt: transaction.receipt,
address: transaction.to,
blockTime: transaction.blockTimeNormalized,
logs: transaction.logs,
abiType: transaction.abiType,
error: transaction.error,
internal: transaction.internal,
Expand Down