Skip to content

Commit

Permalink
Use satoshi amount while searching wallet for redemption (#740)
Browse files Browse the repository at this point in the history
The `RedemptionsService.requestRedemption` function takes an amount in the TBTC
token precision (1e18). However, the `findWalletForRedemption` method called to
find a wallet for redemption expects an amount in the sathoshi precision (1e8).
There is a missing conversion which we are adding in this changeset.
  • Loading branch information
michalsmiarowski authored Nov 7, 2023
2 parents 8852fab + 17455e5 commit ff7dd26
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
6 changes: 3 additions & 3 deletions typescript/api-reference/classes/RedemptionsService.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Promise holding the wallet main UTXO or undefined value.

#### Defined in

[services/redemptions/redemptions-service.ts:215](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L215)
[services/redemptions/redemptions-service.ts:225](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L225)

___

Expand All @@ -116,7 +116,7 @@ Promise with the wallet details needed to request a redemption.

#### Defined in

[services/redemptions/redemptions-service.ts:96](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L96)
[services/redemptions/redemptions-service.ts:106](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L106)

___

Expand Down Expand Up @@ -147,7 +147,7 @@ Throws an error if no redemption request exists for the given

#### Defined in

[services/redemptions/redemptions-service.ts:327](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L327)
[services/redemptions/redemptions-service.ts:337](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L337)

___

Expand Down
12 changes: 11 additions & 1 deletion typescript/src/services/redemptions/redemptions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,19 @@ export class RedemptionsService {
throw new Error("Redeemer output script must be of standard type")
}

const amountToSatoshi = (value: BigNumber): BigNumber => {
const satoshiMultiplier = BigNumber.from(1e10)
const remainder = value.mod(satoshiMultiplier)
const convertibleAmount = amount.sub(remainder)
return convertibleAmount.div(satoshiMultiplier)
}

// The findWalletForRedemption operates on satoshi amount precision (1e8)
// while the amount parameter is TBTC token precision (1e18). We need to
// convert the amount to get proper results.
const { walletPublicKey, mainUtxo } = await this.findWalletForRedemption(
redeemerOutputScript,
amount
amountToSatoshi(amount)
)

const txHash = await this.tbtcContracts.tbtcToken.requestRedemption(
Expand Down
5 changes: 3 additions & 2 deletions typescript/test/services/redemptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ describe("Redemptions", () => {
}
const redeemerOutputScript =
data.pendingRedemptions[0].pendingRedemption.redeemerOutputScript
// Use amount in TBTC token precision (1e18)
const amount =
data.pendingRedemptions[0].pendingRedemption.requestedAmount
data.pendingRedemptions[0].pendingRedemption.requestedAmount.mul(1e10)

let tbtcContracts: MockTBTCContracts
let bitcoinClient: MockBitcoinClient
Expand Down Expand Up @@ -129,7 +130,7 @@ describe("Redemptions", () => {
walletPublicKey,
mainUtxo,
redeemerOutputScript,
amount,
amount: amount.div(1e10),
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/utils/mock-tbtc-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class MockTBTCToken implements TBTCToken {
walletPublicKey,
mainUtxo,
redeemerOutputScript,
amount,
amount: amount.div(1e10), // Store amount in satoshi.
})

return Hex.from(
Expand Down

0 comments on commit ff7dd26

Please sign in to comment.