"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Erc20RelatedFilterTransform = void 0;
const crypto_wallet_core_1 = require("@bitpay-labs/crypto-wallet-core");
const streamWithEventPipe_1 = require("../../../../utils/streamWithEventPipe");
class Erc20RelatedFilterTransform extends streamWithEventPipe_1.TransformWithEventPipe {
    constructor(tokenAddress) {
        super({ objectMode: true });
        this.tokenAddress = tokenAddress;
    }
    async _transform(tx, _, done) {
        this.tokenAddress = crypto_wallet_core_1.Web3.utils.toChecksumAddress(this.tokenAddress);
        if (tx.effects && tx.effects.length) {
            // Get all effects where contractAddress is tokenAddress
            const tokenRelatedInternalTxs = tx.effects.filter((effect) => effect.contractAddress === this.tokenAddress);
            // Create a tx object for each erc20 transfer
            for (const internalTx of tokenRelatedInternalTxs) {
                const _tx = Object.assign({}, tx);
                _tx.value = Number(internalTx.amount);
                _tx.to = internalTx.to;
                _tx.from = internalTx.from;
                if (internalTx.from != tx.from) {
                    _tx.initialFrom = tx.from;
                }
                // This is how a requester can verify uniqueness in light of duplicated txids
                _tx.callStack = internalTx.callStack;
                this.push(_tx);
            }
        }
        return done();
    }
}
exports.Erc20RelatedFilterTransform = Erc20RelatedFilterTransform;
//# sourceMappingURL=erc20Transform.js.map