@yoroi/swap
Version:
The Swap package of Yoroi SDK
242 lines (241 loc) • 10.3 kB
JavaScript
;
var _portfolio = require("@yoroi/portfolio");
var _types = require("@yoroi/types");
var _api = require("./api.mocks");
var _transformers = require("./transformers");
var _types2 = require("./types");
const address = 'addr1q9qhyvkm5fytm5ckgshny0zz08a3urhhh7ckdqxcm27av40eafn3v5lr2w2n2er9uj7c743mt42gpe8tgek6394z9t7qn4yjzl';
const addressHex = 'stake1u9qh50svpn80sk9ftv80l5m57840q3jecluvmjyvz5um46qaa79q4';
const network = _types.Chain.Network.Mainnet;
const stakingKey = 'stake1u8';
const transformers = (0, _transformers.transformersMaker)({
primaryTokenInfo: _api.primaryTokenInfo,
address,
addressHex,
network,
stakingKey,
isPrimaryToken: _portfolio.isPrimaryToken,
partner: 'somePartnerId'
});
describe('transformers', () => {
describe('tokens', () => {
test('should correctly transform the tokens response', () => {
expect(transformers.tokens.response(_api.api.responses.tokens)).toEqual(_api.api.results.tokens);
});
});
describe('orders', () => {
test('should correctly transform the orders response', () => {
expect(transformers.ordersHistory.response(_api.api.responses.orders)).toEqual(_api.api.results.orders);
});
});
describe('cancel', () => {
it.each(_api.api.inputs.cancel)('should correctly transform cancel request', input => {
expect(transformers.cancel.request(input)).toEqual(_api.api.requests.cancel);
});
test('should correctly transform the cancel response', () => {
expect(transformers.cancel.response(_api.api.responses.cancel)).toEqual(_api.api.results.cancel);
});
});
describe('quote', () => {
test('should correctly transform the quote request', () => {
expect(transformers.quote.request(_api.api.inputs.quote)).toEqual({
..._api.api.requests.quote,
// excluded_sources is omitted when providers payload is unavailable
excluded_sources: undefined
});
expect(transformers.quote.request({
slippage: 0.01,
tokenIn: '.',
tokenOut: 'af2e27f580f7f08e93190a81f72462f153026d06450924726645891b.44524950',
amountOut: 10,
protocol: 'minswap-v1',
blockedProtocols: ['wingriders-v1']
})).toEqual({
buy_amount: '10',
buy_token: 'af2e27f580f7f08e93190a81f72462f153026d06450924726645891b.44524950',
// excluded_sources omitted without providers FO mapping
excluded_sources: undefined,
numbers_have_decimals: true,
partner: 'somePartnerId',
sell_token: '.',
slippage: 0.0001
});
const input = {
..._api.api.inputs.quote,
protocol: undefined
};
const request = {
..._api.api.requests.quote,
// When providers mapping is unavailable and protocol is not pinned,
// we omit excluded_sources even if blockedProtocols is present
excluded_sources: undefined
};
expect(transformers.quote.request(input)).toEqual(request);
});
test('should correctly transform the quote response', () => {
const res = transformers.quote.response(_api.api.responses.quote);
expect(res).toMatchObject({
..._api.api.results.quote,
splits: [expect.objectContaining(_api.api.results.quote.splits[0])]
});
});
test('should correctly transform the quote response with missing output', () => {
const res = transformers.quote.response(_api.api.responses.quoteNoOut);
expect(res).toMatchObject({
..._api.api.results.quoteNoOut,
splits: [expect.objectContaining(_api.api.results.quoteNoOut.splits[0])]
});
});
test('should handle undefined blockedProtocols', () => {
const result = transformers.quote.request({
slippage: 0.01,
tokenIn: '.',
tokenOut: 'af2e27f580f7f08e93190a81f72462f153026d06450924726645891b.44524950',
amountIn: 10,
protocol: undefined,
blockedProtocols: undefined
});
expect(result.excluded_sources).toBeUndefined();
});
test('should handle null blockedProtocols', () => {
const result = transformers.quote.request({
slippage: 0.01,
tokenIn: '.',
tokenOut: 'af2e27f580f7f08e93190a81f72462f153026d06450924726645891b.44524950',
amountIn: 10,
protocol: undefined,
blockedProtocols: null
});
expect(result.excluded_sources).toBeUndefined();
});
});
describe('quoteLimit', () => {
test('should correctly transform the quote request', () => {
expect(transformers.limitQuote.request(_api.api.inputs.quoteLimit)).toEqual(_api.api.requests.quoteLimit);
const input = {
..._api.api.inputs.quoteLimit,
protocol: undefined
};
const request = {
..._api.api.requests.quoteLimit,
order_contract: undefined
};
expect(transformers.limitQuote.request(input)).toEqual(request);
});
});
describe('create', () => {
test('should correctly transform the create request', () => {
expect(transformers.create.request(_api.api.inputs.create[0])).toEqual({
..._api.api.requests.create(address)[0],
excluded_sources: undefined,
utxos: undefined
});
expect(transformers.create.request(_api.api.inputs.create[1])).toEqual({
..._api.api.requests.create(address)[1],
excluded_sources: undefined,
utxos: undefined
});
});
test('should correctly transform the create response', () => {
const res = transformers.create.response(_api.api.responses.create);
expect(res).toMatchObject({
..._api.api.results.create,
splits: [expect.objectContaining(_api.api.results.create.splits[0])]
});
});
test('should handle undefined blockedProtocols in create', () => {
const result = transformers.create.request({
slippage: 0.01,
tokenIn: '.',
tokenOut: 'af2e27f580f7f08e93190a81f72462f153026d06450924726645891b.44524950',
amountIn: 10,
protocol: undefined,
blockedProtocols: undefined
});
expect(result.excluded_sources).toBeUndefined();
});
test('should handle null blockedProtocols in create', () => {
const result = transformers.create.request({
slippage: 0.01,
tokenIn: '.',
tokenOut: 'af2e27f580f7f08e93190a81f72462f153026d06450924726645891b.44524950',
amountIn: 10,
protocol: undefined,
blockedProtocols: null
});
expect(result.excluded_sources).toBeUndefined();
});
});
describe('createLimit', () => {
test('should correctly transform the createLimit request', () => {
expect(transformers.createLimit.request(_api.api.inputs.createLimit[0])).toEqual(_api.api.requests.createLimit(address));
expect(transformers.createLimit.request(_api.api.inputs.createLimit[1])).toEqual({
..._api.api.requests.createLimit(address),
buy_amount: '0',
// Avoid sending unsupported order contract; omit when not resolvable
order_contract: undefined,
pool_id: undefined
});
});
test('should correctly transform the createLimit response', () => {
const res = transformers.createLimit.response(_api.api.responses.createLimit);
expect(res).toMatchObject({
..._api.api.results.createLimit,
splits: [expect.objectContaining(_api.api.results.createLimit.splits[0])]
});
});
});
describe('toSwapProtocol', () => {
it.each`
dex | protocol
${_types2.Dex.Minswap_v1} | ${_types.Swap.Protocol.Minswap_v1}
${_types2.Dex.Minswap_v2} | ${_types.Swap.Protocol.Minswap_v2}
${_types2.Dex.Minswap_stable} | ${_types.Swap.Protocol.Minswap_stable}
${_types2.Dex.Wingriders_v1} | ${_types.Swap.Protocol.Wingriders_v1}
${_types2.Dex.Vyfi_v1} | ${_types.Swap.Protocol.Vyfi_v1}
${_types2.Dex.Sundaeswap_v1} | ${_types.Swap.Protocol.Sundaeswap_v1}
${_types2.Dex.Sundaeswap_v3} | ${_types.Swap.Protocol.Sundaeswap_v3}
${_types2.Dex.Muesliswap_v2} | ${_types.Swap.Protocol.Muesliswap_v2}
${_types2.Dex.Muesliswap_clp} | ${_types.Swap.Protocol.Muesliswap_clp}
${_types2.Dex.Spectrum_v1} | ${_types.Swap.Protocol.Spectrum_v1}
${_types2.Dex.Teddy_v1} | ${_types.Swap.Protocol.Teddy_v1}
${_types2.Dex.Unsupported} | ${_types.Swap.Protocol.Unsupported}
${'new-protocol'} | ${_types.Swap.Protocol.Unsupported}
`('should map $dex to $protocol', ({
dex,
protocol
}) => {
expect((0, _transformers.toSwapProtocol)(dex)).toBe(protocol);
});
});
describe('fromSwapProtocol', () => {
it.each`
protocol | dex
${_types.Swap.Protocol.Minswap_v1} | ${_types2.Dex.Minswap_v1}
${_types.Swap.Protocol.Minswap_v2} | ${_types2.Dex.Minswap_v2}
${_types.Swap.Protocol.Minswap_stable} | ${_types2.Dex.Minswap_stable}
${_types.Swap.Protocol.Wingriders_v1} | ${_types2.Dex.Wingriders_v1}
${_types.Swap.Protocol.Vyfi_v1} | ${_types2.Dex.Vyfi_v1}
${_types.Swap.Protocol.Sundaeswap_v1} | ${_types2.Dex.Sundaeswap_v1}
${_types.Swap.Protocol.Sundaeswap_v3} | ${_types2.Dex.Sundaeswap_v3}
${_types.Swap.Protocol.Muesliswap_v2} | ${_types2.Dex.Muesliswap_v2}
${_types.Swap.Protocol.Muesliswap_clp} | ${_types2.Dex.Muesliswap_clp}
${_types.Swap.Protocol.Spectrum_v1} | ${_types2.Dex.Spectrum_v1}
${_types.Swap.Protocol.Teddy_v1} | ${_types2.Dex.Teddy_v1}
${_types.Swap.Protocol.Unsupported} | ${_types2.Dex.Unsupported}
${'new-protocol'} | ${_types2.Dex.Unsupported}
`('should map $protocol to $dex', ({
protocol,
dex
}) => {
expect((0, _transformers.fromSwapProtocol)(protocol)).toBe(dex);
});
it('should return Dex.Cswap_v1 for Cswap protocol', () => {
expect((0, _transformers.fromSwapProtocol)(_types.Swap.Protocol.Cswap)).toBe(_types2.Dex.Cswap_v1);
});
it('should return Dex.Unsupported for Splash_v1 protocol', () => {
expect((0, _transformers.fromSwapProtocol)(_types.Swap.Protocol.Splash_v1)).toBe(_types2.Dex.Unsupported);
});
});
});
//# sourceMappingURL=transformers.test.js.map