diff --git a/.changeset/dirty-rules-wink.md b/.changeset/dirty-rules-wink.md new file mode 100644 index 0000000..62e9878 --- /dev/null +++ b/.changeset/dirty-rules-wink.md @@ -0,0 +1,5 @@ +--- +'@rosen-bridge/watcher': patch +--- + +Fix apis with respect to eRSN addition diff --git a/src/api/general.ts b/src/api/general.ts index b2cc0ed..5236233 100644 --- a/src/api/general.ts +++ b/src/api/general.ts @@ -21,6 +21,7 @@ interface GeneralInfo { health: string; address: string; rsnTokenId: string; + eRsnTokenId: string; collateral: { erg: bigint; rsn: bigint; @@ -48,6 +49,7 @@ generalRouter.get('/', async (req: Request, res: Response) => { health: await HealthCheckSingleton.getInstance().getOverallStatus(), address: getConfig().general.address, rsnTokenId: getConfig().rosen.RSN, + eRsnTokenId: getConfig().rosen.eRSN, collateral: { erg: collateral.erg, rsn: collateral.rsn, diff --git a/src/api/revenue.ts b/src/api/revenue.ts index 8cebfe6..dce26e7 100644 --- a/src/api/revenue.ts +++ b/src/api/revenue.ts @@ -94,7 +94,7 @@ revenueRouter.get('/chart', async (req, res) => { } else { throw new Error('Invalid period'); } - const result = ErgoUtils.transformChartData(queryResult); + const result = await ErgoUtils.transformChartData(queryResult); res.set('Content-Type', 'application/json'); res.status(200).send(result); } catch (e) { diff --git a/src/config/rosenConfig.ts b/src/config/rosenConfig.ts index 7b1e3c0..240dcdf 100644 --- a/src/config/rosenConfig.ts +++ b/src/config/rosenConfig.ts @@ -21,6 +21,7 @@ class RosenConfig { readonly AWC: string; readonly emissionNFT: string; readonly emissionAddress: string; + readonly eRSN: string; constructor(network: string, networkType: string, configRoot: string) { const rosenConfigPath = this.getAddress(network, networkType, configRoot); @@ -49,6 +50,7 @@ class RosenConfig { this.repoConfigNFT = config.tokens.RepoConfigNFT; this.AWC = config.tokens.AwcNFT; this.emissionNFT = config.tokens.EmissionNFT; + this.eRSN = config.tokens.ERSN; this.emissionAddress = config.addresses.Emission; } } diff --git a/src/ergo/boxes.ts b/src/ergo/boxes.ts index 76723c5..246f5bc 100644 --- a/src/ergo/boxes.ts +++ b/src/ergo/boxes.ts @@ -734,8 +734,7 @@ export class Boxes { value: bigint, height: number, rsnCount: bigint, - eRsnCount: bigint, - eRsnTokenId: string + eRsnCount: bigint ) => { const boxBuilder = new wasm.ErgoBoxCandidateBuilder( wasm.BoxValue.from_i64(wasm.I64.from_str(value.toString())), @@ -751,7 +750,7 @@ export class Boxes { wasm.TokenAmount.from_i64(wasm.I64.from_str(rsnCount.toString())) ); boxBuilder.add_token( - wasm.TokenId.from_str(eRsnTokenId), + wasm.TokenId.from_str(getConfig().rosen.eRSN), wasm.TokenAmount.from_i64(wasm.I64.from_str(eRsnCount.toString())) ); return boxBuilder.build(); diff --git a/src/ergo/utils.ts b/src/ergo/utils.ts index 5964660..07234c6 100644 --- a/src/ergo/utils.ts +++ b/src/ergo/utils.ts @@ -450,6 +450,23 @@ export class ErgoUtils { }; }; + /** + * Returns full token data by checking token details in database and tokenMap + * @param tokenId + */ + static tokenDetail = async (tokenId: string) => { + const tokenInfo = (await watcherDatabase.getTokenEntity([tokenId]))[0]; + if (tokenInfo) { + return { + tokenId: tokenId, + name: tokenInfo.tokenName, + decimals: tokenInfo.decimals, + isNativeToken: tokenId == ERGO_NATIVE_ASSET, + }; + } + return this.tokenDetailByTokenMap(tokenId, ERGO_CHAIN_NAME); + }; + /** * Fill token info in events and observations * @param pagedItems @@ -538,7 +555,7 @@ export class ErgoUtils { ); }; - static transformChartData = (chartData: RevenueChartRecord[]) => { + static transformChartData = async (chartData: RevenueChartRecord[]) => { const chartMap = new Map>(); const labels: Array = []; chartData.forEach((record) => { @@ -565,17 +582,19 @@ export class ErgoUtils { title: Omit; data: Array; }[] = []; - chartMap.forEach((records, tokenId) => { - const filledRecords = labels.map((timestamp) => { - const filtered = records.filter((rec) => rec.label === timestamp); - if (filtered.length) return filtered[0]; - return { label: timestamp, amount: '0' }; - }); - jsonObject.push({ - title: this.tokenDetailByTokenMap(tokenId, ERGO_CHAIN_NAME), - data: filledRecords, - }); - }); + await Promise.all( + [...chartMap.entries()].map(async ([tokenId, records]) => { + const filledRecords = labels.map((timestamp) => { + const filtered = records.filter((rec) => rec.label === timestamp); + if (filtered.length) return filtered[0]; + return { label: timestamp, amount: '0' }; + }); + jsonObject.push({ + title: await this.tokenDetail(tokenId), + data: filledRecords, + }); + }) + ); return jsonObject; }; } diff --git a/src/transactions/rewardCollection.ts b/src/transactions/rewardCollection.ts index d85cdc9..304b063 100644 --- a/src/transactions/rewardCollection.ts +++ b/src/transactions/rewardCollection.ts @@ -31,7 +31,6 @@ class RewardCollection { rewardCollectionTx = async ( eRsnBoxes: wasm.ErgoBox[], eRsnCount: bigint, - eRsnTokenId: string, emissionBox: wasm.ErgoBox, feeBoxes: Array ) => { @@ -47,8 +46,7 @@ class RewardCollection { BigInt(emissionBox.tokens().get(1).amount().as_i64().to_str()) - eRsnCount, BigInt(emissionBox.tokens().get(2).amount().as_i64().to_str()) + - eRsnCount, - eRsnTokenId + eRsnCount ); candidates.push(emissionOut); const rewardBox = this.boxes.createCustomBox( @@ -107,15 +105,9 @@ class RewardCollection { ); return; } - const emissionBox = await this.boxes.getEmissionBox(); - logger.debug( - `Found emission box with boxId [${emissionBox.box_id().to_str()}]` - ); - const eRsnTokenId = emissionBox.tokens().get(2).id().to_str(); - logger.debug(`eRsn token id is :[${eRsnTokenId}]`); - const eRsnBoxes = await this.boxes.getERsnBoxes(eRsnTokenId); + const eRsnBoxes = await this.boxes.getERsnBoxes(getConfig().rosen.eRSN); const eRsnCount = ErgoUtils.getBoxAssetsSum(eRsnBoxes).filter( - (token) => token.tokenId == eRsnTokenId + (token) => token.tokenId == getConfig().rosen.eRSN )[0].amount; if (eRsnCount < getConfig().general.rewardCollectionThreshold) { logger.debug( @@ -126,6 +118,10 @@ class RewardCollection { logger.info( `Found ${eRsnCount} eRsn tokens, trying to collect and change eRsn rewards` ); + const emissionBox = await this.boxes.getEmissionBox(); + logger.debug( + `Found emission box with boxId [${emissionBox.box_id().to_str()}]` + ); const fee = BigInt(getConfig().general.fee); const minBoxValue = BigInt(getConfig().general.minBoxValue); let feeBoxes: Array = []; @@ -135,13 +131,7 @@ class RewardCollection { eRsnBoxes.map((box) => box.box_id().to_str()) ); } - this.rewardCollectionTx( - eRsnBoxes, - eRsnCount, - eRsnTokenId, - emissionBox, - feeBoxes - ); + this.rewardCollectionTx(eRsnBoxes, eRsnCount, emissionBox, feeBoxes); } catch (e) { logger.warn( `Skipping reward collection due to occurred error: ${e.message} - ${e.stack}`