"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const chai_1 = require("chai");
const src_1 = require("../../src");
const testUtils_1 = require("../testUtils");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const chai = require("chai");
// eslint-disable-next-line @typescript-eslint/no-var-requires
chai.use(require("chai-as-promised"));
const XRPMccConnection = {
    url: process.env.XRP_URL || "",
    username: process.env.XRP_USERNAME || "",
    password: process.env.XRP_PASSWORD || "",
    apiTokenKey: process.env.FLARE_API_PORTAL_KEY || "",
};
describe(`Transaction Xrp tests (${(0, testUtils_1.getTestFile)(__filename)})`, function () {
    let MccClient;
    before(async function () {
        src_1.traceManager.displayRuntimeTrace = false;
        src_1.traceManager.displayStateOnException = false;
        MccClient = new src_1.MCC.XRP(XRPMccConnection);
    });
    describe("Offer create transaction ", function () {
        let transaction;
        const txid = "C32ACF8CCF4F48B7AE097873AA2B7672DC66E05D4F1B3133DA90D1F476B1EAC6";
        before(async function () {
            transaction = await MccClient.getTransaction(txid);
        });
        it("Should find transaction ", function () {
            (0, chai_1.expect)(transaction).to.not.eq(undefined);
        });
        it("Should get transaction txid ", async function () {
            (0, chai_1.expect)(transaction.txid).to.eq(txid);
        });
        it("Should get standardized txid ", async function () {
            (0, chai_1.expect)(transaction.stdTxid).to.eq(txid);
        });
        it("Should get transaction hash ", async function () {
            (0, chai_1.expect)(transaction.hash).to.eq(txid);
        });
        it("Should get transaction reference array ", async function () {
            (0, chai_1.expect)(transaction.reference.length).to.eq(1);
        });
        it("Should get standardized transaction reference ", async function () {
            (0, chai_1.expect)(transaction.stdPaymentReference).to.eq("0x0000000000000000000000000000000000000000000000000000000000000000");
        });
        it("Should get transaction timestamp ", async function () {
            (0, chai_1.expect)(transaction.unixTimestamp).to.eq(1646397932);
        });
        it("Should get source address ", async function () {
            (0, chai_1.expect)(transaction.sourceAddresses.length).to.eq(1);
            (0, chai_1.expect)(transaction.sourceAddresses[0]).to.eq("rETx8GBiH6fxhTcfHM9fGeyShqxozyD3xe");
        });
        it("Should get receiving address ", async function () {
            (0, chai_1.expect)(transaction.receivingAddresses.length).to.eq(0);
        });
        it("Should get fee ", async function () {
            (0, chai_1.expect)(transaction.fee.toNumber()).to.eq(20);
        });
        it("Should received amount ", async function () {
            (0, chai_1.expect)(transaction.receivedAmounts.length).to.eq(0);
        });
        it("Should get type ", async function () {
            (0, chai_1.expect)(transaction.type).to.eq("OfferCreate");
        });
        it("Should check if native payment ", async function () {
            (0, chai_1.expect)(transaction.isNativePayment).to.eq(false);
        });
        it("Should get currency name ", async function () {
            (0, chai_1.expect)(transaction.currencyName).to.eq("");
        });
        it("Should get elementary unit ", async function () {
            (0, chai_1.expect)(transaction.elementaryUnits.toNumber()).to.eq(1000000);
        });
        it("Should get success status ", async function () {
            (0, chai_1.expect)(transaction.successStatus).to.eq(0);
        });
        it("Should get payment summary ", async function () {
            const summary = await transaction.paymentSummary({ client: MccClient, inUtxo: 0, outUtxo: 0 });
            (0, chai_1.expect)(summary.status).to.eq(src_1.PaymentSummaryStatus.NotNativePayment);
        });
        it("Should spend amount ", async function () {
            (0, chai_1.expect)(transaction.spentAmounts.length).to.eq(1);
            (0, chai_1.expect)(transaction.spentAmounts[0].address).to.eq("rETx8GBiH6fxhTcfHM9fGeyShqxozyD3xe");
            (0, chai_1.expect)(transaction.spentAmounts[0].amount.toNumber()).to.eq(20);
        });
    });
    describe("Token Transfer transaction ", function () {
        let transaction;
        const txid = "AB790D026B7C2E42113A1834A032CED756090D0FCE7EA995E0ACDE9660C88836";
        before(async function () {
            transaction = await MccClient.getTransaction(txid);
        });
        it("Should find transaction ", function () {
            (0, chai_1.expect)(transaction).to.not.eq(undefined);
        });
        it("Should get transaction txid ", async function () {
            (0, chai_1.expect)(transaction.txid).to.eq(txid);
        });
        it("Should get standardized txid ", async function () {
            (0, chai_1.expect)(transaction.stdTxid).to.eq(txid);
        });
        it("Should get transaction hash ", async function () {
            (0, chai_1.expect)(transaction.hash).to.eq(txid);
        });
        it("Should get transaction reference array ", async function () {
            (0, chai_1.expect)(transaction.reference.length).to.eq(1);
        });
        it("Should get standardized transaction reference ", async function () {
            (0, chai_1.expect)(transaction.stdPaymentReference).to.eq("0x0000000000000000000000000000000000000000000000000000000000000000");
        });
        it("Should get transaction timestamp ", async function () {
            (0, chai_1.expect)(transaction.unixTimestamp).to.eq(1646398292);
        });
        it("Should get source address ", async function () {
            (0, chai_1.expect)(transaction.sourceAddresses.length).to.eq(1);
            (0, chai_1.expect)(transaction.sourceAddresses[0]).to.eq("rBy7gEjA6AJytwZAUKYfXvGAf5Y1koFCX1");
        });
        it("Should get receiving address ", async function () {
            (0, chai_1.expect)(transaction.receivingAddresses.length).to.eq(0);
        });
        it("Should get fee ", async function () {
            (0, chai_1.expect)(transaction.fee.toNumber()).to.eq(20);
        });
        it("Should spend amount ", async function () {
            (0, chai_1.expect)(transaction.spentAmounts.length).to.eq(1);
            (0, chai_1.expect)(transaction.spentAmounts[0].address).to.eq("rBy7gEjA6AJytwZAUKYfXvGAf5Y1koFCX1");
            (0, chai_1.expect)(transaction.spentAmounts[0].amount.toNumber()).to.eq(20);
        });
        it("Should received amount ", async function () {
            (0, chai_1.expect)(transaction.receivedAmounts.length).to.eq(0);
        });
        it("Should get type ", async function () {
            (0, chai_1.expect)(transaction.type).to.eq("Payment");
        });
        it("Should check if native payment ", async function () {
            (0, chai_1.expect)(transaction.isNativePayment).to.eq(false);
        });
        it("Should get currency name ", async function () {
            (0, chai_1.expect)(transaction.currencyName).to.eq("CX1");
        });
        it("Should get elementary unit ", async function () {
            (0, chai_1.expect)(transaction.elementaryUnits.toNumber()).to.eq(1000000);
        });
        it("Should get success status ", async function () {
            (0, chai_1.expect)(transaction.successStatus).to.eq(0);
        });
        it.skip("Should get payment summary ", async function () {
            const summary = await transaction.paymentSummary({ client: MccClient, inUtxo: 0, outUtxo: 0 });
            (0, chai_1.expect)(summary.status).to.eq(src_1.PaymentSummaryStatus.Success);
            (0, chai_1.expect)(summary.response).to.exist;
            (0, chai_1.expect)(summary.response.sourceAddress).to.eq("rBy7gEjA6AJytwZAUKYfXvGAf5Y1koFCX1");
            (0, chai_1.expect)(summary.response.receivingAddress).to.eq(undefined);
            (0, chai_1.expect)(summary.response.spentAmount.toNumber()).to.eq(20);
            (0, chai_1.expect)(summary.response.receivedAmount.toNumber()).to.eq(undefined);
            (0, chai_1.expect)(summary.response.paymentReference).to.eq("0x0000000000000000000000000000000000000000000000000000000000000000");
            (0, chai_1.expect)(summary.response.oneToOne).to.eq(true);
            (0, chai_1.expect)(summary.response.isFull).to.eq(true);
        });
    });
    describe("Transaction with native currency ", function () {
        let transaction;
        const txid = "75C7402BB60B574F7876627307EDACAA0A6830EB01692F150555999BDDBB4650";
        before(async function () {
            transaction = await MccClient.getTransaction(txid);
        });
        it("Should find transaction ", function () {
            (0, chai_1.expect)(transaction).to.not.eq(undefined);
        });
        it("Should get transaction txid ", async function () {
            (0, chai_1.expect)(transaction.txid).to.eq(txid);
        });
        it("Should get standardized txid ", async function () {
            (0, chai_1.expect)(transaction.stdTxid).to.eq(txid);
        });
        it("Should get transaction hash ", async function () {
            (0, chai_1.expect)(transaction.hash).to.eq(txid);
        });
        it("Should get transaction reference array ", async function () {
            (0, chai_1.expect)(transaction.reference.length).to.eq(0);
        });
        it("Should get standardized transaction reference ", async function () {
            (0, chai_1.expect)(transaction.stdPaymentReference).to.eq("0x0000000000000000000000000000000000000000000000000000000000000000");
        });
        it("Should get transaction timestamp ", async function () {
            (0, chai_1.expect)(transaction.unixTimestamp).to.eq(1647510771);
        });
        it("Should get source address ", async function () {
            (0, chai_1.expect)(transaction.sourceAddresses.length).to.eq(1);
            (0, chai_1.expect)(transaction.sourceAddresses[0]).to.eq("r3zUhJWabAMMLT5n631r2wDh9RP3dN1bRy");
        });
        it("Should get receiving address ", async function () {
            (0, chai_1.expect)(transaction.receivingAddresses.length).to.eq(1);
            (0, chai_1.expect)(transaction.receivingAddresses[0]).to.eq("rpE6gE8jEN1trDwQwe47VmgDL5y6m3XX2n");
        });
        it("Should get fee ", async function () {
            (0, chai_1.expect)(transaction.fee.toNumber()).to.eq(45);
        });
        it("Should spend amount ", async function () {
            (0, chai_1.expect)(transaction.spentAmounts.length).to.eq(1);
            (0, chai_1.expect)(transaction.spentAmounts[0].address).to.eq("r3zUhJWabAMMLT5n631r2wDh9RP3dN1bRy");
            (0, chai_1.expect)(transaction.spentAmounts[0].amount.toNumber()).to.eq(342390045);
        });
        it("Should received amount ", async function () {
            (0, chai_1.expect)(transaction.receivedAmounts.length).to.eq(1);
            (0, chai_1.expect)(transaction.receivedAmounts[0].address).to.eq("rpE6gE8jEN1trDwQwe47VmgDL5y6m3XX2n");
            (0, chai_1.expect)(transaction.receivedAmounts[0].amount.toNumber()).to.eq(342390000);
        });
        it("Should get type ", async function () {
            (0, chai_1.expect)(transaction.type).to.eq("Payment");
        });
        it("Should check if native payment ", async function () {
            (0, chai_1.expect)(transaction.isNativePayment).to.eq(true);
        });
        it("Should get currency name ", async function () {
            (0, chai_1.expect)(transaction.currencyName).to.eq("XRP");
        });
        it("Should get elementary unit ", async function () {
            (0, chai_1.expect)(transaction.elementaryUnits.toNumber()).to.eq(1000000);
        });
        it("Should get success status ", async function () {
            (0, chai_1.expect)(transaction.successStatus).to.eq(0);
        });
        it.skip("Should get payment summary ", async function () {
            var _a, _b;
            const summary = await transaction.paymentSummary({ client: MccClient, inUtxo: 0, outUtxo: 0 });
            (0, chai_1.expect)(summary.status).to.eq(src_1.PaymentSummaryStatus.Success);
            (0, chai_1.expect)(summary.response).to.exist;
            (0, chai_1.expect)(summary.response.sourceAddress).to.eq("r3zUhJWabAMMLT5n631r2wDh9RP3dN1bRy");
            (0, chai_1.expect)(summary.response.receivingAddress).to.eq("rpE6gE8jEN1trDwQwe47VmgDL5y6m3XX2n");
            (0, chai_1.expect)((_a = summary.response.spentAmount) === null || _a === void 0 ? void 0 : _a.toNumber()).to.eq(342390045);
            (0, chai_1.expect)((_b = summary.response.receivedAmount) === null || _b === void 0 ? void 0 : _b.toNumber()).to.eq(342390000);
            (0, chai_1.expect)(summary.response.paymentReference).to.eq("0x0000000000000000000000000000000000000000000000000000000000000000");
            (0, chai_1.expect)(summary.response.oneToOne).to.eq(true);
            (0, chai_1.expect)(summary.response.isFull).to.eq(true);
        });
    });
    describe("Transaction with no reference token transfer ", function () {
        let transaction;
        const txid = "84464F5001B9E7FD79C448B9C5F01085ACE56E94A1F6E2A737FDE9A993086F16";
        before(async function () {
            transaction = await MccClient.getTransaction(txid);
        });
        it("Should find transaction ", function () {
            (0, chai_1.expect)(transaction).to.not.eq(undefined);
        });
        it("Should get transaction txid ", async function () {
            (0, chai_1.expect)(transaction.txid).to.eq(txid);
        });
        it("Should get standardized txid ", async function () {
            (0, chai_1.expect)(transaction.stdTxid).to.eq(txid);
        });
        it("Should get transaction hash ", async function () {
            (0, chai_1.expect)(transaction.hash).to.eq(txid);
        });
        it("Should get transaction reference array ", async function () {
            (0, chai_1.expect)(transaction.reference.length).to.eq(0);
        });
        it("Should get standardized transaction reference ", async function () {
            (0, chai_1.expect)(transaction.stdPaymentReference).to.eq("0x0000000000000000000000000000000000000000000000000000000000000000");
        });
        it("Should get transaction timestamp ", async function () {
            (0, chai_1.expect)(transaction.unixTimestamp).to.eq(1646141781);
        });
        it("Should get source address ", async function () {
            (0, chai_1.expect)(transaction.sourceAddresses.length).to.eq(1);
            (0, chai_1.expect)(transaction.sourceAddresses[0]).to.eq("rP6JLXtRNs3tjeYnn7zUHpbfLjuyBXqhwF");
        });
        it("Should get receiving address ", async function () {
            (0, chai_1.expect)(transaction.receivingAddresses.length).to.eq(0);
        });
        it("Should get fee ", async function () {
            (0, chai_1.expect)(transaction.fee.toNumber()).to.eq(10);
        });
        it("Should spend amount ", async function () {
            (0, chai_1.expect)(transaction.spentAmounts.length).to.eq(1);
            (0, chai_1.expect)(transaction.spentAmounts[0].address).to.eq("rP6JLXtRNs3tjeYnn7zUHpbfLjuyBXqhwF");
            (0, chai_1.expect)(transaction.spentAmounts[0].amount.toNumber()).to.eq(10);
        });
        it("Should received amount ", async function () {
            (0, chai_1.expect)(transaction.receivedAmounts.length).to.eq(0);
        });
        it("Should get type ", async function () {
            (0, chai_1.expect)(transaction.type).to.eq("Payment");
        });
        it("Should check if native payment ", async function () {
            (0, chai_1.expect)(transaction.isNativePayment).to.eq(false);
        });
        it("Should get currency name ", async function () {
            (0, chai_1.expect)(transaction.currencyName).to.eq("426C657373656400000000000000000000000000");
        });
        it("Should get elementary unit ", async function () {
            (0, chai_1.expect)(transaction.elementaryUnits.toNumber()).to.eq(1000000);
        });
        it("Should get success status ", async function () {
            (0, chai_1.expect)(transaction.successStatus).to.eq(0);
        });
        it.skip("Should get payment summary ", async function () {
            var _a, _b;
            const summary = await transaction.paymentSummary({ client: MccClient, inUtxo: 0, outUtxo: 0 });
            (0, chai_1.expect)(summary.status).to.eq(src_1.PaymentSummaryStatus.Success);
            (0, chai_1.expect)(summary.response).to.exist;
            (0, chai_1.expect)(summary.response.sourceAddress).to.eq("rP6JLXtRNs3tjeYnn7zUHpbfLjuyBXqhwF");
            (0, chai_1.expect)(summary.response.receivingAddress).to.eq(undefined);
            (0, chai_1.expect)((_a = summary.response.spentAmount) === null || _a === void 0 ? void 0 : _a.toNumber()).to.eq(10);
            (0, chai_1.expect)((_b = summary.response.receivedAmount) === null || _b === void 0 ? void 0 : _b.toNumber()).to.eq(undefined);
            (0, chai_1.expect)(summary.response.paymentReference).to.eq("0x0000000000000000000000000000000000000000000000000000000000000000");
            (0, chai_1.expect)(summary.response.oneToOne).to.eq(true);
            (0, chai_1.expect)(summary.response.isFull).to.eq(true);
        });
        // it("Should received amount 2 ", async function () {
        //    //  delete transaction.data.result.meta;
        //    expect(transaction.receivedAmounts.length).to.eq(0);
        // });
    });
    describe("Transaction status and payment summary tests ", function () {
        let transaction1;
        let transaction2;
        let transaction3;
        const txid1 = "529C16436FFF89C1989A8D7B5182278BC6D8E5C93F4D0D052F9E39E27A222BB1";
        const txid2 = "F262BA3BD2575BCAC804E9320FEA90EFEA59BCA6723F431D9A4B80EBF9CC1058";
        const txid3 = "93D194C45CC60B2C17B8747BA50F1C028B637CFD9C5813918DBA73D2C21C2F27";
        before(async function () {
            //sinon.stub(console, "error");
            transaction1 = await MccClient.getTransaction(txid1);
            transaction2 = await MccClient.getTransaction(txid2);
            transaction3 = await MccClient.getTransaction(txid3);
        });
        // after(function () {
        //    sinon.restore();
        // });
        it(`Should get transaction status (${(0, testUtils_1.getTestFile)(__filename)})`, async function () {
            (0, chai_1.expect)(transaction1.successStatus).to.eq(src_1.TransactionSuccessStatus.SENDER_FAILURE);
            (0, chai_1.expect)(transaction2.successStatus).to.eq(src_1.TransactionSuccessStatus.RECEIVER_FAILURE);
            (0, chai_1.expect)(transaction3.successStatus).to.eq(src_1.TransactionSuccessStatus.RECEIVER_FAILURE);
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const metaData = transaction3._data.result.meta || transaction3._data.result.metaData;
            metaData.TransactionResult = "tecDST_TAG_NEEDED";
            (0, chai_1.expect)(transaction3.successStatus).to.eq(src_1.TransactionSuccessStatus.RECEIVER_FAILURE);
            metaData.TransactionResult = "tecNO_DST";
            (0, chai_1.expect)(transaction3.successStatus).to.eq(src_1.TransactionSuccessStatus.RECEIVER_FAILURE);
            metaData.TransactionResult = "tefALREADY";
            (0, chai_1.expect)(transaction3.successStatus).to.eq(src_1.TransactionSuccessStatus.SENDER_FAILURE);
        });
        it.skip("Should not get transaction status ", async function () {
            delete transaction2._data.result.meta;
            const fn = () => {
                return transaction2.successStatus;
            };
            (0, chai_1.expect)(fn).to.throw("OutsideError");
        });
        it("Should get payment summary ", async function () {
            const summary1 = await transaction1.paymentSummary({ client: MccClient, inUtxo: 0, outUtxo: 0 });
            const summary2 = await transaction2.paymentSummary({ client: MccClient, inUtxo: 0, outUtxo: 0 });
            const summary3 = await transaction3.paymentSummary({ client: MccClient, inUtxo: 0, outUtxo: 0 });
            (0, chai_1.expect)(summary1.status).to.eq(src_1.PaymentSummaryStatus.NotNativePayment);
            (0, chai_1.expect)(summary2.status).to.eq(src_1.PaymentSummaryStatus.NotNativePayment);
            // Transaction failed
            (0, chai_1.expect)(summary3.status).to.eq(src_1.PaymentSummaryStatus.UnexpectedNumberOfParticipants);
        });
    });
    describe.skip("Reference tests ", function () {
        let transaction;
        const txid = "C32ACF8CCF4F48B7AE097873AA2B7672DC66E05D4F1B3133DA90D1F476B1EAC6";
        before(async function () {
            transaction = await MccClient.getTransaction(txid);
        });
        it("References ", async () => {
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            transaction._data.result.Memos[0] = { Memo: { MemoType: "string" } };
            (0, chai_1.expect)(transaction.stdPaymentReference).to.eq("0x0000000000000000000000000000000000000000000000000000000000000000");
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            transaction._data.result.Memos[0] = { Memo: { MemoData: txid } };
            (0, chai_1.expect)(transaction.stdPaymentReference).to.eq("0x" + txid);
        });
    });
    describe("Transaction not found ", function () {
        it("Should not found", async () => {
            await (0, chai_1.expect)(MccClient.getTransaction("93D194C45CC60B2C17B8747BA50F1C028B637CFD9C5813918DBA73D2C21C2F20")).to.be.rejected;
        });
    });
});
//# sourceMappingURL=transaction.test.js.map