Skip to content

Commit

Permalink
Merge branch '57-add-getconfig-and-gettokendata' into 'dev'
Browse files Browse the repository at this point in the history
update AbstractChain config

Closes #57

See merge request ergo/rosen-bridge/rosen-chains!63
  • Loading branch information
vorujack committed Oct 1, 2023
2 parents 5545da0 + 892477e commit 9b6527b
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 94 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

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

10 changes: 8 additions & 2 deletions packages/abstract-chain/lib/AbstractChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ abstract class AbstractChain {
* @returns an object containing the amount of each asset
*/
getLockAddressAssets = async (): Promise<AssetBalance> =>
await this.getAddressAssets(this.configs.lockAddress);
await this.getAddressAssets(this.configs.addresses.lock);

/**
* gets the amount of each asset in the cold storage address
* @returns an object containing the amount of each asset
*/
getColdAddressAssets = async (): Promise<AssetBalance> =>
await this.getAddressAssets(this.configs.coldStorageAddress);
await this.getAddressAssets(this.configs.addresses.cold);

/**
* gets the blockchain height
Expand Down Expand Up @@ -247,6 +247,12 @@ abstract class AbstractChain {
abstract PaymentTransactionFromJson: (
jsonString: string
) => PaymentTransaction;

/**
* returns chain config
* @assetId
*/
getChainConfigs = (): ChainConfigs => this.configs;
}

export default AbstractChain;
9 changes: 7 additions & 2 deletions packages/abstract-chain/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ interface ConfirmationConfigs {
cold: number;
manual: number;
}

interface AddressConfigs {
lock: string;
cold: string;
permit: string;
}
interface ChainConfigs {
fee: bigint;
confirmations: ConfirmationConfigs;
lockAddress: string;
coldStorageAddress: string;
addresses: AddressConfigs;
rwtId: string;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/abstract-chain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/abstract-chain",
"version": "1.0.1",
"version": "2.0.0",
"description": "this project contains abstract classes to implement any chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
7 changes: 5 additions & 2 deletions packages/abstract-chain/tests/AbstractChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ describe('AbstractChain', () => {
cold: 7,
manual: 8,
},
lockAddress: 'lock_addr',
coldStorageAddress: 'cold_addr',
addresses: {
lock: 'lock_addr',
cold: 'cold_addr',
permit: 'permit_addr',
},
rwtId: 'rwt',
};
return new TestChain(network, config);
Expand Down
7 changes: 5 additions & 2 deletions packages/abstract-chain/tests/AbstractUtxoChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ describe('AbstractUtxoChain', () => {
cold: 7,
manual: 8,
},
lockAddress: 'lock_addr',
coldStorageAddress: 'cold_addr',
addresses: {
lock: 'lock_addr',
cold: 'cold_addr',
permit: 'permit_addr',
},
rwtId: 'rwt',
};
return new TestUtxoChain(network, config);
Expand Down
12 changes: 6 additions & 6 deletions packages/chains/cardano/lib/CardanoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
// skip change box (last box & address equal to bank address)
if (
i === tx.body().outputs().len() - 1 &&
output.address().to_bech32() === this.configs.lockAddress
output.address().to_bech32() === this.configs.addresses.lock
)
continue;

Expand Down Expand Up @@ -141,11 +141,11 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
serializedSignedTransactions.map((serializedTx) =>
CardanoWasm.Transaction.from_bytes(Buffer.from(serializedTx, 'hex'))
),
this.configs.lockAddress
this.configs.addresses.lock
);

