Skip to content

Commit

Permalink
Update findWalletForRedemption function
Browse files Browse the repository at this point in the history
Take into account pending redemptions value- say Say the wallet has `100
BTC`, redemptions are processed every 3 hours, and so far, requests for
`93 BTC` has been registered in the Bridge. Based on the previous
version of the code, that wallet would still be selected for all
redemptions `<= 100 BTC` and the transaction to Ethereum requesting
redemption higher than `7 BTC` would fail. This commit fixes that issue.
  • Loading branch information
r-czajkowski committed Jul 10, 2023
1 parent b86e251 commit 9a0f289
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
11 changes: 6 additions & 5 deletions typescript/src/redemption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,8 @@ export async function findWalletForRedemption(

for (const wallet of wallets) {
const { walletPublicKeyHash } = wallet
const { state, mainUtxoHash, walletPublicKey } = await bridge.wallets(
walletPublicKeyHash
)
const { state, mainUtxoHash, walletPublicKey, pendingRedemptionsValue } =
await bridge.wallets(walletPublicKeyHash)

// Wallet must be in Live state.
if (state !== WalletState.Live) {
Expand Down Expand Up @@ -514,10 +513,12 @@ export async function findWalletForRedemption(
continue
}

const walletBTCBalance = mainUtxo.value.sub(pendingRedemptionsValue)

// Save the max possible redemption amount.
maxAmount = mainUtxo.value.gt(maxAmount) ? mainUtxo.value : maxAmount
maxAmount = walletBTCBalance.gt(maxAmount) ? walletBTCBalance : maxAmount

if (mainUtxo.value.gte(amount)) {
if (walletBTCBalance.gte(amount)) {
walletData = {
walletPublicKey: walletPublicKey.toString(),
mainUtxo,
Expand Down
9 changes: 6 additions & 3 deletions typescript/test/data/redemption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ interface FindWalletForRedemptionWaleltData {
walletPublicKey: Hex
btcAddress: string
utxos: UnspentTransactionOutput[]
pendingRedemptionsValue: BigNumber
}
event: {
blockNumber: number
Expand Down Expand Up @@ -711,6 +712,7 @@ export const findWalletForRedemptionData: {
value: BigNumber.from("791613461"),
},
],
pendingRedemptionsValue: BigNumber.from(0),
},
event: {
blockNumber: 8367602,
Expand All @@ -728,7 +730,6 @@ export const findWalletForRedemptionData: {
),
},
},

walletWithoutUtxo: {
data: {
state: WalletState.Live,
Expand All @@ -748,6 +749,7 @@ export const findWalletForRedemptionData: {
value: BigNumber.from("0"),
},
],
pendingRedemptionsValue: BigNumber.from(0),
},
event: {
blockNumber: 9103428,
Expand Down Expand Up @@ -785,6 +787,7 @@ export const findWalletForRedemptionData: {
value: BigNumber.from("0"),
},
],
pendingRedemptionsValue: BigNumber.from(0),
},
event: {
blockNumber: 9171960,
Expand All @@ -802,7 +805,6 @@ export const findWalletForRedemptionData: {
),
},
},

walletWithPendingRedemption: {
data: {
state: WalletState.Live,
Expand All @@ -819,9 +821,10 @@ export const findWalletForRedemptionData: {
"0x81c4884a8c2fccbeb57745a5e59f895a9c1bb8fc42eecc82269100a1a46bbb85"
),
outputIndex: 0,
value: BigNumber.from("3370000"),
value: BigNumber.from("3370000"), // 0.0337 BTC
},
],
pendingRedemptionsValue: BigNumber.from(2370000), // 0.0237 BTC
},
event: {
blockNumber: 8981644,
Expand Down
46 changes: 44 additions & 2 deletions typescript/test/redemption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1494,8 +1494,14 @@ describe("Redemption", () => {
>()

walletsOrder.forEach((wallet) => {
const { state, mainUtxoHash, walletPublicKey, btcAddress, utxos } =
wallet.data
const {
state,
mainUtxoHash,
walletPublicKey,
btcAddress,
utxos,
pendingRedemptionsValue,
} = wallet.data

walletsUnspentTransacionOutputs.set(btcAddress, utxos)
bridge.setWallet(
Expand All @@ -1504,6 +1510,7 @@ describe("Redemption", () => {
state,
mainUtxoHash,
walletPublicKey,
pendingRedemptionsValue,
} as Wallet
)
})
Expand Down Expand Up @@ -1660,6 +1667,41 @@ describe("Redemption", () => {
})
}
)

context(
"when wallet has pending redemptions and the requested amount is greater than possible",
() => {
beforeEach(async () => {
const wallet =
findWalletForRedemptionData.walletWithPendingRedemption
const walletBTCBalance = wallet.data.utxos[0].value

const amount: BigNumber = walletBTCBalance
.sub(wallet.data.pendingRedemptionsValue)
.add(BigNumber.from(500000)) // 0.005 BTC

console.log("amount", amount.toString())

result = await findWalletForRedemption(
amount,
redeemerOutputScript,
BitcoinNetwork.Testnet,
bridge,
bitcoinClient
)
})

it("should skip the wallet wallet with pending redemptions and return the wallet data that can handle redemption request ", () => {
const expectedWalletData =
findWalletForRedemptionData.liveWallet.data

expect(result).to.deep.eq({
walletPublicKey: expectedWalletData.walletPublicKey.toString(),
mainUtxo: expectedWalletData.utxos[0],
})
})
}
)
})
})
})
Expand Down

0 comments on commit 9a0f289

Please sign in to comment.