@teleportdao/bitcoin 4.0.3 → 4.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ import type {
5
5
  TargetAddress,
6
6
  } from "./transaction-builder/transaction-builder"
7
7
  import { BitcoinBaseWallet, FeeRateType } from "./bitcoin-wallet-base"
8
- import { generateWrapOpReturn } from "./helper/teleswap-helper"
8
+ import { generateWrapOpReturn, generateWrapOpReturnV2 } from "./helper/teleswap-helper"
9
9
 
10
10
  export type TransferRequest = {
11
11
  changeAddress?: string
@@ -134,5 +134,71 @@ export class TeleswapWallet extends BitcoinBaseWallet {
134
134
 
135
135
  return unsignedTx
136
136
  }
137
+
138
+ async wrapUnsignedV2(
139
+ recipientAddress: string,
140
+ amount: string,
141
+ networkFee: string,
142
+ lockerAddress: string,
143
+ chainId: number, // 80001
144
+ signer: SignerInfo,
145
+ exchange?: {
146
+ outputToken: string
147
+ minOutputAmount: string
148
+ bridgePercentageFee?: number
149
+ },
150
+ appId = exchange
151
+ ? teleswap.requestAppId.WrapAndSwap.default
152
+ : teleswap.requestAppId.Wrap.default,
153
+ speed = false,
154
+ fee: FeeRateType = "normal",
155
+ thirdPartyId?: number,
156
+ staticExtendedUtxo?: ExtendedUtxo[],
157
+ changeAddress?: string,
158
+ fullAmount = false,
159
+ ) {
160
+ // split last 8 bytes of outputToken to get outputTokenId
161
+
162
+ const outputTokenId = exchange?.outputToken
163
+ ? exchange.outputToken.replace("0x", "").slice(-16)
164
+ : undefined
165
+ const dataHex = generateWrapOpReturnV2({
166
+ chainId,
167
+ appId,
168
+ recipientAddress,
169
+ networkFee,
170
+ speed,
171
+ isExchange: !!exchange,
172
+ minOutputAmount: exchange?.minOutputAmount,
173
+ minIntermediaryTokenAmount: "0",
174
+ outputTokenId,
175
+ bridgePercentageFee: exchange?.bridgePercentageFee,
176
+ thirdPartyId,
177
+ })
178
+ let extendedUtxo = staticExtendedUtxo || (await this.getExtendedUtxo(signer))
179
+ let feeRate = await this.getFeeRate(fee)
180
+
181
+ const targets = fullAmount
182
+ ? [this.transactionBuilder.getOpReturnTarget(dataHex)]
183
+ : [
184
+ {
185
+ address: lockerAddress,
186
+ value: +amount,
187
+ },
188
+ this.transactionBuilder.getOpReturnTarget(dataHex),
189
+ ]
190
+
191
+ const sequenceNumber = speed === true ? this.transactionBuilder.NO_RBF_SEQUENCE : undefined
192
+ const unsignedTx = this.transactionBuilder.processUnsignedTransaction({
193
+ extendedUtxo,
194
+ feeRate,
195
+ targets,
196
+ changeAddress: fullAmount ? lockerAddress : changeAddress || signer.address,
197
+ fullAmount,
198
+ sequenceNumber,
199
+ })
200
+
201
+ return unsignedTx
202
+ }
137
203
  }
138
204
  export default TeleswapWallet
@@ -257,10 +257,6 @@ export abstract class BaseTransactionBuilder {
257
257
  }
258
258
 
259
259
  let diff = fee - txFee
260
-
261
- if (Math.abs(diff) >= DUST) {
262
- console.log("diff", diff, "fee", fee, "txFee", txFee)
263
- }
264
260
  let changeIndex = outputs.findIndex((x) => !x?.address && !x.script && (x.value || 0) > 0)
265
261
  let change: ChangeTarget | undefined
