@wasserstoff/tribes-sdk
Version:
SDK for integrating with Tribes by Astrix platform on any EVM compatible chain
242 lines (241 loc) • 7.99 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AstrixSDK = void 0;
const ethers_1 = require("ethers");
const core_1 = require("../types/core");
const errors_1 = require("../types/errors");
const token_1 = require("../modules/token");
const points_1 = require("../modules/points");
const tribes_1 = require("../modules/tribes");
const profiles_1 = require("../modules/profiles");
const content_1 = require("../modules/content");
const organizations_1 = require("../modules/organizations");
const analytics_1 = require("../modules/analytics");
const roles_1 = require("../modules/roles");
const deployedContracts_1 = require("../config/deployedContracts");
/**
* Core SDK class for interacting with the Tribes by Astrix platform
*/
class AstrixSDK {
/**
* Create a new instance of the SDK
* @param config Configuration options
*/
constructor(config) {
this.signer = null;
// Modules
this._token = null;
this._points = null;
this._tribes = null;
this._profiles = null;
this._content = null;
this._organizations = null;
this._analytics = null;
this._roles = null;
// Contract addresses for the Astrix ecosystem
this.contractAddresses = {};
this.config = config;
// Initialize provider
if (typeof config.provider === 'string') {
this.provider = new ethers_1.ethers.JsonRpcProvider(config.provider);
}
else {
this.provider = config.provider;
}
// Initialize modules
this.initializeModules();
}
/**
* Async initialization method to be called after construction
*/
async init() {
await this.initContractAddresses(this.config.contracts);
}
/**
* Initialize all modules
*/
initializeModules() {
try {
// Initialize modules without signer for read-only operations
this._token = new token_1.TokenModule(this.provider, this.config);
this._points = new points_1.PointsModule(this.provider, this.config);
this._tribes = new tribes_1.TribesModule(this.provider, this.config);
this._profiles = new profiles_1.ProfilesModule(this.provider, this.config);
this._content = new content_1.ContentModule(this.provider, this.config);
this._organizations = new organizations_1.OrganizationsModule(this.provider, this.config);
this._analytics = new analytics_1.AnalyticsModule(this.provider, this.config);
this._roles = new roles_1.RolesModule(this.provider, this.config);
}
catch (error) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Failed to initialize SDK modules', undefined, error);
}
}
/**
* Initialize contract addresses based on the connected network and any overrides
* @param contractOverrides Optional contract address overrides
*/
async initContractAddresses(contractAddresses) {
if (contractAddresses) {
this.contractAddresses = contractAddresses;
}
else {
const network = await this.provider.getNetwork();
const chainId = Number(network.chainId);
try {
const addresses = (0, deployedContracts_1.getContractAddressesByChainId)(chainId);
if (!addresses) {
throw new Error(`Unsupported network: ${chainId}. Please provide custom contract addresses.`);
}
this.contractAddresses = addresses;
}
catch (error) {
throw new Error(`Unsupported network: ${chainId}. Please provide custom contract addresses.`);
}
}
}
/**
* Connect with a signer (wallet)
* @param signer Ethers signer object
*/
async connect(signer) {
try {
this.signer = signer;
// Reinitialize modules with signer
if (this._token)
this._token.setSigner(signer);
if (this._points)
this._points.setSigner(signer);
if (this._tribes)
this._tribes.setSigner(signer);
if (this._profiles)
this._profiles.setSigner(signer);
if (this._content)
this._content.setSigner(signer);
if (this._organizations)
this._organizations.setSigner(signer);
if (this._analytics)
this._analytics.setSigner(signer);
}
catch (error) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.CONNECTION_ERROR, 'Failed to connect signer', undefined, error);
}
}
/**
* Check if the SDK is connected with a signer
*/
isConnected() {
return this.signer !== null;
}
/**
* Get the connected address
*/
async getAddress() {
if (!this.signer) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.CONNECTION_ERROR, 'No signer connected');
}
return await this.signer.getAddress();
}
/**
* Get the provider
*/
getProvider() {
return this.provider;
}
/**
* Get the signer
*/
getSigner() {
return this.signer;
}
/**
* Get the configuration
*/
getConfig() {
return this.config;
}
/**
* Get the contract address for a specific contract
* @param contractName Name of the contract
* @returns Contract address
*/
getContractAddress(contractName) {
const address = this.contractAddresses[contractName];
if (!address || address === '0x0000000000000000000000000000000000000000') {
throw new Error(`Contract address not configured for ${String(contractName)}`);
}
return address;
}
/**
* Get the token module
*/
get token() {
if (!this._token) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Token module not initialized');
}
return this._token;
}
/**
* Get the points module
*/
get points() {
if (!this._points) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Points module not initialized');
}
return this._points;
}
/**
* Get the tribes module
*/
get tribes() {
if (!this._tribes) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Tribes module not initialized');
}
return this._tribes;
}
/**
* Get the profiles module
*/
get profiles() {
if (!this._profiles) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Profiles module not initialized');
}
return this._profiles;
}
/**
* Get the content module
*/
get content() {
if (!this._content) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Content module not initialized');
}
return this._content;
}
/**
* Get the organizations module
*/
get organizations() {
if (!this._organizations) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Organizations module not initialized');
}
return this._organizations;
}
/**
* Get the analytics module
*/
get analytics() {
if (!this._analytics) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Analytics module not initialized');
}
return this._analytics;
}
/**
* Get the roles module
*/
get roles() {
if (!this._roles) {
throw new errors_1.AstrixSDKError(core_1.ErrorType.SDK_ERROR, 'Roles module not initialized');
}
return this._roles;
}
}
exports.AstrixSDK = AstrixSDK;