@firefly-exchange/library-sui
Version:
Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui
192 lines (191 loc) • 6.6 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transaction = void 0;
const errors_1 = require("../errors");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const library_1 = require("../library");
class Transaction {
static getStatus(tx) {
return tx.effects?.status.status;
}
// if no error returns error code as 0
static getErrorCode(tx) {
if (Transaction.getStatus(tx) == "failure") {
const error = tx.effects?.status.error;
return error.lastIndexOf(",") > 0
? Number(error.slice(error.lastIndexOf(",") + 2, error.lastIndexOf(")")))
: undefined;
}
return 0;
}
static getError(tx, errorCodes = errors_1.ERROR_CODES) {
const code = Transaction.getErrorCode(tx);
if (code > 0) {
return errorCodes[code];
}
else if (code == undefined) {
return tx.effects?.status.error;
}
else {
return "";
}
}
static getDryRunErrorCode(error) {
const regex = /}, [0-9]+[)]/;
const match = error.match(regex);
let code = "0";
if (match) {
code = match[0].replace(/\D/g, "");
}
return Number(code);
}
static getDryRunError(error) {
const code = Transaction.getDryRunErrorCode(error);
return errors_1.ERROR_CODES[code];
}
static getTxNumber(error) {
const regex = /(\d+)(?!.*\d)/;
const match = error.match(regex);
if (match) {
return Number(match[0]);
}
return -1;
}
static getEvents(tx, eventName) {
let events = [];
if (tx?.events) {
if (eventName != "") {
events = tx?.events
?.filter((x) => x.type.indexOf(eventName) >= 0)
.map((x) => {
return x.parsedJson;
});
}
}
return events;
}
static getCreatedObjectIDs(tx, onlyShared) {
const ids = [];
const objects = tx.effects?.created;
for (const itr in objects) {
// if looking for only shared objects
if (onlyShared) {
if (objects[itr].owner.Shared != undefined)
ids.push(objects[itr].reference.objectId);
}
else {
ids.push(objects[itr].reference.objectId);
}
}
return ids;
}
static getAllMutatedObjectIDs(tx) {
const ids = [];
const objects = tx.objectChanges;
for (const itr in objects) {
ids.push(objects[itr].objectId);
}
return ids;
}
static getMutatedObjectsUsingType(tx, type) {
const objects = tx.objectChanges;
const ids = [];
for (const itr in objects) {
if (objects[itr].objectType.indexOf(type) > 0) {
ids.push(objects[itr].objectId);
}
}
return ids;
}
static getObjectsFromEvents(tx, list, objectType) {
const objects = [];
const events = tx.events;
for (const ev of events) {
const obj = ev[list];
if (obj !== undefined) {
const objType = obj["type"]
.slice(obj["type"].lastIndexOf("::") + 2)
.replace(/[^a-zA-Z ]/g, "");
if (objectType == "" || objType == objectType) {
objects.push({
id: obj["id"],
dataType: objType,
data: obj["parsedJson"]
});
}
}
}
return objects;
}
static getAccountPosition(tx, address) {
const events = Transaction.getEvents(tx, "AccountPositionUpdateEvent");
let userPosition;
if (events[0].position.user == address)
userPosition = events[0].position;
else if (events[1].position.user == address)
userPosition = events[1].position;
else
throw `AccountPositionUpdate event not found for address: ${address}`;
return userPosition;
}
static getAccountPNL(tx, address) {
const events = Transaction.getEvents(tx, "TradeExecuted");
if (events.length == 0) {
throw "No TradeExecuted event found in tx";
}
if (address == events[0].maker) {
return (0, library_1.SignedNumberToBigNumber)(events[0].makerPnl);
}
else if (address == events[0].taker) {
return (0, library_1.SignedNumberToBigNumber)(events[0].takerPnl);
}
else {
throw `TradeExecuted event not found for address: ${address}`;
}
}
static getAccountBankBalance(tx, address) {
const events = Transaction.getEvents(tx, "BankBalanceUpdate");
if (!address.startsWith("0x")) {
address = "0x" + address;
}
if (events.length == 0) {
return (0, bignumber_js_1.default)(0);
}
// assuming the first event will have latest bank balance for account
for (const ev of events) {
if (ev.fields.destAddress == address) {
return (0, bignumber_js_1.default)(ev.fields.destBalance);
}
else if (ev.fields.srcAddress == address) {
return (0, bignumber_js_1.default)(ev.fields.srcBalance);
}
}
return (0, bignumber_js_1.default)(0);
}
// assumes if there was any object created, it was bank account
static getBankAccountID(tx) {
// if an object is created its bank account
const createdObjects = this.getCreatedObjectIDs(tx);
const mutatedObjects = this.getMutatedObjectsUsingType(tx, "BankAccount");
if (createdObjects.length > 0) {
return createdObjects[0];
}
else if (mutatedObjects.length > 0) {
return mutatedObjects[0];
}
else {
return "";
}
}
static getTxGasCost(tx) {
tx = tx;
return (Number(tx.effects.gasUsed.computationCost) +
Number(tx.effects.gasUsed.nonRefundableStorageFee) +
Number(tx.effects.gasUsed.storageCost) -
Number(tx.effects.gasUsed.storageRebate));
}
}
exports.Transaction = Transaction;
;