@etherspot/prime-sdk
Version:
Etherspot Prime (Account Abstraction) SDK
57 lines (56 loc) • 2.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkService = void 0;
const common_1 = require("../common");
const constants_1 = require("./constants");
class NetworkService extends common_1.Service {
constructor(defaultChainId) {
super();
this.network$ = new common_1.ObjectSubject(null);
this.externalContractAddresses = new Map();
this.supportedNetworks = constants_1.SupportedNetworks
.map((chainId) => {
const name = constants_1.CHAIN_ID_TO_NETWORK_NAME[chainId];
return !name
? null
: {
chainId,
name,
};
})
.filter((value) => !!value);
if (!this.supportedNetworks.length) {
throw new common_1.Exception('Invalid network config');
}
this.defaultNetwork = defaultChainId
? this.supportedNetworks.find(({ chainId }) => chainId === defaultChainId)
: this.supportedNetworks[0];
if (!this.defaultNetwork) {
this.defaultNetwork = this.supportedNetworks.find(({ chainId }) => chainId === 1);
}
this.chainId$ = this.network$.observeKey('chainId');
}
get network() {
return this.network$.value;
}
get chainId() {
return this.network ? this.network.chainId : null;
}
useDefaultNetwork() {
this.network$.next(this.defaultNetwork);
}
switchNetwork(networkName) {
this.network$.next(this.supportedNetworks.find(({ name }) => name === networkName) || null);
}
isNetworkSupported(chainId) {
return constants_1.SupportedNetworks.includes(chainId);
}
getNetworkConfig(chainId) {
const networkConfig = constants_1.Networks[chainId];
if (!networkConfig) {
throw new Error(`No network config found for network '${chainId}'`);
}
return networkConfig;
}
}
exports.NetworkService = NetworkService;
;