Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codes documentation #6

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/chains/ergo/dex.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import axios from 'axios';
import { DEXTokensResponse } from './interfaces/dex.interface';

/**
* This class allows you to access elements of a DEX
* @class
* @param {string} dexURL - The DEX's base URL
* @param {number} [timeout=5000] - Timeout
*/
export class DexService {
constructor(
private dexURL: string,
Expand All @@ -25,6 +31,11 @@ export class DexService {
return response.data;
}

/**
* This function allow you to get Ergo's token list from DEX
* @function
* @async
*/
async getTokens() {
return this.request<DEXTokensResponse>('GET', '/ergo-token-list.json');
}
Expand Down
6 changes: 6 additions & 0 deletions src/chains/ergo/ergo.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { ConfigManagerV2 } from '../../services/config-manager-v2';
import { NetworkPrefix } from 'ergo-lib-wasm-nodejs';
import { ErgoConfig } from './interfaces/ergo.interface';

/**
* This function return configuration for Ergo
* @param {string} network - mainnet or testnet
* @returns ErgoConfig
* @function
*/
export function getErgoConfig(network: string): ErgoConfig {
return {
network: {
Expand Down
71 changes: 70 additions & 1 deletion src/chains/ergo/ergo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Explorer } from '@ergolabs/ergo-sdk';

class Pool extends AmmPool {
private name: string;

constructor(public pool: AmmPool) {
super(pool.id, pool.lp, pool.x, pool.y, pool.poolFeeNum);

Expand Down Expand Up @@ -52,6 +53,11 @@ class Pool extends AmmPool {
// }
}

/**
* Ergo chain class
* @param {string} network - mainnet or testnet
* @class
*/
export class Ergo {
private _assetMap: Record<string, ErgoAsset> = {};
private static _instances: LRUCache<string, Ergo>;
Expand Down Expand Up @@ -105,10 +111,16 @@ export class Ergo {
return Object.values(this._assetMap);
}

public ready(): boolean {
public get ready(): boolean {
return this._ready;
}

/**
* This function initializes the Ergo class' instance
* @returns
* @function
* @async
*/
public async init(): Promise<void> {
await this.loadAssets();
await this.loadPools();
Expand All @@ -120,6 +132,13 @@ export class Ergo {
return;
}

/**
* This static function returns the exists or create new Ergo class' instance based on the network
* @param {string} network - mainnet or testnet
* @returns Ergo
* @function
* @static
*/
public static getInstance(network: string): Ergo {
const config = getErgoConfig(network);

Expand All @@ -142,6 +161,12 @@ export class Ergo {
return Ergo._instances.get(config.network.name) as Ergo;
}

/**
* This static function returns the connected instances
* @returns ErgoConnectedInstance
* @function
* @static
*/
public static getConnectedInstances(): ErgoConnectedInstance {
const connectedInstances: ErgoConnectedInstance = {};

Expand All @@ -158,11 +183,23 @@ export class Ergo {
return connectedInstances;
}

/**
* This function returns the current network height(Block number)
* @returns number
* @function
* @async
*/
async getCurrentBlockNumber(): Promise<number> {
const status = await this._node.getNetworkHeight();
return status + 1;
}

/**
* This function returns the unspent boxes based on the address from node
* @returns ErgoBox[]
* @function
* @async
*/
async getAddressUnspentBoxes(address: string) {
let utxos: Array<ErgoBox> = [];
let offset = 0;
Expand All @@ -185,6 +222,12 @@ export class Ergo {
return utxos;
}

/**
* Retrieves Ergo Account from secret key
* @param {string} secret - Secret key
* @returns ErgoAccount
* @function
*/
public getAccountFromSecretKey(secret: string): ErgoAccount {
const sks = new SecretKeys();
const secretKey = SecretKey.dlog_from_bytes(Buffer.from(secret, 'hex'));
Expand All @@ -200,6 +243,13 @@ export class Ergo {
};
}

/**
* Encrypt secret via password
* @param {string} secret - Secret key
* @param {string} password - password
* @returns string
* @function
*/
public encrypt(secret: string, password: string): string {
const iv = randomBytes(16);
const key = Buffer.alloc(32);
Expand All @@ -212,6 +262,13 @@ export class Ergo {
return `${iv.toString('hex')}:${encrypted.toString('hex')}`;
}

/**
* Decrypt encrypted secret key via password
* @param {string} encryptedSecret - Secret key
* @param {string} password - password
* @returns string
* @function
*/
public decrypt(encryptedSecret: string, password: string): string {
const [iv, encryptedKey] = encryptedSecret.split(':');
const key = Buffer.alloc(32);
Expand All @@ -231,6 +288,14 @@ export class Ergo {
return decrypted.toString();
}

/**
* Gets asset balance from unspent boxes
* @param {ErgoAccount} account
* @param {string} assetName
* @returns string
* @function
* @async
*/
public async getAssetBalance(
account: ErgoAccount,
assetName: string,
Expand Down Expand Up @@ -298,6 +363,10 @@ export class Ergo {
return await makeNativePools(this._explorer).getAll({ limit, offset });
}

/**
* Returns a map of asset name and Ergo Asset
* @returns assetMap
*/
public get storedTokenList() {
return this._assetMap;
}
Expand Down
22 changes: 22 additions & 0 deletions src/chains/ergo/node.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import axios from 'axios';
import { NodeInfoResponse } from './interfaces/node.interface';
import { NodeErgoBoxResponse } from './types/node.type';

/**
* This class allows you to access elements of a node
* @class
* @param {string} nodeURL - The node's base URL
* @param {number} timeout - Timeout
*/
export class NodeService {
constructor(
private nodeURL: string,
Expand All @@ -26,12 +32,28 @@ export class NodeService {
return response.data;
}

/**
* Gets network full height
* @returns number
* @function
* @async
*/
async getNetworkHeight(): Promise<number> {
const info = await this.request<NodeInfoResponse>('GET', '/info');

return info.fullHeight;
}

/**
* Get unspent boxes via wallet address
* @param {string} address
* @param {string} offset
* @param {string} limit
* @param {string} sortDirection
* @returns NodeErgoBoxResponse
* @function
* @async
*/
async getUnspentBoxesByAddress(
address: string,
offset: number,
Expand Down