Skip to content

Commit

Permalink
change two utxo functions to protected and their logic to the actual …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
RaaCT0R committed Jul 21, 2024
1 parent 0f4cde0 commit 4cb8086
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 41 deletions.
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
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
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
9 changes: 8 additions & 1 deletion packages/abstract-chain/tests/TestUtxoChain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractUtxoChain, BoxInfo } from '../lib';
import { AbstractUtxoChain, AssetBalance, BoxInfo } from '../lib';
import TestRosenDataExtractor from './extractor/TestRosenDataExtractor';

class TestUtxoChain extends AbstractUtxoChain<string, string> {
Expand Down Expand Up @@ -31,6 +31,13 @@ class TestUtxoChain extends AbstractUtxoChain<string, string> {
};

serializeTx = (tx: string) => tx;
callGetCoveringBoxes = async (
address: string,
requiredAssets: AssetBalance,
forbiddenBoxIds: Array<string>,
trackMap: Map<string, string | undefined>
) =>
this.getCoveringBoxes(address, requiredAssets, forbiddenBoxIds, trackMap);
}

export default TestUtxoChain;
28 changes: 17 additions & 11 deletions packages/chains/bitcoin/lib/BitcoinChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,22 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
);

// fetch input boxes
const unwrappedRequiredAssets = ChainUtils.unwrapAssetBalance(
requiredAssets,
this.tokenMap,
this.NATIVE_TOKEN_ID,
this.CHAIN
);
const coveredBoxes = await this.getCoveringBoxes(
this.configs.addresses.lock,
requiredAssets,
unwrappedRequiredAssets,
forbiddenBoxIds,
trackMap
);
if (!coveredBoxes.covered) {
const neededBtc = this.unwrapBtc(requiredAssets.nativeToken);
const neededBtc = unwrappedRequiredAssets.nativeToken;
throw new NotEnoughValidBoxesError(
`Available boxes didn't cover required assets. BTC: ${neededBtc.amount.toString()}`
`Available boxes didn't cover required assets. BTC: ${neededBtc.toString()}`
);
}

Expand All @@ -144,14 +150,12 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
index: box.index,
witnessUtxo: {
script: Buffer.from(this.lockScript, 'hex'),
value: Number(this.unwrapBtc(box.value).amount),
value: Number(box.value),
},
});
});
// calculate input boxes assets
let remainingBtc = this.unwrapBtc(
coveredBoxes.boxes.reduce((a, b) => a + b.value, 0n)
).amount;
let remainingBtc = coveredBoxes.boxes.reduce((a, b) => a + b.value, 0n);
this.logger.debug(`Input BTC: ${remainingBtc}`);