const coveredBoxes = await this.getCoveringBoxes(
this.configs.lockAddress,
this.configs.addresses.lock,
requiredAssets,
forbiddenBoxIds,
trackMap
Expand Down Expand Up @@ -216,7 +216,7 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
);
const inputBox = CardanoWasm.TransactionInput.new(txHash, box.index);
txBuilder.add_input(
CardanoWasm.Address.from_bech32(this.configs.lockAddress),
CardanoWasm.Address.from_bech32(this.configs.addresses.lock),
inputBox,
CardanoWasm.Value.new(orderValue)
);
Expand Down Expand Up @@ -251,7 +251,7 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
changeAmount.set_multiasset(changeBoxMultiAsset);
this.logger.debug(`Change box amount: ${changeAmount.to_json()}`);
const changeBox = CardanoWasm.TransactionOutput.new(
CardanoWasm.Address.from_bech32(this.configs.lockAddress),
CardanoWasm.Address.from_bech32(this.configs.addresses.lock),
changeAmount
);
txBuilder.add_output(changeBox);
Expand Down Expand Up @@ -626,7 +626,7 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
// check change box
const changeBoxIndex = tx.body().outputs().len() - 1;
const changeBox = tx.body().outputs().get(changeBoxIndex);
if (changeBox.address().to_bech32() !== this.configs.lockAddress) {
if (changeBox.address().to_bech32() !== this.configs.addresses.lock) {
this.logger.debug(
`Tx [${transaction.txId}] invalid: Change box address is wrong`
);
Expand Down
1 change: 0 additions & 1 deletion packages/chains/cardano/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { BigNum } from '@emurgo/cardano-serialization-lib-nodejs';

interface CardanoConfigs extends ChainConfigs {
minBoxValue: bigint;
lockAddress: string;
txTtl: number;
aggregatedPublicKey: string;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/chains/cardano/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/cardano",
"version": "1.0.1",
"version": "2.0.0",
"description": "this project contains cardano chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -24,7 +24,7 @@
"@rosen-bridge/logger-interface": "^0.1.0",
"@rosen-bridge/rosen-extractor": "^0.1.11",
"@rosen-bridge/json-bigint": "^0.1.0",
"@rosen-chains/abstract-chain": "^1.0.1",
"@rosen-chains/abstract-chain": "^2.0.0",
"bech32": "^2.0.0",
"blake2b": "^2.1.3"
},
Expand Down
22 changes: 11 additions & 11 deletions packages/chains/cardano/tests/CardanoChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ describe('CardanoChain', () => {
fee: 1000000n,
minBoxValue: minBoxValue,
txTtl: 64,
lockAddress:
'addr1qxwkc9uhw02wvkgw9qkrw2twescuc2ss53t5yaedl0zcyen2a0y7redvgjx0t0al56q9dkyzw095eh8jw7luan2kh38qpw3xgs',
coldStorageAddress: 'cold',
addresses: {
lock: 'addr1qxwkc9uhw02wvkgw9qkrw2twescuc2ss53t5yaedl0zcyen2a0y7redvgjx0t0al56q9dkyzw095eh8jw7luan2kh38qpw3xgs',
cold: 'cold',
permit: 'permit',
},
rwtId: rwtId,
confirmations: {
observation: observationTxConfirmation,
Expand Down Expand Up @@ -138,7 +140,7 @@ describe('CardanoChain', () => {
);
expectedRequiredAssets.nativeToken += minBoxValue + configs.fee;
expect(getCovBoxesSpy).toHaveBeenCalledWith(
configs.lockAddress,
configs.addresses.lock,
expectedRequiredAssets,
TestData.transaction1InputIds,
new Map()
Expand Down Expand Up @@ -599,7 +601,7 @@ describe('CardanoChain', () => {
// call the function
const result = testInstance.callGetTransactionsBoxMapping(
transactions,
configs.lockAddress
configs.addresses.lock
);

// check returned value
Expand Down Expand Up @@ -640,7 +642,7 @@ describe('CardanoChain', () => {
const trackingTokenId = 'asset1jy5q5a0vpstutq5q6d8cgdmrd4qu5yefcdnjgz';
const result = testInstance.callGetTransactionsBoxMapping(
transactions,
configs.lockAddress,
configs.addresses.lock,
trackingTokenId
);

Expand Down Expand Up @@ -682,7 +684,7 @@ describe('CardanoChain', () => {
const trackingTokenId = 'asset1v25eyenfzrv6me9hw4vczfprdctzy5ed3x99p0';
const result = testInstance.callGetTransactionsBoxMapping(
transactions,
configs.lockAddress,
configs.addresses.lock,
trackingTokenId
);

Expand Down Expand Up @@ -1508,10 +1510,8 @@ describe('CardanoChain', () => {
);

// create a new CardanoChain object with custom lock address
const newConfigs = {
...configs,
lockAddress: 'TEST',
};
const newConfigs = structuredClone(configs);
newConfigs.addresses.lock = 'TEST';
const cardanoChain = new CardanoChain(
network,
newConfigs,
Expand Down
16 changes: 8 additions & 8 deletions packages/chains/ergo/lib/ErgoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
serializedSignedTransactions.map((serializedTx) =>
Serializer.signedDeserialize(Buffer.from(serializedTx, 'hex'))
),
this.configs.lockAddress
this.configs.addresses.lock
);
(await this.getMempoolBoxMapping(this.configs.lockAddress)).forEach(
(await this.getMempoolBoxMapping(this.configs.addresses.lock)).forEach(
(value, key) => trackMap.set(key, value)
);

// call getCovering to get enough boxes
const coveredBoxes = await this.getCoveringBoxes(
this.configs.lockAddress,
this.configs.addresses.lock,
requiredAssets,
forbiddenBoxIds,
trackMap
Expand Down Expand Up @@ -252,7 +252,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
)
),
wasm.Contract.new(
wasm.Address.from_base58(this.configs.lockAddress).to_ergo_tree()
wasm.Address.from_base58(this.configs.addresses.lock).to_ergo_tree()
),
currentHeight
);
Expand All @@ -279,7 +279,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
outBoxCandidates,
currentHeight,
wasm.BoxValue.from_i64(wasm.I64.from_str(this.configs.fee.toString())),
wasm.Address.from_base58(this.configs.lockAddress)
wasm.Address.from_base58(this.configs.addresses.lock)
);
txCandidate.set_data_inputs(inData);
const tx = txCandidate.build();
Expand Down Expand Up @@ -382,7 +382,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
output.ergo_tree().to_base16_bytes() === ErgoChain.feeBoxErgoTree ||
(tx.output_candidates().len() - i === 2 &&
output.ergo_tree().to_base16_bytes() ===
wasm.Address.from_base58(this.configs.lockAddress)
wasm.Address.from_base58(this.configs.addresses.lock)
.to_ergo_tree()
.to_base16_bytes())
)
Expand Down Expand Up @@ -552,7 +552,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {

const box = outputBoxes.get(outputBoxes.len() - 2);
const boxErgoTree = box.ergo_tree().to_base16_bytes();
const lockErgoTree = wasm.Address.from_base58(this.configs.lockAddress)
const lockErgoTree = wasm.Address.from_base58(this.configs.addresses.lock)
.to_ergo_tree()
.to_base16_bytes();
if (boxErgoTree === lockErgoTree) {
Expand Down Expand Up @@ -989,7 +989,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
for (let i = 0; i < tx.outputs().len(); i++) {
const output = tx.outputs().get(i);
const boxErgoTree = output.ergo_tree().to_base16_bytes();
const lockErgoTree = wasm.Address.from_base58(this.configs.lockAddress)
const lockErgoTree = wasm.Address.from_base58(this.configs.addresses.lock)
.to_ergo_tree()
.to_base16_bytes();

Expand Down
Loading

0 comments on commit 9b6527b

Please sign in to comment.