hector-rubic-sdk
Version:
Simplify dApp creation
229 lines • 12.6 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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 __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SymbiosisCrossChainTrade = void 0;
var cross_chain_trade_1 = require("../common/cross-chain-trade");
var core_1 = require("../../../../core");
var injector_1 = require("../../../../core/sdk/injector");
var contract_address_1 = require("./constants/contract-address");
var contract_abi_1 = require("./constants/contract-abi");
var common_1 = require("../../../../common");
var empty_address_1 = require("../../../../core/blockchain/constants/empty-address");
var bignumber_js_1 = __importDefault(require("bignumber.js"));
var SymbiosisCrossChainTrade = /** @class */ (function (_super) {
__extends(SymbiosisCrossChainTrade, _super);
function SymbiosisCrossChainTrade(crossChainTrade, providerAddress) {
var _this = _super.call(this, providerAddress) || this;
_this.from = crossChainTrade.from;
_this.to = crossChainTrade.to;
_this.transactionRequest = crossChainTrade.transactionRequest;
_this.gasData = crossChainTrade.gasData;
_this.priceImpact = crossChainTrade.priceImpact;
_this.fromWeb3Public = injector_1.Injector.web3PublicService.getWeb3Public(_this.from.blockchain);
return _this;
}
SymbiosisCrossChainTrade.getGasData = function (from, to, transactionRequest) {
return __awaiter(this, void 0, void 0, function () {
var fromBlockchain, walletAddress, _a, contractAddress, contractAbi, methodName, methodArguments, value, web3Public, _b, gasLimit, gasPrice, _c, _d, _e, _f, increasedGasLimit, _err_1;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
fromBlockchain = from.blockchain;
walletAddress = injector_1.Injector.web3Private.address;
if (!walletAddress) {
return [2 /*return*/, null];
}
_g.label = 1;
case 1:
_g.trys.push([1, 5, , 6]);
return [4 /*yield*/, new SymbiosisCrossChainTrade({
from: from,
to: to,
transactionRequest: transactionRequest,
gasData: null,
priceImpact: 0
}, empty_address_1.EMPTY_ADDRESS).getContractParams()];
case 2:
_a = _g.sent(), contractAddress = _a.contractAddress, contractAbi = _a.contractAbi, methodName = _a.methodName, methodArguments = _a.methodArguments, value = _a.value;
web3Public = injector_1.Injector.web3PublicService.getWeb3Public(fromBlockchain);
_d = (_c = Promise).all;
_e = [web3Public.getEstimatedGas(contractAbi, contractAddress, methodName, methodArguments, walletAddress, value)];
_f = bignumber_js_1.default.bind;
return [4 /*yield*/, injector_1.Injector.gasPriceApi.getGasPrice(from.blockchain)];
case 3: return [4 /*yield*/, _d.apply(_c, [_e.concat([
new (_f.apply(bignumber_js_1.default, [void 0, _g.sent()]))()
])])];
case 4:
_b = _g.sent(), gasLimit = _b[0], gasPrice = _b[1];
if (!(gasLimit === null || gasLimit === void 0 ? void 0 : gasLimit.isFinite())) {
return [2 /*return*/, null];
}
increasedGasLimit = core_1.Web3Pure.calculateGasMargin(gasLimit, 1.2);
return [2 /*return*/, {
gasLimit: increasedGasLimit,
gasPrice: gasPrice
}];
case 5:
_err_1 = _g.sent();
return [2 /*return*/, null];
case 6: return [2 /*return*/];
}
});
});
};
Object.defineProperty(SymbiosisCrossChainTrade.prototype, "fromBlockchain", {
get: function () {
return this.from.blockchain;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SymbiosisCrossChainTrade.prototype, "fromContractAddress", {
get: function () {
return contract_address_1.SYMBIOSIS_CONTRACT_ADDRESS[this.fromBlockchain];
},
enumerable: false,
configurable: true
});
SymbiosisCrossChainTrade.prototype.checkTradeErrors = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.checkWalletConnected();
this.checkBlockchainCorrect();
return [4 /*yield*/, this.checkUserBalance()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
SymbiosisCrossChainTrade.prototype.swap = function (options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var onConfirm, gasLimit, gasPrice, _a, contractAddress, contractAbi, methodName, methodArguments, value, transactionHash, onTransactionHash, err_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.checkTradeErrors()];
case 1:
_b.sent();
return [4 /*yield*/, this.checkAllowanceAndApprove(options)];
case 2:
_b.sent();
onConfirm = options.onConfirm, gasLimit = options.gasLimit, gasPrice = options.gasPrice;
return [4 /*yield*/, this.getContractParams()];
case 3:
_a = _b.sent(), contractAddress = _a.contractAddress, contractAbi = _a.contractAbi, methodName = _a.methodName, methodArguments = _a.methodArguments, value = _a.value;
onTransactionHash = function (hash) {
if (onConfirm) {
onConfirm(hash);
}
transactionHash = hash;
};
_b.label = 4;
case 4:
_b.trys.push([4, 6, , 7]);
return [4 /*yield*/, injector_1.Injector.web3Private.tryExecuteContractMethod(contractAddress, contractAbi, methodName, methodArguments, { value: value, onTransactionHash: onTransactionHash, gas: gasLimit, gasPrice: gasPrice })];
case 5:
_b.sent();
return [2 /*return*/, transactionHash];
case 6:
err_1 = _b.sent();
if (err_1 instanceof common_1.FailedToCheckForTransactionReceiptError) {
return [2 /*return*/, transactionHash];
}
throw err_1;
case 7: return [2 /*return*/];
}
});
});
};
SymbiosisCrossChainTrade.prototype.getContractParams = function () {
return __awaiter(this, void 0, void 0, function () {
var contractAddress, contractAbi;
return __generator(this, function (_a) {
contractAddress = contract_address_1.SYMBIOSIS_CONTRACT_ADDRESS[this.fromBlockchain];
contractAbi = contract_abi_1.SYMBIOSIS_CONTRACT_ABI;
if (this.from.isNative) {
return [2 /*return*/, {
contractAddress: contractAddress,
contractAbi: contractAbi,
methodName: 'SymbiosisCallWithNative',
methodArguments: [this.providerAddress, this.transactionRequest.data],
value: this.from.stringWeiAmount
}];
}
return [2 /*return*/, {
contractAddress: contractAddress,
contractAbi: contractAbi,
methodName: 'SymbiosisCall',
methodArguments: [
this.from.address,
this.from.stringWeiAmount,
this.providerAddress,
this.transactionRequest.data
],
value: '0'
}];
});
});
};
return SymbiosisCrossChainTrade;
}(cross_chain_trade_1.CrossChainTrade));
exports.SymbiosisCrossChainTrade = SymbiosisCrossChainTrade;
//# sourceMappingURL=symbiosis-cross-chain-trade.js.map