@wasserstoff/tribes-sdk
Version:
SDK for integrating with Tribes by Astrix platform on any EVM compatible chain
373 lines (372 loc) • 14.5 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PointsModule = void 0;
const ethers_1 = require("ethers");
const BaseModule_1 = require("../core/BaseModule");
const core_1 = require("../types/core");
// Import ABI
const AstrixPointSystem_json_1 = __importDefault(require("../../abis/AstrixPointSystem.json"));
/**
* Module for interacting with the point system
*/
class PointsModule extends BaseModule_1.BaseModule {
/**
* Get the point system contract
* @param useSigner Whether to use the signer
*/
getPointSystemContract(useSigner = false) {
return this. // eslint-disable-next-line @typescript-eslint/no-explicit-any
getContract(this.config.contracts.astrixPointSystem, AstrixPointSystem_json_1.default, useSigner);
}
/**
* Set the organization that will fund points for a tribe
* @param params Parameters for setting the tribe organization
*/
async setTribeOrganization(params) {
try {
const pointSystem = this.getPointSystemContract(true);
const tx = await pointSystem.setTribeOrganization(params.tribeId, params.organization);
const receipt = await tx.wait();
this.log(`Set tribe organization`, {
tribeId: params.tribeId,
organization: params.organization,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to set tribe organization', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Set the exchange rate for a tribe
* @param params Parameters for setting the exchange rate
*/
async setExchangeRate(params) {
try {
const pointSystem = this.getPointSystemContract(true);
const tx = await pointSystem.setExchangeRate(params.tribeId, params.rate);
const receipt = await tx.wait();
this.log(`Set exchange rate`, {
tribeId: params.tribeId,
rate: params.rate,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to set exchange rate', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Create a new tribe token
* @param params Parameters for creating a tribe token
*/
async createTribeToken(params) {
try {
const pointSystem = this.getPointSystemContract(true);
const tx = await pointSystem.createTribeToken(params.tribeId, params.name, params.symbol);
const receipt = await tx.wait();
this.log(`Created tribe token`, {
tribeId: params.tribeId,
name: params.name,
symbol: params.symbol,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to create tribe token', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Set points for an action type
* @param params Parameters for setting action points
*/
async setActionPoints(params) {
try {
const pointSystem = this.getPointSystemContract(true);
// Convert action type to bytes32 if it's a string from the enum
let actionType;
if (typeof params.actionType === 'string') {
actionType = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(params.actionType));
}
else {
actionType = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(params.actionType));
}
const tx = await pointSystem.setActionPoints(params.tribeId, actionType, params.points);
const receipt = await tx.wait();
this.log(`Set action points`, {
tribeId: params.tribeId,
actionType: params.actionType,
points: params.points,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to set action points', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Award points to a member
* @param params Parameters for awarding points
*/
async awardPoints(params) {
try {
const pointSystem = this.getPointSystemContract(true);
// Convert action type to bytes32 if it's a string from the enum
let actionType;
if (typeof params.actionType === 'string') {
actionType = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(params.actionType));
}
else {
actionType = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(params.actionType));
}
const tx = await pointSystem.awardPoints(params.tribeId, params.member, params.points, actionType);
const receipt = await tx.wait();
this.log(`Awarded points`, {
tribeId: params.tribeId,
member: params.member,
points: params.points,
actionType: params.actionType,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to award points', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Deduct points from a member
* @param params Parameters for deducting points
*/
async deductPoints(params) {
try {
const pointSystem = this.getPointSystemContract(true);
const tx = await pointSystem.deductPoints(params.tribeId, params.member, params.points, params.reason);
const receipt = await tx.wait();
this.log(`Deducted points`, {
tribeId: params.tribeId,
member: params.member,
points: params.points,
reason: params.reason,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to deduct points', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Record an action performed by a member
* @param params Parameters for recording an action
*/
async recordAction(params) {
try {
const pointSystem = this.getPointSystemContract(true);
// Convert action type to bytes32 if it's a string from the enum
let actionType;
if (typeof params.actionType === 'string') {
actionType = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(params.actionType));
}
else {
actionType = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(params.actionType));
}
const tx = await pointSystem.recordAction(params.tribeId, params.member, actionType);
const receipt = await tx.wait();
this.log(`Recorded action`, {
tribeId: params.tribeId,
member: params.member,
actionType: params.actionType,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to record action', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Award points to multiple members
* @param params Parameters for batch awarding points
*/
async batchAwardPoints(params) {
try {
const pointSystem = this.getPointSystemContract(true);
// Convert action type to bytes32 if it's a string from the enum
let actionType;
if (typeof params.actionType === 'string') {
actionType = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(params.actionType));
}
else {
actionType = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(params.actionType));
}
const tx = await pointSystem.batchAwardPoints(params.tribeId, params.members, params.points, actionType);
const receipt = await tx.wait();
this.log(`Batch awarded points`, {
tribeId: params.tribeId,
memberCount: params.members.length,
points: params.points,
actionType: params.actionType,
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to batch award points', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Exchange Astrix tokens for tribe tokens
* @param params Parameters for exchanging tokens
*/
async exchangeTokens(params) {
try {
const pointSystem = this.getPointSystemContract(true);
const tx = await pointSystem.exchangeAstrixForTribeTokens(params.tribeId, params.astrixAmount);
const receipt = await tx.wait();
this.log(`Exchanged tokens`, {
tribeId: params.tribeId,
astrixAmount: params.astrixAmount.toString(),
txHash: receipt.hash
});
return receipt.hash;
}
catch (error) {
return this.handleError(error, 'Failed to exchange tokens', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Get a member's point balance
* @param tribeId Tribe ID
* @param member Member address
*/
async getMemberPoints(tribeId, member) {
try {
const pointSystem = this.getPointSystemContract();
const points = await pointSystem.getMemberPoints(tribeId, member);
return Number(points);
}
catch (error) {
return this.handleError(error, 'Failed to get member points', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Get the points value for an action
* @param tribeId Tribe ID
* @param actionType Action type
*/
async getActionPoints(tribeId, actionType) {
try {
const pointSystem = this.getPointSystemContract();
// Convert action type to bytes32 if it's a string from the enum
let actionTypeBytes;
if (typeof actionType === 'string') {
actionTypeBytes = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(actionType));
}
else {
actionTypeBytes = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(actionType));
}
const points = await pointSystem.getActionPoints(tribeId, actionTypeBytes);
return Number(points);
}
catch (error) {
return this.handleError(error, 'Failed to get action points', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Get the count of actions performed by a member
* @param tribeId Tribe ID
* @param member Member address
* @param actionType Action type
*/
async getActionCount(tribeId, member, actionType) {
try {
const pointSystem = this.getPointSystemContract();
// Convert action type to bytes32 if it's a string from the enum
let actionTypeBytes;
if (typeof actionType === 'string') {
actionTypeBytes = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(actionType));
}
else {
actionTypeBytes = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(actionType));
}
const count = await pointSystem.getActionCount(tribeId, member, actionTypeBytes);
return Number(count);
}
catch (error) {
return this.handleError(error, 'Failed to get action count', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Get the top members by points
* @param tribeId Tribe ID
* @param limit Maximum number of members to return
*/
async getTopMembers(tribeId, limit = 10) {
try {
const pointSystem = this.getPointSystemContract();
const [members, points] = await pointSystem.getTopMembers(tribeId, limit);
const results = [];
for (let i = 0; i < members.length; i++) {
if (members[i] !== ethers_1.ethers.ZeroAddress) {
results.push({
address: members[i],
points: Number(points[i])
});
}
}
return results;
}
catch (error) {
return this.handleError(error, 'Failed to get top members', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Get the tribe token address
* @param tribeId Tribe ID
*/
async getTribeTokenAddress(tribeId) {
try {
const pointSystem = this.getPointSystemContract();
return await pointSystem.tribeTokens(tribeId);
}
catch (error) {
return this.handleError(error, 'Failed to get tribe token address', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Get the tribe organization address
* @param tribeId Tribe ID
*/
async getTribeOrganization(tribeId) {
try {
const pointSystem = this.getPointSystemContract();
return await pointSystem.tribeOrganizations(tribeId);
}
catch (error) {
return this.handleError(error, 'Failed to get tribe organization', core_1.ErrorType.CONTRACT_ERROR);
}
}
/**
* Get the exchange rate for a tribe
* @param tribeId Tribe ID
*/
async getExchangeRate(tribeId) {
try {
const pointSystem = this.getPointSystemContract();
const rate = await pointSystem.exchangeRates(tribeId);
return Number(rate);
}
catch (error) {
return this.handleError(error, 'Failed to get exchange rate', core_1.ErrorType.CONTRACT_ERROR);
}
}
}
exports.PointsModule = PointsModule;