@mak201010/bluefin-v2-client
Version:
The Bluefin client Library allows traders to sign, create, retrieve and listen to orders on Bluefin Exchange.
229 lines • 12.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContractCalls = void 0;
const library_sui_1 = require("@mak201010/library-sui");
const interpolate_1 = __importDefault(require("interpolate"));
const contractErrorHandling_service_1 = require("./contractErrorHandling.service");
class ContractCalls {
constructor(signer, deployment, provider, is_zkLogin, zkPayload, walletAddress, is_wallet_extension) {
/**
* Withdraws funds from the margin bank contract
* @param amount the amount to withdraw
* @returns ResponseSchema
* */
this.withdrawFromMarginBankContractCall = (amount) => __awaiter(this, void 0, void 0, function* () {
return (0, contractErrorHandling_service_1.TransformToResponseSchema)(() => __awaiter(this, void 0, void 0, function* () {
const tx = yield this.onChainCalls.withdrawFromBank({
amount: (0, library_sui_1.toBigNumberStr)(amount.toString(), 6),
accountAddress: this.walletAddress,
}, this.signer);
if (tx && !this.marginBankId) {
this.marginBankId = library_sui_1.Transaction.getBankAccountID(tx);
}
return tx;
}), (0, interpolate_1.default)(contractErrorHandling_service_1.SuccessMessages.withdrawMargin, { amount }));
});
/**
* Withdraws all funds from the margin bank contract
* @returns ResponseSchema
* */
this.withdrawAllFromMarginBankContractCall = () => __awaiter(this, void 0, void 0, function* () {
return (0, contractErrorHandling_service_1.TransformToResponseSchema)(() => __awaiter(this, void 0, void 0, function* () {
return yield this.onChainCalls.withdrawAllMarginFromBank(this.signer, this.walletAddress);
}), (0, interpolate_1.default)(contractErrorHandling_service_1.SuccessMessages.withdrawMargin, { amount: "all" }));
});
/**
* Deposits funds to the margin bank contract
* @param amount the amount to deposit
* @param coinID the coinID to deposit
* @returns ResponseSchema
* */
this.depositToMarginBankContractCall = (amount, coinID, getPublicAddress) => __awaiter(this, void 0, void 0, function* () {
return (0, contractErrorHandling_service_1.TransformToResponseSchema)(() => __awaiter(this, void 0, void 0, function* () {
const tx = yield this.onChainCalls.depositToBank({
amount: (0, library_sui_1.toBigNumberStr)(amount.toString(), 6),
coinID,
bankID: this.onChainCalls.getBankID(),
accountAddress: this.walletAddress || getPublicAddress(),
}, this.signer);
if (tx && !this.marginBankId) {
this.marginBankId = library_sui_1.Transaction.getBankAccountID(tx);
}
return tx;
}), (0, interpolate_1.default)(contractErrorHandling_service_1.SuccessMessages.depositToBank, { amount }));
});
/**
* adjusts the leverage of the desiered position
* @param leverage the leverage to set
* @param symbol the position's market symbol
* @returns ResponseSchema
* */
this.adjustLeverageContractCall = (leverage, symbol, parentAddress) => __awaiter(this, void 0, void 0, function* () {
const perpId = this.onChainCalls.getPerpetualID(symbol);
return (0, contractErrorHandling_service_1.TransformToResponseSchema)(() => __awaiter(this, void 0, void 0, function* () {
return yield this.onChainCalls.adjustLeverage({
leverage,
perpID: perpId,
account: parentAddress || this.walletAddress,
market: symbol,
}, this.signer);
}), (0, interpolate_1.default)(contractErrorHandling_service_1.SuccessMessages.adjustLeverage, { leverage }));
});
this.adjustLeverageContractCallRawTransaction = (leverage, symbol, getPublicAddress, parentAddress) => __awaiter(this, void 0, void 0, function* () {
const perpId = this.onChainCalls.getPerpetualID(symbol);
const signedTx = yield this.onChainCalls.signAdjustLeverage({
leverage,
perpID: perpId,
account: parentAddress || getPublicAddress(),
market: symbol,
}, this.signer);
//serialize
const separator = "||||"; // Choose a separator that won't appear in txBytes or signature
const combinedData = `${signedTx.bytes}${separator}${signedTx.signature}`;
// Encode to hex for transmission
const encodedData = Buffer.from(combinedData, "utf-8").toString("hex");
return encodedData;
});
/**
* This method return the signed Transaction for adding/removing the subaccount(s) on chain
* @param account The sub account address
* @param accountsToRemove The array of sub account addresses that need to be removed on-chain (optional param)
* @param subAccountsMapID The id of the chain object that holds subaccounts mapping (optional param)
* @param gasBudget The gas budget to be passed to execute the on-chain transaction (optional param)
* @returns string
* */
this.upsertSubAccountContractCallRawTransaction = (account, accountsToRemove, subAccountsMapID, gasBudget) => __awaiter(this, void 0, void 0, function* () {
const signedTx = yield this.onChainCalls.signUpsertSubAccount({
account,
accountsToRemove,
subAccountsMapID,
gasBudget,
}, this.signer);
//serialize
const separator = "||||"; // Choose a separator that won't appear in txBytes or signature
const combinedData = `${signedTx.bytes}${separator}${signedTx.signature}`;
// Encode to hex for transmission
const encodedData = Buffer.from(combinedData, "utf-8").toString("hex");
return encodedData;
});
/**
* closes the desiered position
* @param publicAddress the sub account's public address
* @param status the status to set for sub account true = add, false = remove
* @returns ResponseSchema
* */
this.setSubAccount = (publicAddress, status) => __awaiter(this, void 0, void 0, function* () {
return (0, contractErrorHandling_service_1.TransformToResponseSchema)(() => __awaiter(this, void 0, void 0, function* () {
return yield this.onChainCalls.setSubAccount({
account: publicAddress,
status,
}, this.signer);
}), (0, interpolate_1.default)(contractErrorHandling_service_1.SuccessMessages.setSubAccounts, { address: publicAddress, status: status ? "added" : "removed" }));
});
/**
* adjusts the margin of the desiered position
* @param symbol the position's market symbol
* @operationType the operation type to perform (add or remove)
* @amount the amount to add or remove
* @returns Response Schemea
* */
this.adjustMarginContractCall = (symbol, operationType, amount) => __awaiter(this, void 0, void 0, function* () {
const perpId = this.onChainCalls.getPerpetualID(symbol);
const msg = operationType == library_sui_1.ADJUST_MARGIN.Add
? (0, interpolate_1.default)(contractErrorHandling_service_1.SuccessMessages.adjustMarginAdd, { amount })
: (0, interpolate_1.default)(contractErrorHandling_service_1.SuccessMessages.adjustMarginRemove, { amount });
return (0, contractErrorHandling_service_1.TransformToResponseSchema)(() => __awaiter(this, void 0, void 0, function* () {
if (operationType === library_sui_1.ADJUST_MARGIN.Add) {
return this.onChainCalls.addMargin({
amount,
perpID: perpId,
market: symbol,
account: this.walletAddress,
}, this.signer);
}
return yield this.onChainCalls.removeMargin({
amount,
perpID: perpId,
market: symbol,
account: this.walletAddress,
}, this.signer);
}), msg);
});
/**
* Get the margin bank balance
* @returns number
* */
this.getMarginBankBalance = () => __awaiter(this, void 0, void 0, function* () {
if (this.marginBankId) {
return (0, library_sui_1.toBaseNumber)((yield this.onChainCalls.getBankAccountDetailsUsingID(this.marginBankId)).balance);
}
return 0;
});
/**
* transfer coins
* @param to recipient wallet address
* @param balance amount to transfer
* @param coin coin to transfer
* @returns Response Schema
* */
this.transferCoins = (to, balance, coin) => __awaiter(this, void 0, void 0, function* () {
return (0, contractErrorHandling_service_1.TransformToResponseSchema)(() => __awaiter(this, void 0, void 0, function* () {
return yield this.onChainCalls.transferCoins({
to,
balance,
coin,
}, this.signer);
}), (0, interpolate_1.default)(contractErrorHandling_service_1.SuccessMessages.transferCoins, { balance, coin, walletAddress: to }));
});
/**
* estimate gas for sui token transfer
* @param to recipient wallet address
* @param balance SUI amount to transfer
* @returns Response Schema
* */
this.estimateGasForSuiTransfer = (to, balance) => __awaiter(this, void 0, void 0, function* () {
return yield this.onChainCalls.estimateGasForSuiTransfer({
to,
balance,
});
});
/**
* esimate gas for USDC token transfer
* @param to recipient wallet address
* @param balance USDC amount to transfer
* @returns Response Schema
* */
this.estimateGasForUsdcTransfer = (to, balance) => __awaiter(this, void 0, void 0, function* () {
return yield this.onChainCalls.estimateGasForUSDCTransfer({
to,
balance,
});
});
/**
* fetch user sui balance
* @param walletAddress wallet address of the user
* @returns string
* */
this.getSUIBalance = (walletAddress) => __awaiter(this, void 0, void 0, function* () {
return yield this.onChainCalls.getUserSuiBalance(walletAddress);
});
this.signer = signer;
this.walletAddress = walletAddress || signer.toSuiAddress();
this.is_wallet_extension = is_wallet_extension;
this.onChainCalls = new library_sui_1.OnChainCalls(this.signer, deployment, provider, is_zkLogin, zkPayload, walletAddress, is_wallet_extension);
}
}
exports.ContractCalls = ContractCalls;
//# sourceMappingURL=contractService.js.map