Skip to content

Commit

Permalink
Merge branch 'Add-CommitmentTx.getInstance-and-CommitmentTx.newBuilde…
Browse files Browse the repository at this point in the history
…r' into 'dev'

implement getInstance and newBuilder methods and unit tests

Closes #159

See merge request ergo/rosen-bridge/watcher!158
  • Loading branch information
vorujack committed Oct 15, 2023
2 parents 325363e + 5e049a6 commit b4bd5d6
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ bin
.DS_Store
.env
yarn.lock

# TypeScript cache
*.tsbuildinfo
58 changes: 53 additions & 5 deletions packages/transactions/commitment-tx/lib/commitmentTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RWTRepo } from '@rosen-bridge/rwt-repo';
import { blake2b } from 'blakejs';
import * as ergoLib from 'ergo-lib-wasm-nodejs';

class CommitmentTx {
export class CommitmentTx {
private static _instance?: CommitmentTx;

private permitScriptHash: string;
Expand All @@ -18,14 +18,28 @@ class CommitmentTx {
private logger?: AbstractLogger
) {}

static init(
/**
* creates the singleton instance of CommitmentTx and stores it in _instance
* property
*
* @static
* @param {string} permitAddress
* @param {string} commitmentAddress
* @param {string} rwt
* @param {string} txFee
* @param {RWTRepo} rwtRepo
* @param {AbstractLogger} [logger]
* @return {void}
* @memberof CommitmentTx
*/
static init = (
permitAddress: string,
commitmentAddress: string,
rwt: string,
txFee: string,
rwtRepo: RWTRepo,
logger?: AbstractLogger
) {
): void => {
if (CommitmentTx._instance != undefined) {
return;
}
Expand All @@ -51,10 +65,44 @@ class CommitmentTx {
32
)
).toString('hex');
}
};

/**
* return the singleton instance of CommitmentTx
*
* @static
* @return {CommitmentTx}
* @memberof CommitmentTx
*/
static getInstance = (): CommitmentTx => {
if (!this._instance) {
throw new Error('CommitmentTx instance is not initialized yet');
}
return this._instance;
};

/**
* creates a new instance of CommitmentTxBuilder
*
* @param {ObservationEntity} observation
* @return {CommitmentTxBuilder}
* @memberof CommitmentTx
*/
newBuilder = (observation: ObservationEntity): CommitmentTxBuilder => {
return new CommitmentTxBuilder(
this.permitAddress,
this.permitScriptHash,
this.commitmentAddress,
this.rwt,
this.txFee,
this.rwtRepo,
observation,
this.logger
);
};
}

