Skip to content

Commit

Permalink
Merge branch '103-decimals-drop-bitcoin' into 'dev'
Browse files Browse the repository at this point in the history
Resolve "Decimal Drop: Bitcoin Refactor"

Closes #103

See merge request ergo/rosen-bridge/rosen-chains!118
  • Loading branch information
vorujack committed Jul 23, 2024
2 parents a16a126 + 0d23b07 commit b2df4ec
Show file tree
Hide file tree
Showing 23 changed files with 581 additions and 130 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-rings-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-chains/bitcoin': patch
---

export bitcoin native token id var
5 changes: 5 additions & 0 deletions .changeset/selfish-waves-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-chains/bitcoin': major
---

consider decimals drop
8 changes: 8 additions & 0 deletions .changeset/six-jobs-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@rosen-chains/abstract-chain': major
'@rosen-chains/bitcoin': major
'@rosen-chains/cardano': major
'@rosen-chains/ergo': major
---

changed `getBoxInfo` and `getCoveringBoxes` functions to protected
34 changes: 17 additions & 17 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion packages/abstract-chain/lib/AbstractUtxoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ abstract class AbstractUtxoChain<

/**
* extracts box id and assets of a box
* Note: it returns the actual value
* @param box the box
* @returns an object containing the box id and assets
*/
protected abstract getBoxInfo: (box: BoxType) => BoxInfo;

/**
* gets useful, allowable and last boxes for an address until required assets are satisfied
* Note: it returns the actual value
* @param address the address
* @param requiredAssets the required assets
* @param forbiddenBoxIds the id of forbidden boxes
* @param trackMap the mapping of a box id to it's next box
* @returns an object containing the selected boxes with a boolean showing if requirements covered or not
*/
getCoveringBoxes = async (
protected getCoveringBoxes = async (
address: string,
requiredAssets: AssetBalance,
forbiddenBoxIds: Array<string>,
Expand Down
57 changes: 57 additions & 0 deletions packages/abstract-chain/lib/ChainUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AssetBalance, TokenInfo } from './types';
import { ValueError } from './errors';
import { TokenMap } from '@rosen-bridge/tokens';

class ChainUtils {
/**
Expand Down Expand Up @@ -102,6 +103,62 @@ class ChainUtils {
tokens,
};
};

/**
* wraps amount of the native token and all tokens in AssetBalance
* @param a the AssetBalance object
* @param tokenMap
* @param nativeTokenId
* @param chain
*/
static wrapAssetBalance = (
a: AssetBalance,
tokenMap: TokenMap,
nativeTokenId: string,
chain: string
): AssetBalance => {
const result = structuredClone(a);
result.nativeToken = tokenMap.wrapAmount(
nativeTokenId,
result.nativeToken,
chain
).amount;
result.tokens.forEach(
(token) =>
(token.value = tokenMap.wrapAmount(token.id, token.value, chain).amount)
);
return result;
};

/**
* unwraps amount of the native token and all tokens in AssetBalance
* @param a the AssetBalance object
* @param tokenMap
* @param nativeTokenId
* @param chain
*/
static unwrapAssetBalance = (
a: AssetBalance,
tokenMap: TokenMap,
nativeTokenId: string,
chain: string
): AssetBalance => {
const result = structuredClone(a);
result.nativeToken = tokenMap.unwrapAmount(
nativeTokenId,
result.nativeToken,
chain
).amount;
result.tokens.forEach(
(token) =>
(token.value = tokenMap.unwrapAmount(
token.id,
token.value,
chain
).amount)
);
return result;
};
}

export default ChainUtils;
4 changes: 2 additions & 2 deletions packages/abstract-chain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"@rosen-bridge/abstract-logger": "^1.0.0",
"@rosen-bridge/json-bigint": "^0.1.0",
"@rosen-bridge/minimum-fee": "^2.1.0",
"@rosen-bridge/rosen-extractor": "^6.0.0",
"@rosen-bridge/tokens": "^1.2.0",
"@rosen-bridge/rosen-extractor": "^6.0.1",
"@rosen-bridge/tokens": "^1.2.1",
"blakejs": "^1.2.1"
},
"devDependencies": {
Expand Down
22 changes: 11 additions & 11 deletions packages/abstract-chain/tests/AbstractUtxoChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -301,7 +301,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -342,7 +342,7 @@ describe('AbstractUtxoChain', () => {

// Run test
const chain = generateUtxoChainObject(network);
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -417,7 +417,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -492,7 +492,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -560,7 +560,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
forbiddenIds,
Expand Down Expand Up @@ -617,7 +617,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down Expand Up @@ -693,7 +693,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const result = await chain.getCoveringBoxes(
const result = await chain.callGetCoveringBoxes(
'',
requiredAssets,
[],
Expand Down
63 changes: 63 additions & 0 deletions packages/abstract-chain/tests/ChainUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { TokenMap } from '@rosen-bridge/tokens';
import { AssetBalance, ChainUtils, ValueError } from '../lib';
import {
actualBalance,
testTokenMap,
unwrappedBalance,
wrappedBalance,
} from './testData';

describe('ChainUtils', () => {
describe('isEqualAssetBalance', () => {
Expand Down Expand Up @@ -587,4 +594,60 @@ describe('ChainUtils', () => {
});
});
});

describe('wrapAssetBalance', () => {
/**
* @target ChainUtils.wrapAssetBalance should wrap all values successfully
* @dependencies
* @scenario
* - generate tokenMap object with multi decimals
* - run test
* - check returned value
* @expected
* - it should return the expected wrapped values
*/
it('should wrap all values successfully', () => {
// mock an AssetBalance
const tokenMap = new TokenMap(testTokenMap);

// run test
const result = ChainUtils.wrapAssetBalance(
actualBalance,
tokenMap,
'test-native-token',
'test'
);

// check returned value
expect(result).toEqual(wrappedBalance);
});
});

describe('unwrapAssetBalance', () => {
/**
* @target ChainUtils.unwrapAssetBalance should unwrap all values successfully
* @dependencies
* @scenario
* - generate tokenMap object with multi decimals
* - run test
* - check returned value
* @expected
* - it should return the expected unwrapped values
*/
it('should unwrap all values successfully', () => {
// mock an AssetBalance
const tokenMap = new TokenMap(testTokenMap);

// run test
const result = ChainUtils.unwrapAssetBalance(
wrappedBalance,
tokenMap,
'test-native-token',
'test'
);

// check returned value
expect(result).toEqual(unwrappedBalance);
});
});
});
Loading

0 comments on commit b2df4ec

Please sign in to comment.