@volare.finance/utils.js
Version:
The Blockchain Utils
209 lines • 8.56 kB
JavaScript
"use strict";
/**
* @file index.ts
* @author astra <astra@volare.finance>
* @date 2022
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Provider = void 0;
const tslib_1 = require("tslib");
const ethers_1 = require("ethers");
const type_1 = require("../type");
class Provider {
static Init(endpoint) {
Provider.endpoint = endpoint;
Provider.provider = new ethers_1.providers.JsonRpcProvider(Provider.endpoint);
Provider.inited = true;
}
static IsInited() {
return Provider.inited;
}
static ChainId() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const network = yield Provider.provider.getNetwork();
return network.chainId;
});
}
static Call(tx, blockTag) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return Provider.provider.call(tx, blockTag);
});
}
static GetTransactionCount(addressOrName, blockTag) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return Provider.provider.getTransactionCount(addressOrName, blockTag);
});
}
static GetTransaction(txHash) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return Provider.provider.getTransaction(txHash);
});
}
static GetTransactionReceipt(txHash) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return Provider.provider.getTransactionReceipt(txHash);
});
}
static GetTransactionFailedReason(tx) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const response = yield Provider.Call({
from: tx.from,
to: tx.to,
value: tx.value,
data: tx.data,
nonce: tx.nonce,
type: (_a = tx.type) !== null && _a !== void 0 ? _a : undefined,
accessList: tx === null || tx === void 0 ? void 0 : tx.accessList,
}, tx.blockNumber);
return response === '0x' ? '' : ethers_1.utils.toUtf8String('0x' + response.substring(138).replace(/(0+)$/g, ''));
});
}
static EstimateGas(tx) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return new type_1.BigNumber((yield Provider.provider.estimateGas(tx)).toString());
});
}
static GetGasPrice() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return new type_1.BigNumber((yield Provider.provider.getGasPrice()).toString());
});
}
static SendTransaction(owner, tx) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return owner.connect(Provider.provider).sendTransaction(tx);
});
}
static SpeedUpTransaction(owner, tx, gasPrice, gasLimit) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return owner.connect(Provider.provider).sendTransaction({
to: tx.to,
value: tx.value,
data: tx.data,
nonce: tx.nonce,
type: (_a = tx.type) !== null && _a !== void 0 ? _a : undefined,
accessList: tx === null || tx === void 0 ? void 0 : tx.accessList,
gasPrice: gasPrice === null || gasPrice === void 0 ? void 0 : gasPrice.toString(10),
gasLimit: gasLimit === null || gasLimit === void 0 ? void 0 : gasLimit.toString(10),
});
});
}
static CancelTransaction(owner, tx, gasPrice, gasLimit) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return owner.connect(Provider.provider).sendTransaction({
to: tx.to,
value: tx.value,
data: tx.data,
nonce: tx.nonce,
type: (_a = tx.type) !== null && _a !== void 0 ? _a : undefined,
accessList: tx === null || tx === void 0 ? void 0 : tx.accessList,
gasPrice: gasPrice === null || gasPrice === void 0 ? void 0 : gasPrice.toString(10),
gasLimit: gasLimit === null || gasLimit === void 0 ? void 0 : gasLimit.toString(10),
});
});
}
constructor(endpoint) {
this._endpoint = endpoint;
this.provider = new ethers_1.providers.JsonRpcProvider(endpoint);
}
get endpoint() {
return this._endpoint;
}
get address() {
var _a;
return ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.address) || '';
}
chainId() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const network = yield this.provider.getNetwork();
return network.chainId;
});
}
call(tx, blockTag) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this.provider.call(tx, blockTag);
});
}
getTransactionCount(addressOrName, blockTag) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this.provider.getTransactionCount(addressOrName, blockTag);
});
}
getTransaction(txHash) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this.provider.getTransaction(txHash);
});
}
getTransactionReceipt(txHash) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this.provider.getTransactionReceipt(txHash);
});
}
getTransactionFailedReason(tx) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const response = yield this.call({
from: tx.from,
to: tx.to,
value: tx.value,
data: tx.data,
nonce: tx.nonce,
type: (_a = tx.type) !== null && _a !== void 0 ? _a : undefined,
accessList: tx === null || tx === void 0 ? void 0 : tx.accessList,
}, tx.blockNumber);
return response === '0x' ? '' : ethers_1.utils.toUtf8String('0x' + response.substring(138).replace(/(0+)$/g, ''));
});
}
estimateGas(tx) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return new type_1.BigNumber((yield this.provider.estimateGas(tx)).toString());
});
}
getGasPrice() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return new type_1.BigNumber((yield this.provider.getGasPrice()).toString());
});
}
sendTransaction(owner, tx) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return owner.connect(this.provider).sendTransaction(tx);
});
}
speedUpTransaction(owner, tx, gasPrice, gasLimit) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return owner.connect(this.provider).sendTransaction({
to: tx.to,
value: tx.value,
data: tx.data,
nonce: tx.nonce,
type: (_a = tx.type) !== null && _a !== void 0 ? _a : undefined,
accessList: tx === null || tx === void 0 ? void 0 : tx.accessList,
gasPrice: gasPrice === null || gasPrice === void 0 ? void 0 : gasPrice.toString(10),
gasLimit: gasLimit === null || gasLimit === void 0 ? void 0 : gasLimit.toString(10),
});
});
}
cancelTransaction(owner, tx, gasPrice, gasLimit) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return owner.connect(this.provider).sendTransaction({
to: tx.to,
value: tx.value,
data: tx.data,
nonce: tx.nonce,
type: (_a = tx.type) !== null && _a !== void 0 ? _a : undefined,
accessList: tx === null || tx === void 0 ? void 0 : tx.accessList,
gasPrice: gasPrice === null || gasPrice === void 0 ? void 0 : gasPrice.toString(10),
gasLimit: gasLimit === null || gasLimit === void 0 ? void 0 : gasLimit.toString(10),
});
});
}
}
exports.Provider = Provider;
Provider.inited = false;
Provider.endpoint = 'http://127.0.0.1:8545';
Provider.provider = new ethers_1.providers.JsonRpcProvider(Provider.endpoint);
//# sourceMappingURL=index.js.map