From cc731e56d4a7c6b3176557756d4716696dc2d6fc Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Sun, 14 Jul 2024 11:06:40 +0330 Subject: [PATCH 1/6] Use eRSN token id from rosen config in reward collection --- src/config/rosenConfig.ts | 2 ++ src/ergo/boxes.ts | 5 ++--- src/transactions/rewardCollection.ts | 26 ++++++++------------------ 3 files changed, 12 insertions(+), 21 deletions(-) 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/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}` From a29ba18d7b1583b9bb76ddd7fa3c21b9ca9fe9c3 Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Sun, 14 Jul 2024 11:10:28 +0330 Subject: [PATCH 2/6] Add eRSN token id to info api --- src/api/general.ts | 2 ++ 1 file changed, 2 insertions(+) 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, From 44c302e01229eeb0308773b5745b6da3f2728780 Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Sun, 14 Jul 2024 11:52:26 +0330 Subject: [PATCH 3/6] Use token detail table to fill chart information --- src/ergo/utils.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/ergo/utils.ts b/src/ergo/utils.ts index 5964660..8c2d3c3 100644 --- a/src/ergo/utils.ts +++ b/src/ergo/utils.ts @@ -450,6 +450,29 @@ export class ErgoUtils { }; }; + /** + * Returns full token data by checking token details table + * @param tokenId + */ + static tokenDetailByDatabase = async (tokenId: string) => { + let name = 'Unsupported token'; + let decimals = 0; + let isNativeToken = false; + const tokenInfo = (await watcherDatabase.getTokenEntity([tokenId]))[0]; + if (tokenInfo) { + name = tokenInfo.tokenName; + decimals = tokenInfo.decimals; + isNativeToken = tokenId == ERGO_NATIVE_ASSET; + } + + return { + tokenId: tokenId, + name: name, + decimals: decimals, + isNativeToken, + }; + }; + /** * Fill token info in events and observations * @param pagedItems @@ -565,14 +588,14 @@ export class ErgoUtils { title: Omit; data: Array; }[] = []; - chartMap.forEach((records, tokenId) => { + chartMap.forEach(async (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), + title: await this.tokenDetailByDatabase(tokenId), data: filledRecords, }); }); From 406274296e639f4ff99205bdd8c8bf044bd7654b Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Sun, 14 Jul 2024 12:21:12 +0330 Subject: [PATCH 4/6] Fix promise bug --- src/ergo/utils.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/ergo/utils.ts b/src/ergo/utils.ts index 8c2d3c3..5cedb6c 100644 --- a/src/ergo/utils.ts +++ b/src/ergo/utils.ts @@ -561,7 +561,7 @@ export class ErgoUtils { ); }; - static transformChartData = (chartData: RevenueChartRecord[]) => { + static transformChartData = async (chartData: RevenueChartRecord[]) => { const chartMap = new Map>(); const labels: Array = []; chartData.forEach((record) => { @@ -588,17 +588,19 @@ export class ErgoUtils { title: Omit; data: Array; }[] = []; - chartMap.forEach(async (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: await this.tokenDetailByDatabase(tokenId), - 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.tokenDetailByDatabase(tokenId), + data: filledRecords, + }); + }) + ); return jsonObject; }; } From 0aac0f7a5b5d1a164a962094d89523ac0d6a1ef3 Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Sun, 14 Jul 2024 13:53:37 +0330 Subject: [PATCH 5/6] Fix token details --- src/api/revenue.ts | 2 +- src/ergo/utils.ts | 26 ++++++++++---------------- 2 files changed, 11 insertions(+), 17 deletions(-) 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/ergo/utils.ts b/src/ergo/utils.ts index 5cedb6c..07234c6 100644 --- a/src/ergo/utils.ts +++ b/src/ergo/utils.ts @@ -451,26 +451,20 @@ export class ErgoUtils { }; /** - * Returns full token data by checking token details table + * Returns full token data by checking token details in database and tokenMap * @param tokenId */ - static tokenDetailByDatabase = async (tokenId: string) => { - let name = 'Unsupported token'; - let decimals = 0; - let isNativeToken = false; + static tokenDetail = async (tokenId: string) => { const tokenInfo = (await watcherDatabase.getTokenEntity([tokenId]))[0]; if (tokenInfo) { - name = tokenInfo.tokenName; - decimals = tokenInfo.decimals; - isNativeToken = tokenId == ERGO_NATIVE_ASSET; + return { + tokenId: tokenId, + name: tokenInfo.tokenName, + decimals: tokenInfo.decimals, + isNativeToken: tokenId == ERGO_NATIVE_ASSET, + }; } - - return { - tokenId: tokenId, - name: name, - decimals: decimals, - isNativeToken, - }; + return this.tokenDetailByTokenMap(tokenId, ERGO_CHAIN_NAME); }; /** @@ -596,7 +590,7 @@ export class ErgoUtils { return { label: timestamp, amount: '0' }; }); jsonObject.push({ - title: await this.tokenDetailByDatabase(tokenId), + title: await this.tokenDetail(tokenId), data: filledRecords, }); }) From bb5110a3296be4a0b895062c66788f024a87914b Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Sun, 14 Jul 2024 14:30:01 +0330 Subject: [PATCH 6/6] Add changeset --- .changeset/dirty-rules-wink.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dirty-rules-wink.md 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