Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export class InternalTxRelatedFilterTransform extends TransformWithEventPipe {
let internalTxsToProcess: Effect[] = [];
if (tx.effects && tx.effects.length) {
const walletRelatedInternalTxs = tx.effects.filter((internalTx: any) =>
walletAddresses.includes(internalTx.to) && !internalTx.contractAddress
walletAddresses.includes(internalTx.to) &&
!internalTx.contractAddress &&
!this.isRootNativeTransfer(tx, internalTx)
);

const refundTxs = walletRelatedInternalTxs.filter(i => i.to === tx.from);
Expand Down Expand Up @@ -67,6 +69,14 @@ export class InternalTxRelatedFilterTransform extends TransformWithEventPipe {
return done();
}

private isRootNativeTransfer(tx: MongoBound<IEVMTransactionInProcess>, internalTx: Effect) {
const callStack = internalTx.callStack || '';
return (callStack === '0' || callStack === '')
&& internalTx.from?.toLowerCase() === tx.from?.toLowerCase()
&& internalTx.to?.toLowerCase() === tx.to?.toLowerCase()
&& Number(internalTx.amount) === Number(tx.value);
}

async getWalletAddresses(tx) {
if (!this.walletAddresses.length) {
const cursor = WalletAddressStorage.collection
Expand Down
93 changes: 93 additions & 0 deletions packages/bitcore-node/test/integration/matic/csp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,57 @@ describe('Polygon/MATIC API', function() {
it('should stream wallet\'s valid & invalid MATIC transactions', async () =>
await streamWalletTransactionsTest(chain, network, true)
);

it('should not duplicate top-level native EVM receives with root effects', async () => {
const sender = '0x9F1a8A1bCc8e45Db2aD542e98b7174a9b519A45C';
const amount = 17000;
const txid = '0xf1b8ad69d71cc68eb73a3778457fcb74f9a4471df9afae03dedd3e0de7ae7f58';

await EVMTransactionStorage.collection.insertOne({
chain,
network,
txid,
blockHeight: 1,
blockHash: '0xblock',
blockTime: new Date('2024-01-01T00:00:00.000Z'),
blockTimeNormalized: new Date('2024-01-01T00:00:00.000Z'),
fee: 21000,
value: amount,
wallets: [wallet._id as ObjectId],
to: address,
from: sender,
gasLimit: 21000,
gasPrice: 1,
nonce: 1,
transactionIndex: 0,
data: Buffer.from(''),
internal: [],
calls: [],
receipt: {
status: true,
transactionHash: txid,
transactionIndex: 0,
blockHash: '0xblock',
blockNumber: 1,
cumulativeGasUsed: 21000,
gasUsed: 21000,
logs: []
},
effects: [{
to: address,
from: sender,
amount: String(amount),
callStack: '0'
}]
} as IEVMTransactionInProcess);

const txs = await streamWalletTransactionRows(chain, network, wallet);

expect(txs).to.have.lengthOf(1);
expect(txs[0].txid).to.equal(txid);
expect(txs[0].category).to.equal('receive');
expect(txs[0].satoshis).to.equal(String(amount));
});
});
});

Expand Down Expand Up @@ -426,3 +477,45 @@ const streamWalletTransactionsTest = async (chain: string, network: string, incl
expect(counter).to.eq(includeInvalidTxs ? txCount * 2 : txCount);
sandbox.restore();
};

const streamWalletTransactionRows = async (chain: string, network: string, wallet: IWallet) => {
const chunks: string[] = [];
const req = (new Writable({
write: function(_data, _, cb) {
cb();
}
}) as unknown) as Request;

const res = (new Writable({
write: function(data, _, cb) {
chunks.push(data.toString());
cb();
}
}) as unknown) as Response;
res.type = () => res;

const err = await new Promise(r => {
res
.on('error', r)
.on('finish', r);

MATIC.streamWalletTransactions({
chain,
network,
wallet,
req,
res,
args: {}
} as StreamWalletTransactionsParams)
.catch(e => r(e));
});

expect(err).to.not.exist;

return chunks
.join('')
.trim()
.split('\n')
.filter(Boolean)
.map(line => JSON.parse(line));
};