Skip to content

Commit

Permalink
revamp package bases to consider decimals drop
Browse files Browse the repository at this point in the history
  • Loading branch information
RaaCT0R committed Jul 13, 2024
1 parent 2d76c9a commit 336b17f
Show file tree
Hide file tree
Showing 27 changed files with 311 additions and 142 deletions.
9 changes: 9 additions & 0 deletions .changeset/honest-pigs-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@rosen-chains/abstract-chain': major
---

- add abstract NATIVE_TOKEN_ID variable
- add RosenTokens to constructor arguments
- consider decimals drop
- every function of `AbstractChain` and `AbstractUtxoChain` gets and returns the wrapped values
- network functions (functions of `AbstractChainNetwork` and `AbstractUtxoChainNetwork`) should still return **the actual values**
37 changes: 21 additions & 16 deletions package-lock.json

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

22 changes: 20 additions & 2 deletions packages/abstract-chain/lib/AbstractChain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AbstractLogger, DummyLogger } from '@rosen-bridge/abstract-logger';
import { ChainMinimumFee } from '@rosen-bridge/minimum-fee';
import { AbstractRosenDataExtractor } from '@rosen-bridge/rosen-extractor';
import { RosenTokens, TokenMap } from '@rosen-bridge/tokens';
import { blake2b } from 'blakejs';
import ChainUtils from './ChainUtils';
import {
Expand All @@ -27,19 +28,23 @@ import {
import PaymentTransaction from './PaymentTransaction';

abstract class AbstractChain<TxType> {
protected abstract CHAIN: string;
abstract readonly CHAIN: string;
abstract readonly NATIVE_TOKEN_ID: string;
protected abstract extractor: AbstractRosenDataExtractor<string> | undefined;
protected network: AbstractChainNetwork<TxType>;
protected configs: ChainConfigs;
protected tokenMap: TokenMap;
logger: AbstractLogger;

constructor(
network: AbstractChainNetwork<TxType>,
configs: ChainConfigs,
tokens: RosenTokens,
logger?: AbstractLogger
) {
this.network = network;
this.configs = configs;
this.tokenMap = new TokenMap(tokens);
this.logger = logger ? logger : new DummyLogger();
}

Expand Down Expand Up @@ -345,7 +350,20 @@ abstract class AbstractChain<TxType> {
this.logger.debug(`returning empty assets for address [${address}]`);
return { nativeToken: 0n, tokens: [] };
}
return await this.network.getAddressAssets(address);
const rawBalance = await this.network.getAddressAssets(address);
const wrappedNativeToken = this.tokenMap.wrapAmount(
this.NATIVE_TOKEN_ID,
rawBalance.nativeToken,
this.CHAIN
).amount;
const wrappedAssets = rawBalance.tokens.map((token) => ({
id: token.id,
value: this.tokenMap.wrapAmount(token.id, token.value, this.CHAIN).amount,
}));
return {
nativeToken: wrappedNativeToken,
tokens: wrappedAssets,
};
};

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/abstract-chain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"dependencies": {
"@rosen-bridge/abstract-logger": "^1.0.0",
"@rosen-bridge/json-bigint": "^0.1.0",
"@rosen-bridge/minimum-fee": "^2.0.0",
"@rosen-bridge/rosen-extractor": "^5.0.1",
"@rosen-bridge/minimum-fee": "^2.1.0",
"@rosen-bridge/rosen-extractor": "^6.0.0",
"@rosen-bridge/tokens": "^1.2.0",
"blakejs": "^1.2.1"
},
"devDependencies": {
Expand Down
33 changes: 30 additions & 3 deletions packages/abstract-chain/tests/AbstractChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ describe('AbstractChain', () => {
const network = new TestChainNetwork();

/**
* @target ErgoChain.getTxConfirmationStatus should return
* @target AbstractChain.getTxConfirmationStatus should return
* ConfirmedEnough when tx confirmation is more than required number
* @dependencies
* @scenario
Expand All @@ -789,7 +789,7 @@ describe('AbstractChain', () => {
});

/**
* @target ErgoChain.getTxConfirmationStatus should return
* @target AbstractChain.getTxConfirmationStatus should return
* NotConfirmedEnough when payment tx confirmation is less than required number
* @dependencies
* @scenario
Expand All @@ -816,7 +816,7 @@ describe('AbstractChain', () => {
});

/**
* @target ErgoChain.getTxConfirmationStatus should return
* @target AbstractChain.getTxConfirmationStatus should return
* NotFound when tx confirmation is -1
* @dependencies
* @scenario
Expand All @@ -840,6 +840,33 @@ describe('AbstractChain', () => {
});
});

describe('getAddressAssets', () => {
const network = new TestChainNetwork();

/**
* @target AbstractChain.getAddressAssets should return wrapped values
* @dependencies
* @scenario
* - mock a network object to return actual values for the assets
* - run test
* - check returned value
* @expected
* - it should return the wrapped values
*/
it('should return wrapped values', async () => {
// mock a network object to return actual values for the assets
const getAddressAssetsSpy = spyOn(network, 'getAddressAssets');
getAddressAssetsSpy.mockResolvedValueOnce(testData.actualBalance);

// run test
const chain = generateChainObject(network);
const result = await chain.getAddressAssets('address');

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

describe('hasLockAddressEnoughAssets', () => {
const requiredAssets = {
nativeToken: 100n,
Expand Down
48 changes: 14 additions & 34 deletions packages/abstract-chain/tests/AbstractUtxoChain.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import TestUtxoChain from './TestUtxoChain';
import TestUtxoChainNetwork from './network/TestUtxoChainNetwork';
import { AssetBalance, ChainConfigs } from '../lib';
import { when } from 'jest-when';
import TestUtxoChainNetwork from './network/TestUtxoChainNetwork';
import { AssetBalance } from '../lib';
import { generateUtxoChainObject } from './testUtils';

const spyOn = jest.spyOn;

describe('AbstractUtxoChain', () => {
const generateChainObject = (network: TestUtxoChainNetwork) => {
const config: ChainConfigs = {
fee: 100n,
confirmations: {
observation: 5,
payment: 6,
cold: 7,
manual: 8,
},
addresses: {
lock: 'lock_addr',
cold: 'cold_addr',
permit: 'permit_addr',
fraud: 'fraud_addr',
},
rwtId: 'rwt',
};
return new TestUtxoChain(network, config);
};

describe('getCoveringBoxes', () => {
const emptyMap = new Map<string, string>();

Expand All @@ -50,7 +30,7 @@ describe('AbstractUtxoChain', () => {
.mockResolvedValueOnce(['serialized-box-1', 'serialized-box-2']);

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
when(getBoxInfoSpy)
.calledWith('serialized-box-1')
Expand Down Expand Up @@ -111,7 +91,7 @@ describe('AbstractUtxoChain', () => {
.mockResolvedValueOnce(['serialized-box-1', 'serialized-box-2']);

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
when(getBoxInfoSpy)
.calledWith('serialized-box-1')
Expand Down Expand Up @@ -174,7 +154,7 @@ describe('AbstractUtxoChain', () => {

// Mock chain 'getBoxInfo' function to return mocked boxes assets
// (second box doesn't contain required token)
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
when(getBoxInfoSpy)
.calledWith('serialized-box-1')
Expand Down Expand Up @@ -240,7 +220,7 @@ describe('AbstractUtxoChain', () => {
.mockResolvedValueOnce(['serialized-box-11', 'serialized-box-12']);

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
Array.from({ length: 12 }, (x, i) => i).map((i) => {
when(getBoxInfoSpy)
Expand Down Expand Up @@ -303,7 +283,7 @@ describe('AbstractUtxoChain', () => {
.mockResolvedValueOnce(['serialized-box-11', 'serialized-box-12']);

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
Array.from({ length: 12 }, (x, i) => i).map((i) => {
when(getBoxInfoSpy)
Expand Down Expand Up @@ -361,7 +341,7 @@ describe('AbstractUtxoChain', () => {
};

// Run test
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const result = await chain.getCoveringBoxes(
'',
requiredAssets,
Expand Down Expand Up @@ -400,7 +380,7 @@ describe('AbstractUtxoChain', () => {
trackMap.set('box1', 'serialized-tracked-box-1');

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
when(getBoxInfoSpy)
.calledWith('serialized-box-1')
Expand Down Expand Up @@ -475,7 +455,7 @@ describe('AbstractUtxoChain', () => {
trackMap.set('box1', 'serialized-tracked-box-1');

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
when(getBoxInfoSpy)
.calledWith('serialized-box-1')
Expand Down Expand Up @@ -552,7 +532,7 @@ describe('AbstractUtxoChain', () => {
const forbiddenIds = ['box1'];

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
when(getBoxInfoSpy)
.calledWith('serialized-box-1')
Expand Down Expand Up @@ -618,7 +598,7 @@ describe('AbstractUtxoChain', () => {
trackMap.set('box1', undefined);

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
when(getBoxInfoSpy)
.calledWith('serialized-box-1')
Expand Down Expand Up @@ -676,7 +656,7 @@ describe('AbstractUtxoChain', () => {
trackMap.set('box2', 'serialized-tracked-box-1');

// Mock chain 'getBoxInfo' function to return mocked boxes assets
const chain = generateChainObject(network);
const chain = generateUtxoChainObject(network);
const getBoxInfoSpy = spyOn(chain, 'getBoxInfo');
when(getBoxInfoSpy)
.calledWith('serialized-box-1')
Expand Down
5 changes: 3 additions & 2 deletions packages/abstract-chain/tests/TestChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
TransactionAssetBalance,
TransactionType,
} from '../lib';
import TestRosenDataExtractor from './TestRosenDataExtractor';
import TestRosenDataExtractor from './extractor/TestRosenDataExtractor';

class TestChain extends AbstractChain<string> {
protected CHAIN = 'test';
NATIVE_TOKEN_ID = 'test-native-token';
CHAIN = 'test';
protected extractor = new TestRosenDataExtractor();

notImplemented = () => {
Expand Down
Loading

0 comments on commit 336b17f

Please sign in to comment.