@exodus/bitcoin-plugin 1.5.1 → 1.5.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bitcoin-plugin",
3
- "version": "1.5.1",
3
+ "version": "1.5.4",
4
4
  "description": "Exodus bitcoin-plugin",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -19,13 +19,14 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@exodus/asset-lib": "^3.7.1",
22
- "@exodus/bitcoin-api": "^2.5.1",
22
+ "@exodus/bitcoin-api": "^2.5.4",
23
23
  "@exodus/bitcoin-lib": "^2.2.8",
24
+ "@exodus/text-encoding-utf8": "^1.0.2",
24
25
  "bitcoinjs-lib": "^6.1.5",
25
26
  "minimalistic-assert": "^1.0.1"
26
27
  },
27
28
  "devDependencies": {
28
29
  "@exodus/keychain": "^3.0.0"
29
30
  },
30
- "gitHead": "562c288cde1c1d7c7ab2c7bc6b1804ccca9b37f2"
31
+ "gitHead": "a75b2bb02cea9bab6146a9a704ef2a191b2d3b49"
31
32
  }
@@ -2,6 +2,7 @@ import assert from 'minimalistic-assert'
2
2
  import { BumpType } from '@exodus/bitcoin-lib/lib/selectors/get-can-bump-tx-factory'
3
3
  import { brc20Utils } from './brc-20-utils'
4
4
  import { createBrc20Command } from './create-brc-20-command'
5
+ import { DEFAULT_BRC20_POSTAGE } from './ordinals-constants'
5
6
 
