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 all commits
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
23 changes: 19 additions & 4 deletions typescript/src/redemption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,12 @@ export async function getRedemptionRequest(
}

/**
* Finds the oldest active wallet that has enough BTC to handle a redemption request.
* Finds the oldest live wallet that has enough BTC to handle a redemption
* request.
* @param amount The amount to be redeemed in satoshis.
* @param redeemerOutputScript The redeemer output script the redeemed funds
* are supposed to be locked on. Must be un-prefixed and not prepended
* with length.
* @param redeemerOutputScript The redeemer output script the redeemed funds are
* supposed to be locked on. Must be un-prefixed and not prepended with
* length.
* @param bitcoinNetwork Bitcoin network.
* @param bridge The handle to the Bridge on-chain contract.
* @param bitcoinClient Bitcoin client used to interact with the network.
Expand All @@ -441,6 +442,7 @@ export async function findWalletForRedemption(
}
| undefined = undefined
let maxAmount = BigNumber.from(0)
let liveWalletsCounter = 0

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

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

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

// Cover a corner case when the user requested redemption for all live wallets
// in the network using the same Bitcoin address.
if (!walletData && liveWalletsCounter > 0 && maxAmount.eq(0)) {
throw new Error(
"All live 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 live 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 live wallets in the network have the pending redemption for a given Bitcoin address. Please use another Bitcoin address."
)
})
}
)
})
})
})
Expand Down