@cyberk/hyperion-sdk
Version:
Hyperion SDK from Cyberk
66 lines • 2.3 kB
JavaScript
import { buildExecuteContractMsg } from "../utils/buildExecuteContractMsg";
export const getExecuteSwapContractExecuteMsg = (options) => {
const { offerAsset, askAsset, minimumReceiveAmount, to = null, deadline = null, signerAddress, offerAmount, routerAddress, } = options;
const msg = {
execute_swap_operations: {
operations: [
{
hyperion: {
offer_asset_info: {
native_token: {
denom: offerAsset,
},
},
ask_asset_info: {
native_token: {
denom: askAsset,
},
},
},
},
],
minimum_receive: minimumReceiveAmount,
to,
deadline,
},
};
return buildExecuteContractMsg(routerAddress, msg, signerAddress, [
{
denom: offerAsset,
amount: offerAmount,
},
]);
};
export const getExecuteSwapQueryKey = (options) => {
return [
"executeSwap",
options.isSimulate ? "simulate" : "execute",
options.routerAddress,
options.offerAsset,
options.askAsset,
options.offerAmount,
options.minimumReceiveAmount,
options.signerAddress,
];
};
export const executeSwapFn = async (variables) => {
const { isSimulate = false } = variables;
const { signingClient, signerAddress, fee, memo } = variables;
const message = getExecuteSwapContractExecuteMsg(variables);
if (isSimulate) {
return await signingClient.simulate(signerAddress, [message], memo);
}
else {
if (!fee) {
throw new Error("Fee is required");
}
return await signingClient.signAndBroadcast(signerAddress, [message], fee, memo);
}
};
export const getExecuteSwapMutationOptions = (options) => {
return {
mutationFn: async (variables) => executeSwapFn({ ...variables, isSimulate: options.isSimulate }),
mutationKey: ["executeSwap", options.isSimulate ? "simulate" : "execute"],
};
};
//# sourceMappingURL=executeSwap.js.map