Skip to content

Commit

Permalink
Increase unit test coverage: unit tests for DepositService.initiateDe…
Browse files Browse the repository at this point in the history
…posit (#746)

#Refs: #714.
This PR adds missing unit tests for `DepositService.initDeposit`.
  • Loading branch information
lukasz-zimnoch committed Nov 15, 2023
2 parents bd04b6c + 0ae8557 commit 07b52de
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 5 deletions.
6 changes: 3 additions & 3 deletions typescript/api-reference/classes/DepositsService.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Handle to tBTC contracts.

#### Defined in

[src/services/deposits/deposits-service.ts:62](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/deposits/deposits-service.ts#L62)
[src/services/deposits/deposits-service.ts:61](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/deposits/deposits-service.ts#L61)

___

Expand Down Expand Up @@ -141,7 +141,7 @@ Throws an error if one of the following occurs:

#### Defined in

[src/services/deposits/deposits-service.ts:57](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/deposits/deposits-service.ts#L57)
[src/services/deposits/deposits-service.ts:56](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/deposits/deposits-service.ts#L56)

___

Expand Down Expand Up @@ -170,4 +170,4 @@ Typically, there is no need to use this method when DepositsService

#### Defined in

[src/services/deposits/deposits-service.ts:125](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/deposits/deposits-service.ts#L125)
[src/services/deposits/deposits-service.ts:124](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/deposits/deposits-service.ts#L124)
1 change: 0 additions & 1 deletion typescript/src/services/deposits/deposits-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class DepositsService {
* - There are no active wallet in the Bridge contract
* - The Bitcoin recovery address is not a valid P2(W)PKH
*/
// TODO: Cover with unit tests.
async initiateDeposit(bitcoinRecoveryAddress: string): Promise<Deposit> {
const receipt = await this.generateDepositReceipt(bitcoinRecoveryAddress)
return Deposit.fromReceipt(receipt, this.tbtcContracts, this.bitcoinClient)
Expand Down
126 changes: 125 additions & 1 deletion typescript/test/services/deposits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
DepositReceipt,
DepositRefund,
DepositScript,
DepositsService,
EthereumAddress,
extractBitcoinRawTxVectors,
} from "../../src"
Expand Down Expand Up @@ -947,7 +948,130 @@ describe("Deposits", () => {
})

describe("DepositsService", () => {
// TODO: Implement unit tests.
describe("initiateDeposit", () => {
const depositor = EthereumAddress.from(
"934b98637ca318a4d6e7ca6ffd1690b8e77df637"
)
const bitcoinClient = new MockBitcoinClient()
const tbtcContracts = new MockTBTCContracts()
let depositService: DepositsService

beforeEach(async () => {
depositService = new DepositsService(tbtcContracts, bitcoinClient)
})

context("when default depositor is not set", () => {
it("should throw", async () => {
await expect(
depositService.initiateDeposit("mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc")
).to.be.rejectedWith(
"Default depositor is not set; use setDefaultDepositor first"
)
})
})

context("when default depositor is set", () => {
beforeEach(async () => {
depositService.setDefaultDepositor(depositor)
})

context("when active wallet is not set", () => {
it("should throw", async () => {
await expect(
depositService.initiateDeposit(
"mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc"
)
).to.be.rejectedWith("Could not get active wallet public key")
})
})

context("when active wallet is set", () => {
beforeEach(async () => {
tbtcContracts.bridge.setActiveWalletPublicKey(
Hex.from(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
)
)
})

context("when recovery address is incorrect", () => {
it("should throw", async () => {
await expect(
depositService.initiateDeposit(
"2N5WZpig3vgpSdjSherS2Lv7GnPuxCvkQjT" // p2sh address
)
).to.be.rejectedWith(
"Bitcoin recovery address must be P2PKH or P2WPKH"
)
})
})

context("when recovery address is correct", () => {
context("when recovery address is P2PKH", () => {
let deposit: Deposit

beforeEach(async () => {
deposit = await depositService.initiateDeposit(
"mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc"
)
})

it("should initiate deposit correctly", async () => {
// Inspect the deposit object by looking at its receipt.
const receipt = deposit.getReceipt()
expect(receipt.depositor).to.be.equal(depositor)
expect(receipt.refundPublicKeyHash).to.be.deep.equal(
Hex.from("2cd680318747b720d67bf4246eb7403b476adb34")
)
expect(receipt.walletPublicKeyHash).to.be.deep.equal(
Hex.from("8db50eb52063ea9d98b3eac91489a90f738986f6")
)
// Expect the refund locktime to be in the future.
const receiptTimestamp = BigNumber.from(
receipt.refundLocktime.reverse().toPrefixedString()
).toNumber()
const currentTimestamp = Math.floor(new Date().getTime() / 1000)
expect(receiptTimestamp).to.be.greaterThan(currentTimestamp)
// Expect blinding factor to be set and 8-byte long.
expect(receipt.blindingFactor).not.to.be.undefined
expect(receipt.blindingFactor.toBuffer().length).to.be.equal(8)
})
})

context("when recovery address is P2WPKH", () => {
let deposit: Deposit

beforeEach(async () => {
deposit = await depositService.initiateDeposit(
"tb1qumuaw3exkxdhtut0u85latkqfz4ylgwstkdzsx"
)
})

it("should initiate deposit correctly", async () => {
// Inspect the deposit object by looking at its receipt.
const receipt = deposit.getReceipt()
expect(receipt.depositor).to.be.equal(depositor)
expect(receipt.refundPublicKeyHash).to.be.deep.equal(
Hex.from("e6f9d74726b19b75f16fe1e9feaec048aa4fa1d0")
)
expect(receipt.walletPublicKeyHash).to.be.deep.equal(
Hex.from("8db50eb52063ea9d98b3eac91489a90f738986f6")
)
// Expect the refund locktime to be in the future.
const receiptTimestamp = BigNumber.from(
receipt.refundLocktime.reverse().toPrefixedString()
).toNumber()
const currentTimestamp = Math.floor(new Date().getTime() / 1000)
expect(receiptTimestamp).to.be.greaterThan(currentTimestamp)
// Expect blinding factor to be set and 8-byte long.
expect(receipt.blindingFactor).not.to.be.undefined
expect(receipt.blindingFactor.toBuffer().length).to.be.equal(8)
})
})
})
})
})
})
})

describe("DepositRefund", () => {
Expand Down

0 comments on commit 07b52de

Please sign in to comment.