@firefly-exchange/library-sui
Version:
Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui
308 lines (307 loc) • 17.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequencerCalls = void 0;
const types_1 = require("../../types");
const bcs_1 = require("../utils/bcs");
const on_chain_calls_1 = require("./on-chain-calls");
class SequencerCalls extends on_chain_calls_1.OnChainCalls {
/**
* Create and executes internal data store transfer call
* @param sequencer address of the new sequencer that will own ids
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
async transferInternalDataStore(sequencer, options) {
const txb = this.txBuilder.transferInternalDataStore(sequencer || this.parser.getInternalDataStore(), options);
return this.execCall(txb, options);
}
/**
* Allows the owner of internal bank to invoke the method and
* update/deposit user funds into internal bank from external
* @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 execution params
* @returns OnChainCallResponse & (assetSymbol of the deposit asset)
*/
async depositToInternalBank(nonce, sequenceHash, options) {
const txb = this.txBuilder.depositToInternalBank(options?.assetSymbol || "USDC", nonce, sequenceHash, options);
return this.execCall(txb, options);
}
/**
* Allows the sequencer to remove a tainted asset from EDS
* @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 execution params
* @returns OnChainCallResponse & (assetSymbol of the deposit asset)
*/
async removeTaintedAsset(nonce, sequenceHash, options) {
const txb = this.txBuilder.removeTaintedAsset(options?.assetSymbol || "USDC", nonce, sequenceHash, options);
return this.execCall(txb, options);
}
/**
* Allows the sequencer to invoke 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 bas64 signature generated by the user by signing the withdrawal request
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which withdraw was performed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
async withdrawFromBank(data, signature, oraclePrices, sequenceHash, timestamp, options) {
const txb = this.txBuilder.withdrawFromBank(data, signature, oraclePrices.map(op => op.perpetual), oraclePrices.map(op => new types_1.BigNumber(op.price).toFixed(0)), sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Allows the owner of internal bank to invoke the method and
* 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 execution params
* @returns OnChainCallResponse
*/
async syncPerpetual(perpetual, sequenceHash, options) {
const txb = this.txBuilder.syncPerpetual(perpetual, sequenceHash, options);
return this.execCall(txb, options);
}
/**
* Allows the sequencer to execute the authorized user call to whitelist/blacklist
* the given user to use the account
* @param data serialized hex string of the authorization payload
* @param signature bas64 signature by singing the request payload data
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which withdraw was performed off-chain
* @param options Optional tx execution params & timestamp - this is the time at which withdraw was performed off-chain
* @returns OnChainCallResponse
*/
async authorizeUser(data, signature, sequenceHash, timestamp, options) {
const txb = this.txBuilder.authorizeUser(data, signature, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Allows sequencer to replicate asset details from external data store to internal data store
* @param symbol the symbol of the asset
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
async syncSupportedAsset(symbol, sequenceHash, options) {
const txb = this.txBuilder.syncSupportedAsset(symbol, sequenceHash, options);
return this.execCall(txb, options);
}
/**
* Allows sequencer to execute trade between two orders
* @param makerOrder The signed maker order
* @param takerOrder The signed taker order
* @param quantity The quantity to be traded
* @param oraclePrice The list of perpetuals and their new oracle prices
* @param sequenceHash Sequence hash
* @param timestamp The timestamp at which withdraw was performed off-chain
* @param options Optional tx execution params & execution time
* @returns OnChainCallResponse
*/
async performTrade(makerOrder, takerOrder, quantity, oraclePrices, sequenceHash, timestamp, options) {
const txb = this.txBuilder.performTrade(bcs_1.BCSUtils.getSerializedDataBytes(makerOrder, bcs_1.Order), bcs_1.BCSUtils.getSerializedDataBytes(takerOrder, bcs_1.Order), makerOrder.signature, takerOrder.signature, new types_1.BigNumber(quantity).toFixed(0), oraclePrices.map(op => op.perpetual), oraclePrices.map(op => new types_1.BigNumber(op.price).toFixed(0)), sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Allows sequencer to execute liquidation trade
* @param data The serialized liquidation payload
* @param signature The liquidator'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 liquidation was executed off-chain
* @param options Optional tx execution params & execution time
* @returns OnChainCallResponse
*/
async performLiquidation(data, signature, oraclePrices, sequenceHash, timestamp, options) {
const txb = this.txBuilder.performLiquidation(data, signature, oraclePrices.map(op => op.perpetual), oraclePrices.map(op => new types_1.BigNumber(op.price).toFixed(0)), sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Allows sequencer to execute batch of trades
* @param args Array of trade arguments
* @param options Optional tx execution params & execution time
* @returns OnChainCallResponse
*/
async performTradeBatch(args, options) {
const txb = options?.txBlock || new types_1.TransactionBlock();
for (const tradeArgs of args) {
this.txBuilder.performTrade(bcs_1.BCSUtils.getSerializedDataBytes(tradeArgs.makerOrder, bcs_1.Order), bcs_1.BCSUtils.getSerializedDataBytes(tradeArgs.takerOrder, bcs_1.Order), tradeArgs.makerOrder.signature, tradeArgs.takerOrder.signature, new types_1.BigNumber(tradeArgs.quantity).toFixed(0), tradeArgs.oraclePrices.map(op => op.perpetual), tradeArgs.oraclePrices.map(op => new types_1.BigNumber(op.price).toFixed(0)), tradeArgs.sequenceHash, tradeArgs.timestamp, { ...options, txBlock: txb });
}
return this.execCall(txb, options);
}
/**
* Allows the sequencer to invoke adjust margin call on-chain for the provided signed payload
* to add/remove margin from a user's isolated position to/from cross account
* @param data serialized hex string of the adjust margin payload
* @param signature bas64 signature generated by the user by signing the adjust margin request
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param oraclePrices: The oracle prices of perpetuals at the time this tx was executed
* @param timestamp The time at which adjust margin was executed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
async adjustMargin(data, signature, oraclePrices, sequenceHash, timestamp, options) {
const txb = this.txBuilder.adjustMargin(data, signature, oraclePrices.map(op => op.perpetual), oraclePrices.map(op => new types_1.BigNumber(op.price).toFixed(0)), sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Allows the sequencer to invoke adjust leverage call on-chain for the provided signed payload
* to change the leverage for provided perpetual position
* @param data serialized hex string of the adjust leverage payload
* @param signature bas64 signature generated by the user by signing the adjust leverage request
* @param oraclePrices: The oracle prices of perpetuals at the time this tx was executed
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The time at which adjust leverage was executed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
async adjustLeverage(data, signature, oraclePrices, sequenceHash, timestamp, options) {
const txb = this.txBuilder.adjustLeverage(data, signature, oraclePrices.map(op => op.perpetual), oraclePrices.map(op => new types_1.BigNumber(op.price).toFixed(0)), sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Create and executes transaction to synchronize the operator between eds and ids
* @param type The type of the operator be synced
* @param sequenceHash The sequence hash computed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
async syncOperator(type, sequenceHash, options) {
const txb = this.txBuilder.syncOperator(type, sequenceHash, options);
return this.execCall(txb, options);
}
/**
* Create and executes transaction to set funding rate on chain
* @param data serialized hex string of the set funding rate payload
* @param signature bas64 signature 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 time at which funding rate was set off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
async setFundingRate(data, signature, sequenceHash, timestamp, options) {
const txb = this.txBuilder.setFundingRate(data, signature, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Create and executes transaction to apply funding rate 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 OnChainCallResponse
*/
async applyFundingRate(data, sequenceHash, timestamp, options) {
const txb = this.txBuilder.applyFundingRate(data, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Create and executes transaction to prune table
* @param data serialized hex string of purging table
* @param signature base64 signature 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 Optional tx execution params
* @returns OnChainCallResponse
*/
async pruneTable(data, signature, sequenceHash, timestamp, options) {
const txb = this.txBuilder.pruneTable(data, signature, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Create and executes transaction to authorize an account as a liquidator
* @param data serialized hex string of whitelisting liquidator payload
* @param signature base64 signature generated by the signer/guardian
* @param sequenceHash the expected sequence hash on-chain after the tx execution
* @param timestamp The timestamp at which request was processed off-chain
* @param options Optional tx execution params
* @returns OnChainCallResponse
*/
async authorizeLiquidator(data, signature, sequenceHash, timestamp, options) {
const txb = this.txBuilder.authorizeLiquidator(data, signature, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Create and executes 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
*/
async setFeeTier(data, signature, sequenceHash, timestamp, options) {
const txb = this.txBuilder.setFeeTier(data, signature, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Create and executes transaction to set an account 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
*/
async setAccountType(data, signature, sequenceHash, timestamp, options) {
const txb = this.txBuilder.setAccountType(data, signature, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Create and executes transaction to set 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
*/
async setGasFee(data, signature, sequenceHash, timestamp, options) {
const txb = this.txBuilder.setGasFee(data, signature, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Create and executes transaction to set gas pool
* @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
*/
async setGasPool(data, signature, sequenceHash, timestamp, options) {
const txb = this.txBuilder.setGasPool(data, signature, sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Allows sequencer to execute adl trade
* @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 OnChainCallResponse
*/
async performADL(data, signature, oraclePrices, sequenceHash, timestamp, options) {
const txb = this.txBuilder.performADL(data, signature, oraclePrices.map(op => op.perpetual), oraclePrices.map(op => new types_1.BigNumber(op.price).toFixed(0)), sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
/**
* Allows sequencer to execute close position call for delisted markets
* @param data The serialized close position payload
* @param signature The account 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 position was closed was executed off-chain
* @param options Optional tx execution params & execution time
* @returns OnChainCallResponse
*/
async closePosition(data, signature, oraclePrices, sequenceHash, timestamp, options) {
const txb = this.txBuilder.closePosition(data, signature, oraclePrices.map(op => op.perpetual), oraclePrices.map(op => new types_1.BigNumber(op.price).toFixed(0)), sequenceHash, timestamp, options);
return this.execCall(txb, options);
}
}
exports.SequencerCalls = SequencerCalls;