fsl-js-sdk
Version:
sdk for web
929 lines • 59.1 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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 (g && (g = 0, op[0] && (_ = 0)), _) 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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var ethers_1 = require("ethers");
var ethereum_erc20_token_balances_multicall_1 = require("ethereum-erc20-token-balances-multicall");
var bip39_1 = require("bip39");
var axios_1 = __importDefault(require("axios"));
var constants_1 = require("../../constants");
var utils_1 = require("ethers/lib/utils");
var aggAbi_1 = require("../../constants/aggAbi");
var feeUrl = 'https://gasstation.polygon.technology/v2';
var FSLWeb3 = /** @class */ (function () {
// private solanaKeypaire?: web3.Keypair;
function FSLWeb3(_a) {
var mnemonic = _a.mnemonic, gasPriorityPolygon = _a.gasPriorityPolygon, gasPriorityExtraPolygon = _a.gasPriorityExtraPolygon, gasPriorityExtraEvm = _a.gasPriorityExtraEvm;
this.DOMAIN_PERMISSIONS = [
'https://id.fsl.com',
'https://fslid.joysteps.io',
'http://localhost:8080',
'http://localhost:8081',
'https://9ijsflpfgm3.joysteps.io',
'https://9ijsflpfgm6.joysteps.io',
'https://9ijsflpfgm16.joysteps.io',
'https://9ijsflpfgm18.joysteps.io',
'https://account.findsatoshilab.com',
'https://gmtpay_id.fsl.com',
'https://xh.dyna1.asia',
'https://gmtpay_id_v2.fsl.com',
'https://account.fsl.com',
];
this.permission = false;
this.permission = this.DOMAIN_PERMISSIONS.includes(window.location.origin);
if (mnemonic && this.permission) {
this.generateWallet(mnemonic);
}
// gas only support EIP-1559
// polygon gas priority from chain return 0, so we need set a default gasPriority
this.gasPriorityPolygon = gasPriorityPolygon !== null && gasPriorityPolygon !== void 0 ? gasPriorityPolygon : '200';
// polygon gas priority from gas station url, add extra gas priority
this.gasPriorityExtraPolygon = gasPriorityExtraPolygon !== null && gasPriorityExtraPolygon !== void 0 ? gasPriorityExtraPolygon : '50';
// Evm gas priority from chain, add extra gas priority
this.gasPriorityExtraEvm = gasPriorityExtraEvm !== null && gasPriorityExtraEvm !== void 0 ? gasPriorityExtraEvm : '0';
}
FSLWeb3.prototype.generateWallet = function (mnemonic) {
if (!mnemonic)
throw new Error('mnemonic is required');
if (!this.permission)
throw new Error('This domain is not registered');
this.wallet = ethers_1.Wallet.fromMnemonic(mnemonic);
return this.wallet.address;
};
// create account
FSLWeb3.prototype.createAccount = function () {
var mnemonic = (0, bip39_1.generateMnemonic)();
return mnemonic;
};
FSLWeb3.isMnemonic = function (mnemonic) {
return (0, bip39_1.validateMnemonic)(mnemonic);
};
FSLWeb3.isPrivateKey = function (privateKey) {
try {
var wallet = new ethers_1.Wallet(privateKey);
return wallet.address;
}
catch (error) {
return null;
}
};
FSLWeb3.toBigInt = function (value) {
if (!value)
throw new Error('value is required');
return ethers_1.utils.parseEther(value);
};
FSLWeb3.getEnsAddress = function (ensName, rpc) {
if (rpc === void 0) { rpc = 'https://rpc.ankr.com/eth'; }
return __awaiter(this, void 0, void 0, function () {
var provider, address;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [4 /*yield*/, provider.resolveName(ensName)];
case 1:
address = _a.sent();
return [2 /*return*/, address];
}
});
});
};
FSLWeb3.lookupEnsName = function (address, rpc) {
if (rpc === void 0) { rpc = 'https://rpc.ankr.com/eth'; }
return __awaiter(this, void 0, void 0, function () {
var provider, ensName;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [4 /*yield*/, provider.lookupAddress(address)];
case 1:
ensName = _a.sent();
return [2 /*return*/, ensName];
}
});
});
};
FSLWeb3.prototype.exportWallet = function () {
if (this.wallet) {
return this.wallet;
}
throw new Error('Wallet not initialized');
};
FSLWeb3.prototype.exportPrivateKey = function () {
if (this.wallet) {
return this.wallet.privateKey;
}
return '';
};
FSLWeb3.prototype.exportMnemonic = function () {
if (this.wallet) {
return this.wallet.mnemonic;
}
return '';
};
FSLWeb3.prototype.transferEvmNativeToken = function (_a) {
var to = _a.to, amount = _a.amount, rpc = _a.rpc, chainId = _a.chainId, nonce = _a.nonce, maxPriorityFeePerGasValue = _a.maxPriorityFeePerGasValue, maxFeePerGasValue = _a.maxFeePerGasValue;
return __awaiter(this, void 0, void 0, function () {
var provider, newNonce, _b, newMaxPriorityFeePerGasValue, newMaxFeePerGasValue, _c, maxPriorityFeePerGas, maxFeePerGas, gasEstimate, tx, signedTx;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!this.wallet)
throw new Error('Wallet not initialized');
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
if (!(nonce !== null && nonce !== void 0)) return [3 /*break*/, 1];
_b = nonce;
return [3 /*break*/, 3];
case 1: return [4 /*yield*/, provider.getTransactionCount(this.wallet.address)];
case 2:
_b = (_d.sent());
_d.label = 3;
case 3:
newNonce = _b;
newMaxPriorityFeePerGasValue = maxPriorityFeePerGasValue;
newMaxFeePerGasValue = maxFeePerGasValue;
if (!(!newMaxPriorityFeePerGasValue || !newMaxFeePerGasValue)) return [3 /*break*/, 5];
return [4 /*yield*/, this.getEvmFeeFast(rpc, chainId)];
case 4:
_c = _d.sent(), maxPriorityFeePerGas = _c.maxPriorityFeePerGas, maxFeePerGas = _c.maxFeePerGas;
newMaxPriorityFeePerGasValue = maxPriorityFeePerGas;
newMaxFeePerGasValue = maxFeePerGas;
_d.label = 5;
case 5: return [4 /*yield*/, provider.estimateGas({
from: this.wallet.address,
to: to,
value: ethers_1.ethers.utils.parseEther(amount),
})];
case 6:
gasEstimate = _d.sent();
tx = {
type: 2,
nonce: newNonce,
to: to,
value: ethers_1.ethers.utils.parseEther(amount),
maxPriorityFeePerGas: newMaxPriorityFeePerGasValue,
maxFeePerGas: newMaxFeePerGasValue,
gasLimit: gasEstimate.mul(12).div(10).toString(),
chainId: chainId,
};
return [4 /*yield*/, this.wallet.signTransaction(tx)];
case 7:
signedTx = _d.sent();
return [2 /*return*/, signedTx];
}
});
});
};
FSLWeb3.prototype.callEvmContractReadMethod = function (rpc, address, contractAddresses, abi, method) {
return __awaiter(this, void 0, void 0, function () {
var provider, tokenContract;
return __generator(this, function (_a) {
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
tokenContract = new ethers_1.ethers.Contract(contractAddresses, abi, provider);
return [2 /*return*/, tokenContract[method](address)];
});
});
};
FSLWeb3.prototype.callEvmContractMethod = function (_a) {
var contractAddress = _a.contractAddress, abi = _a.abi, methodName = _a.methodName, params = _a.params, rpc = _a.rpc, chainId = _a.chainId, gasLimit = _a.gasLimit, amount = _a.amount, nonce = _a.nonce, maxPriorityFeePerGasValue = _a.maxPriorityFeePerGasValue, maxFeePerGasValue = _a.maxFeePerGasValue;
return __awaiter(this, void 0, void 0, function () {
var provider, newNonce, _b, newMaxPriorityFeePerGasValue, newMaxFeePerGasValue, _c, maxPriorityFeePerGas, maxFeePerGas, walletSigner, tokenContract, transferData, tx, signedTx;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!this.wallet)
throw new Error('Wallet not initialized');
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
if (!(nonce !== null && nonce !== void 0)) return [3 /*break*/, 1];
_b = nonce;
return [3 /*break*/, 3];
case 1: return [4 /*yield*/, provider.getTransactionCount(this.wallet.address)];
case 2:
_b = (_d.sent());
_d.label = 3;
case 3:
newNonce = _b;
newMaxPriorityFeePerGasValue = maxPriorityFeePerGasValue;
newMaxFeePerGasValue = maxFeePerGasValue;
if (!(!newMaxPriorityFeePerGasValue || !newMaxFeePerGasValue)) return [3 /*break*/, 5];
return [4 /*yield*/, this.getEvmFeeFast(rpc, chainId)];
case 4:
_c = _d.sent(), maxPriorityFeePerGas = _c.maxPriorityFeePerGas, maxFeePerGas = _c.maxFeePerGas;
newMaxPriorityFeePerGasValue = maxPriorityFeePerGas;
newMaxFeePerGasValue = maxFeePerGas;
_d.label = 5;
case 5:
walletSigner = this.wallet.connect(provider);
tokenContract = new ethers_1.ethers.Contract(contractAddress, abi, walletSigner);
transferData = tokenContract.interface.encodeFunctionData(methodName, params);
tx = {
type: 2,
nonce: newNonce,
to: contractAddress,
value: amount ? ethers_1.ethers.utils.parseEther(amount) : 0,
gasLimit: gasLimit,
maxPriorityFeePerGas: newMaxPriorityFeePerGasValue,
maxFeePerGas: newMaxFeePerGasValue,
chainId: chainId,
data: transferData,
};
return [4 /*yield*/, this.wallet.signTransaction(tx)];
case 6:
signedTx = _d.sent();
return [2 /*return*/, signedTx];
}
});
});
};
FSLWeb3.prototype.callEvmContractByCallData = function (_a) {
var contractAddress = _a.contractAddress, callData = _a.callData, rpc = _a.rpc, chainId = _a.chainId, gasLimit = _a.gasLimit, maxPriorityFeePerGasValue = _a.maxPriorityFeePerGasValue, maxFeePerGasValue = _a.maxFeePerGasValue, value = _a.value, nonce = _a.nonce;
return __awaiter(this, void 0, void 0, function () {
var provider, walletSigner, newNonce, _b, newMaxPriorityFeePerGasValue, newMaxFeePerGasValue, _c, maxPriorityFeePerGas, maxFeePerGas, tx, signedTx;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!this.wallet)
throw new Error('Wallet not initialized');
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
walletSigner = this.wallet.connect(provider);
if (!(nonce !== null && nonce !== void 0)) return [3 /*break*/, 1];
_b = nonce;
return [3 /*break*/, 3];
case 1: return [4 /*yield*/, provider.getTransactionCount(this.wallet.address)];
case 2:
_b = (_d.sent());
_d.label = 3;
case 3:
newNonce = _b;
newMaxPriorityFeePerGasValue = maxPriorityFeePerGasValue;
newMaxFeePerGasValue = maxFeePerGasValue;
if (!(!newMaxPriorityFeePerGasValue || !newMaxFeePerGasValue)) return [3 /*break*/, 5];
return [4 /*yield*/, this.getEvmFeeFast(rpc, chainId)];
case 4:
_c = _d.sent(), maxPriorityFeePerGas = _c.maxPriorityFeePerGas, maxFeePerGas = _c.maxFeePerGas;
newMaxPriorityFeePerGasValue = maxPriorityFeePerGas;
newMaxFeePerGasValue = maxFeePerGas;
_d.label = 5;
case 5:
tx = {
type: 2,
nonce: newNonce,
gasLimit: gasLimit,
to: contractAddress,
data: callData,
value: value ? ethers_1.ethers.utils.parseEther(value) : 0,
chainId: chainId,
maxPriorityFeePerGas: newMaxPriorityFeePerGasValue,
maxFeePerGas: newMaxFeePerGasValue,
};
return [4 /*yield*/, walletSigner.signTransaction(tx)];
case 6:
signedTx = _d.sent();
return [2 /*return*/, signedTx];
}
});
});
};
FSLWeb3.prototype.evmSignMessage = function (msg, type) {
return __awaiter(this, void 0, void 0, function () {
var handleMsg, formMsg;
return __generator(this, function (_a) {
if (!this.wallet)
throw new Error('Wallet not initialized');
handleMsg = msg;
if (type && Object.prototype.toString.call(msg) === '[object Object]') {
formMsg = Array.from(__assign(__assign({}, msg), { length: Object.keys(msg).length }));
switch (type) {
case 'Unit8Array':
handleMsg = new Uint8Array(formMsg);
break;
case 'Unit16Array':
handleMsg = new Uint16Array(formMsg);
break;
default:
handleMsg = new Uint32Array(formMsg);
break;
}
}
return [2 /*return*/, this.wallet.signMessage(handleMsg)];
});
});
};
FSLWeb3.prototype.signDigest = function (hexInput) {
if (!this.wallet)
throw new Error('Wallet not initialized');
var byteArray = ethers_1.ethers.utils.arrayify(hexInput);
return this.wallet._signingKey().signDigest(byteArray);
};
FSLWeb3.evmVerifyMessage = function (msg, signature) {
return (0, utils_1.verifyMessage)(msg, signature);
};
FSLWeb3.prototype.evmSignTypedData = function (domain, types, message) {
return __awaiter(this, void 0, void 0, function () {
var signature;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.wallet)
throw new Error('Wallet not initialized');
return [4 /*yield*/, this.wallet._signTypedData(domain, types, message)];
case 1:
signature = _a.sent();
return [2 /*return*/, signature];
}
});
});
};
FSLWeb3.evmVerifyTypedData = function (domain, types, message, signature) {
return (0, utils_1.verifyTypedData)(domain, types, message, signature);
};
FSLWeb3.prototype.getTransactionNonceCount = function (rpc) {
return __awaiter(this, void 0, void 0, function () {
var provider, nonce;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.wallet)
throw new Error('Wallet not initialized');
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [4 /*yield*/, provider.getTransactionCount(this.wallet.address)];
case 1:
nonce = _a.sent();
return [2 /*return*/, nonce];
}
});
});
};
FSLWeb3.prototype.callEvmSignMethod = function (_a) {
var contractAddress = _a.contractAddress, data = _a.data, rpc = _a.rpc, chainId = _a.chainId, gasLimit = _a.gasLimit, amount = _a.amount, nonce = _a.nonce, maxPriorityFeePerGasValue = _a.maxPriorityFeePerGasValue, maxFeePerGasValue = _a.maxFeePerGasValue;
return __awaiter(this, void 0, void 0, function () {
var provider, newNonce, _b, newMaxPriorityFeePerGasValue, newMaxFeePerGasValue, _c, maxPriorityFeePerGas, maxFeePerGas, tx, signedTx;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
if (!this.wallet)
throw new Error('Wallet not initialized');
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
if (!(nonce !== null && nonce !== void 0)) return [3 /*break*/, 1];
_b = nonce;
return [3 /*break*/, 3];
case 1: return [4 /*yield*/, provider.getTransactionCount(this.wallet.address)];
case 2:
_b = (_d.sent());
_d.label = 3;
case 3:
newNonce = _b;
newMaxPriorityFeePerGasValue = maxPriorityFeePerGasValue;
newMaxFeePerGasValue = maxFeePerGasValue;
if (!(!newMaxPriorityFeePerGasValue || !newMaxFeePerGasValue)) return [3 /*break*/, 5];
return [4 /*yield*/, this.getEvmFeeFast(rpc, chainId)];
case 4:
_c = _d.sent(), maxPriorityFeePerGas = _c.maxPriorityFeePerGas, maxFeePerGas = _c.maxFeePerGas;
newMaxPriorityFeePerGasValue = maxPriorityFeePerGas;
newMaxFeePerGasValue = maxFeePerGas;
_d.label = 5;
case 5:
tx = {
type: 2,
nonce: newNonce,
to: contractAddress,
value: amount ? ethers_1.ethers.utils.parseEther(amount) : 0,
maxPriorityFeePerGas: newMaxPriorityFeePerGasValue,
maxFeePerGas: newMaxFeePerGasValue,
gasLimit: gasLimit,
chainId: chainId,
data: data,
};
return [4 /*yield*/, this.wallet.signTransaction(tx)];
case 6:
signedTx = _d.sent();
return [2 /*return*/, signedTx];
}
});
});
};
FSLWeb3.prototype.sendEvmTransaction = function (signedTx, rpc, confirmed) {
if (confirmed === void 0) { confirmed = true; }
return __awaiter(this, void 0, void 0, function () {
var txHash, provider, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
txHash = ethers_1.ethers.utils.keccak256(signedTx);
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [4 /*yield*/, provider.sendTransaction(signedTx)];
case 1:
_b.sent();
_a = confirmed;
if (!_a) return [3 /*break*/, 3];
return [4 /*yield*/, provider.waitForTransaction(txHash)];
case 2:
_a = (_b.sent());
_b.label = 3;
case 3:
_a;
return [2 /*return*/, txHash];
}
});
});
};
FSLWeb3.prototype.checkEvmTransactionHashStatus = function (txHash, rpc) {
return __awaiter(this, void 0, void 0, function () {
var provider;
return __generator(this, function (_a) {
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [2 /*return*/, provider.getTransactionReceipt(txHash)];
});
});
};
FSLWeb3.prototype.getEvmTransactionBlock = function (blockNum, rpc) {
return __awaiter(this, void 0, void 0, function () {
var provider;
return __generator(this, function (_a) {
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [2 /*return*/, provider.getBlock(blockNum)];
});
});
};
FSLWeb3.prototype.tokenMintAddress = function (tokenSymbol) {
switch (tokenSymbol) {
case constants_1.TOKEN_NAME.GST:
return constants_1.SOLTokenMintAddress[constants_1.TOKEN_NAME.GST];
case constants_1.TOKEN_NAME.GMT:
return constants_1.SOLTokenMintAddress[constants_1.TOKEN_NAME.GMT];
case constants_1.TOKEN_NAME.USDC:
return constants_1.SOLTokenMintAddress[constants_1.TOKEN_NAME.USDC];
default:
return '';
}
};
FSLWeb3.prototype.getEvmErc20TokenBalance = function (walletAddress, rpc, contractAddresses) {
return __awaiter(this, void 0, void 0, function () {
var provider;
return __generator(this, function (_a) {
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [2 /*return*/, (0, ethereum_erc20_token_balances_multicall_1.getBalancesForEthereumAddress)({
contractAddresses: contractAddresses,
ethereumAddress: walletAddress,
providerOptions: {
ethersProvider: provider,
},
})];
});
});
};
FSLWeb3.prototype.getEvmNativeTokenBalance = function (walletAddress, rpc) {
return __awaiter(this, void 0, void 0, function () {
var provider;
return __generator(this, function (_a) {
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [2 /*return*/, provider.getBalance(walletAddress)];
});
});
};
FSLWeb3.prototype.calculateEvmNativeTokenTransferGasFee = function (_a) {
var rpc = _a.rpc, chainId = _a.chainId, from = _a.from;
return __awaiter(this, void 0, void 0, function () {
var provider, maxFeePerGas, price, tx, gasEstimate;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [4 /*yield*/, this.getEvmFeeFast(rpc, chainId)];
case 1:
maxFeePerGas = (_b.sent()).maxFeePerGas;
price = maxFeePerGas;
if (!!price) return [3 /*break*/, 3];
return [4 /*yield*/, provider.getGasPrice()];
case 2:
price = (_b.sent()).mul(ethers_1.ethers.BigNumber.from('1.5'));
_b.label = 3;
case 3:
tx = {
from: from,
to: from,
value: ethers_1.ethers.utils.parseEther('0'),
};
return [4 /*yield*/, provider.estimateGas(tx)];
case 4:
gasEstimate = _b.sent();
return [2 /*return*/, ethers_1.ethers.utils.formatEther(gasEstimate.mul(price))];
}
});
});
};
FSLWeb3.prototype.calculateCallEvmContractMethodGasFee = function (_a) {
var rpc = _a.rpc, chainId = _a.chainId, contractAddress = _a.contractAddress, abi = _a.abi, methodName = _a.methodName, params = _a.params, from = _a.from, amount = _a.amount;
return __awaiter(this, void 0, void 0, function () {
var provider, tokenContract, maxFeePerGas, price, tx, gasEstimate;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
tokenContract = new ethers_1.ethers.Contract(contractAddress, abi, provider);
return [4 /*yield*/, this.getEvmFeeFast(rpc, chainId)];
case 1:
maxFeePerGas = (_b.sent()).maxFeePerGas;
price = maxFeePerGas;
if (!!price) return [3 /*break*/, 3];
return [4 /*yield*/, provider.getGasPrice()];
case 2:
price = (_b.sent()).mul(ethers_1.ethers.BigNumber.from('1.5'));
_b.label = 3;
case 3:
tx = {
from: from,
to: contractAddress,
value: amount !== null && amount !== void 0 ? amount : ethers_1.ethers.utils.parseEther('0'),
data: tokenContract.interface.encodeFunctionData(methodName, params),
};
return [4 /*yield*/, provider.estimateGas(tx)];
case 4:
gasEstimate = _b.sent();
return [2 /*return*/, ethers_1.ethers.utils.formatEther(gasEstimate.mul(price))];
}
});
});
};
FSLWeb3.prototype.getEvmNativeTokenTransactions = function (rpc, chainId, from, blockNum) {
return __awaiter(this, void 0, void 0, function () {
var provider, latestBlock, startBlock, scanUrl, transactions;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [4 /*yield*/, provider.getBlockNumber()];
case 1:
latestBlock = _a.sent();
startBlock = latestBlock - blockNum;
if (chainId === constants_1.ChainId.Ethereum) {
scanUrl = "https://api.etherscan.io/api?module=account&action=txlist&address=".concat(from, "&startblock=").concat(startBlock, "&endblock=").concat(latestBlock, "&page=$page&offset=$pageSize&sort=desc&apikey=ADBBZG1D3Z3MM7SYDTE5A4V312XEWITEGJ");
}
else if (chainId === constants_1.ChainId.BNBChain) {
scanUrl = "https://api.bscscan.com/api?module=account&action=txlist&address=".concat(from, "&startblock=").concat(startBlock, "&endblock=").concat(latestBlock, "&page=1&offset=10&sort=asc&apikey=PJ531IB5XTNREDGMFB29ISM1MEXH9QISPZ");
}
else if (chainId === constants_1.ChainId.POLYGON) {
scanUrl = "https://api.polygonscan.com/api?module=account&action=txlist&address=".concat(from, "&startblock=").concat(startBlock, "&endblock=").concat(latestBlock, "&page=1&offset=10&sort=asc&apikey=CGJM2Q4CQJE122MGIS2E9N8IQWKMERWWWK");
}
else {
scanUrl = "https://api-amoy.polygonscan.com/api?module=account&action=txlist&address=".concat(from, "&startblock=").concat(startBlock, "&endblock=").concat(latestBlock, "&page=1&offset=10&sort=asc&apikey=YourApiKeyToken");
}
return [4 /*yield*/, axios_1.default.get(scanUrl)];
case 2:
transactions = _a.sent();
return [2 /*return*/, transactions];
}
});
});
};
FSLWeb3.prototype.getEvmErcTokenTransactions = function (rpc, contractAddress, abi, from, blockNum) {
return __awaiter(this, void 0, void 0, function () {
var provider, tokenContract, latestBlock, fromFilter, toFilter, startBlock, fromTransfers, toTransfers;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
tokenContract = new ethers_1.ethers.Contract(contractAddress, abi, provider);
return [4 /*yield*/, provider.getBlockNumber()];
case 1:
latestBlock = _a.sent();
fromFilter = tokenContract.filters.Transfer(from, null);
toFilter = tokenContract.filters.Transfer(null, from);
startBlock = latestBlock - blockNum;
return [4 /*yield*/, this.queryTransfers(tokenContract, fromFilter, startBlock, latestBlock)];
case 2:
fromTransfers = _a.sent();
return [4 /*yield*/, this.queryTransfers(tokenContract, toFilter, startBlock, latestBlock)];
case 3:
toTransfers = _a.sent();
return [2 /*return*/, __spreadArray(__spreadArray([], fromTransfers, true), toTransfers, true)];
}
});
});
};
FSLWeb3.prototype.queryTransfers = function (contract, filter, fromBlock, toBlock) {
return __awaiter(this, void 0, void 0, function () {
var blockStep, events, getEventsFns, errorHandles, _loop_1, startBlock, newEventsList, errorEventList, finalEventList, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
blockStep = 1024;
events = [];
getEventsFns = [];
errorHandles = [];
_loop_1 = function (startBlock) {
var endBlock = Math.min(startBlock + blockStep, toBlock);
var getEvent = function () {
return contract.queryFilter(filter, startBlock, endBlock).catch(function () {
errorHandles.push(getEvent);
return [];
});
};
getEventsFns.push(getEvent);
};
for (startBlock = fromBlock; startBlock < toBlock; startBlock += blockStep) {
_loop_1(startBlock);
}
return [4 /*yield*/, Promise.all(getEventsFns.map(function (fn) { return fn(); }))];
case 1:
newEventsList = _a.sent();
errorEventList = [];
if (!(errorHandles.length > 0)) return [3 /*break*/, 3];
return [4 /*yield*/, Promise.all(errorHandles.map(function (fn) { return fn(); }))];
case 2:
errorEventList = _a.sent();
_a.label = 3;
case 3:
finalEventList = __spreadArray(__spreadArray([], newEventsList, true), errorEventList, true);
for (i = 0; i < finalEventList.length; i++) {
events = events.concat(finalEventList[i]);
}
return [2 /*return*/, events];
}
});
});
};
FSLWeb3.prototype.getEvmSwapRouterAmountsOut = function (rpc, contractAddress, path, amountIn) {
return __awaiter(this, void 0, void 0, function () {
var provider, swapRouterContract;
return __generator(this, function (_a) {
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
swapRouterContract = new ethers_1.ethers.Contract(contractAddress, constants_1.EvmSwapRouterAbi, provider);
return [2 /*return*/, swapRouterContract.getAmountsOut(amountIn, path)];
});
});
};
FSLWeb3.prototype.getEvmErc20TokenAllowance = function (rpc, contractAddress, from, to) {
return __awaiter(this, void 0, void 0, function () {
var provider, tokenContract;
return __generator(this, function (_a) {
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
tokenContract = new ethers_1.ethers.Contract(contractAddress, constants_1.EvmErc20TokenAbi, provider);
return [2 /*return*/, tokenContract.allowance(from, to)];
});
});
};
FSLWeb3.prototype.getEvmErc20TokenBalanceOf = function (walletAddress, rpc, contractAddresses) {
return __awaiter(this, void 0, void 0, function () {
var provider, tokenContract;
return __generator(this, function (_a) {
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
tokenContract = new ethers_1.ethers.Contract(contractAddresses, constants_1.EvmErc20TokenAbi, provider);
return [2 /*return*/, tokenContract.balanceOf(walletAddress)];
});
});
};
FSLWeb3.prototype.getEvmBlockTime = function (rpc, blockNum) {
return __awaiter(this, void 0, void 0, function () {
var provider, block;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
return [4 /*yield*/, provider.getBlock(blockNum)];
case 1:
block = _a.sent();
return [2 /*return*/, block.timestamp];
}
});
});
};
FSLWeb3.prototype.getEvmFeeFast = function (rpc, chainId) {
return __awaiter(this, void 0, void 0, function () {
var provider, gasPriorityDefault, gasPriorityExtraDefault, fast, result, maxFeePerGas, maxPriorityFeePerGas, maxPriorityFee, maxFee, gasPriorityExtra, result, maxFeePerGas, maxPriorityFeePerGas;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
if (![constants_1.ChainId.POLYGON, constants_1.ChainId.POLYGON_AMOY].includes(chainId)) return [3 /*break*/, 4];
gasPriorityDefault = ethers_1.ethers.utils.parseUnits(this.gasPriorityPolygon, 'gwei');
gasPriorityExtraDefault = ethers_1.ethers.utils.parseUnits(this.gasPriorityExtraPolygon, 'gwei');
return [4 /*yield*/, axios_1.default
.get(feeUrl)
.then(function (_a) {
var data = _a.data;
return data;
})
.catch(function () {
return {};
})];
case 1:
fast = (_a.sent()).fast;
if (!(!fast || !Object.keys(fast).length)) return [3 /*break*/, 3];
return [4 /*yield*/, provider.getFeeData().catch(function () {
return {};
})];
case 2:
result = _a.sent();
if ('maxFeePerGas' in result && 'maxPriorityFeePerGas' in result) {
maxFeePerGas = result.maxFeePerGas, maxPriorityFeePerGas = result.maxPriorityFeePerGas;
if (!maxFeePerGas || !maxPriorityFeePerGas) {
return [2 /*return*/, {}];
}
return [2 /*return*/, {
maxPriorityFeePerGas: maxPriorityFeePerGas.add(gasPriorityDefault),
maxFeePerGas: maxFeePerGas.add(gasPriorityDefault),
}];
}
else {
return [2 /*return*/, {}];
}
_a.label = 3;
case 3:
maxPriorityFee = fast.maxPriorityFee, maxFee = fast.maxFee;
return [2 /*return*/, {
maxPriorityFeePerGas: ethers_1.ethers.utils
.parseUnits(maxPriorityFee + '', 'gwei')
.add(gasPriorityExtraDefault),
maxFeePerGas: ethers_1.ethers.utils
.parseUnits(maxFee + '', 'gwei')
.add(gasPriorityExtraDefault),
}];
case 4:
gasPriorityExtra = ethers_1.ethers.utils.parseUnits(this.gasPriorityExtraEvm, 'gwei');
return [4 /*yield*/, provider.getFeeData().catch(function () {
return {};
})];
case 5:
result = _a.sent();
if ('maxFeePerGas' in result && 'maxPriorityFeePerGas' in result) {
maxFeePerGas = result.maxFeePerGas, maxPriorityFeePerGas = result.maxPriorityFeePerGas;
if (!maxFeePerGas || !maxPriorityFeePerGas) {
return [2 /*return*/, {}];
}
return [2 /*return*/, {
maxPriorityFeePerGas: maxPriorityFeePerGas.add(gasPriorityExtra),
maxFeePerGas: maxFeePerGas.add(gasPriorityExtra),
}];
}
else {
return [2 /*return*/, {}];
}
_a.label = 6;
case 6: return [2 /*return*/];
}
});
});
};
FSLWeb3.checkContractType = function (address, rpc) {
return __awaiter(this, void 0, void 0, function () {
var provider, contract, ERC721_INTERFACE_ID, isERC721;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
contract = new ethers_1.ethers.Contract(address, [
'function supportsInterface(bytes4 interfaceID) external view returns (bool)',
], provider);
ERC721_INTERFACE_ID = '0x80ac58cd';
return [4 /*yield*/, contract
.supportsInterface(ERC721_INTERFACE_ID)
.catch(function () { return false; })];
case 1:
isERC721 = _a.sent();
if (isERC721) {
return [2 /*return*/, 'NFT'];
}
return [2 /*return*/, ''];
}
});
});
};
FSLWeb3.getTokenTicker = function (contractAddresses, rpc) {
return __awaiter(this, void 0, void 0, function () {
var provider, tokenContract, tokenTicker, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
provider = new ethers_1.ethers.providers.JsonRpcProvider(rpc);
tokenContract = new ethers_1.ethers.Contract(contractAddresses, [
{
inputs: [],
name: 'symbol',
outputs: [{ internalType: 'string', name: '', type: 'string' }],
stateMutability: 'view',
type: 'function',
},
], provider);
tokenTicker = '';
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, tokenContract.symbol()];
case 2:
tokenTicker = _a.sent();
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
return [3 /*break*/, 4];
case 4: return [2 /*return*/, tokenTicker];
}
});
});
};
FSLWeb3.prototype.getOutputSwapQuote = function (params) {
return __awaiter(this, void 0, void 0, function () {
var rpc, amountOut, tokenIn, tokenOut, chains, tokenInDecimals, step, net, provider, tokenOutAmount, GUSDAggretagor, quote;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
rpc = params.rpc, amountOut = params.amountOut, tokenIn = params.tokenIn, tokenOut = params.tokenOut, chains = params.chains, tokenInDecimals = params.tokenInDecimals, step = params.step;
net = rpc || constants_1.CHAIN_LIST[chains].rpc;
provider = new ethers_1.ethers.providers.JsonRpcProvider(net);
tokenOutAmount = ethers_1.ethers.utils.parseUnits(amountOut, tokenInDecimals);
GUSDAggretagor = new ethers_1.ethers.Contract(constants_1.AGGAddress, aggAbi_1.aggABI, provider);
return [4 /*yield*/, GUSDAggretagor.callStatic.quoteExactOutput(tokenOutAmount, tokenIn, tokenOut, step !== null && step !== void 0 ? step : 4)];
case 1:
quote = _a.sent();
return [2 /*return*/, quote];
}
});
});
};
FSLWeb3.prototype.getSwapQuote = function (params) {
return __awaiter(this, void 0, void 0, function () {
var rpc, amount, tokenIn, tokenOut, chains, tokenInDecimals, step, net, provider, tokenInAmount, GUSDAggretagor, quote;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
rpc = params.rpc, amount = params.amount, tokenIn = params.tokenIn, tokenOut = params.tokenOut, chains = params.chains, tokenInDecimals = params.tokenInDecimals, step = params.step;
net = rpc || constants_1.CHAIN_LIST[chains].rpc;
provider = new ethers_1.ethers.providers.JsonRpcProvider(net);
tokenInAmount = ethers_1.ethers.utils.parseUnits(amount, tokenInDecimals);
GUSDAggretagor = new ethers_1.ethers.Contract(constants_1.AGGAddress, aggAbi_1.aggABI, provider);
return [4 /*yield*/, GUSDAggretagor.callStatic.quoteExactInput(tokenInAmount, tokenIn, tokenOut, step !== null && step !== void 0 ? step : 4)];
case 1:
quote = _a.sent();
return [2 /*return*/, quote];
}
});
});
};
FSLWeb3.prototype.getSwapGas = function (quote, chains, fnName, bscGas) {
return __awaiter(this, void 0, void 0, function () {
var net, provider, wallet, gasPriceValue, gasPrice, GUSDAggretagor, gasEstimate;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.wallet)
throw new Error('Wallet not initialized');
net = constants_1.CHAIN_LIST[chains].rpc;
provi