@firefly-exchange/library-sui
Version:
Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui
110 lines (109 loc) • 4.77 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.RFQChainCalls = void 0;
const classes_1 = require("../classes");
const library_1 = require("../library");
const types_1 = require("../types");
const utils_1 = require("@mysten/sui/utils");
const quote_1 = require("./quote");
const ChainCallsUtils_1 = require("../classes/ChainCallsUtils");
const utils_2 = require("../utils");
class RFQChainCalls extends ChainCallsUtils_1.ChainCallsUtils {
constructor(_suiClient, _config, options) {
super(_suiClient, _config, options);
}
/**
* Allows caller to execute authorized swap in vault
* @param swapParams params required for swap call
* @param options: OnChain params call
* Returns OnChainCallResponse
*/
async swap(swapParams, options) {
const txb = options?.txb || new types_1.TransactionBlock();
const quoteBytes = new quote_1.Quote(swapParams.quote).serialize();
// set gas object to false as we need to sponsor RFQ swap call , and using gas objects (txb.gas) is not allowed in sponsorship
const [splitCoin, mergeCoin] = await classes_1.CoinUtils.createCoinWithBalance(this.suiClient, txb, swapParams.quote.token_in_amount, (0, utils_2.addPrefix)(swapParams.quote.token_in_type), this.signerConfig.address, {
useGasObject: false
});
const buffer = Buffer.from(swapParams.signature, "base64");
const signature = buffer.toString("hex");
txb.moveCall({
arguments: [
txb.object(utils_1.SUI_CLOCK_OBJECT_ID),
txb.object(swapParams.quote.vault),
txb.object(this.config.ProtocolConfig),
txb.pure.vector("u8", Array.from(quoteBytes)),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(signature))),
txb.object(splitCoin)
],
target: `${this.config.Package}::gateway::swap`,
typeArguments: [
(0, utils_2.addPrefix)(swapParams.quote.token_in_type),
(0, utils_2.addPrefix)(swapParams.quote.token_out_type)
]
});
if (mergeCoin) {
txb.transferObjects([mergeCoin], this.signerConfig.address);
}
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return this.handleReturn(txb, options);
}
/**
* Allows caller to deposit token in the vault
* @param vault address of the vault
* @param amount The amount of token to be deposited in vault
* @param tokenType The type of the token to be deposited in vault
* @param options: OnChain params call
* Returns OnChainCallResponse
*/
async depositInVault(vault, amount, tokenType, options) {
const txb = options?.txb || new types_1.TransactionBlock();
const [splitCoin, mergeCoin] = await classes_1.CoinUtils.createCoinWithBalance(this.suiClient, txb, amount, tokenType, this.signerConfig.address);
txb.moveCall({
arguments: [
txb.object(vault),
txb.object(this.config.ProtocolConfig),
txb.object(splitCoin)
],
target: `${this.config.Package}::gateway::deposit`,
typeArguments: [tokenType]
});
if (mergeCoin) {
txb.transferObjects([mergeCoin], this.signerConfig.address);
}
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return this.handleReturn(txb, options);
}
/**
* Allows only manager of the vault to withdraw token from the vault
* @param vault address of the vault
* @param amount The amount of token to be withdraw from vault
* @param tokenType The type of the token to be withdraw from vault
* @param options: OnChain params call
* Returns OnChainCallResponse
*/
async withdrawFromVault(vault, amount, tokenType, options) {
const txb = options?.txb || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(vault),
txb.object(this.config.ProtocolConfig),
txb.pure.u64(amount)
],
target: `${this.config.Package}::gateway::withdraw`,
typeArguments: [tokenType]
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return this.handleReturn(txb, options);
}
}
exports.RFQChainCalls = RFQChainCalls;
;