class CommitmentTxBuilder {
export class CommitmentTxBuilder {
private eventId: string;
constructor(
private permitAddress: string,
Expand Down
1 change: 1 addition & 0 deletions packages/transactions/commitment-tx/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './commitmentTx';
4 changes: 2 additions & 2 deletions packages/transactions/commitment-tx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc --build tsconfig.build.json",
"coverage": "vitest run --coverage",
"coverage": "npm run test -- --coverage",
"lint": "eslint --fix . && npm run prettify",
"prettify": "prettier --write . --ignore-path ./.gitignore",
"release": "npm run build && npm publish --access public",
"test": "vitest",
"test": "NODE_OPTIONS=--experimental-specifier-resolution=node vitest",
"type-check": "tsc --noEmit"
},
"devDependencies": {
Expand Down
51 changes: 51 additions & 0 deletions packages/transactions/commitment-tx/tests/commitmentTx.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ObservationEntity } from '@rosen-bridge/observation-extractor';
import { RWTRepo } from '@rosen-bridge/rwt-repo';
import { ErgoNetworkType } from '@rosen-bridge/scanner';
import { blake2b } from 'blakejs';
import * as ergoLib from 'ergo-lib-wasm-nodejs';

export const commitmentTxParams = {
permitAddress:
'EE7687i4URb4YuSGSQXPCbAjMfN4dUt5Qx8BqKZJiZhDY8fdnSUwcAGqAsqfn1tW1byXB8nDrgkFzkAFgaaempKxfcPtDzAbnu9QfknzmtfnLYHdxPPg7Qtjy7jK5yUpPQ2M4Ps3h5kH57xWDJxcKviEMY11rQnxATjTKTQgGtfzsAPpqsUyT2ZpVYsFzUGJ4nSj4WaDZSU1Hovv6dPkSTArLQSjp38wE72ae6hbNJ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FCBbHcd3X4MTskaSNKYcWgx4QPSf2GAg2xcsgRePe6ZKRuRLqoZ8dJKyZRc911UUxkY7qd4ZaBrp8ymmWy2s3mjbN3CY9uqXTLTdokNVUzdvAcrC8SKUAqbX567RN9TcuE5FmagD7RFpmy6eVME1MWSvdscheeoXWcvMCYPwVAvotnFrsypXmnZHXgEdNLQVsk19iNQKYG7Lxu51msGC7gKmVGaiifzrB',
'32ee5d947cfe8db5480157ffa566b9b7d9faf41fa145c9d00628c7c1599878f6',
'3825b2b4acaaaba626440113153246c65ddb2e9df406c4a56418b5842c9f839a',
ErgoNetworkType.Explorer,
''
),
permitScriptHash: '',
};
commitmentTxParams.permitScriptHash = Buffer.from(
blake2b(
Buffer.from(
ergoLib.Address.from_base58(commitmentTxParams.permitAddress)
.to_ergo_tree()
.to_base16_bytes(),
'hex'
),
undefined,
32
)
).toString('hex');

export const observationEntity1 = new ObservationEntity();
observationEntity1.height = 1;
observationEntity1.amount = '10';
observationEntity1.extractor = 'observation-extractor';
observationEntity1.bridgeFee = '100';
observationEntity1.fromAddress = 'fromAddress';
observationEntity1.block = 'blockHash';
observationEntity1.fromChain = 'ergo';
observationEntity1.networkFee = '1000';
observationEntity1.sourceBlockId = 'block';
observationEntity1.sourceTxId = 'txId';
observationEntity1.sourceChainTokenId = 'sourceToken';
observationEntity1.toAddress = 'addr1';
observationEntity1.targetChainTokenId = 'targetToken';
observationEntity1.toChain = 'cardano';
observationEntity1.requestId = 'reqId2';
76 changes: 76 additions & 0 deletions packages/transactions/commitment-tx/tests/commitmentTx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, expect, it, beforeEach } from 'vitest';
import { CommitmentTx } from '../lib';
import { commitmentTxParams, observationEntity1 } from './commitmentTx.mock';

describe('CommitmentTx', () => {
beforeEach(() => {
CommitmentTx['_instance'] = undefined;
});

describe('getInstance', () => {
/**
* @target should throw exception when CommitmentTx._instance is not yet
* initialized
* @dependencies
* - None
* @scenario
* - call CommitmentTx.getInstance without calling CommitmentTx.init
* - check CommitmentTx.getInstance to throw an exception
* @expected
* - CommitmentTx.getInstance should throw an exception
*/
it(`should throw exception when CommitmentTx._instance is not yet
initialized`, async () => {
expect(() => CommitmentTx.getInstance()).toThrowError();
});
});

describe('newBuilder', () => {
/**
* @target should create a new instance of CommitmentTxBuilder with the set
* properties and passed arguments
* @dependencies
* - None
* @scenario
* - call CommitmentTx.getInstance after calling CommitmentTx.init
* - call CommitmentTx.newBuilder on returned CommitmentTx instance to get a
* new instance of CommitmentTxBuilder
* - check returned CommitmentTxBuilder instance to have correct properties
* set
* @expected
* - returned CommitmentTxBuilder should have correct properties set
*/
it(`should create a new instance of CommitmentTxBuilder with the set
properties and passed arguments`, async () => {
CommitmentTx.init(
commitmentTxParams.permitAddress,
commitmentTxParams.commitmentAddress,
commitmentTxParams.rwt,
commitmentTxParams.txFee,
commitmentTxParams.rwtRepo
);

const commitmentTxBuilder =
CommitmentTx.getInstance().newBuilder(observationEntity1);

expect(commitmentTxBuilder['permitAddress']).toEqual(
commitmentTxParams.permitAddress
);
expect(commitmentTxBuilder['permitScriptHash']).toEqual(
commitmentTxParams.permitScriptHash
);
expect(commitmentTxBuilder['commitmentAddress']).toEqual(
commitmentTxParams.commitmentAddress
);
expect(commitmentTxBuilder['rwt']).toEqual(commitmentTxParams.rwt);
expect(commitmentTxBuilder['txFee']).toEqual(commitmentTxParams.txFee);
expect(commitmentTxBuilder['rwtRepo']).toEqual(
commitmentTxParams.rwtRepo
);
expect(commitmentTxBuilder['observation']).toEqual(observationEntity1);
expect(commitmentTxBuilder['eventId']).toEqual(
observationEntity1.requestId
);
});
});
});

0 comments on commit b4bd5d6

Please sign in to comment.