Skip to content

Commit

Permalink
Merge branch '46-automate-cardano-protocol-parameter' into 'dev'
Browse files Browse the repository at this point in the history
Resolve "Automate Cardano Protocol Parameter"

Closes #46

See merge request ergo/rosen-bridge/rosen-chains!72
  • Loading branch information
vorujack committed Nov 8, 2023
2 parents 53dc4fa + 8a9f511 commit 15e9dd6
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 57 deletions.
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.

31 changes: 29 additions & 2 deletions packages/chains/cardano/lib/CardanoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import JSONBigInt from '@rosen-bridge/json-bigint';
import CardanoTransaction from './CardanoTransaction';
import CardanoUtils from './CardanoUtils';
import cardanoUtils from './CardanoUtils';
import { CARDANO_CHAIN, txBuilderConfig } from './constants';
import { CARDANO_CHAIN } from './constants';
import AbstractCardanoNetwork from './network/AbstractCardanoNetwork';
import Serializer from './Serializer';
import {
Expand Down Expand Up @@ -159,7 +159,9 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
}
const bankBoxes = coveredBoxes.boxes;

const txBuilder = CardanoWasm.TransactionBuilder.new(txBuilderConfig);
const txBuilder = CardanoWasm.TransactionBuilder.new(
await this.getTxBuilderConfig()
);
let orderValue = BigNum.zero();
const orderAssets: Map<string, bigint> = new Map();

Expand Down Expand Up @@ -777,6 +779,31 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
*/
PaymentTransactionFromJson = (jsonString: string): CardanoTransaction =>
CardanoTransaction.fromJson(jsonString);

/**
* generates transaction builder config using protocol params
* @returns TransactionBuilderConfig
*/
protected getTxBuilderConfig =
async (): Promise<CardanoWasm.TransactionBuilderConfig> => {
const params = await this.network.getProtocolParameters();
return CardanoWasm.TransactionBuilderConfigBuilder.new()
.fee_algo(
CardanoWasm.LinearFee.new(
CardanoWasm.BigNum.from_str(params.minFeeA.toString()),
CardanoWasm.BigNum.from_str(params.minFeeB.toString())
)
)
.pool_deposit(CardanoWasm.BigNum.from_str(params.poolDeposit))
.key_deposit(CardanoWasm.BigNum.from_str(params.keyDeposit))
.coins_per_utxo_byte(
CardanoWasm.BigNum.from_str(params.coinsPerUtxoSize)
)
.max_value_size(params.maxValueSize)
.max_tx_size(params.maxTxSize)
.prefer_pure_change(true)
.build();
};
}

export default CardanoChain;
29 changes: 1 addition & 28 deletions packages/chains/cardano/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
import * as CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs';

const CARDANO_CHAIN = 'cardano';
const ADA = 'ada';

const protocolParameters = {
minFeeA: CardanoWasm.BigNum.from_str('44'),
minFeeB: CardanoWasm.BigNum.from_str('155381'),
poolDeposit: CardanoWasm.BigNum.from_str('500000000'),
keyDeposit: CardanoWasm.BigNum.from_str('2000000'),
maxValueSize: 4000,
maxTxSize: 8000,
coinsPerUtxoWord: CardanoWasm.BigNum.from_str('34482'),
};

const linearFee = CardanoWasm.LinearFee.new(
protocolParameters.minFeeA,
protocolParameters.minFeeB
);

const txBuilderConfig: CardanoWasm.TransactionBuilderConfig =
CardanoWasm.TransactionBuilderConfigBuilder.new()
.fee_algo(linearFee)
.pool_deposit(protocolParameters.poolDeposit)
.key_deposit(protocolParameters.keyDeposit)
.max_value_size(protocolParameters.maxValueSize)
.max_tx_size(protocolParameters.maxTxSize)
.coins_per_utxo_word(protocolParameters.coinsPerUtxoWord)
.build();

export { txBuilderConfig, CARDANO_CHAIN, ADA };
export { CARDANO_CHAIN, ADA };
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractUtxoChainNetwork } from '@rosen-chains/abstract-chain';
import { CardanoTx, CardanoUtxo } from '../types';
import { CardanoProtocolParameters, CardanoTx, CardanoUtxo } from '../types';
import { CardanoRosenExtractor } from '@rosen-bridge/rosen-extractor';
import { Transaction } from '@emurgo/cardano-serialization-lib-nodejs';

Expand Down Expand Up @@ -27,6 +27,12 @@ abstract class AbstractCardanoNetwork extends AbstractUtxoChainNetwork<
* @returns the utxo in CardanoUtxo format
*/
abstract getUtxo: (boxId: string) => Promise<CardanoUtxo>;

/**
* gets required parameters of Cardano Protocol
* @returns an object containing required protocol parameters
*/
abstract getProtocolParameters: () => Promise<CardanoProtocolParameters>;
}

export default AbstractCardanoNetwork;
11 changes: 11 additions & 0 deletions packages/chains/cardano/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ interface CardanoTransactionJsonModel extends PaymentTransactionJsonModel {
inputUtxos: Array<string>;
}

interface CardanoProtocolParameters {
minFeeA: number;
minFeeB: number;
poolDeposit: string;
keyDeposit: string;
maxValueSize: number;
maxTxSize: number;
coinsPerUtxoSize: string;
}

