@yoroi/swap
Version:
The Swap package of Yoroi SDK
256 lines (251 loc) • 7.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.minswapApiMaker = void 0;
var _common = require("@yoroi/common");
var _types = require("@yoroi/types");
var _immer = require("immer");
var _transformers = require("./transformers");
const baseUrls = {
[_types.Chain.Network.Mainnet]: 'https://agg-api.minswap.org/aggregator'
};
const minswapApiMaker = config => {
const {
address,
network,
request = _common.fetchData
} = config;
if (network !== _types.Chain.Network.Mainnet) return new Proxy({}, {
get() {
return () => Promise.resolve((0, _immer.freeze)({
tag: 'left',
error: {
status: -3,
message: 'Minswap api only works on mainnet'
}
}, true));
}
});
const baseUrl = baseUrls[network];
const transformers = (0, _transformers.transformersMaker)(config);
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
return (0, _immer.freeze)({
async tokens() {
const requestBody = {
query: '',
only_verified: true,
page: 1,
limit: 1000
};
try {
const response = await request({
method: 'post',
url: `${baseUrl}/tokens`,
headers,
data: requestBody
});
if ((0, _common.isLeft)(response)) return parseMinswapError(response);
return (0, _immer.freeze)({
tag: 'right',
value: {
status: response.value.status,
data: transformers.tokens.response(response.value.data)
}
}, true);
} catch (error) {
return (0, _immer.freeze)({
tag: 'left',
error: {
status: -1,
message: error instanceof Error ? error.message : 'Network error',
responseData: null
}
}, true);
}
},
async orders() {
try {
const response = await request({
method: 'get',
url: `${baseUrl}/pending-orders?owner_address=${address}&amount_in_decimal=true`,
headers
});
if ((0, _common.isLeft)(response)) return parseMinswapError(response);
return (0, _immer.freeze)({
tag: 'right',
value: {
status: response.value.status,
data: transformers.orders.response(response.value.data)
}
}, true);
} catch (error) {
return (0, _immer.freeze)({
tag: 'left',
error: {
status: -1,
message: error instanceof Error ? error.message : 'Network error',
responseData: null
}
}, true);
}
},
async limitOptions() {
// Minswap Aggregator doesn't support limit swaps yet
return (0, _immer.freeze)({
tag: 'left',
error: {
status: -3,
message: 'Limit options not supported',
responseData: null
}
}, true);
},
async estimate(body) {
const kind = body.wantedPrice !== undefined ? 'limitEstimate' : body.amountOut !== undefined ? 'reverseEstimate' : 'estimate';
if (kind !== 'estimate') {
return (0, _immer.freeze)({
tag: 'left',
error: {
status: -1,
message: kind === 'reverseEstimate' ? 'Set input amount' : 'Minswap Aggregator only supports market',
responseData: null
}
});
}
const requestBody = transformers.estimate.request(body);
try {
const response = await request({
method: 'post',
url: `${baseUrl}/estimate`,
headers,
data: requestBody
});
if ((0, _common.isLeft)(response)) return parseMinswapError(response);
return (0, _immer.freeze)({
tag: 'right',
value: {
status: response.value.status,
data: transformers.estimate.response(response.value.data)
}
}, true);
} catch (error) {
return (0, _immer.freeze)({
tag: 'left',
error: {
status: -1,
message: error instanceof Error ? error.message : 'Network error',
responseData: null
}
}, true);
}
},
async create(body) {
const requestBody = transformers.create.request(body);
// Make the build-tx call
let response;
try {
response = await request({
method: 'post',
url: `${baseUrl}/build-tx`,
headers,
data: requestBody
});
} catch (error) {
return (0, _immer.freeze)({
tag: 'left',
error: {
status: -1,
message: error instanceof Error ? error.message : 'Network error',
responseData: null
}
}, true);
}
if ((0, _common.isLeft)(response)) return parseMinswapError(response);
// Make an ad-hoc estimate call to get the swap details
const estimateRequest = {
amountIn: body.amountIn,
tokenIn: body.tokenIn,
tokenOut: body.tokenOut,
slippage: body.slippage ?? 0,
blockedProtocols: body.blockedProtocols,
protocol: body.protocol
};
const estimateResponse = await this.estimate(estimateRequest);
// If estimate fails, return the create response with minimal data
if ((0, _common.isLeft)(estimateResponse)) {
return (0, _immer.freeze)({
tag: 'right',
value: {
status: response.value.status,
data: transformers.create.response(response.value.data)
}
}, true);
}
// Merge the CBOR from create with the estimate data
const estimateData = estimateResponse.value.data;
const mergedData = {
...estimateData,
cbor: response.value.data.cbor,
aggregator: _types.Swap.Aggregator.Minswap,
totalInput: estimateData.totalInput ?? body.amountIn
};
return (0, _immer.freeze)({
tag: 'right',
value: {
status: response.value.status,
data: mergedData
}
}, true);
},
async cancel(body) {
const requestBody = {
sender: address,
orders: [{
tx_in: `${body.order.txHash}#${body.order.outputIndex}`,
protocol: body.order.protocol
}]
};
try {
const response = await request({
method: 'post',
url: `${baseUrl}/cancel-tx`,
headers,
data: requestBody
});
if ((0, _common.isLeft)(response)) return parseMinswapError(response);
return (0, _immer.freeze)({
tag: 'right',
value: {
status: response.value.status,
data: transformers.cancel.response(response.value.data)
}
}, true);
} catch (error) {
return (0, _immer.freeze)({
tag: 'left',
error: {
status: -1,
message: error instanceof Error ? error.message : 'Network error',
responseData: null
}
}, true);
}
}
}, true);
};
exports.minswapApiMaker = minswapApiMaker;
const parseMinswapError = ({
tag,
error
}) => (0, _immer.freeze)({
tag,
error: {
...error,
message: typeof error.responseData?.message === 'string' ? error.responseData.message : error.message || 'Minswap API error'
}
}, true);
//# sourceMappingURL=api-maker.js.map