6
7
  export const brc20BalancesAndFeeAdapterFactory = ({
7
8
  inscriptionsService,
@@ -28,12 +29,14 @@ export const brc20BalancesAndFeeAdapterFactory = ({
28
29
  return !options.feeOpts.inscriptionIds?.length
29
30
  }
30
31
 
31
- const createAndBroadcastInscription = async (
32
+ const sendBrc20 = async (
32
33
  { asset: token, walletAccount, address, amount, options },
33
34
  { assetClientInterface }
34
35
  ) => {
35
36
  const { broadcastInscription, createBrc20Inscription } = inscriptionsService
36
37
 
38
+ const { customFee, isSendAll } = options
39
+
37
40
  const asset = token.baseAsset
38
41
  const accountState = await assetClientInterface.getAccountState({
39
42
  walletAccount,
@@ -42,7 +45,8 @@ export const brc20BalancesAndFeeAdapterFactory = ({
42
45
 
43
46
  const tick = await tickInscriptionIdIndexer.getBrc20TickFromAsset(token)
44
47
 
45
- const tokenAmount = options.isSendAll ? getTokenBalance({ asset: token, accountState }) : amount
48
+ const tokenAmount =
49
+ (isSendAll ? getTokenBalance({ asset: token, accountState }) : amount) || token.currency.ZERO
46
50
 
47
51
  if (tokenAmount.lte(token.currency.ZERO)) {
48
52
  throw new Error(
@@ -52,17 +56,14 @@ export const brc20BalancesAndFeeAdapterFactory = ({
52
56
  )
53
57
  }
54
58
 
55
- const brc20Command = createBrc20Command({
59
+ const { inscriptionIds, newInscriptions } = createBrc20Command({
56
60
  asset: token,
57
61
  amount: tokenAmount,
58
62
  accountState,
59
63
  })
60
64
 
61
- const inscriptionIds = brc20Command.inscriptionIds
62
-
63
- for (const newInscription of brc20Command.newInscriptions) {
64
- const customFee = asset.currency.baseUnit(20000) // why 64k is te default?
65
- const inscription = await createBrc20Inscription({
65
+ for (const newInscription of newInscriptions) {
66
+ const inscription = createBrc20Inscription({
66
67
  op: 'transfer',
67
68
  tick,
68
69
  amt: newInscription.amount.toDefaultString({ unit: false }),
@@ -96,6 +97,67 @@ export const brc20BalancesAndFeeAdapterFactory = ({
96
97
  )
97
98
  }
98
99
 
100
+ const getFeeBrc20 = ({
101
+ asset: token,
102
+ accountState,
103
+ txSet,
104
+ feeData,
105
+ amount,
106
+ customFee,
107
+ isSendAll,
108
+ }) => {
109
+ const { getInscriptionFees } = inscriptionsService
110
+
111
+ const asset = token.baseAsset
112
+
113
+ const availableBalance = getTokenBalance({ asset: token, accountState })
114
+
115
+ const tokenAmount = (isSendAll ? availableBalance : amount) || token.currency.ZERO
116
+
117
+ if (tokenAmount.lte(token.currency.ZERO) || availableBalance.lt(tokenAmount)) {
118
+ return { fee: asset.currency.ZERO }
119
+ }
120
+
121
+ const { inscriptionIds, newInscriptions } = createBrc20Command({
122
+ asset: token,
123
+ amount: tokenAmount,
124
+ accountState,
125
+ })
126
+
127
+ const newInscriptionFees = []
128
+
129
+ for (const index of Object.keys(newInscriptions)) {
130
+ const newInscriptionFee = getInscriptionFees({
131
+ asset,
132
+ getFee,
133
+ txSet,
134
+ postage: DEFAULT_BRC20_POSTAGE,
135
+ customFee,
136
+ accountState,
137
+ feeData,
138
+ })
139
+ newInscriptionFees.push(newInscriptionFee)
140
+ inscriptionIds.push(`new_inscription_${index}`) // this is just to count how many utxos needs to be send in the last txs
141
+ }
142
+
143
+ const { fee: lastSendingFee } = getFee({
144
+ asset,
145
+ accountState,
146
+ txSet,
147
+ feeData,
148
+ amount: asset.currency.ZERO,
149
+ customFee,
150
+ brc20: { inscriptionIds },
151
+ })
152
+
153
+ const fee = newInscriptionFees.reduce(
154
+ (total, newInscriptionFee) => total.add(newInscriptionFee.totalFee),
155
+ lastSendingFee
156
+ )
157
+
158
+ return { fee, lastSendingFee, newInscriptionFees }
159
+ }
160
+
99
161
  return {
100
162
  canBumpTx: (args) => {
101
163
  return isToken(args) ? { bumpType: BumpType.NONE } : canBumpTx(args)
@@ -106,7 +168,7 @@ export const brc20BalancesAndFeeAdapterFactory = ({
106
168
  },
107
169
 
108
170
  getFee: (args) => {
109
- return isToken(args) ? getFee({ ...args, asset: args.asset.baseAsset }) : getFee(args)
171
+ return isToken(args) ? getFeeBrc20(args) : getFee(args)
110
172
  },
111
173
 
112
174
  getAvailableBalance: (args) => {
@@ -118,9 +180,7 @@ export const brc20BalancesAndFeeAdapterFactory = ({
118
180
  },
119
181
 
120
182
  sendTx: (...args) => {
121
- return isToken(args[0]) && isInscription(...args)
122
- ? createAndBroadcastInscription(...args)
123
- : sendTx(...args)
183
+ return isToken(args[0]) && isInscription(...args) ? sendBrc20(...args) : sendTx(...args)
124
184
  },
125
185
  }
126
186
  }
@@ -15,28 +15,45 @@ export const brc20Utils = {
15
15
  assert(accountState, 'accountState is required')
16
16
  assert(asset, 'asset is required')
17
17
  const assetBalances = Object.values(accountState?.brc20Balances?.[asset.name] || {})
18
+ const knownInscriptionIds = brc20Utils.getKnownInscriptionIds({ asset, accountState })
18
19
  return assetBalances
19
20
  .flatMap((item) => item.transferableList)
20
- .map((item) => ({
21
- inscriptionId: item.inscriptionId,
22
- txId: item.txId,
23
- amount: asset.currency.defaultUnit(item.brc20Amt),
24
- }))
21
+ .map((item) => {
22
+ const sent = item.sent
23
+ const ordinalUtxoKnown = knownInscriptionIds.includes(item.inscriptionId)
24
+ return {
25
+ inscriptionId: item.inscriptionId,
26
+ inscriptionNumber: item.inscriptionNumber,
27
+ txId: item.txId,
28
+ amount: asset.currency.defaultUnit(item.brc20Amt),
29
+ sent: sent,
30
+ isOrdinalUtxoKnown: ordinalUtxoKnown,
31
+ valid: !sent && ordinalUtxoKnown,
32
+ }
33
+ })
34
+ .sort((a, b) => {
35
+ // valid first, then by inscription number descending
36
+ if (a.valid && !b.valid) return -1
37
+ if (!a.valid && b.valid) return 1
38
+ return b.inscriptionNumber - a.inscriptionNumber
39
+ })
25
40
  },
26
41
 
27
42
  getTransferableList: ({ asset, accountState }) => {
28
- const knownInscriptionIds = brc20Utils.getKnownInscriptionIds({ asset, accountState })
29
43
  const transferableList = brc20Utils.getAllTransferableList({ asset, accountState })
30
44
  // return the list of transferable inscription items that we hold of the utxos
31
- return transferableList.filter((item) => knownInscriptionIds.includes(item.inscriptionId))
45
+ return transferableList
46
+ .filter((item) => item.valid)
47
+ .sort((a, b) => {
48
+ // let's consume old ones first!
49
+ return a.inscriptionNumber - b.inscriptionNumber
50
+ })
32
51
  },
33
52
 
34
53
  getInTransitBalance: ({ asset, accountState }) => {
35
- const knownInscriptionIds = brc20Utils.getKnownInscriptionIds({ asset, accountState })
36
54
  const transferableList = brc20Utils.getAllTransferableList({ asset, accountState })
37
- return transferableList
38
- .filter((item) => !knownInscriptionIds.includes(item.inscriptionId))
39
- .reduce((amount, item) => amount.add(item.amount), asset.currency.ZERO)
55
+ const inTransitList = transferableList.filter((item) => !item.isOrdinalUtxoKnown && !item.sent)
56
+ return inTransitList.reduce((amount, item) => amount.add(item.amount), asset.currency.ZERO)
40
57
  },
41
58
 
42
59
  isToken: ({ asset }) => {
@@ -1,4 +1,5 @@
1
1
  import assert from 'minimalistic-assert'
2
+ import { getOrdinalsUtxos } from '@exodus/bitcoin-api'
2
3
 
3
4
  export const getInscriptionAddresses = async ({
4
5
  assetClientInterface: aci,
@@ -17,7 +18,7 @@ export const getInscriptionAddresses = async ({
17
18
 
18
19
  // all the known utxos addresses are monitored,
19
20
  // just in case the user received it to a standard btc address
20
- const allUtxos = accountState.ordinalsUtxos.union(accountState.utxos)
21
+ const allUtxos = getOrdinalsUtxos({ accountState, asset }).union(accountState.utxos)
21
22
  const addresses = allUtxos.toArray().map((utxos) => utxos.address)
22
23
  const specialAddress1 = await aci.getAddress({
23
24
  assetName,
@@ -1,6 +1,6 @@
1
1
  import assert from 'minimalistic-assert'
2
2
  import * as bitcoinjsLib from 'bitcoinjs-lib'
3
-
3
+ import { TextEncoder } from '@exodus/text-encoding-utf8'
4
4
  import ECPairFactory from 'ecpair'
5
5
  import { randomBytes } from '@exodus/crypto/randomBytes'
6
6
  import secp256k1 from 'tiny-secp256k1'
@@ -199,6 +199,11 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
199
199
 
200
200
  const resolvedReceiverWalletAccount = receiverWalletAccount || walletAccount
201
201
 
202
+ const accountState = await assetClientInterface.getAccountState({
203
+ assetName,
204
+ walletAccount: resolvedReceiverWalletAccount,
205
+ })
206
+
202
207
  const toAddress =
203
208
  receiverAddress ||
204
209
  (await assetClientInterface.getAddress({
@@ -255,12 +260,10 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
255
260
  })
256
261
 
257
262
  if (!receiverAddress && resolvedReceiverWalletAccount) {
258
- const accountState = await assetClientInterface.getAccountState({
259
- assetName,
260
- walletAccount: resolvedReceiverWalletAccount,
261
- })
262
263
  // only add the utxo
263
- const ordinalsUtxos = getOrdinalsUtxos({ accountState, asset }).addUtxo({
264
+ const storedOrdinalsUtxos = getOrdinalsUtxos({ accountState, asset })
265
+
266
+ const ordinalsUtxos = storedOrdinalsUtxos.addUtxo({
264
267
  txId: revealTx.txId,
265
268
  address: toAddress,
266
269
  vout: revealTx.vout,
@@ -282,11 +285,47 @@ export const inscriptionsServiceFactory = ({ network, ordinalChainIndex }) => {
282
285
  return { ...revealTx, ...commitTxData }
283
286
  }
284
287
 
288
+ const getInscriptionFees = ({
289
+ asset,
290
+ getFee,
291
+ txSet,
292
+ postage,
293
+ customFee,
294
+ accountState,
295
+ feeData,
296
+ }) => {
297
+ assert(asset, 'asset is required')
298
+ assert(postage, 'postage is required')
299
+ const txSize = 139 // remove hardcoded number! // simulate to get size
300
+ const feePerKB = customFee || feeData.feePerKB
301
+ const minersFee = asset.currency.baseUnit(Math.ceil((feePerKB.toBaseNumber() * txSize) / 1000))
302
+ const postageFee = asset.currency.baseUnit(postage)
303
+ // Total fees: postage + fee of sending from wallet => inscribe taproot address + fee of sending inscribe taproot address => wallet
304
+ const { fee: foundInscriptionFee } = getFee({
305
+ asset,
306
+ accountState,
307
+ txSet,
308
+ feeData,
309
+ amount: minersFee.add(postageFee),
310
+ customFee,
311
+ isSendAll: false,
312
+ receiveAddress: 'P2TR',
313
+ })
314
+
315
+ return {
316
+ totalFee: foundInscriptionFee.add(minersFee).add(postageFee),
317
+ foundInscriptionFee,
318
+ minersFee,
319
+ postageFee,
320
+ }
321
+ }
322
+
285
323
  return {
286
324
  createTextInscription,
287
325
  createBrc20Inscription,
288
326
  createCommitTxData,
289
327
  createRevealTx,
290
328
  broadcastInscription,
329
+ getInscriptionFees,
291
330
  }
292
331
  }
@@ -8,7 +8,7 @@ const toTxLog = ({ historyItem, baseAsset, asset }) => {
8
8
  const isSend = historyItem.brc20Op === 'send'
9
9
  const isReceive = historyItem.brc20Op === 'receive'
10
10
 
11
- const data = { nftId: historyItem.inscriptionId, operation: historyItem.brc20Op }
11
+ const data = { operation: historyItem.brc20Op }
12
12
  return {
13
13
  txId: historyItem.txId,
14
14
  date: new Date(historyItem.createdAt),
@@ -108,13 +108,14 @@ export const reloadBrc20TokenBalances = async ({
108
108
  .map((event) => event.inscriptionId)
109
109
 
110
110
  const transferableList = events
111
- .filter((event) => event.brc20Op === 'transfer' && !sentList.includes(event.inscriptionId))
111
+ .filter((event) => event.brc20Op === 'transfer')
112
112
  .map((event) => {
113
113
  return {
114
114
  inscriptionId: event.inscriptionId,
115
115
  inscriptionNumber: event.inscriptionNumber,
116
116
  brc20Amt: event.brc20Amt,
117
117
  txId: event.txId,
118
+ sent: sentList.includes(event.inscriptionId),
118
119
  }
119
120
  })
120
121
  return {