export {
CardanoConfigs,
CardanoAsset,
Expand All @@ -61,4 +71,5 @@ export {
CardanoTx,
UtxoBoxesAssets,
CardanoTransactionJsonModel,
CardanoProtocolParameters,
};
2 changes: 1 addition & 1 deletion packages/chains/cardano/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/cardano",
"version": "2.2.1",
"version": "2.3.0",
"description": "this project contains cardano chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
10 changes: 9 additions & 1 deletion packages/chains/cardano/tests/network/TestCardanoNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { AbstractCardanoNetwork } from '../../lib';
import { BlockInfo, ConfirmationStatus } from '@rosen-chains/abstract-chain';
import { CardanoTx, CardanoUtxo } from '../../lib/types';
import {
CardanoProtocolParameters,
CardanoTx,
CardanoUtxo,
} from '../../lib/types';
import { CardanoRosenExtractor } from '@rosen-bridge/rosen-extractor';
import { protocolParameters } from '../testUtils';

class TestCardanoNetwork extends AbstractCardanoNetwork {
extractor = new CardanoRosenExtractor(
Expand Down Expand Up @@ -61,6 +66,9 @@ class TestCardanoNetwork extends AbstractCardanoNetwork {
getBlockInfo = (blockId: string): Promise<BlockInfo> => {
throw Error('Not mocked');
};

getProtocolParameters = (): Promise<CardanoProtocolParameters> =>
Promise.resolve(protocolParameters);
}

export default TestCardanoNetwork;
10 changes: 10 additions & 0 deletions packages/chains/cardano/tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,13 @@ export const adaToLovelace = (ada: number): bigint =>
BigInt((ada * 1000000).toString());

export const generateRandomId = (): string => randomBytes(32).toString('hex');

export const protocolParameters = {
minFeeA: 44,
minFeeB: 155381,
poolDeposit: '500000000',
keyDeposit: '2000000',
maxValueSize: 4000,
maxTxSize: 8000,
coinsPerUtxoSize: '4311',
};
38 changes: 38 additions & 0 deletions packages/networks/cardano-koios/lib/CardanoKoiosNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CardanoUtxo,
CardanoTx,
CardanoAsset,
CardanoProtocolParameters,
} from '@rosen-chains/cardano';
import { RosenTokens } from '@rosen-bridge/tokens';
import {
Expand Down Expand Up @@ -531,6 +532,43 @@ class CardanoKoiosNetwork extends AbstractCardanoNetwork {
assets: input.asset_list.map(this.parseAssetList),
};
};

/**
* gets required parameters of Cardano Protocol
* @returns an object containing required protocol parameters
*/
getProtocolParameters = async (): Promise<CardanoProtocolParameters> => {
const allParams = await this.client.getEpochParams();
const epochParams = allParams[0];
this.logger.debug(
`requested 'getEpochParams'. index 0: ${JsonBigInt.stringify(
epochParams
)}`
);

if (
!epochParams.min_fee_a ||
!epochParams.min_fee_b ||
!epochParams.pool_deposit ||
!epochParams.key_deposit ||
!epochParams.max_val_size ||
!epochParams.max_tx_size ||
!epochParams.coins_per_utxo_size
)
throw new KoiosNullValueError(
`Some required Cardano protocol params are undefined or null `
);

return {
minFeeA: epochParams.min_fee_a,
minFeeB: epochParams.min_fee_b,
poolDeposit: epochParams.pool_deposit,
keyDeposit: epochParams.key_deposit,
maxValueSize: epochParams.max_val_size,
maxTxSize: epochParams.max_tx_size,
coinsPerUtxoSize: epochParams.coins_per_utxo_size,
};
};
}

export default CardanoKoiosNetwork;
6 changes: 3 additions & 3 deletions packages/networks/cardano-koios/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/cardano-koios-network",
"version": "3.0.0",
"version": "3.1.0",
"description": "cardano koios network package for rosen cardano chain",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -26,8 +26,8 @@
"@rosen-bridge/rosen-extractor": "^1.0.1",
"@rosen-bridge/tokens": "^1.0.0",
"@rosen-chains/abstract-chain": "^2.2.1",
"@rosen-chains/cardano": "^2.2.1",
"@rosen-clients/cardano-koios": "^2.0.0"
"@rosen-chains/cardano": "^2.3.0",
"@rosen-clients/cardano-koios": "^2.0.1"
},
"devDependencies": {
"@babel/preset-env": "^7.19.0",
Expand Down
25 changes: 25 additions & 0 deletions packages/networks/cardano-koios/tests/CardanoKoiosNetwork.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import CardanoKoiosNetwork from '../lib';
import {
mockAddressAssets,
mockEmptyAddressAssets,
mockGetEpochParams,
mockGetTip,
mockNoHistoryAddressAssets,
mockPostAddressInfo,
Expand Down Expand Up @@ -603,4 +604,28 @@ describe('CardanoKoiosNetwork', () => {
expect(result).toEqual(testData.expectedUtxo);
});
});

describe('getProtocolParameters', () => {
/**
* @target `CardanoKoiosNetwork.getProtocolParameters` should return required parameters
* @dependencies
* @scenario
* - mock `getEpochParams` of cardano koios client
* - run test
* - check returned value
* @expected
* - it should be mocked slot
*/
it('should return required parameters', async () => {
// mock client response
mockGetEpochParams();

// run test
const network = mockNetwork();
const result = await network.getProtocolParameters();

// check returned value
expect(result).toEqual(testData.expectedRequiredParams);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,12 @@ export const mockPostTxUtxos = (utxos: TxUtxos) => {
postTxInfo: async () => [utxos],
} as any);
};

/**
* mock `getEpochParams` of cardano koios client
*/
export const mockGetEpochParams = () => {
jest.mocked(cardanoKoiosClientFactory).mockReturnValueOnce({
getEpochParams: async () => [testData.epochParams],
} as any);
};
Loading

0 comments on commit 15e9dd6

Please sign in to comment.