266
262
  if (diff > 0) {
@@ -312,9 +308,7 @@ export abstract class BaseTransactionBuilder {
312
308
  }
313
309
 
314
310
  if (Math.abs(fee - newFee) > DUST) {
315
- console.log("fee", fee)
316
- console.log("newFee", newFee)
317
- throw new Error("fee mismatch")
311
+ throw new Error(`fee mismatch: ${JSON.stringify({ fee, newFee })}`)
318
312
  }
319
313
 
320
314
  // check outputs
@@ -322,32 +316,38 @@ export abstract class BaseTransactionBuilder {
322
316
  let totalNewOutputValue = newOutputs.reduce((a, b) => a + b.value, 0)
323
317
 
324
318
  if (totalOutputValue - totalNewOutputValue !== 0) {
325
- console.log("totalOutputValue", totalOutputValue)
326
- console.log("totalNewOutputValue", totalNewOutputValue)
327
- console.log("diff", totalOutputValue - totalNewOutputValue)
328
- throw new Error("output value mismatch")
319
+ throw new Error(
320
+ `output value mismatch${JSON.stringify({ totalOutputValue, totalNewOutputValue })}`,
321
+ )
329
322
  }
330
323
 
331
324
  if (outputs.length !== newOutputs.length) {
332
- console.log("outputs", outputs)
333
- console.log("newOutputs", newOutputs)
334
- throw new Error("output length mismatch")
325
+ throw new Error(`output length mismatch: ${JSON.stringify({ outputs, newOutputs })}`)
335
326
  }
336
327
  for (let i = 0; i < outputs.length; i += 1) {
337
328
  if (outputs[i].value !== newOutputs[i].value) {
338
- console.log("outputs[i].value", outputs[i].value)
339
- console.log("newOutputs[i].value", newOutputs[i].value)
340
- throw new Error("output value mismatch")
329
+ throw new Error(
330
+ `output value mismatch: ${JSON.stringify({
331
+ old: outputs[i].value,
332
+ new: newOutputs[i].value,
333
+ })}`,
334
+ )
341
335
  }
342
336
  if (outputs[i].address !== newOutputs[i].address) {
343
- console.log("outputs[i].address", outputs[i].address)
344
- console.log("newOutputs[i].address", newOutputs[i].address)
345
- throw new Error("output address mismatch")
337
+ throw new Error(
338
+ `output address mismatch: ${JSON.stringify({
339
+ old: outputs[i].address,
340
+ new: newOutputs[i].address,
341
+ })}`,
342
+ )
346
343
  }
347
344
  if (outputs[i].script !== newOutputs[i].script) {
348
- console.log("outputs[i].script", outputs[i].script)
349
- console.log("newOutputs[i].script", newOutputs[i].script)
350
- throw new Error("output script mismatch")
345
+ throw new Error(
346
+ `output script mismatch: ${JSON.stringify({
347
+ old: outputs[i].script,
348
+ new: newOutputs[i].script,
349
+ })}`,
350
+ )
351
351
  }
352
352
  }
353
353
 
@@ -360,8 +360,14 @@ export abstract class BaseTransactionBuilder {
360
360
  totalInputValue - (change?.value || 0) - (totalNewInputValue - (newChange?.value || 0)),
361
361
  ) > DUST
362
362
  ) {
363
- console.log("totalInputValue", totalInputValue)
364
- throw new Error("input value mismatch")
363
+ throw new Error(
364
+ `input value mismatch: ${JSON.stringify({
365
+ totalInputValue,
366
+ totalNewInputValue,
367
+ changeValue: change?.value,
368
+ newChangeValue: newChange?.value,
369
+ })}`,
370
+ )
365
371
  }
366
372
 
367
373
  return { inputs, outputs, change, fee }
@@ -25,7 +25,6 @@ async function runWithRetries<T>(
25
25
  return await action()
26
26
  } catch (error: any) {
27
27
  error.message += `\n${trace}`
28
- console.log(`Attempt ${count + 1} failed: ${error.message}`)
29
28
  lastError = error
30
29
  if (count < maxTries - 1) {
31
30
  await sleep(retrySleep)