// add outputs
Expand Down Expand Up @@ -537,14 +541,15 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {

/**
* 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
*/
getBoxInfo = (box: BitcoinUtxo): BoxInfo => {
protected getBoxInfo = (box: BitcoinUtxo): BoxInfo => {
return {
id: this.getBoxId(box),
assets: {
nativeToken: this.wrapBtc(box.value).amount,
nativeToken: box.value,
tokens: [],
},
};
Expand Down Expand Up @@ -639,13 +644,14 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {

/**
* 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 requiredAssets the required assets (actual values)
* @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
17 changes: 11 additions & 6 deletions packages/chains/bitcoin/tests/BitcoinChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('BitcoinChain', () => {

// mock getCoveringBoxes, hasLockAddressEnoughAssets
const bitcoinChain = testUtils.generateChainObject(network);
const getCovBoxesSpy = vi.spyOn(bitcoinChain, 'getCoveringBoxes');
const getCovBoxesSpy = vi.spyOn(bitcoinChain as any, 'getCoveringBoxes');
getCovBoxesSpy.mockResolvedValue({
covered: true,
boxes: testData.lockAddressUtxos,
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('BitcoinChain', () => {
it('should throw error when bank boxes can not cover order assets', async () => {
// mock getCoveringBoxes, hasLockAddressEnoughAssets
const bitcoinChain = testUtils.generateChainObject(network);
const getCovBoxesSpy = vi.spyOn(bitcoinChain, 'getCoveringBoxes');
const getCovBoxesSpy = vi.spyOn(bitcoinChain as any, 'getCoveringBoxes');
getCovBoxesSpy.mockResolvedValue({
covered: false,
boxes: testData.lockAddressUtxos,
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('BitcoinChain', () => {
// mock getCoveringBoxes, hasLockAddressEnoughAssets
const bitcoinChain =
testUtils.generateChainObjectWithMultiDecimalTokenMap(network);
const getCovBoxesSpy = vi.spyOn(bitcoinChain, 'getCoveringBoxes');
const getCovBoxesSpy = vi.spyOn(bitcoinChain as any, 'getCoveringBoxes');
getCovBoxesSpy.mockResolvedValue({
covered: true,
boxes: testData.lockAddressUtxos,
Expand Down Expand Up @@ -242,7 +242,12 @@ describe('BitcoinChain', () => {
).amount;
expect(getCovBoxesSpy).toHaveBeenCalledWith(
testUtils.configs.addresses.lock,
expectedRequiredAssets,
ChainUtils.unwrapAssetBalance(
expectedRequiredAssets,
tokenMap,
bitcoinChain.NATIVE_TOKEN_ID,
bitcoinChain.CHAIN
),
testData.transaction1InputIds,
new Map()
);
Expand Down Expand Up @@ -716,15 +721,15 @@ describe('BitcoinChain', () => {
* @expected
* - it should return constructed BoxInfo
*/
it('should get box info successfully', async () => {
it('should get box info successfully', () => {
// mock a BitcoinUtxo with assets
const rawBox = testData.lockUtxo;

// run test
const bitcoinChain = testUtils.generateChainObject(network);

// check returned value
const result = await bitcoinChain.getBoxInfo(rawBox);
const result = (bitcoinChain as any).getBoxInfo(rawBox);
expect(result.id).toEqual(rawBox.txId + '.' + rawBox.index);
expect(result.assets.nativeToken.toString()).toEqual(
rawBox.value.toString()
Expand Down
2 changes: 1 addition & 1 deletion packages/chains/cardano/lib/CardanoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class CardanoChain extends AbstractUtxoChain<CardanoTx, CardanoUtxo> {
* @param box the box
* @returns an object containing the box id and assets
*/
getBoxInfo = (box: CardanoUtxo): BoxInfo => {
protected getBoxInfo = (box: CardanoUtxo): BoxInfo => {
return {
id: CardanoUtils.getBoxId(box),
assets: {
Expand Down
10 changes: 5 additions & 5 deletions packages/chains/cardano/tests/CardanoChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('CardanoChain', () => {

// mock getCoveringBoxes, hasLockAddressEnoughAssets
const cardanoChain = TestUtils.generateChainObject(network);
const getCovBoxesSpy = spyOn(cardanoChain, 'getCoveringBoxes');
const getCovBoxesSpy = spyOn(cardanoChain as any, 'getCoveringBoxes');
getCovBoxesSpy.mockResolvedValue({
covered: true,
boxes: bankBoxes,
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('CardanoChain', () => {

// mock getCoveringBoxes, hasLockAddressEnoughAssets
const cardanoChain = TestUtils.generateChainObject(network);
const getCovBoxesSpy = spyOn(cardanoChain, 'getCoveringBoxes');
const getCovBoxesSpy = spyOn(cardanoChain as any, 'getCoveringBoxes');
getCovBoxesSpy.mockResolvedValue({
covered: true,
boxes: bankBoxes.slice(2),
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('CardanoChain', () => {
it('should throw error when bank boxes can not cover order assets', async () => {
// mock getCoveringBoxes, hasLockAddressEnoughAssets
const cardanoChain = TestUtils.generateChainObject(network);
const getCovBoxesSpy = spyOn(cardanoChain, 'getCoveringBoxes');
const getCovBoxesSpy = spyOn(cardanoChain as any, 'getCoveringBoxes');
getCovBoxesSpy.mockResolvedValue({
covered: false,
boxes: bankBoxes,
Expand Down Expand Up @@ -290,15 +290,15 @@ describe('CardanoChain', () => {
* @expected
* - it should return constructed BoxInfo
*/
it('should get box info successfully', async () => {
it('should get box info successfully', () => {
// mock a CardanoBox with assets
const rawBox = bankBoxes[0];

// call the function
const cardanoChain = TestUtils.generateChainObject(network);

// check returned value
const result = await cardanoChain.getBoxInfo(rawBox);
const result = (cardanoChain as any).getBoxInfo(rawBox);
expect(result.id).toEqual(rawBox.txId + '.' + rawBox.index);
expect(result.assets.nativeToken.toString()).toEqual(
rawBox.value.toString()
Expand Down
2 changes: 1 addition & 1 deletion packages/chains/ergo/lib/ErgoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.Transaction, wasm.ErgoBox> {
* @param box the box
* @returns an object containing the box id and assets
*/
getBoxInfo = (box: wasm.ErgoBox): BoxInfo => {
protected getBoxInfo = (box: wasm.ErgoBox): BoxInfo => {
return {
id: box.box_id().to_str(),
assets: ErgoUtils.getBoxAssets(box),
Expand Down
Loading

0 comments on commit 4cb8086

Please sign in to comment.