Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve errors in findWalletForRedemption function #652

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions typescript/src/redemption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ export async function findWalletForRedemption(
}
| undefined = undefined
let maxAmount = BigNumber.from(0)
let activeWalletsCounter = 0
lukasz-zimnoch marked this conversation as resolved.
Show resolved Hide resolved

for (const wallet of wallets) {
const { walletPublicKeyHash } = wallet
Expand All @@ -456,6 +457,7 @@ export async function findWalletForRedemption(
)
continue
}
activeWalletsCounter++

// Wallet must have a main UTXO that can be determined.
const mainUtxo = await determineWalletMainUtxo(
Expand Down Expand Up @@ -511,6 +513,18 @@ export async function findWalletForRedemption(
)
}

if (activeWalletsCounter === 0) {
throw new Error("Currently, there are no active wallets in the network.")
}

// Cover a corner case when the user requested redemption for all active
// wallets in the network using the same Bitcoin address.
if (!walletData && activeWalletsCounter > 0 && maxAmount.eq(0)) {
throw new Error(
"All active wallets in the network have the pending redemption for a given Bitcoin address. Please use another Bitcoin address."
)
}

if (!walletData)
throw new Error(
`Could not find a wallet with enough funds. Maximum redemption amount is ${maxAmount} Satoshi.`
Expand Down
57 changes: 56 additions & 1 deletion typescript/test/redemption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@ describe("Redemption", () => {
bitcoinClient
)
).to.be.rejectedWith(
"Could not find a wallet with enough funds. Maximum redemption amount is 0 Satoshi."
"Currently, there are no active wallets in the network."
)
})
}
Expand Down Expand Up @@ -1676,6 +1676,61 @@ describe("Redemption", () => {
})
}
)

context(
"when all active wallets has pending redemption for a given Bitcoin address",
() => {
const amount: BigNumber = BigNumber.from("1000000") // 0.01 BTC
const redeemerOutputScript =
findWalletForRedemptionData.pendingRedemption.redeemerOutputScript

beforeEach(async () => {
const walletPublicKeyHash =
findWalletForRedemptionData.walletWithPendingRedemption.event
.walletPublicKeyHash

const pendingRedemptions = new Map<
BigNumberish,
RedemptionRequest
>()

const pendingRedemption1 = MockBridge.buildRedemptionKey(
walletPublicKeyHash.toString(),
redeemerOutputScript
)

const pendingRedemption2 = MockBridge.buildRedemptionKey(
findWalletForRedemptionData.liveWallet.event.walletPublicKeyHash.toString(),
redeemerOutputScript
)

pendingRedemptions.set(
pendingRedemption1,
findWalletForRedemptionData.pendingRedemption
)

pendingRedemptions.set(
pendingRedemption2,
findWalletForRedemptionData.pendingRedemption
)
bridge.setPendingRedemptions(pendingRedemptions)
})

it("should throw an error", async () => {
await expect(
findWalletForRedemption(
amount,
redeemerOutputScript,
BitcoinNetwork.Testnet,
bridge,
bitcoinClient
)
).to.be.rejectedWith(
"All active wallets in the network have the pending redemption for a given Bitcoin address. Please use another Bitcoin address."
)
})
}
)
})
})
})
Expand Down