Skip to content

Commit

Permalink
Improve errors in findWalletForRedemption function (#652)
Browse files Browse the repository at this point in the history
Throw error when currently, there are no active wallets in the network
and when the user requested redemption for all active wallets in the
network using the same Bitcoin address - in that case, a user should use
another Bitcoin address.
  • Loading branch information
lukasz-zimnoch committed Jul 13, 2023
2 parents 55e2735 + 5f6b241 commit f987652
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
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

0 comments on commit f987652

Please sign in to comment.