Skip to content

Commit

Permalink
Merge branch 'eRSN-api-changes' into 'dev'
Browse files Browse the repository at this point in the history
E rsn api changes

See merge request ergo/rosen-bridge/watcher!282
  • Loading branch information
vorujack committed Jul 17, 2024
2 parents bf3124d + bb5110a commit f33a38d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-rules-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/watcher': patch
---

Fix apis with respect to eRSN addition
2 changes: 2 additions & 0 deletions src/api/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface GeneralInfo {
health: string;
address: string;
rsnTokenId: string;
eRsnTokenId: string;
collateral: {
erg: bigint;
rsn: bigint;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/api/revenue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/config/rosenConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/ergo/boxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
Expand All @@ -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();
Expand Down
43 changes: 31 additions & 12 deletions src/ergo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -538,7 +555,7 @@ export class ErgoUtils {
);
};

static transformChartData = (chartData: RevenueChartRecord[]) => {
static transformChartData = async (chartData: RevenueChartRecord[]) => {
const chartMap = new Map<string, Array<ChartRecord>>();
const labels: Array<string> = [];
chartData.forEach((record) => {
Expand All @@ -565,17 +582,19 @@ export class ErgoUtils {
title: Omit<TokenData, 'amount'>;
data: Array<ChartRecord>;
}[] = [];
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;
};
}
26 changes: 8 additions & 18 deletions src/transactions/rewardCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class RewardCollection {
rewardCollectionTx = async (
eRsnBoxes: wasm.ErgoBox[],
eRsnCount: bigint,
eRsnTokenId: string,
emissionBox: wasm.ErgoBox,
feeBoxes: Array<wasm.ErgoBox>
) => {
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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<wasm.ErgoBox> = [];
Expand All @@ -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}`
Expand Down

0 comments on commit f33a38d

Please sign in to comment.