-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrunes.ts
613 lines (512 loc) · 19.5 KB
/
runes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
import { getRunesClient } from '../api';
import { PsbtSummary, TransactionContext, TransactionSummary } from '../transactions/bitcoin';
import { CreateEtchOrderRequest, FungibleToken, Marketplace, NetworkType, Override } from '../types';
import { BigNumber, bigUtils } from './bignumber';
export type RuneBase = {
runeName: string;
runeId: string;
amount: bigint;
divisibility: number;
symbol: string;
inscriptionId: string;
};
export type RuneMint = RuneBase & {
runeIsOpen: boolean;
runeIsMintable: boolean;
};
export type RuneTransfer = RuneBase & {
sourceAddress: string;
destinationAddresses: string[];
hasSufficientBalance: boolean;
};
export type RuneReceipt = RuneBase & {
sourceAddresses: string[];
destinationAddress: string;
};
export type RuneBurn = RuneBase & { sourceAddresses: string[] };
export type RuneSummaryParseOptions = { separateTransfersOnNoExternalInputs?: boolean };
export type RuneSummary = {
inputsHadRunes: boolean;
// can only do 1 mint per txn
mint?: RuneMint;
transfers: RuneTransfer[];
receipts: RuneReceipt[];
burns: RuneBurn[];
outputMapping: Record<number, (RuneBase & { destinationAddress: string })[]>;
};
export type EtchActionDetails = Omit<
CreateEtchOrderRequest,
'appServiceFee' | 'appServiceFeeAddress' | 'refundAddress'
>;
export type MintActionDetails = RuneMint & {
repeats: number;
runeSize: number;
destinationAddress: string;
};
/**
* RuneSummaryActions is a RuneSummary with the mint and etch properties extended
* with ordinals service specific properties.
* for usage with the tx confirmations and etch/mint screens
*/
export type RuneSummaryActions = Override<
RuneSummary,
{
mint: MintActionDetails;
etch: EtchActionDetails;
}
>;
const getSpacedName = (name: string, spacerRaw: bigint | BigNumber): string => {
const spacer = BigInt(spacerRaw.toString(10));
if (spacer === 0n) {
return name;
}
const nameArr = [...name];
const spacerBin = spacer.toString(2).split('').reverse();
while (spacerBin.length) {
const isSpace = spacerBin.pop();
if (isSpace === '0') {
continue;
}
nameArr.splice(spacerBin.length + 1, 0, '•');
}
return nameArr.join('');
};
const extractRuneInputs = async (context: TransactionContext, summary: TransactionSummary | PsbtSummary) => {
const userAddresses = new Set([context.paymentAddress.address, context.ordinalsAddress.address]);
const inputRuneData = await Promise.all(
summary.inputs.map(async (input) => {
const hasRunes = await input.extendedUtxo.hasRunes();
const balances = await input.extendedUtxo.getRuneBalances();
return {
input,
hasRunes,
balances,
isUserAddress: userAddresses.has(input.extendedUtxo.address),
};
}),
);
return inputRuneData.filter((input) => input.hasRunes);
};
const parseSummaryWithBurnRuneScript = async (
context: TransactionContext,
summary: TransactionSummary | PsbtSummary,
network: NetworkType,
): Promise<RuneSummary> => {
const runeClient = getRunesClient(network);
const runeInputs = await extractRuneInputs(context, summary);
const inputsHadRunes = runeInputs.length > 0;
const burns = runeInputs.reduce((acc, input) => {
const inputAddress = input.input.extendedUtxo.address;
const inputBalances = input.balances;
for (const runeName in inputBalances) {
const addressRuneKey = `${inputAddress}:${runeName}`;
if (acc[addressRuneKey] === undefined) {
acc[addressRuneKey] = {
runeName,
amount: BigInt(inputBalances[runeName].toString()),
sourceAddresses: [inputAddress],
};
} else {
acc[addressRuneKey].amount += BigInt(inputBalances[runeName].toString());
}
}
return acc;
}, {} as Record<string, Omit<RuneBurn, 'runeId' | 'divisibility' | 'symbol' | 'inscriptionId'>>);
const embellishedBurns: RuneBurn[] = [];
for (const burn of Object.values(burns)) {
const runeInfo = await runeClient.getRuneInfo(burn.runeName);
embellishedBurns.push({
...burn,
runeId: runeInfo?.id || '',
divisibility: runeInfo?.entry.divisibility.toNumber() || 0,
symbol: runeInfo?.entry.symbol || '',
inscriptionId: runeInfo?.parent || '',
});
}
return {
inputsHadRunes,
receipts: [],
transfers: [],
burns: embellishedBurns,
outputMapping: {},
};
};
const parseSummaryWithRuneScript = async (
context: TransactionContext,
summary: TransactionSummary | PsbtSummary,
network: NetworkType,
): Promise<RuneSummary> => {
const runeOp = summary.runeOp;
const runeClient = getRunesClient(network);
const runeInputs = await extractRuneInputs(context, summary);
const userAddresses = new Set([context.paymentAddress.address, context.ordinalsAddress.address]);
const inputsHadRunes = runeInputs.filter((r) => r.isUserAddress).length > 0;
const burns: RuneBurn[] = [];
// calculate initial unallocated balance without mint
const unallocatedBalance = runeInputs.reduce((acc, input) => {
for (const runeName in input.balances) {
const amount = BigInt(input.balances[runeName].toString());
const sourceAddress = input.input.extendedUtxo.address;
if (runeName in acc) {
acc[runeName].amount += amount;
acc[runeName].sourceAddresses.add(sourceAddress);
} else {
acc[runeName] = {
amount,
sourceAddresses: new Set([sourceAddress]),
};
}
}
return acc;
}, {} as Record<string, { amount: bigint; sourceAddresses: Set<string> }>);
// parse mint and add to unallocated balance if valid
let mint: RuneMint | undefined = undefined;
if (runeOp?.Runestone?.mint) {
const runeInfo = await runeClient.getRuneInfo(runeOp.Runestone.mint);
if (runeInfo && runeInfo.entry.terms.cap) {
const runeName = runeInfo.entry.spaced_rune || '';
const mintAmount = BigInt(runeInfo.entry.terms.amount?.toString(10) ?? 0);
const runeIsOpen = !!(runeInfo.entry.terms.amount && runeInfo.entry.terms.cap);
mint = {
runeName,
runeId: runeInfo.id,
amount: mintAmount,
runeIsOpen,
runeIsMintable: runeInfo.mintable,
divisibility: runeInfo.entry.divisibility.toNumber(),
symbol: runeInfo.entry.symbol || '¤',
inscriptionId: runeInfo.parent || '',
};
if (runeInfo.mintable) {
if (runeName in unallocatedBalance) {
unallocatedBalance[runeName].amount += mintAmount;
unallocatedBalance[runeName].sourceAddresses.add('mint');
} else {
unallocatedBalance[runeName] = {
amount: mintAmount,
sourceAddresses: new Set(['mint']),
};
}
}
} else {
// rune being minted does not exist or is not open
mint = {
runeName: runeInfo?.entry.spaced_rune || '',
runeId: runeInfo?.id || '',
amount: 0n,
divisibility: 0,
symbol: runeInfo?.entry.symbol || '',
inscriptionId: runeInfo?.parent || '',
runeIsOpen: false,
runeIsMintable: false,
};
}
}
// start compiling transfers and receipts
type PartialTransfer = Omit<
RuneTransfer,
'runeId' | 'hasSufficientBalance' | 'destinationAddresses' | 'divisibility' | 'symbol' | 'inscriptionId'
>;
const transfersByRuneAndAddress = runeInputs
.filter((r) => r.isUserAddress)
.reduce((acc, input) => {
const inputAddress = input.input.extendedUtxo.address;
for (const runeName in input.balances) {
const balance = BigInt(input.balances[runeName].toString());
acc[runeName] ||= {};
if (!(inputAddress in acc[runeName])) {
acc[runeName][inputAddress] = {
sourceAddress: inputAddress,
runeName,
amount: balance,
};
} else {
acc[runeName][inputAddress].amount += balance;
}
}
return acc;
}, {} as Record<string, Record<string, PartialTransfer>>);
// process edicts into receipts if not burnt on script
const allocated = {} as Record<
number,
Record<string, { amount: bigint; sourceAddresses: string[]; destinationAddress: string }>
>;
for (const edict of runeOp?.Runestone?.edicts ?? []) {
let runeName = '';
if (edict.id === '0:0') {
runeName = getSpacedName(runeOp?.Runestone?.etching?.rune || '', runeOp?.Runestone?.etching?.spacers || 0n);
} else {
const runeInfo = await runeClient.getRuneInfo(edict.id);
if (runeInfo?.entry.spaced_rune) {
runeName = runeInfo?.entry.spaced_rune;
}
}
const amount = BigInt(edict.amount.toString(10));
if (!(runeName in unallocatedBalance) || unallocatedBalance[runeName].amount <= 0n) {
// there are no runes available for the transfer, decrease the unallocated balance and continue to next edict
// we decrease the unallocated balance to pick up insufficient balance errors
unallocatedBalance[runeName] = unallocatedBalance[runeName] ?? { amount: 0n, sourceAddresses: new Set() };
unallocatedBalance[runeName].amount -= amount;
continue;
}
const outputIndex = Number(edict.output.toString(10));
const sourceAddresses = [...unallocatedBalance[runeName].sourceAddresses];
if (outputIndex < summary.outputs.length) {
const amountToTransfer =
// if amount is 0, transfer entire unallocated balance
amount === 0n
? unallocatedBalance[runeName].amount
: // else transfer min of amount and unallocated balance
BigInt(bigUtils.min(amount, unallocatedBalance[runeName].amount).toString(10));
// this could overflow to negative, which is fine and will show that there are not enough funds
unallocatedBalance[runeName].amount -= amount === 0n ? unallocatedBalance[runeName].amount : amount;
if (amountToTransfer < 0n) {
// we've already hit insufficient balance
continue;
}
const output = summary.outputs[outputIndex];
if (output.type !== 'address') {
const runeInfo = await runeClient.getRuneInfo(runeName);
if (!runeInfo) {
throw new Error('Cannot find rune info for transfer');
}
burns.push({
runeName,
runeId: runeInfo.id,
amount: amountToTransfer,
sourceAddresses: sourceAddresses.filter((a) => a !== 'mint'),
divisibility: runeInfo.entry.divisibility.toNumber(),
symbol: runeInfo.entry.symbol,
inscriptionId: runeInfo.parent || '',
});
continue;
}
// assign to specific output
if (allocated[outputIndex] === undefined) {
allocated[outputIndex] = {};
}
if (runeName in allocated[outputIndex]) {
allocated[outputIndex][runeName].amount += amountToTransfer;
} else {
allocated[outputIndex][runeName] = {
amount: amountToTransfer,
sourceAddresses,
destinationAddress: output.address,
};
}
} else if (outputIndex === summary.outputs.length) {
// distribute between outputs
let availableAmount = unallocatedBalance[runeName].amount;
if (availableAmount <= 0n) {
// no balance to distribute
continue;
}
const nonOpReturnOutputIndexes = summary.outputs
.map((output, index) => ({ output, index }))
.filter((o) => o.output.type !== 'script');
const amountPerOutput = amount === 0n ? availableAmount / BigInt(nonOpReturnOutputIndexes.length) : amount;
let remainder = amount === 0n ? availableAmount % BigInt(nonOpReturnOutputIndexes.length) : 0n;
for (const output of nonOpReturnOutputIndexes) {
if (output.output.type !== 'address') {
throw new Error('Something went wrong. Output should be an address.');
}
if (availableAmount <= 0n) {
break;
}
let outputAmount = BigInt(bigUtils.min(amountPerOutput, availableAmount).toString(10));
if (remainder > 0n) {
outputAmount += 1n;
remainder -= 1n;
}
unallocatedBalance[runeName].amount -= outputAmount;
availableAmount -= outputAmount;
if (allocated[output.index] === undefined) {
allocated[output.index] = {};
}
if (runeName in allocated[output.index]) {
allocated[output.index][runeName].amount += outputAmount;
} else {
allocated[output.index][runeName] = {
amount: outputAmount,
sourceAddresses,
destinationAddress: output.output.address,
};
}
}
} else {
throw new Error('Invalid runeOp. Entire Rune op should be burnt.');
}
}
// allocate unallocated
let changeOutputIndex: number | undefined = undefined;
if (
runeOp?.Runestone?.pointer === undefined ||
runeOp?.Runestone?.pointer === null ||
runeOp.Runestone.pointer.gte(summary.outputs.length)
) {
let index = 0;
for (const output of summary.outputs) {
if (output.type === 'address') {
changeOutputIndex = Number(index);
break;
}
index++;
}
} else {
changeOutputIndex = Number(runeOp.Runestone.pointer.toString(10));
}
const changeOutput = changeOutputIndex !== undefined ? summary.outputs[Number(changeOutputIndex)] : undefined;
if (changeOutputIndex !== undefined && changeOutput?.type === 'address') {
// if there is a default change output, allocate unallocated balance to change output
for (const runeName in unallocatedBalance) {
const amount = unallocatedBalance[runeName].amount;
const sourceAddresses = [...unallocatedBalance[runeName].sourceAddresses];
if (amount > 0n) {
unallocatedBalance[runeName].amount -= amount;
if (allocated[changeOutputIndex] === undefined) {
allocated[changeOutputIndex] = {};
}
if (runeName in allocated[changeOutputIndex]) {
allocated[changeOutputIndex][runeName].amount += amount;
} else {
allocated[changeOutputIndex][runeName] = {
amount,
sourceAddresses,
destinationAddress: changeOutput.address,
};
}
}
}
} else {
// if selected change output is an op_return, runes get burnt
for (const runeName in unallocatedBalance) {
const amount = unallocatedBalance[runeName].amount;
const sourceAddresses = [...unallocatedBalance[runeName].sourceAddresses];
if (amount > 0n) {
unallocatedBalance[runeName].amount -= amount;
// only track burns for user addresses
if (sourceAddresses.some((address) => userAddresses.has(address))) {
const runeInfo = await runeClient.getRuneInfo(runeName);
if (!runeInfo) {
throw new Error('Cannot find rune info for transfer');
}
burns.push({
runeName,
amount,
runeId: runeInfo.id,
sourceAddresses: sourceAddresses.filter((a) => a !== 'mint'),
divisibility: runeInfo.entry.divisibility.toNumber(),
symbol: runeInfo.entry.symbol,
inscriptionId: runeInfo.parent || '',
});
}
}
}
}
// generate receipts and add destination addresses
const receipts: RuneReceipt[] = [];
const runeRecipients: Record<string, string[]> = {};
const userReceiptRuneAmounts = {} as Record<string, Record<string, bigint>>;
const outputMapping: Record<number, (RuneBase & { destinationAddress: string })[]> = {};
for (const i in allocated) {
const index = Number(i);
const summaryOutput = summary.outputs[index];
if (summaryOutput.type === 'script') {
throw new Error('Something went wrong while parsing the runeOp. All allocated outputs should be non-script.');
}
const output = allocated[index];
outputMapping[index] = [];
for (const runeName in output) {
const amount = output[runeName].amount;
const sourceAddresses = output[runeName].sourceAddresses;
const destinationAddress = output[runeName].destinationAddress;
runeRecipients[runeName] ||= [];
runeRecipients[runeName].push(destinationAddress);
const runeInfo = await runeClient.getRuneInfo(runeName);
outputMapping[index].push({
runeName,
amount,
destinationAddress,
divisibility: runeInfo?.entry.divisibility.toNumber() || 0,
runeId: runeInfo?.id || '',
symbol: runeInfo?.entry.symbol || '',
inscriptionId: runeInfo?.parent || '',
});
if (userAddresses.has(destinationAddress)) {
// we only show receipts if we are sending to another address or if we transfer between payment and ordinals
// otherwise, transfers are just change and there is no need to show them
if (sourceAddresses.length !== 1 || sourceAddresses[0] !== destinationAddress) {
receipts.push({
sourceAddresses: sourceAddresses.filter((a) => a !== 'mint'),
destinationAddress,
runeName,
amount,
runeId: runeInfo?.id || '',
divisibility: runeInfo?.entry.divisibility.toNumber() || 0,
symbol: runeInfo?.entry.symbol || '',
inscriptionId: runeInfo?.parent || '',
});
}
userReceiptRuneAmounts[destinationAddress] = userReceiptRuneAmounts[destinationAddress] ?? {};
userReceiptRuneAmounts[destinationAddress][runeName] =
userReceiptRuneAmounts[destinationAddress][runeName] || 0n;
userReceiptRuneAmounts[destinationAddress][runeName] += amount;
}
}
}
// calculate hasSufficientBalance to transfers from receipts and unallocatedBalance
const burnt = burns.reduce((acc, burn) => {
acc[burn.runeName] = acc[burn.runeName] || 0n;
acc[burn.runeName] += burn.amount;
return acc;
}, {} as Record<string, bigint>);
const transfers: RuneTransfer[] = [];
for (const [runeName, addressTransfers] of Object.entries(transfersByRuneAndAddress)) {
for (const [sourceAddress, transfer] of Object.entries(addressTransfers)) {
const runeInfo = await runeClient.getRuneInfo(runeName);
const amount = BigInt(
transfer.amount - (userReceiptRuneAmounts[sourceAddress]?.[runeName] ?? 0n) - (burnt[runeName] ?? 0n),
);
if (amount <= 0n) {
continue;
}
transfers.push({
sourceAddress,
destinationAddresses: runeRecipients[runeName].filter((a) => a !== sourceAddress) || [],
runeName,
amount,
runeId: runeInfo?.id || '',
divisibility: runeInfo?.entry.divisibility.toNumber() || 0,
symbol: runeInfo?.entry.symbol || '',
inscriptionId: runeInfo?.parent || '',
hasSufficientBalance: unallocatedBalance[runeName].amount >= 0n,
});
}
}
return {
inputsHadRunes,
mint,
receipts,
transfers,
burns,
outputMapping,
};
};
export const parseSummaryForRunes = async (
context: TransactionContext,
summary: TransactionSummary | PsbtSummary,
network: NetworkType,
): Promise<RuneSummary> => {
if ((summary.runeOp?.Cenotaph?.flaws ?? 0) > 0) {
return parseSummaryWithBurnRuneScript(context, summary, network);
}
return parseSummaryWithRuneScript(context, summary, network);
};
export const marketplaceRuneDashboardUrl = (rune: FungibleToken, marketplace: Marketplace): string => {
const marketplaceToUrl: { [key in Marketplace]: string } = {
'Magic Eden': `https://magiceden.io/runes/${rune.name}`,
Unisat: `https://unisat.io/runes/market?tick=${rune.name}`,
OKX: `https://www.okx.com/web3/marketplace/runes/token/${rune.name}/${rune.principal}`,
};
return marketplaceToUrl[marketplace];
};