@firefly-exchange/library-sui
Version:
Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui
951 lines (950 loc) • 45.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TxBuilder = void 0;
const utils_1 = require("../utils");
const types_1 = require("../../types");
const library_1 = require("../../library");
const bcs_1 = require("../utils/bcs");
const blv_1 = require("../../blv");
class TxBuilder {
constructor(_deployment) {
this.parser = new utils_1.DeploymentParser(_deployment);
}
/**
* Creates support asset transaction
* @param supportedCoin supported coin address. should be of format 0xax....bs::coin::COIN
* @param supportedCoinSymbol Name of the supported coin
* @param coinDecimals The number of decimals in supported coin
* @param weight The discounted price percentage to be used for asset
* @param collateral True if the asset can be used to collateralize a position
* @param options Optional tx building params
* @returns TransactionBlock
*/
supportAsset(supportedCoin, supportedCoinSymbol, coinDecimals, weight, price, collateral, minDeposit, maxDeposit, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getAdminCap()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.string(supportedCoinSymbol),
txb.pure.u8(Number(coinDecimals)),
txb.pure.u64(weight),
txb.pure.u64(price),
txb.pure.bool(collateral),
txb.pure.u64(minDeposit),
txb.pure.u64(maxDeposit)
],
typeArguments: [supportedCoin],
target: `${this.parser.getPackageId()}::data_store::support_asset`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create data store creation transaction
* @param sequencer address of the sequencer that will own the data store
* @param options Optional tx building params
* @returns TransactionBlock
*/
createInternalDataStore(sequencer, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getAdminCap()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.address(sequencer)
],
target: `${this.parser.getPackageId()}::data_store::create_internal_data_store`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create internal data store transfer call
* @param sequencer address of the new sequencer that will own ids
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
transferInternalDataStore(sequencer, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.address(sequencer)
],
target: `${this.parser.getPackageId()}::data_store::transfer_ids`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Builds USDC mint coin transaction
* @param amount the amount to be minted
* @param to the receiver of the amount
* @param options Optional tx building params
* @returns TransactionBlock
*/
mintUSDC(amount, to, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getTreasuryCap()),
txb.pure.u64(amount),
txb.pure.address(to)
],
target: `${this.parser.getPackageId()}::coin::mint`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Builds transaction to deposit usdc to external bank
* @param assetSymbol name of the asset to be deposited
* @param destination the receiver of the deposited coins on the bank
* @param amount the amount to be deposited - should be in the decimals of the coin being deposited
* @param coinID the id of the usdc coin to be deposited
* @param options Optional tx building params
* @returns TransactionBlock
*/
depositToAssetBank(assetSymbol, destination, amount, coinID, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getExternalDataStore()),
txb.pure.string(assetSymbol),
txb.pure.address(destination),
txb.pure.u64((0, library_1.bigNumber)(amount).toFixed(0)),
txb.object(coinID)
],
typeArguments: [this.parser.getAssetCoinType(assetSymbol)],
target: `${this.parser.getPackageId()}::exchange::deposit_to_asset_bank`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Builds transaction to deposit usdc to internal bank
* @param assetSymbol The symbol of the asset being deposited to internal bank
* @param nonce the nonce emitted during asset deposit in shared bank
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param options Optional tx building params
* @returns TransactionBlock
*/
depositToInternalBank(assetSymbol, nonce, sequenceHash, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.u128(nonce),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash)))
],
typeArguments: [this.parser.getAssetCoinType(assetSymbol)],
target: `${this.parser.getPackageId()}::exchange::deposit_to_internal_bank`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Builds transaction to remove tainted asset from EDS
* @param assetSymbol The symbol of the asset being deposited to internal bank
* @param nonce the nonce emitted during asset deposit in shared bank
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param options Optional tx building params
* @returns TransactionBlock
*/
removeTaintedAsset(assetSymbol, nonce, sequenceHash, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.u128(nonce),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash)))
],
typeArguments: [this.parser.getAssetCoinType(assetSymbol)],
target: `${this.parser.getPackageId()}::exchange::remove_tainted_asset`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates perpetual creation transaction
* @param PerpetualCreationParams take a look at IPerpetualConfig
* @param options Optional tx building params
* @returns TransactionBlock
*/
createPerpetual(symbol, imr, mmr, stepSize, tickSize, minTradeQuantity, maxTradeQuantity, minTradePrice, maxTradePrice, maxNotionalAtOpen, mtbLong, mtbShort, makerFee, takerFee, maxFundingRate, insurancePoolRatio, tradingStartTime, insurancePool, feePool, isolatedOnly, baseAssetSymbol, baseAssetName, baseAssetDecimals, maxLimitOrderQuantity, maxMarketOrderQuantity, defaultLeverage, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getAdminCap()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.string(symbol),
txb.pure.u64(imr),
txb.pure.u64(mmr),
txb.pure.u64(stepSize),
txb.pure.u64(tickSize),
txb.pure.u64(minTradeQuantity),
txb.pure.u64(maxTradeQuantity),
txb.pure.u64(minTradePrice),
txb.pure.u64(maxTradePrice),
txb.pure.vector("u64", maxNotionalAtOpen),
txb.pure.u64(mtbLong),
txb.pure.u64(mtbShort),
txb.pure.u64(makerFee),
txb.pure.u64(takerFee),
txb.pure.u64(maxFundingRate),
txb.pure.u64(insurancePoolRatio),
txb.pure.u64(tradingStartTime),
txb.pure.address(insurancePool),
txb.pure.address(feePool),
txb.pure.bool(isolatedOnly),
txb.pure.string(baseAssetSymbol),
txb.pure.string(baseAssetName),
txb.pure.u64(baseAssetDecimals),
txb.pure.u64(maxLimitOrderQuantity),
txb.pure.u64(maxMarketOrderQuantity),
txb.pure.u64(defaultLeverage)
],
target: `${this.parser.getPackageId()}::data_store::create_perpetual`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create transaction to update perpetual
* @param perpetual the symbol of the perpetual/market to be updated
* @param field the name of the field/config being updated
* @param serialized_value A BCS serialized value to be set on perpetual config
* @param options Optional tx building params
* @returns OnChainCallResponse
*/
updatePerpetual(perpetual, field, serialized_value, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getAdminCap()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.string(perpetual),
txb.pure.vector("u8", Array.from(Buffer.from(field))),
txb.pure.vector("u8", serialized_value)
],
target: `${this.parser.getPackageId()}::data_store::update_perpetual`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction to synchronize the attributes of provided perpetual between ids and eds
* @param perpetual the symbol of the perpetual/market to be synced
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param options Optional tx building params
* @returns Transaction Block
*/
syncPerpetual(perpetual, sequenceHash, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
const ids = options?.ids || this.parser.getInternalDataStore();
const eds = options?.eds || this.parser.getExternalDataStore();
txb.moveCall({
arguments: [
txb.object(ids),
txb.object(eds),
txb.pure.string(perpetual),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash)))
],
target: `${this.parser.getPackageId()}::data_store::sync_perpetual`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction to synchronize the supported asset among data stores
* @param symbol the symbol of the asset
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param options Optional tx building params
* @returns Transaction Block
*/
syncSupportedAsset(symbol, sequenceHash, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.string(symbol),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash)))
],
target: `${this.parser.getPackageId()}::data_store::sync_supported_asset`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction for withdraw call on-chain to move funds for a user
* from the bank to user address
* @param data serialized hex string of the withdrawal payload
* @param signature base64 signature payload generated by the user by signing the withdrawal request
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param options Optional tx execution params
* @returns TransactionBlock
*/
withdrawFromBank(data, signature, perpetuals, oraclePrices, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
const rawData = bcs_1.BCSUtils.deserializeData(data, bcs_1.Withdrawal);
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("string", perpetuals),
txb.pure.vector("u64", oraclePrices),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
typeArguments: [this.parser.getAssetCoinType(rawData.assetSymbol)],
target: `${this.parser.getPackageId()}::exchange::withdraw_from_bank`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction for user authorization call on-chain to authorize/un-authorized given user
* @param data serialized hex string of the authorization payload
* @param signature base64 signature generate by singing the request payload data
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which off-chain margining engine processed this request
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
authorizeUser(data, signature, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::authorize_account`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates trade transaction block
* @param makerOrder The signed maker order
* @param takerOrder The signed taker order
* @param quantity The quantity to be trade
* @param perpetuals The list of perpetual address for which oracle prices are to be updated
* @param oraclePrice The list of oracle prices
* @param sequenceHash Sequence hash
* @param timestamp The timestamp at which trade was executed off-chain ( on margining engine )
* @param options Optional tx execution params & execution time
* @returns TransactionBlock
*/
performTrade(makerOrder, takerOrder, makerSignature, takerSignature, quantity, perpetuals, oraclePrices, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from(makerOrder)),
txb.pure.vector("u8", Array.from(takerOrder)),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(makerSignature))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(takerSignature))),
txb.pure.u64(quantity),
txb.pure.vector("string", perpetuals),
txb.pure.vector("u64", oraclePrices),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::trade`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates batch trade transaction block
* @param trades The batch of trade to be executed
* @param perpetuals The list of perpetual address for which oracle prices are to be updated
* @param oraclePrice The list of oracle prices
* @param sequenceHash Sequence hash
* @param timestamp The timestamp at which trade was executed off-chain ( on margining engine )
* @param options Optional tx execution params & execution time
* @returns TransactionBlock
*/
performBatchTrade(batchTrade, perpetuals, oraclePrices, batchHash, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
const isGasFeeProvided = options?.gasFee !== undefined;
let method = "batch_trade";
const args = [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("address", batchTrade.makerOrders.map(o => o.ids)),
txb.pure.vector("address", batchTrade.makerOrders.map(o => o.account)),
txb.pure.vector("string", batchTrade.makerOrders.map(o => o.market)),
txb.pure.vector("u64", batchTrade.makerOrders.map(o => o.price)),
txb.pure.vector("u64", batchTrade.makerOrders.map(o => o.quantity)),
txb.pure.vector("u64", batchTrade.makerOrders.map(o => o.leverage)),
txb.pure.vector("bool", batchTrade.makerOrders.map(o => o.side == "LONG")),
txb.pure.vector("bool", batchTrade.makerOrders.map(o => o.positionType == "ISOLATED")),
txb.pure.vector("u64", batchTrade.makerOrders.map(o => o.expiration)),
txb.pure.vector("u64", batchTrade.makerOrders.map(o => o.signedAt)),
txb.pure.vector("vector<u8>", batchTrade.makerOrders.map(o => Array.from((0, blv_1.fromBase64)(o.signature)))),
txb.pure.vector("vector<u8>", batchTrade.makerOrders.map(o => Array.from((0, library_1.hexStrToUint8)(o.hash)))),
txb.pure.vector("address", batchTrade.takerOrders.map(o => o.ids)),
txb.pure.vector("address", batchTrade.takerOrders.map(o => o.account)),
txb.pure.vector("string", batchTrade.takerOrders.map(o => o.market)),
txb.pure.vector("u64", batchTrade.takerOrders.map(o => o.price)),
txb.pure.vector("u64", batchTrade.takerOrders.map(o => o.quantity)),
txb.pure.vector("u64", batchTrade.takerOrders.map(o => o.leverage)),
txb.pure.vector("bool", batchTrade.takerOrders.map(o => o.side == "LONG")),
txb.pure.vector("bool", batchTrade.takerOrders.map(o => o.positionType == "ISOLATED")),
txb.pure.vector("u64", batchTrade.takerOrders.map(o => o.expiration)),
txb.pure.vector("u64", batchTrade.takerOrders.map(o => o.signedAt)),
txb.pure.vector("vector<u8>", batchTrade.takerOrders.map(o => Array.from((0, blv_1.fromBase64)(o.signature)))),
txb.pure.vector("vector<u8>", batchTrade.takerOrders.map(o => Array.from((0, library_1.hexStrToUint8)(o.hash)))),
txb.pure.vector("u64", batchTrade.fills),
txb.pure.vector("string", perpetuals),
txb.pure.vector("u64", oraclePrices)
];
if (isGasFeeProvided) {
args.push(txb.pure.u64(options.gasFee));
method = "batch_trade_with_provided_gas_fee";
}
args.push(txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(batchHash))));
args.push(txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))));
args.push(txb.pure.u64(timestamp));
txb.moveCall({
arguments: args,
target: `${this.parser.getPackageId()}::exchange::${method}`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates liquidation transaction block
* @param payload The liquidation payload that was signed by the liquidator
* @param signature The liquidator's signature (base64 format)
* @param perpetuals The list of perpetual address for which oracle prices are to be updated
* @param oraclePrice The list of oracle prices
* @param sequenceHash Sequence hash
* @param timestamp The timestamp at which liquidation was executed off-chain ( on margining engine )
* @param options Optional tx execution params & execution time
* @returns TransactionBlock
*/
performLiquidation(payload, signature, perpetuals, oraclePrices, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(payload))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("string", perpetuals),
txb.pure.vector("u64", oraclePrices),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::liquidate`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates adjust margin transaction block
* @param data serialized hex string of the adjust margin payload
* @param signature base64 signature payload generated by the user by signing the adjust margin request
* @param perpetuals The list of perpetual address for which oracle prices are to be updated
* @param oraclePrice The list of oracle prices
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The time at which margin got adjusted off-chain
* @param options Optional tx execution params & execution time
* @returns TransactionBlock
*/
adjustMargin(data, signature, perpetuals, oraclePrices, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("string", perpetuals),
txb.pure.vector("u64", oraclePrices),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::adjust_margin`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates adjust leverage transaction block
* @param data serialized hex string of the adjust leverage payload
* @param signature base64 signature payload generated by the user by signing the adjust leverage request
* @param perpetuals The list of perpetual address for which oracle prices are to be updated
* @param oraclePrice The list of oracle prices
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The time at which leverage got adjusted off-chain
* @param options Optional tx execution params
* @returns TransactionBlock
*/
adjustLeverage(data, signature, perpetuals, oraclePrices, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("string", perpetuals),
txb.pure.vector("u64", oraclePrices),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::adjust_leverage`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates set operator on eds transaction block
* @param type The type of the operator be updated
* @param newOperator The address of the new operator to be set
* @param options Optional tx execution params
* @returns TransactionBlock
*/
setOperatorEDS(type, newOperator, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getAdminCap()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.string(type),
txb.pure.address(newOperator)
],
target: `${this.parser.getPackageId()}::data_store::set_operator`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction to synchronize the provided operator among data stores
* @param type The operator key/type to be synced
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param options Optional tx building params
* @returns Transaction Block
*/
syncOperator(type, sequenceHash, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.object(this.parser.getExternalDataStore()),
txb.pure.string(type),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash)))
],
target: `${this.parser.getPackageId()}::data_store::sync_operator`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction for setting funding rate for markets/perpetuals on-chain
* @param data serialized hex string of the set funding rate payload
* @param signature base64 signature payload generated by the user by signing the funding rate request
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp the timestamp at which funding rate was set off-chain
* @param options Optional tx execution params
* @returns TransactionBlock
*/
setFundingRate(data, signature, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::set_funding_rate`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction for applying funding rate to provided accounts on-chain
* @param data serialized hex string of the apply funding rate payload
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The time at which funding rate was applied off-chain
* @param options Optional tx execution params
* @returns TransactionBlock
*/
applyFundingRate(data, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::apply_funding_rate`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create transaction to prune table
* @param data serialized hex string of purging table
* @param signature base64 signature payload generated by the user by signing the prune table request
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which table got pruned off-chain
* @param options?: ITxBuilderOptionalParams
* @returns TransactionBlock
*/
pruneTable(data, signature, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::prune_table`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create transaction to authorize a liquidator
* @param data serialized hex string of purging table
* @param signature base64 signature payload generated by the user by signing the request
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which the request was processed off-chain
* @param options?: ITxBuilderOptionalParams
* @returns TransactionBlock
*/
authorizeLiquidator(data, signature, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::authorize_liquidator`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create transaction to set fee tier of an account
* @param data bcs serialized data
* @param signature base64 signature encoded signature of the data
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which the request was processed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
setFeeTier(data, signature, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::set_fee_tier`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create transaction to set an account's type
* @param data bcs serialized data
* @param signature base64 signature encoded signature of the data
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which the request was processed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
setAccountType(data, signature, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::set_account_type`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create transaction to set an gas fee
* @param data bcs serialized data
* @param signature base64 signature encoded signature of the data
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which the request was processed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
setGasFee(data, signature, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::set_gas_fee`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Create transaction to set gas pool address
* @param data bcs serialized data
* @param signature base64 signature encoded signature of the data
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which the request was processed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
setGasPool(data, signature, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(data))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::set_gas_pool`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates ADL transaction and returns it
* @param data The serialized adl payload
* @param signature The adl operator's signature on the data payload
* @param oraclePrice The list of perpetuals and their new oracle prices
* @param sequenceHash Sequence hash
* @param timestamp The time at which adl was executed off-chain
* @param options Optional tx execution params & execution time
* @returns TransactionBlock
*/
performADL(payload, signature, perpetuals, oraclePrices, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(payload))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("string", perpetuals),
txb.pure.vector("u64", oraclePrices),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::deleverage`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates close position transaction and returns it
* @param data The serialized close position payload
* @param signature The account signature
* @param oraclePrice The list of perpetuals and their new oracle prices
* @param sequenceHash Sequence hash
* @param timestamp The time at which adl was executed off-chain
* @param options Optional tx execution params & execution time
* @returns TransactionBlock
*/
closePosition(payload, signature, perpetuals, oraclePrices, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(payload))),
txb.pure.vector("u8", Array.from((0, blv_1.fromBase64)(signature))),
txb.pure.vector("string", perpetuals),
txb.pure.vector("u64", oraclePrices),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::exchange::close_position`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction for overwriting an account's position on-chain
* @param account The address of the account being updated
* @param positon The position to be updated
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which the request was processed off-chain
* @param options Optional tx execution params & execution time
* @returns TransactionBlock
*/
overwriteUserPosition(account, positon, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.address(account),
txb.pure.string(positon.perpetual),
txb.pure.u64(positon.size),
txb.pure.u64(positon.average_entry_price),
txb.pure.u64(positon.leverage),
txb.pure.u64(positon.margin),
txb.pure.bool(positon.is_long),
txb.pure.bool(positon.is_isolated),
txb.pure.u64(positon.pending_funding_payment),
txb.pure.u64(positon.funding.timestamp),
txb.pure.u64(positon.funding.rate.value),
txb.pure.bool(positon.funding.rate.sign),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::data_store::overwrite_user_position`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
/**
* Creates transaction for overwriting an account's position on-chain
* @param account The address of the account being updated
* @param amount The amount of USDC to be updated
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which the request was processed off-chain
* @param options Optional tx execution params & execution time
* @returns TransactionBlock
*/
overwriteUserAssets(account, amount, sequenceHash, timestamp, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
txb.moveCall({
arguments: [
txb.object(this.parser.getInternalDataStore()),
txb.pure.address(account),
txb.pure.u64(amount),
txb.pure.vector("u8", Array.from((0, library_1.hexStrToUint8)(sequenceHash))),
txb.pure.u64(timestamp)
],
target: `${this.parser.getPackageId()}::data_store::overwrite_user_assets`
});
if (options?.gasBudget)
txb.setGasBudget(options.gasBudget);
if (options?.sender)
txb.setSenderIfNotSet(options.sender);
return txb;
}
}
exports.TxBuilder = TxBuilder;