Skip to content

Commit

Permalink
Merge branch '218-minimum-fee-prune' into 'dev'
Browse files Browse the repository at this point in the history
add prune function to MinimumFeeBoxBuilder

Closes #218

See merge request ergo/rosen-bridge/utils!230
  • Loading branch information
zargarzadehm committed Sep 11, 2024
2 parents e3fc363 + d5f920f commit 8264342
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 3 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/boxes/minimum-fee/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @rosen-bridge/minimum-fee

## 2.2.0

### Minor Changes

- add `prune` function to remove unused chains (i.e. chains without config)

## 2.1.0

### Minor Changes
Expand Down
36 changes: 35 additions & 1 deletion packages/boxes/minimum-fee/lib/MinimumFeeBoxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
I64,
TokenAmount,
TokenId,
Constant,
ErgoBoxCandidate,
} from 'ergo-lib-wasm-nodejs';
import { MinimumFeeConfig } from './MinimumFeeConfig';
Expand Down Expand Up @@ -116,6 +115,41 @@ export class MinimumFeeBoxBuilder {
}
};

/**
* removes heights and configs of all chains which don't have any configs
*/
prune = (): MinimumFeeBoxBuilder => {
const activeChains: string[] = [];

for (let i = 0; i < this.fees.length; i++) {
const chains = Object.keys(this.fees[i].heights);
chains.forEach((chain) => {
const feeConfig = this.fees[i].configs[chain];
if (
feeConfig &&
(feeConfig.bridgeFee !== -1n ||
feeConfig.networkFee !== -1n ||
feeConfig.rsnRatio !== -1n ||
feeConfig.rsnRatioDivisor !== -1n ||
feeConfig.feeRatio !== -1n)
)
activeChains.push(chain);
});
}

for (let i = 0; i < this.fees.length; i++) {
const chains = Object.keys(this.fees[i].heights);
chains.forEach((chain) => {
if (!activeChains.includes(chain)) {
delete this.fees[i].heights[chain];
delete this.fees[i].configs[chain];
}
});
}

return this;
};

/**
* validates specified configs and builds ErgoBoxCandidate of config box using them
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/boxes/minimum-fee/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-bridge/minimum-fee",
"version": "2.1.0",
"version": "2.2.0",
"description": "this project gets minimum fee of the bridge for supported tokens from blockchain",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
93 changes: 93 additions & 0 deletions packages/boxes/minimum-fee/tests/MinimumFeeBoxBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,99 @@ describe('MinimumFeeBoxBuilder', () => {
});
});

describe('prune', () => {
/**
* @target MinimumFeeBoxBuilder.prune should remove unused
* chain from config
* @dependencies
* @scenario
* - mock test data
* - run test
* - check builder config
* @expected
* - chain 'cardano' should be removed from config
*/
it('should remove unused chain from config', () => {
// mock test data
const fees = testData.unusedCardanoChainFee;

const feeConfig: Array<MinimumFeeConfig> = [];
fees.forEach((fee) => {
const minimumFeeConfig = new MinimumFeeConfig();
Object.keys(fee.heights).forEach((chain) =>
minimumFeeConfig.setChainConfig(
chain,
fee.heights[chain],
fee.configs[chain]
)
);
feeConfig.push(minimumFeeConfig);
});

// run test
const builder = new MinimumFeeBoxBuilder(
defaultMinimumFeeNFT,
defaultAddress
)
.setValue(defaultValue)
.setHeight(defaultHeight)
.setToken(defaultTokenId);
feeConfig.forEach((fee) => builder.addConfig(fee));
builder.prune();

// check returned value
builder.getConfigs().forEach((fee) => {
expect(fee.heights['cardano']).toBeUndefined();
expect(fee.configs['cardano']).toBeUndefined();
});
});

/**
* @target MinimumFeeBoxBuilder.prune should NOT remove
* chain if has at least one active config
* @dependencies
* @scenario
* - mock test data
* - run test
* - check builder config
* @expected
* - chain 'cardano' should NOT be removed
*/
it('should NOT remove chain if has at least one active config', () => {
// mock test data
const fees = testData.removedPreviousChainFee;

const feeConfig: Array<MinimumFeeConfig> = [];
fees.forEach((fee) => {
const minimumFeeConfig = new MinimumFeeConfig();
Object.keys(fee.heights).forEach((chain) =>
minimumFeeConfig.setChainConfig(
chain,
fee.heights[chain],
fee.configs[chain]
)
);
feeConfig.push(minimumFeeConfig);
});

// run test
const builder = new MinimumFeeBoxBuilder(
defaultMinimumFeeNFT,
defaultAddress
)
.setValue(defaultValue)
.setHeight(defaultHeight)
.setToken(defaultTokenId);
feeConfig.forEach((fee) => builder.addConfig(fee));
builder.prune();

// check returned value
builder.getConfigs().forEach((fee) => {
expect(fee.heights['cardano']).toBeDefined();
});
});
});

describe('removeConfig', () => {
/**
* @target MinimumFeeBoxBuilder.removeConfig should remove
Expand Down
89 changes: 89 additions & 0 deletions packages/boxes/minimum-fee/tests/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,95 @@ export const nonAscendingHeightsFee: Array<Fee> = [
},
];

export const unusedCardanoChainFee: Array<Fee> = [
{
heights: { ergo: 11111, cardano: 444444, binance: 666 },
configs: {
ergo: {
bridgeFee: 100n,
networkFee: 30n,
rsnRatio: 10n,
rsnRatioDivisor: 1000n,
feeRatio: 40n,
},
binance: {
bridgeFee: 700n,
networkFee: 93n,
rsnRatio: 30n,
rsnRatioDivisor: 3000n,
feeRatio: 60n,
},
},
},
{
heights: { ergo: 22222, cardano: 555555, binance: 777 },
configs: {
ergo: {
bridgeFee: 200n,
networkFee: 40n,
rsnRatio: 11n,
rsnRatioDivisor: 1100n,
feeRatio: 41n,
},
binance: {
bridgeFee: 800n,
networkFee: 96n,
rsnRatio: 31n,
rsnRatioDivisor: 3100n,
feeRatio: 61n,
},
},
},
];

export const removedPreviousChainFee: Array<Fee> = [
{
heights: { ergo: 11111, cardano: 444444, binance: 666 },
configs: {
ergo: {
bridgeFee: 100n,
networkFee: 30n,
rsnRatio: 10n,
rsnRatioDivisor: 1000n,
feeRatio: 40n,
},
cardano: {
bridgeFee: 400n,
networkFee: 70n,
rsnRatio: 20n,
rsnRatioDivisor: 2000n,
feeRatio: 50n,
},
binance: {
bridgeFee: 700n,
networkFee: 93n,
rsnRatio: 30n,
rsnRatioDivisor: 3000n,
feeRatio: 60n,
},
},
},
{
heights: { ergo: 22222, cardano: 555555, binance: 777 },
configs: {
ergo: {
bridgeFee: 200n,
networkFee: 40n,
rsnRatio: 11n,
rsnRatioDivisor: 1100n,
feeRatio: 41n,
},
binance: {
bridgeFee: 800n,
networkFee: 96n,
rsnRatio: 31n,
rsnRatioDivisor: 3100n,
feeRatio: 61n,
},
},
},
];

export const explorerTestBoxes = [
{
boxId: 'e13ebf5a32c0a4f4235676ec2be560b15a030723ce6c2cff1f7f34787565d1ba',
Expand Down

0 comments on commit 8264342

Please sign in to comment.