@soundsright/chain
Version:
soundsright contracts
191 lines (190 loc) • 7.21 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorChain = exports.ProviderChain = exports.BaseChain = void 0;
const ethers_1 = require("ethers");
const provider_1 = require("@soundsright/provider");
const connector_1 = __importStar(require("@soundsright/connector"));
const types_1 = require("./types");
class BaseChain {
getBalance(address) {
return __awaiter(this, void 0, void 0, function* () {
const bn = yield this.provider.getBalance(address);
return ethers_1.utils.formatEther(bn);
});
}
tryGet(txPromise) {
return __awaiter(this, void 0, void 0, function* () {
try {
const res = yield txPromise;
return res;
}
catch (e) {
throw new types_1.TransactionError(e);
}
});
}
checkTransaction(tx) {
return __awaiter(this, void 0, void 0, function* () {
try {
const receipt = yield tx.wait();
if (receipt.status === 1) {
return true;
}
throw new Error('transaction failed');
}
catch (e) {
if (e.code === 'TRANSACTION_REPLACED') {
return true;
}
throw new types_1.TransactionError(e);
}
});
}
tryTransaction(txPromise) {
return __awaiter(this, void 0, void 0, function* () {
try {
const tx = yield txPromise;
const receipt = yield tx.wait();
if (receipt.status === 1) {
return receipt;
}
throw new Error('transaction failed');
}
catch (e) {
if (e.code === 'TRANSACTION_REPLACED') {
return e.receipt;
}
throw new types_1.TransactionError(e);
}
});
}
}
exports.BaseChain = BaseChain;
class ProviderChain extends BaseChain {
constructor(chainId) {
super();
this.chainId = chainId;
this.localProvider = null;
this.setLocalProvider(chainId);
}
get provider() {
return this.localProvider;
}
setLocalProvider(chainId) {
this.localProvider = chainId ? (0, provider_1.getLocalProvider)(chainId) : null;
}
getContract(address, abi) {
return new ethers_1.Contract(address, abi, this.provider);
}
}
exports.ProviderChain = ProviderChain;
class ConnectorChain extends BaseChain {
constructor(supportedChainId) {
super();
this.supportedChainId = supportedChainId;
this.connector = connector_1.default.getInstance();
}
get provider() {
return this.connector.state.provider;
}
get signer() {
return this.connector.state.signer;
}
get account() {
return this.connector.state.account;
}
get chainId() {
return this.connector.state.chainId;
}
get connectType() {
return this.connector.type;
}
checkConnected() {
if (!this.connector.state.connected) {
throw new connector_1.WalletNotConnectedError();
}
}
switchChain(chainId) {
return this.connector.switchChain(chainId);
}
getContract(address, abi) {
return new ethers_1.Contract(address, abi, this.signer || this.provider);
}
getUncheckedContract(address, abi) {
return new ethers_1.Contract(address, abi, this.provider.getUncheckedSigner());
}
setSupportedChainId(chainId) {
this.supportedChainId = chainId;
}
checkChain({ beforeSwitchChain, switchChainSuccess, switchChainFailed } = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (this.supportedChainId && this.chainId !== this.supportedChainId) {
const switchChainData = { oldChainId: this.chainId, targetChainId: this.supportedChainId };
if (beforeSwitchChain) {
yield beforeSwitchChain(switchChainData);
}
try {
yield this.switchChain(this.supportedChainId);
}
catch (e) {
e.oldChainId = switchChainData.oldChainId;
e.targetChainId = switchChainData.targetChainId;
if (switchChainFailed) {
switchChainFailed(e);
}
throw e;
}
if (switchChainSuccess) {
switchChainSuccess(switchChainData);
}
}
});
}
signMessage(message) {
var _a;
return (_a = this.signer) === null || _a === void 0 ? void 0 : _a.signMessage(message);
}
signTypedMessage(domain, types, value) {
// @ts-ignore
return this.signer._signTypedData(domain, types, value);
}
getBalance(address) {
return __awaiter(this, void 0, void 0, function* () {
const bn = yield this.provider.getBalance(address || this.account);
return ethers_1.utils.formatEther(bn);
});
}
}
exports.ConnectorChain = ConnectorChain;