stellar-plus
Version:
beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain
157 lines (156 loc) • 8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const simulate_transaction_1 = require("../../../../stellar-plus/core/pipelines/simulate-transaction");
const errors_1 = require("../../../../stellar-plus/core/pipelines/simulate-transaction/errors");
const types_1 = require("../../../../stellar-plus/core/pipelines/simulate-transaction/types");
const network_1 = require("../../../../stellar-plus/network");
const MOCKED_SIMULATION_RESPONSE_BASE = {
events: [],
id: 'mocked-id',
latestLedger: 0,
_parsed: true,
};
const MOCKED_SIMULATION_RESPONSE_ERROR = Object.assign(Object.assign({}, MOCKED_SIMULATION_RESPONSE_BASE), { error: 'mocked error' });
const MOCKED_SIMULATION_RESPONSE_SUCCESS = Object.assign(Object.assign({}, MOCKED_SIMULATION_RESPONSE_BASE), { transactionData: new stellar_sdk_1.SorobanDataBuilder(), minResourceFee: '0', cost: {
cpuInsns: '0',
memBytes: '0',
} });
const MOCKED_SIMULATION_RESPONSE_RESTORE = Object.assign(Object.assign({}, MOCKED_SIMULATION_RESPONSE_SUCCESS), { result: {
auth: [],
xdr: 'mocked-xdr',
retval: stellar_sdk_1.xdr.ScVal.scvVoid(),
}, restorePreamble: {
minResourceFee: '0',
transactionData: new stellar_sdk_1.SorobanDataBuilder(),
} });
const MOCKED_INVALID_SIMULATION_RESPONSE = MOCKED_SIMULATION_RESPONSE_BASE;
const MOCKED_PK_A = 'GACF23GKVFTU77K6W6PWSVN7YBM63UHDULILIEXJO6FR4YKMJ7FW3DTI';
const MOCKED_ACCOUNT_A = new stellar_sdk_1.Account(MOCKED_PK_A, '100');
const TESTNET_PASSPHRASE = (0, network_1.TestNet)().networkPassphrase;
const MOCKED_FEE = '100';
const MOCKED_TX_OPTIONS = {
fee: MOCKED_FEE,
networkPassphrase: TESTNET_PASSPHRASE,
timebounds: {
minTime: 0,
maxTime: 0,
},
};
const MOCKED_TRANSACTION = new stellar_sdk_1.TransactionBuilder(MOCKED_ACCOUNT_A, MOCKED_TX_OPTIONS).build();
const MOCKED_EXCEPTION = new Error('simulateTransaction failed');
const mockConveyorBeltErrorMeta = (item) => {
return {
item,
meta: {
itemId: 'mocked-id',
beltId: 'mocked-belt-id',
beltType: types_1.SimulateTransactionPipelineType.id,
},
};
};
describe('SimulateTransactionPipeline', () => {
describe('Initialization', () => {
it('should initialize the pipeline successfully', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const pipeline = new simulate_transaction_1.SimulateTransactionPipeline();
expect(pipeline).toBeInstanceOf(simulate_transaction_1.SimulateTransactionPipeline);
}));
});
describe('errors', () => {
it('should throw failedToSimulateTransaction error when simulateTransaction fails to perform the simulation', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const MOCKED_RPC = {
simulateTransaction: jest.fn().mockImplementation(() => {
throw MOCKED_EXCEPTION;
}),
};
const pipeline = new simulate_transaction_1.SimulateTransactionPipeline();
const item = {
transaction: MOCKED_TRANSACTION,
rpcHandler: MOCKED_RPC,
};
yield expect(pipeline.execute(item)).rejects.toThrow(errors_1.PSIError.failedToSimulateTransaction(MOCKED_EXCEPTION, mockConveyorBeltErrorMeta(item)));
}));
it('should throw simulationFailed error when simulateTransaction provides a simulation that will fail to be executed', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const MOCKED_RPC = {
simulateTransaction: jest.fn().mockImplementation(() => {
return MOCKED_SIMULATION_RESPONSE_ERROR;
}),
};
const pipeline = new simulate_transaction_1.SimulateTransactionPipeline();
const item = {
transaction: MOCKED_TRANSACTION,
rpcHandler: MOCKED_RPC,
};
yield expect(pipeline.execute(item)).rejects.toThrow(errors_1.PSIError.simulationFailed(MOCKED_SIMULATION_RESPONSE_ERROR, mockConveyorBeltErrorMeta(item)));
}));
it('should throw simulationResultCouldNotBeVerified error when simulateTransaction provides a simulation that cannot be verified', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const MOCKED_RPC = {
simulateTransaction: jest.fn().mockImplementation(() => {
return MOCKED_INVALID_SIMULATION_RESPONSE;
}),
};
const pipeline = new simulate_transaction_1.SimulateTransactionPipeline();
const item = {
transaction: MOCKED_TRANSACTION,
rpcHandler: MOCKED_RPC,
};
yield expect(pipeline.execute(item)).rejects.toThrow(errors_1.PSIError.simulationResultCouldNotBeVerified(MOCKED_INVALID_SIMULATION_RESPONSE, mockConveyorBeltErrorMeta(item)));
}));
it('should throw failedToAssembleTransaction error when assembleTransaction fails to assemble the transaction', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const MOCKED_RPC = {
simulateTransaction: jest.fn().mockImplementation(() => {
return MOCKED_SIMULATION_RESPONSE_SUCCESS;
}),
};
const pipeline = new simulate_transaction_1.SimulateTransactionPipeline();
const item = {
transaction: MOCKED_TRANSACTION,
rpcHandler: MOCKED_RPC,
};
yield expect(pipeline.execute(item)).rejects.toThrow('Failed to assemble transaction!');
}));
});
describe('success', () => {
it('should return a successful response when simulateTransaction provides a simulation that can be verified as successfull', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const MOCKED_RPC = {
simulateTransaction: jest.fn().mockImplementation(() => {
return MOCKED_SIMULATION_RESPONSE_SUCCESS;
}),
};
const pipeline = new simulate_transaction_1.SimulateTransactionPipeline();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jest.spyOn(pipeline, 'assembleTransaction').mockImplementation(() => {
return MOCKED_TRANSACTION;
});
const item = {
transaction: MOCKED_TRANSACTION,
rpcHandler: MOCKED_RPC,
};
expect(yield pipeline.execute(item)).toEqual({
response: MOCKED_SIMULATION_RESPONSE_SUCCESS,
assembledTransaction: MOCKED_TRANSACTION,
});
}));
it('should return a successful response when simulateTransaction provides a simulation that can be verified as successfull but needs to be restored', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const MOCKED_RPC = {
simulateTransaction: jest.fn().mockImplementation(() => {
return MOCKED_SIMULATION_RESPONSE_RESTORE;
}),
};
const pipeline = new simulate_transaction_1.SimulateTransactionPipeline();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jest.spyOn(pipeline, 'assembleTransaction').mockImplementation(() => {
return MOCKED_TRANSACTION;
});
const item = {
transaction: MOCKED_TRANSACTION,
rpcHandler: MOCKED_RPC,
};
expect(yield pipeline.execute(item)).toEqual({
response: MOCKED_SIMULATION_RESPONSE_RESTORE,
assembledTransaction: MOCKED_TRANSACTION,
});
}));
});
});