@kaspacom/kcc20-tx-builder 0.1.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.
- package/LICENSE +201 -0
- package/README.md +104 -0
- package/artifacts/KCC20.placeholder.json +77479 -0
- package/artifacts/KCC20FeeTicket.placeholder.json +16846 -0
- package/artifacts/KCC20Orderbook.placeholder.json +78001 -0
- package/artifacts/KCC20Vesting.placeholder.json +4298 -0
- package/artifacts/KCC20Wrapper.placeholder.json +12246 -0
- package/dist/abi.d.ts +44 -0
- package/dist/abi.d.ts.map +1 -0
- package/dist/abi.js +336 -0
- package/dist/abi.js.map +1 -0
- package/dist/action-source-resolver.d.ts +80 -0
- package/dist/action-source-resolver.d.ts.map +1 -0
- package/dist/action-source-resolver.js +412 -0
- package/dist/action-source-resolver.js.map +1 -0
- package/dist/artifacts.d.ts +14 -0
- package/dist/artifacts.d.ts.map +1 -0
- package/dist/artifacts.js +44 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/builder.d.ts +12 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +18 -0
- package/dist/builder.js.map +1 -0
- package/dist/cjs/abi.d.ts +43 -0
- package/dist/cjs/abi.js +361 -0
- package/dist/cjs/abi.js.map +1 -0
- package/dist/cjs/action-source-resolver.d.ts +79 -0
- package/dist/cjs/action-source-resolver.js +435 -0
- package/dist/cjs/action-source-resolver.js.map +1 -0
- package/dist/cjs/artifacts.d.ts +13 -0
- package/dist/cjs/artifacts.js +51 -0
- package/dist/cjs/artifacts.js.map +1 -0
- package/dist/cjs/builder.d.ts +11 -0
- package/dist/cjs/builder.js +22 -0
- package/dist/cjs/builder.js.map +1 -0
- package/dist/cjs/deploy-operation.d.ts +103 -0
- package/dist/cjs/deploy-operation.js +335 -0
- package/dist/cjs/deploy-operation.js.map +1 -0
- package/dist/cjs/encoding.d.ts +5 -0
- package/dist/cjs/encoding.js +51 -0
- package/dist/cjs/encoding.js.map +1 -0
- package/dist/cjs/engine.d.ts +40 -0
- package/dist/cjs/engine.js +8972 -0
- package/dist/cjs/engine.js.map +1 -0
- package/dist/cjs/errors.d.ts +7 -0
- package/dist/cjs/errors.js +23 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/fee-ticket-operation.d.ts +204 -0
- package/dist/cjs/fee-ticket-operation.js +327 -0
- package/dist/cjs/fee-ticket-operation.js.map +1 -0
- package/dist/cjs/index.d.ts +27 -0
- package/dist/cjs/index.js +45 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/ladder-limit-orders.d.ts +41 -0
- package/dist/cjs/ladder-limit-orders.js +129 -0
- package/dist/cjs/ladder-limit-orders.js.map +1 -0
- package/dist/cjs/mint-availability-operation.d.ts +56 -0
- package/dist/cjs/mint-availability-operation.js +95 -0
- package/dist/cjs/mint-availability-operation.js.map +1 -0
- package/dist/cjs/models.d.ts +90 -0
- package/dist/cjs/models.js +6 -0
- package/dist/cjs/models.js.map +1 -0
- package/dist/cjs/operations.d.ts +6 -0
- package/dist/cjs/operations.js +41 -0
- package/dist/cjs/operations.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/paid-mint-amount.d.ts +69 -0
- package/dist/cjs/paid-mint-amount.js +281 -0
- package/dist/cjs/paid-mint-amount.js.map +1 -0
- package/dist/cjs/protocol.d.ts +21 -0
- package/dist/cjs/protocol.js +25 -0
- package/dist/cjs/protocol.js.map +1 -0
- package/dist/cjs/runtime.d.ts +27 -0
- package/dist/cjs/runtime.js +3 -0
- package/dist/cjs/runtime.js.map +1 -0
- package/dist/cjs/sale-common.d.ts +287 -0
- package/dist/cjs/sale-common.js +928 -0
- package/dist/cjs/sale-common.js.map +1 -0
- package/dist/cjs/source-provider.d.ts +11 -0
- package/dist/cjs/source-provider.js +3 -0
- package/dist/cjs/source-provider.js.map +1 -0
- package/dist/cjs/source-validation.d.ts +9 -0
- package/dist/cjs/source-validation.js +33 -0
- package/dist/cjs/source-validation.js.map +1 -0
- package/dist/cjs/token-amount.d.ts +31 -0
- package/dist/cjs/token-amount.js +252 -0
- package/dist/cjs/token-amount.js.map +1 -0
- package/dist/cjs/token-operation.d.ts +135 -0
- package/dist/cjs/token-operation.js +236 -0
- package/dist/cjs/token-operation.js.map +1 -0
- package/dist/cjs/trade-fee.d.ts +60 -0
- package/dist/cjs/trade-fee.js +185 -0
- package/dist/cjs/trade-fee.js.map +1 -0
- package/dist/cjs/trading-operation.d.ts +212 -0
- package/dist/cjs/trading-operation.js +587 -0
- package/dist/cjs/trading-operation.js.map +1 -0
- package/dist/cjs/verify-operation.d.ts +79 -0
- package/dist/cjs/verify-operation.js +246 -0
- package/dist/cjs/verify-operation.js.map +1 -0
- package/dist/cjs/wallet-operation.d.ts +52 -0
- package/dist/cjs/wallet-operation.js +58 -0
- package/dist/cjs/wallet-operation.js.map +1 -0
- package/dist/cjs/wasm-provider.d.ts +15 -0
- package/dist/cjs/wasm-provider.js +20 -0
- package/dist/cjs/wasm-provider.js.map +1 -0
- package/dist/cjs/wrapper-operation.d.ts +196 -0
- package/dist/cjs/wrapper-operation.js +317 -0
- package/dist/cjs/wrapper-operation.js.map +1 -0
- package/dist/deploy-operation.d.ts +104 -0
- package/dist/deploy-operation.d.ts.map +1 -0
- package/dist/deploy-operation.js +327 -0
- package/dist/deploy-operation.js.map +1 -0
- package/dist/encoding.d.ts +6 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +44 -0
- package/dist/encoding.js.map +1 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +8969 -0
- package/dist/engine.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +18 -0
- package/dist/errors.js.map +1 -0
- package/dist/fee-ticket-operation.d.ts +205 -0
- package/dist/fee-ticket-operation.d.ts.map +1 -0
- package/dist/fee-ticket-operation.js +320 -0
- package/dist/fee-ticket-operation.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/ladder-limit-orders.d.ts +42 -0
- package/dist/ladder-limit-orders.d.ts.map +1 -0
- package/dist/ladder-limit-orders.js +121 -0
- package/dist/ladder-limit-orders.js.map +1 -0
- package/dist/mint-availability-operation.d.ts +57 -0
- package/dist/mint-availability-operation.d.ts.map +1 -0
- package/dist/mint-availability-operation.js +91 -0
- package/dist/mint-availability-operation.js.map +1 -0
- package/dist/models.d.ts +91 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +3 -0
- package/dist/models.js.map +1 -0
- package/dist/operations.d.ts +7 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/operations.js +36 -0
- package/dist/operations.js.map +1 -0
- package/dist/paid-mint-amount.d.ts +70 -0
- package/dist/paid-mint-amount.d.ts.map +1 -0
- package/dist/paid-mint-amount.js +268 -0
- package/dist/paid-mint-amount.js.map +1 -0
- package/dist/protocol.d.ts +22 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +22 -0
- package/dist/protocol.js.map +1 -0
- package/dist/runtime.d.ts +28 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +2 -0
- package/dist/runtime.js.map +1 -0
- package/dist/sale-common.d.ts +288 -0
- package/dist/sale-common.d.ts.map +1 -0
- package/dist/sale-common.js +844 -0
- package/dist/sale-common.js.map +1 -0
- package/dist/source-provider.d.ts +12 -0
- package/dist/source-provider.d.ts.map +1 -0
- package/dist/source-provider.js +2 -0
- package/dist/source-provider.js.map +1 -0
- package/dist/source-validation.d.ts +10 -0
- package/dist/source-validation.d.ts.map +1 -0
- package/dist/source-validation.js +28 -0
- package/dist/source-validation.js.map +1 -0
- package/dist/token-amount.d.ts +32 -0
- package/dist/token-amount.d.ts.map +1 -0
- package/dist/token-amount.js +237 -0
- package/dist/token-amount.js.map +1 -0
- package/dist/token-operation.d.ts +136 -0
- package/dist/token-operation.d.ts.map +1 -0
- package/dist/token-operation.js +231 -0
- package/dist/token-operation.js.map +1 -0
- package/dist/trade-fee.d.ts +61 -0
- package/dist/trade-fee.d.ts.map +1 -0
- package/dist/trade-fee.js +173 -0
- package/dist/trade-fee.js.map +1 -0
- package/dist/trading-operation.d.ts +213 -0
- package/dist/trading-operation.d.ts.map +1 -0
- package/dist/trading-operation.js +580 -0
- package/dist/trading-operation.js.map +1 -0
- package/dist/verify-operation.d.ts +80 -0
- package/dist/verify-operation.d.ts.map +1 -0
- package/dist/verify-operation.js +243 -0
- package/dist/verify-operation.js.map +1 -0
- package/dist/wallet-operation.d.ts +53 -0
- package/dist/wallet-operation.d.ts.map +1 -0
- package/dist/wallet-operation.js +55 -0
- package/dist/wallet-operation.js.map +1 -0
- package/dist/wasm-provider.d.ts +16 -0
- package/dist/wasm-provider.d.ts.map +1 -0
- package/dist/wasm-provider.js +17 -0
- package/dist/wasm-provider.js.map +1 -0
- package/dist/wrapper-operation.d.ts +197 -0
- package/dist/wrapper-operation.d.ts.map +1 -0
- package/dist/wrapper-operation.js +311 -0
- package/dist/wrapper-operation.js.map +1 -0
- package/index.d.ts +1 -0
- package/package.json +84 -0
- package/src/abi.ts +493 -0
- package/src/action-source-resolver.ts +660 -0
- package/src/artifacts.ts +87 -0
- package/src/builder.ts +37 -0
- package/src/deploy-operation.ts +458 -0
- package/src/encoding.ts +58 -0
- package/src/engine.ts +13226 -0
- package/src/errors.ts +35 -0
- package/src/fee-ticket-operation.ts +518 -0
- package/src/index.ts +27 -0
- package/src/ladder-limit-orders.ts +184 -0
- package/src/mint-availability-operation.ts +146 -0
- package/src/models.ts +103 -0
- package/src/operations.ts +46 -0
- package/src/paid-mint-amount.ts +402 -0
- package/src/protocol.ts +28 -0
- package/src/runtime.ts +29 -0
- package/src/sale-common.ts +1238 -0
- package/src/source-provider.ts +12 -0
- package/src/source-validation.ts +47 -0
- package/src/token-amount.ts +302 -0
- package/src/token-operation.ts +384 -0
- package/src/trade-fee.ts +264 -0
- package/src/trading-operation.ts +842 -0
- package/src/verify-operation.ts +400 -0
- package/src/wallet-operation.ts +86 -0
- package/src/wasm-provider.ts +32 -0
- package/src/wrapper-operation.ts +531 -0
|
@@ -0,0 +1,928 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_KCC20_MIN_GROSS_FILL_SOMPI = exports.DEFAULT_FEE = exports.ZERO_HASH = exports.SUBNETWORK_ID_NATIVE = exports.SOMPI_PER_TKAS = exports.KCC20_PLATFORM_PROTOCOL_FEE_RECIPIENT = exports.KCC20_ORDERBOOK_PROTOCOL_FEE_RECIPIENT = exports.KCC20_ORDERBOOK_MAX_SWEEP_BID_LEGS = exports.KCC20_ORDERBOOK_MAX_EXPANDED_SWEEP_BID_LEGS = exports.KCC20_ORDERBOOK_EXPANDED_SWEEP_OUTPUT_SOMPI = exports.DEFAULT_TOKEN_OUTPUT_SOMPI = exports.DEFAULT_KCC20_TOKEN_DECIMALS = exports.DEFAULT_KCC20_PRICE_SCALE = exports.DEFAULT_KCC20_MIN_PROTOCOL_FEE_SOMPI = exports.DEFAULT_KCC20_FEE_BPS = exports.DEFAULT_COMPUTE_BUDGET = void 0;
|
|
4
|
+
exports.kcc20PsktBuilderError = kcc20PsktBuilderError;
|
|
5
|
+
exports.parseArgs = parseArgs;
|
|
6
|
+
exports.parseTokenDecimalsArg = parseTokenDecimalsArg;
|
|
7
|
+
exports.resolveKcc20TokenDecimals = resolveKcc20TokenDecimals;
|
|
8
|
+
exports.parseKcc20TokenAmount = parseKcc20TokenAmount;
|
|
9
|
+
exports.parseKcc20TokenAmountArg = parseKcc20TokenAmountArg;
|
|
10
|
+
exports.resolveKcc20PriceScale = resolveKcc20PriceScale;
|
|
11
|
+
exports.requireHex32 = requireHex32;
|
|
12
|
+
exports.identifierTypeName = identifierTypeName;
|
|
13
|
+
exports.parseMintPolicy = parseMintPolicy;
|
|
14
|
+
exports.mintPolicyName = mintPolicyName;
|
|
15
|
+
exports.hexToBytes = hexToBytes;
|
|
16
|
+
exports.bytesToHex = bytesToHex;
|
|
17
|
+
exports.asciiToBytes32 = asciiToBytes32;
|
|
18
|
+
exports.bytes32ToAscii = bytes32ToAscii;
|
|
19
|
+
exports.writeI64LE = writeI64LE;
|
|
20
|
+
exports.readI64LE = readI64LE;
|
|
21
|
+
exports.normalizeSignature = normalizeSignature;
|
|
22
|
+
exports.patchRange = patchRange;
|
|
23
|
+
exports.buildNativeKcc20ScriptForState = buildNativeKcc20ScriptForState;
|
|
24
|
+
exports.buildKcc20V3ScriptForState = buildKcc20V3ScriptForState;
|
|
25
|
+
exports.buildApiPaymentChannelScriptForState = buildApiPaymentChannelScriptForState;
|
|
26
|
+
exports.buildKcc20OrderbookScriptForState = buildKcc20OrderbookScriptForState;
|
|
27
|
+
exports.buildKcc20FeeTicketScriptForState = buildKcc20FeeTicketScriptForState;
|
|
28
|
+
exports.buildKcc20VestingScriptForState = buildKcc20VestingScriptForState;
|
|
29
|
+
exports.buildKaspaFomoRoundScript = buildKaspaFomoRoundScript;
|
|
30
|
+
exports.buildKaspaFomoRoundScriptForState = buildKaspaFomoRoundScriptForState;
|
|
31
|
+
exports.readKaspaFomoRoundState = readKaspaFomoRoundState;
|
|
32
|
+
exports.buildKcc20WrapperScriptForState = buildKcc20WrapperScriptForState;
|
|
33
|
+
exports.orderbookModeName = orderbookModeName;
|
|
34
|
+
exports.stateToReceiptFields = stateToReceiptFields;
|
|
35
|
+
exports.stateToV3ReceiptFields = stateToV3ReceiptFields;
|
|
36
|
+
exports.receiptToKcc20OrderbookState = receiptToKcc20OrderbookState;
|
|
37
|
+
exports.buildKcc20V3IssueSigScript = buildKcc20V3IssueSigScript;
|
|
38
|
+
exports.buildKcc20V3CreateBidSigScript = buildKcc20V3CreateBidSigScript;
|
|
39
|
+
exports.buildKcc20V3CreateAskSigScript = buildKcc20V3CreateAskSigScript;
|
|
40
|
+
exports.buildKcc20V3FillAskSigScript = buildKcc20V3FillAskSigScript;
|
|
41
|
+
exports.buildKcc20OrderbookFillAskSigScript = buildKcc20OrderbookFillAskSigScript;
|
|
42
|
+
exports.buildKcc20V3FillBidSigScript = buildKcc20V3FillBidSigScript;
|
|
43
|
+
exports.buildKcc20OrderbookFillBidSigScript = buildKcc20OrderbookFillBidSigScript;
|
|
44
|
+
exports.buildKcc20V3SellIntoBidSigScript = buildKcc20V3SellIntoBidSigScript;
|
|
45
|
+
exports.buildKcc20OrderbookCrossAskSigScript = buildKcc20OrderbookCrossAskSigScript;
|
|
46
|
+
exports.buildKcc20OrderbookCrossAskLegacySigScript = buildKcc20OrderbookCrossAskLegacySigScript;
|
|
47
|
+
exports.buildKcc20OrderbookCrossBidSigScript = buildKcc20OrderbookCrossBidSigScript;
|
|
48
|
+
exports.buildKcc20OrderbookCrossBidLegacySigScript = buildKcc20OrderbookCrossBidLegacySigScript;
|
|
49
|
+
exports.buildKcc20OrderbookCancelAskSigScript = buildKcc20OrderbookCancelAskSigScript;
|
|
50
|
+
exports.buildKcc20OrderbookCancelBidSigScript = buildKcc20OrderbookCancelBidSigScript;
|
|
51
|
+
exports.buildKcc20OrderbookMergePrimarySigScript = buildKcc20OrderbookMergePrimarySigScript;
|
|
52
|
+
exports.buildKcc20OrderbookMergePeerSigScript = buildKcc20OrderbookMergePeerSigScript;
|
|
53
|
+
exports.buildKcc20OrderbookFillBidFromSellerSweepSigScript = buildKcc20OrderbookFillBidFromSellerSweepSigScript;
|
|
54
|
+
exports.buildKcc20OrderbookSellIntoBidsSigScript = buildKcc20OrderbookSellIntoBidsSigScript;
|
|
55
|
+
exports.buildKcc20OrderbookSellIntoBids10SigScript = buildKcc20OrderbookSellIntoBids10SigScript;
|
|
56
|
+
exports.argByteArray = argByteArray;
|
|
57
|
+
exports.argByte = argByte;
|
|
58
|
+
exports.argInt = argInt;
|
|
59
|
+
exports.argBool = argBool;
|
|
60
|
+
exports.p2pkScriptPubKey = p2pkScriptPubKey;
|
|
61
|
+
exports.p2shScriptHashHex = p2shScriptHashHex;
|
|
62
|
+
exports.calculateFeeSplit = calculateFeeSplit;
|
|
63
|
+
exports.exactGrossSompi = exactGrossSompi;
|
|
64
|
+
exports.calculateFeeSompi = calculateFeeSompi;
|
|
65
|
+
exports.selectFundingEntry = selectFundingEntry;
|
|
66
|
+
exports.requiredToccataFeeSompi = requiredToccataFeeSompi;
|
|
67
|
+
exports.utxoAmountSompi = utxoAmountSompi;
|
|
68
|
+
exports.findOutputIndex = findOutputIndex;
|
|
69
|
+
exports.setVersionOneInputMassFields = setVersionOneInputMassFields;
|
|
70
|
+
exports.extractLockedOutput = extractLockedOutput;
|
|
71
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unused-vars */
|
|
72
|
+
// @ts-nocheck
|
|
73
|
+
const abi_js_1 = require("./abi.js");
|
|
74
|
+
const protocol_js_1 = require("./protocol.js");
|
|
75
|
+
var protocol_js_2 = require("./protocol.js");
|
|
76
|
+
Object.defineProperty(exports, "DEFAULT_COMPUTE_BUDGET", { enumerable: true, get: function () { return protocol_js_2.DEFAULT_COMPUTE_BUDGET; } });
|
|
77
|
+
Object.defineProperty(exports, "DEFAULT_KCC20_FEE_BPS", { enumerable: true, get: function () { return protocol_js_2.DEFAULT_KCC20_FEE_BPS; } });
|
|
78
|
+
Object.defineProperty(exports, "DEFAULT_KCC20_MIN_PROTOCOL_FEE_SOMPI", { enumerable: true, get: function () { return protocol_js_2.DEFAULT_KCC20_MIN_PROTOCOL_FEE_SOMPI; } });
|
|
79
|
+
Object.defineProperty(exports, "DEFAULT_KCC20_PRICE_SCALE", { enumerable: true, get: function () { return protocol_js_2.DEFAULT_KCC20_PRICE_SCALE; } });
|
|
80
|
+
Object.defineProperty(exports, "DEFAULT_KCC20_TOKEN_DECIMALS", { enumerable: true, get: function () { return protocol_js_2.DEFAULT_KCC20_TOKEN_DECIMALS; } });
|
|
81
|
+
Object.defineProperty(exports, "DEFAULT_TOKEN_OUTPUT_SOMPI", { enumerable: true, get: function () { return protocol_js_2.DEFAULT_TOKEN_OUTPUT_SOMPI; } });
|
|
82
|
+
Object.defineProperty(exports, "KCC20_ORDERBOOK_EXPANDED_SWEEP_OUTPUT_SOMPI", { enumerable: true, get: function () { return protocol_js_2.KCC20_ORDERBOOK_EXPANDED_SWEEP_OUTPUT_SOMPI; } });
|
|
83
|
+
Object.defineProperty(exports, "KCC20_ORDERBOOK_MAX_EXPANDED_SWEEP_BID_LEGS", { enumerable: true, get: function () { return protocol_js_2.KCC20_ORDERBOOK_MAX_EXPANDED_SWEEP_BID_LEGS; } });
|
|
84
|
+
Object.defineProperty(exports, "KCC20_ORDERBOOK_MAX_SWEEP_BID_LEGS", { enumerable: true, get: function () { return protocol_js_2.KCC20_ORDERBOOK_MAX_SWEEP_BID_LEGS; } });
|
|
85
|
+
Object.defineProperty(exports, "KCC20_ORDERBOOK_PROTOCOL_FEE_RECIPIENT", { enumerable: true, get: function () { return protocol_js_2.KCC20_ORDERBOOK_PROTOCOL_FEE_RECIPIENT; } });
|
|
86
|
+
Object.defineProperty(exports, "KCC20_PLATFORM_PROTOCOL_FEE_RECIPIENT", { enumerable: true, get: function () { return protocol_js_2.KCC20_PLATFORM_PROTOCOL_FEE_RECIPIENT; } });
|
|
87
|
+
Object.defineProperty(exports, "SOMPI_PER_TKAS", { enumerable: true, get: function () { return protocol_js_2.SOMPI_PER_TKAS; } });
|
|
88
|
+
Object.defineProperty(exports, "SUBNETWORK_ID_NATIVE", { enumerable: true, get: function () { return protocol_js_2.SUBNETWORK_ID_NATIVE; } });
|
|
89
|
+
Object.defineProperty(exports, "ZERO_HASH", { enumerable: true, get: function () { return protocol_js_2.ZERO_HASH; } });
|
|
90
|
+
exports.DEFAULT_FEE = 50000n;
|
|
91
|
+
exports.DEFAULT_KCC20_MIN_GROSS_FILL_SOMPI = 50000000n;
|
|
92
|
+
function kcc20PsktBuilderError(message, code, details = {}) {
|
|
93
|
+
const error = new Error(message);
|
|
94
|
+
error.builderErrorCode = code;
|
|
95
|
+
error.builderErrorDetails = Object.fromEntries(Object.entries(details)
|
|
96
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
97
|
+
.map(([key, value]) => [key, String(value)]));
|
|
98
|
+
return error;
|
|
99
|
+
}
|
|
100
|
+
const WRAPPED_ACTION_TAGS = Object.freeze({
|
|
101
|
+
unwrapByOwnerSig: kcc1Tag("unwrapByOwnerSig(byte[],int,int,int,int)"),
|
|
102
|
+
createBidFromRoot: kcc1Tag("createBidFromRoot(byte[32],int,int,int,int)"),
|
|
103
|
+
createAskByOwnerSig: kcc1Tag("createAskByOwnerSig(byte[],int,int,int,int)"),
|
|
104
|
+
fillAskPartial: kcc1Tag("fillAskPartial(byte[32],int,int,int,int,int)"),
|
|
105
|
+
fillBidPartial: kcc1Tag("fillBidPartial(int,int,int,int,int,pubkey,int)"),
|
|
106
|
+
sellIntoBidByOwnerSig: kcc1Tag("sellIntoBidByOwnerSig(byte[],byte[32],int,int,int,int,int)"),
|
|
107
|
+
crossAskSide: kcc1Tag("crossAskSide(byte[32],int,int,int,int,int,int,int,int,int,int)"),
|
|
108
|
+
crossBidSide: kcc1Tag("crossBidSide(int,int,int,int,int,int,int,int,int,pubkey,int)"),
|
|
109
|
+
cancelAskByOwnerSig: kcc1Tag("cancelAskByOwnerSig(byte[],int)"),
|
|
110
|
+
cancelBidByOwnerSig: kcc1Tag("cancelBidByOwnerSig(byte[],int)"),
|
|
111
|
+
mergeByOwnerSig: kcc1Tag("mergeByOwnerSig(byte[])"),
|
|
112
|
+
mergePeerByOwnerSig: kcc1Tag("mergePeerByOwnerSig(byte[],int,int)"),
|
|
113
|
+
fillBidFromSellerSweep: kcc1Tag("fillBidFromSellerSweep(int,int,int,int,int,int,pubkey,int)"),
|
|
114
|
+
sellIntoBidsByOwnerSig: kcc1Tag("sellIntoBidsByOwnerSig(byte[],byte[32],int,int,byte[32],int,int,byte[32],int,int,int,int,int)"),
|
|
115
|
+
sellIntoBids10ByOwnerSig: kcc1Tag("sellIntoBids10ByOwnerSig(byte[],byte[32],int,int,byte[32],int,int,byte[32],int,int,byte[32],int,int,byte[32],int,int,byte[32],int,int,byte[32],int,int,byte[32],int,int,byte[32],int,int,byte[32],int,int,int,int,int)"),
|
|
116
|
+
});
|
|
117
|
+
function kcc1Tag(signature) {
|
|
118
|
+
const match = /^([A-Za-z_][A-Za-z0-9_]*)\((.*)\)$/.exec(signature);
|
|
119
|
+
if (!match)
|
|
120
|
+
throw new Error(`invalid KCC1 signature ${signature}`);
|
|
121
|
+
const inputs = match[2]
|
|
122
|
+
? match[2].split(",").map((type_name) => ({ type_name }))
|
|
123
|
+
: [];
|
|
124
|
+
return (0, abi_js_1.dispatchTag)({ name: match[1], inputs });
|
|
125
|
+
}
|
|
126
|
+
function parseArgs(argv) {
|
|
127
|
+
const map = { flags: new Set() };
|
|
128
|
+
for (let i = 0; i < argv.length; i++) {
|
|
129
|
+
const a = argv[i];
|
|
130
|
+
if (!a.startsWith("--"))
|
|
131
|
+
continue;
|
|
132
|
+
const next = argv[i + 1];
|
|
133
|
+
if (!next || next.startsWith("--")) {
|
|
134
|
+
map.flags.add(a);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
map[a.slice(2)] = next;
|
|
138
|
+
i++;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return map;
|
|
142
|
+
}
|
|
143
|
+
function parseTokenDecimalsArg(args) {
|
|
144
|
+
if (args["token-decimals"] == null)
|
|
145
|
+
return null;
|
|
146
|
+
const raw = String(args["token-decimals"]).trim();
|
|
147
|
+
if (!/^\d+$/.test(raw))
|
|
148
|
+
throw new Error(`invalid --token-decimals ${args["token-decimals"]}`);
|
|
149
|
+
const decimals = Number(raw);
|
|
150
|
+
if (!Number.isInteger(decimals) || decimals < 0 || decimals > 8) {
|
|
151
|
+
throw new Error(`invalid --token-decimals ${args["token-decimals"]}`);
|
|
152
|
+
}
|
|
153
|
+
return decimals;
|
|
154
|
+
}
|
|
155
|
+
function resolveKcc20TokenDecimals(args, ...values) {
|
|
156
|
+
const explicitDecimals = parseTokenDecimalsArg(args);
|
|
157
|
+
if (explicitDecimals != null)
|
|
158
|
+
return explicitDecimals;
|
|
159
|
+
return values.some((value) => String(value ?? "").includes("."))
|
|
160
|
+
? protocol_js_1.DEFAULT_KCC20_TOKEN_DECIMALS
|
|
161
|
+
: 0;
|
|
162
|
+
}
|
|
163
|
+
function parseKcc20TokenAmount(value, label = "token amount", decimals = 0) {
|
|
164
|
+
const raw = String(value ?? "").trim();
|
|
165
|
+
if (!raw)
|
|
166
|
+
throw new Error(`missing ${label}`);
|
|
167
|
+
if (!/^(?:0|[1-9]\d*)(?:\.\d+)?$/.test(raw)) {
|
|
168
|
+
throw new Error(`${label} must be a non-negative decimal amount`);
|
|
169
|
+
}
|
|
170
|
+
const [whole, fraction = ""] = raw.split(".");
|
|
171
|
+
if (decimals === 0 && fraction.length > 0) {
|
|
172
|
+
throw new Error(`${label} requires --token-decimals for fractional input`);
|
|
173
|
+
}
|
|
174
|
+
if (fraction.length > decimals) {
|
|
175
|
+
throw new Error(`${label} supports at most ${decimals} decimal places`);
|
|
176
|
+
}
|
|
177
|
+
const scale = 10n ** BigInt(decimals);
|
|
178
|
+
return (BigInt(whole) * scale +
|
|
179
|
+
BigInt((fraction + "0".repeat(decimals)).slice(0, decimals) || "0"));
|
|
180
|
+
}
|
|
181
|
+
function parseKcc20TokenAmountArg(args, key, fallback, label = key) {
|
|
182
|
+
const raw = args[key] ?? fallback;
|
|
183
|
+
const decimals = resolveKcc20TokenDecimals(args, raw);
|
|
184
|
+
return parseKcc20TokenAmount(raw, label, decimals);
|
|
185
|
+
}
|
|
186
|
+
function resolveKcc20PriceScale(args, fallback = protocol_js_1.DEFAULT_KCC20_PRICE_SCALE) {
|
|
187
|
+
if (args["price-scale"] != null) {
|
|
188
|
+
const raw = String(args["price-scale"]).trim();
|
|
189
|
+
if (!/^[1-9]\d*$/.test(raw))
|
|
190
|
+
throw new Error(`invalid --price-scale ${args["price-scale"]}`);
|
|
191
|
+
return BigInt(raw);
|
|
192
|
+
}
|
|
193
|
+
const decimals = parseTokenDecimalsArg(args);
|
|
194
|
+
if (decimals != null)
|
|
195
|
+
return 10n ** BigInt(decimals);
|
|
196
|
+
return BigInt(fallback);
|
|
197
|
+
}
|
|
198
|
+
function requireHex32(value, label) {
|
|
199
|
+
const clean = String(value || "")
|
|
200
|
+
.trim()
|
|
201
|
+
.replace(/^0x/i, "")
|
|
202
|
+
.toLowerCase();
|
|
203
|
+
if (!/^[0-9a-f]{64}$/.test(clean)) {
|
|
204
|
+
throw new Error(`${label} must be 32-byte hex, got: ${value}`);
|
|
205
|
+
}
|
|
206
|
+
return clean;
|
|
207
|
+
}
|
|
208
|
+
function identifierTypeName(t) {
|
|
209
|
+
if (t === 0)
|
|
210
|
+
return "p2pk-schnorr/v1";
|
|
211
|
+
if (t === 1)
|
|
212
|
+
return "p2pkh-schnorr/v1";
|
|
213
|
+
if (t === 2)
|
|
214
|
+
return "p2pkh-ecdsa/v1";
|
|
215
|
+
if (t === 3)
|
|
216
|
+
return "p2sh/v1";
|
|
217
|
+
if (t === 4)
|
|
218
|
+
return "covenant-id/v1";
|
|
219
|
+
return "unknown";
|
|
220
|
+
}
|
|
221
|
+
function parseMintPolicy(value) {
|
|
222
|
+
const raw = String(value).trim().toLowerCase();
|
|
223
|
+
if (raw === "0" ||
|
|
224
|
+
raw === "holder" ||
|
|
225
|
+
raw === "fixed" ||
|
|
226
|
+
raw === "fixed-supply" ||
|
|
227
|
+
raw === "fixedsupply")
|
|
228
|
+
return 0;
|
|
229
|
+
if (raw === "1" ||
|
|
230
|
+
raw === "controlled" ||
|
|
231
|
+
raw === "controlled-mint" ||
|
|
232
|
+
raw === "controlledmint")
|
|
233
|
+
return 1;
|
|
234
|
+
if (raw === "2" ||
|
|
235
|
+
raw === "public" ||
|
|
236
|
+
raw === "public-mint" ||
|
|
237
|
+
raw === "publicmint")
|
|
238
|
+
return 2;
|
|
239
|
+
throw new Error(`invalid KCC20 mint policy: ${value}`);
|
|
240
|
+
}
|
|
241
|
+
function mintPolicyName(mode) {
|
|
242
|
+
if (mode === 0)
|
|
243
|
+
return "fixed";
|
|
244
|
+
if (mode === 1)
|
|
245
|
+
return "controlled";
|
|
246
|
+
if (mode === 2)
|
|
247
|
+
return "public";
|
|
248
|
+
return "unknown";
|
|
249
|
+
}
|
|
250
|
+
function hexToBytes(hex) {
|
|
251
|
+
const clean = String(hex).trim().replace(/^0x/i, "");
|
|
252
|
+
if (!/^(?:[0-9a-f]{2})*$/i.test(clean)) {
|
|
253
|
+
throw new Error(`invalid even-length hex value: ${hex}`);
|
|
254
|
+
}
|
|
255
|
+
const out = new Uint8Array(clean.length / 2);
|
|
256
|
+
for (let i = 0; i < out.length; i++) {
|
|
257
|
+
out[i] = Number.parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
258
|
+
}
|
|
259
|
+
return out;
|
|
260
|
+
}
|
|
261
|
+
function bytesToHex(bytes) {
|
|
262
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
263
|
+
}
|
|
264
|
+
function asciiToBytes32(value, label) {
|
|
265
|
+
const clean = String(value || "")
|
|
266
|
+
.trim()
|
|
267
|
+
.replace(/\s+/g, " ");
|
|
268
|
+
if (!/^[\x20-\x7E]*$/.test(clean)) {
|
|
269
|
+
throw new Error(`${label} must be printable ASCII`);
|
|
270
|
+
}
|
|
271
|
+
const encoded = new TextEncoder().encode(clean);
|
|
272
|
+
if (encoded.length > 32) {
|
|
273
|
+
throw new Error(`${label} must be 32 bytes or fewer`);
|
|
274
|
+
}
|
|
275
|
+
const out = new Uint8Array(32);
|
|
276
|
+
out.set(encoded);
|
|
277
|
+
return out;
|
|
278
|
+
}
|
|
279
|
+
function bytes32ToAscii(bytes) {
|
|
280
|
+
const arr = Uint8Array.from(bytes || []);
|
|
281
|
+
const end = arr.indexOf(0);
|
|
282
|
+
return new TextDecoder().decode(end >= 0 ? arr.slice(0, end) : arr).trimEnd();
|
|
283
|
+
}
|
|
284
|
+
function writeI64LE(value) {
|
|
285
|
+
let n = BigInt(value);
|
|
286
|
+
if (n > 9223372036854775807n) {
|
|
287
|
+
throw new Error(`amount exceeds signed i64 range: ${value}`);
|
|
288
|
+
}
|
|
289
|
+
if (n < 0n) {
|
|
290
|
+
n = (1n << 64n) + n;
|
|
291
|
+
}
|
|
292
|
+
const out = new Uint8Array(8);
|
|
293
|
+
for (let i = 0; i < out.length; i++) {
|
|
294
|
+
out[i] = Number((n >> BigInt(8 * i)) & 0xffn);
|
|
295
|
+
}
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
function readI64LE(bytes) {
|
|
299
|
+
if (bytes.length !== 8) {
|
|
300
|
+
throw new Error(`readI64LE expected 8 bytes, got ${bytes.length}`);
|
|
301
|
+
}
|
|
302
|
+
let n = 0n;
|
|
303
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
304
|
+
n |= BigInt(bytes[i]) << BigInt(8 * i);
|
|
305
|
+
}
|
|
306
|
+
if (n & (1n << 63n)) {
|
|
307
|
+
n -= 1n << 64n;
|
|
308
|
+
}
|
|
309
|
+
return n;
|
|
310
|
+
}
|
|
311
|
+
function normalizeSignature(bytes) {
|
|
312
|
+
if (bytes.length === 66 && bytes[0] === 65) {
|
|
313
|
+
return bytes.slice(1);
|
|
314
|
+
}
|
|
315
|
+
if (bytes.length !== 65) {
|
|
316
|
+
throw new Error(`expected 65-byte Schnorr signature+sighash, got ${bytes.length}`);
|
|
317
|
+
}
|
|
318
|
+
return bytes;
|
|
319
|
+
}
|
|
320
|
+
function patchRange(target, start, end, replacement) {
|
|
321
|
+
if (replacement.length !== end - start) {
|
|
322
|
+
throw new Error(`patch length mismatch for ${start}..${end}: ${replacement.length}`);
|
|
323
|
+
}
|
|
324
|
+
target.set(replacement, start);
|
|
325
|
+
}
|
|
326
|
+
function buildNativeKcc20ScriptForState(baseScript, state) {
|
|
327
|
+
return (0, abi_js_1.buildKcc20ScriptForState)({ bytecode: Array.from(baseScript), state_layout: { start: 1, len: 112 } }, {
|
|
328
|
+
amount: BigInt(state.amount),
|
|
329
|
+
owner: state.ownerIdentifier || state.owner,
|
|
330
|
+
ownerScheme: Number(state.identifierType ?? state.ownerScheme ?? 0),
|
|
331
|
+
borrowScheme: Number(state.borrowScheme ?? 0),
|
|
332
|
+
borrowGuard: state.borrowGuard || hexToBytes(protocol_js_1.ZERO_HASH),
|
|
333
|
+
extensionCommitment: state.extensionCommitment,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
function buildKcc20V3ScriptForState(baseScript, state) {
|
|
337
|
+
const script = Uint8Array.from(baseScript);
|
|
338
|
+
patchRange(script, 2, 34, state.ownerIdentifier);
|
|
339
|
+
expectPushLen(script, 34, 8, "amount");
|
|
340
|
+
patchRange(script, 35, 43, writeI64LE(state.amount));
|
|
341
|
+
expectPushLen(script, 43, 8, "mode");
|
|
342
|
+
patchRange(script, 44, 52, writeI64LE(BigInt(state.mode)));
|
|
343
|
+
expectPushLen(script, 52, 8, "unitPriceSompi");
|
|
344
|
+
patchRange(script, 53, 61, writeI64LE(state.unitPriceSompi));
|
|
345
|
+
return script;
|
|
346
|
+
}
|
|
347
|
+
function buildApiPaymentChannelScriptForState(baseScript, state) {
|
|
348
|
+
const script = Uint8Array.from(baseScript);
|
|
349
|
+
patchRange(script, 2, 34, hexToBytes(state.payerPubkey));
|
|
350
|
+
patchRange(script, 35, 67, hexToBytes(state.providerPubkey));
|
|
351
|
+
patchRange(script, 68, 100, hexToBytes(state.providerPayoutPubkey));
|
|
352
|
+
patchRange(script, 101, 133, hexToBytes(state.voucherDomainHash));
|
|
353
|
+
patchRange(script, 134, 142, writeI64LE(state.refundAfterMs));
|
|
354
|
+
patchRange(script, 143, 151, writeI64LE(state.reserveSompi));
|
|
355
|
+
patchRange(script, 152, 160, writeI64LE(state.maxSpendSompi));
|
|
356
|
+
patchRange(script, 161, 169, writeI64LE(state.closeAfterMs || 0n));
|
|
357
|
+
return script;
|
|
358
|
+
}
|
|
359
|
+
function buildKcc20OrderbookScriptForState(baseScript, state) {
|
|
360
|
+
if (!state.canonicalTokenId)
|
|
361
|
+
throw new Error("KCC20Orderbook state missing canonicalTokenId");
|
|
362
|
+
const priceScale = requireSupportedKcc20DisplayScale(state.priceScale ?? protocol_js_1.DEFAULT_KCC20_PRICE_SCALE, "KCC20Orderbook priceScale");
|
|
363
|
+
const script = Uint8Array.from(baseScript);
|
|
364
|
+
expectPushLen(script, 1, 32, "canonicalTokenId");
|
|
365
|
+
patchRange(script, 2, 34, state.canonicalTokenId);
|
|
366
|
+
expectPushLen(script, 34, 32, "ownerIdentifier");
|
|
367
|
+
patchRange(script, 35, 67, state.ownerIdentifier);
|
|
368
|
+
expectPushLen(script, 67, 1, "ownerScheme");
|
|
369
|
+
patchRange(script, 68, 69, Uint8Array.of(Number(state.ownerScheme ?? state.identifierType ?? 0)));
|
|
370
|
+
expectPushLen(script, 69, 8, "amount");
|
|
371
|
+
patchRange(script, 70, 78, writeI64LE(state.amount));
|
|
372
|
+
expectPushLen(script, 78, 8, "mode");
|
|
373
|
+
patchRange(script, 79, 87, writeI64LE(BigInt(state.mode)));
|
|
374
|
+
expectPushLen(script, 87, 8, "unitPriceSompi");
|
|
375
|
+
patchRange(script, 88, 96, writeI64LE(state.unitPriceSompi));
|
|
376
|
+
expectPushLen(script, 96, 32, "feeTicketId");
|
|
377
|
+
patchRange(script, 97, 129, state.feeTicketId || hexToBytes(protocol_js_1.ZERO_HASH));
|
|
378
|
+
expectPushLen(script, 129, 8, "priceScale");
|
|
379
|
+
patchRange(script, 130, 138, writeI64LE(priceScale));
|
|
380
|
+
return script;
|
|
381
|
+
}
|
|
382
|
+
function buildKcc20FeeTicketScriptForState(baseScript, state) {
|
|
383
|
+
const script = Uint8Array.from(baseScript);
|
|
384
|
+
patchRange(script, 2, 34, state.ownerIdentifier);
|
|
385
|
+
expectPushLen(script, 34, 1, "ownerScheme");
|
|
386
|
+
patchRange(script, 35, 36, Uint8Array.of(Number(state.ownerScheme ?? state.identifierType ?? 0)));
|
|
387
|
+
expectPushLen(script, 36, 8, "mode");
|
|
388
|
+
patchRange(script, 37, 45, writeI64LE(BigInt(state.mode)));
|
|
389
|
+
expectPushLen(script, 45, 32, "utilityTokenId");
|
|
390
|
+
patchRange(script, 46, 78, state.utilityTokenId);
|
|
391
|
+
expectPushLen(script, 78, 8, "denomination");
|
|
392
|
+
patchRange(script, 79, 87, writeI64LE(BigInt(state.denomination)));
|
|
393
|
+
return script;
|
|
394
|
+
}
|
|
395
|
+
function buildKcc20VestingScriptForState(baseScript, state) {
|
|
396
|
+
const script = Uint8Array.from(baseScript);
|
|
397
|
+
expectPushLen(script, 1, 32, "canonicalTokenId");
|
|
398
|
+
patchRange(script, 2, 34, state.canonicalTokenId);
|
|
399
|
+
expectPushLen(script, 34, 32, "beneficiary");
|
|
400
|
+
patchRange(script, 35, 67, state.beneficiary);
|
|
401
|
+
expectPushLen(script, 67, 1, "beneficiaryScheme");
|
|
402
|
+
patchRange(script, 68, 69, Uint8Array.of(Number(state.beneficiaryScheme ?? state.beneficiaryType ?? 0)));
|
|
403
|
+
expectPushLen(script, 69, 8, "unlockTimeMs");
|
|
404
|
+
patchRange(script, 70, 78, writeI64LE(BigInt(state.unlockTimeMs)));
|
|
405
|
+
return script;
|
|
406
|
+
}
|
|
407
|
+
function buildKaspaFomoRoundScript(baseScript, state, recipients = {}) {
|
|
408
|
+
const script = buildKaspaFomoRoundScriptForState(baseScript, state);
|
|
409
|
+
if (recipients.roundSigner) {
|
|
410
|
+
const signer = hexToBytes(requireHex32(recipients.roundSigner, "round signer"));
|
|
411
|
+
patchRange(script, 118, 150, signer);
|
|
412
|
+
patchRange(script, 357, 389, signer);
|
|
413
|
+
}
|
|
414
|
+
if (recipients.marketingRecipient) {
|
|
415
|
+
patchRange(script, 537, 569, hexToBytes(requireHex32(recipients.marketingRecipient, "marketing recipient")));
|
|
416
|
+
}
|
|
417
|
+
if (recipients.platformRecipient) {
|
|
418
|
+
patchRange(script, 599, 631, hexToBytes(requireHex32(recipients.platformRecipient, "platform recipient")));
|
|
419
|
+
}
|
|
420
|
+
return script;
|
|
421
|
+
}
|
|
422
|
+
function buildKaspaFomoRoundScriptForState(baseScript, state) {
|
|
423
|
+
const script = Uint8Array.from(baseScript);
|
|
424
|
+
patchRange(script, 2, 34, hexToBytes(requireHex32(state.lastBuyer, "last buyer")));
|
|
425
|
+
expectPushLen(script, 34, 8, "endTimeMs");
|
|
426
|
+
patchRange(script, 35, 43, writeI64LE(BigInt(state.endTimeMs)));
|
|
427
|
+
expectPushLen(script, 43, 8, "ticketCount");
|
|
428
|
+
patchRange(script, 44, 52, writeI64LE(BigInt(state.ticketCount)));
|
|
429
|
+
return script;
|
|
430
|
+
}
|
|
431
|
+
function readKaspaFomoRoundState(script) {
|
|
432
|
+
const bytes = Uint8Array.from(script);
|
|
433
|
+
expectPushLen(bytes, 1, 32, "lastBuyer");
|
|
434
|
+
expectPushLen(bytes, 34, 8, "endTimeMs");
|
|
435
|
+
expectPushLen(bytes, 43, 8, "ticketCount");
|
|
436
|
+
return {
|
|
437
|
+
lastBuyer: bytesToHex(bytes.slice(2, 34)),
|
|
438
|
+
endTimeMs: readI64LE(bytes.slice(35, 43)),
|
|
439
|
+
ticketCount: readI64LE(bytes.slice(44, 52)),
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function buildKcc20WrapperScriptForState(baseScript, state) {
|
|
443
|
+
const script = Uint8Array.from(baseScript);
|
|
444
|
+
patchRange(script, 2, 34, state.canonicalTokenId);
|
|
445
|
+
if (script[34] !== 1) {
|
|
446
|
+
throw new Error(`unsupported KCC20Wrapper enabled push length ${script[34]}; expected 1`);
|
|
447
|
+
}
|
|
448
|
+
script[35] = state.enabled ? 1 : 0;
|
|
449
|
+
expectPushLen(script, 36, 8, "priceScale");
|
|
450
|
+
patchRange(script, 37, 45, writeI64LE(requireSupportedKcc20DisplayScale(state.priceScale ?? protocol_js_1.DEFAULT_KCC20_PRICE_SCALE, "KCC20Wrapper priceScale")));
|
|
451
|
+
return script;
|
|
452
|
+
}
|
|
453
|
+
function kcc20DecimalsFromDisplayScale(value) {
|
|
454
|
+
const scale = BigInt(value);
|
|
455
|
+
for (let decimals = 0; decimals <= 8; decimals += 1) {
|
|
456
|
+
if (10n ** BigInt(decimals) === scale)
|
|
457
|
+
return decimals;
|
|
458
|
+
}
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
function kcc20DisplayScaleForDecimals(decimals) {
|
|
462
|
+
if (!Number.isInteger(decimals) || decimals < 0 || decimals > 8) {
|
|
463
|
+
throw new Error(`unsupported KCC20 decimals: ${decimals}`);
|
|
464
|
+
}
|
|
465
|
+
return 10n ** BigInt(decimals);
|
|
466
|
+
}
|
|
467
|
+
function requireSupportedKcc20DisplayScale(value, label) {
|
|
468
|
+
const decimals = kcc20DecimalsFromDisplayScale(value);
|
|
469
|
+
if (decimals === null) {
|
|
470
|
+
throw new Error(`${label} must be an exact power-of-ten display scale for decimals 0-8`);
|
|
471
|
+
}
|
|
472
|
+
return kcc20DisplayScaleForDecimals(decimals);
|
|
473
|
+
}
|
|
474
|
+
function orderbookModeName(mode) {
|
|
475
|
+
if (Number(mode) === 0)
|
|
476
|
+
return "holder";
|
|
477
|
+
if (Number(mode) === 1)
|
|
478
|
+
return "root";
|
|
479
|
+
if (Number(mode) === 2)
|
|
480
|
+
return "ask";
|
|
481
|
+
if (Number(mode) === 3)
|
|
482
|
+
return "bid";
|
|
483
|
+
return "unknown";
|
|
484
|
+
}
|
|
485
|
+
function stateToReceiptFields(state) {
|
|
486
|
+
const amount = BigInt(state.amount).toString();
|
|
487
|
+
const owner = state.ownerIdentifier || state.owner;
|
|
488
|
+
const ownerScheme = Number(state.ownerScheme ?? state.identifierType ?? 0);
|
|
489
|
+
return {
|
|
490
|
+
owner: bytesToHex(owner),
|
|
491
|
+
ownerScheme,
|
|
492
|
+
ownerSchemeName: identifierTypeName(ownerScheme),
|
|
493
|
+
borrowScheme: Number(state.borrowScheme ?? 0),
|
|
494
|
+
borrowGuard: bytesToHex(state.borrowGuard || hexToBytes(protocol_js_1.ZERO_HASH)),
|
|
495
|
+
extensionCommitment: bytesToHex(state.extensionCommitment),
|
|
496
|
+
tokenAmount: amount,
|
|
497
|
+
stateAmount: amount,
|
|
498
|
+
balance: amount,
|
|
499
|
+
amount,
|
|
500
|
+
isMintAuthority: amount === "0",
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
function stateToV3ReceiptFields(state) {
|
|
504
|
+
const owner = bytesToHex(state.ownerIdentifier);
|
|
505
|
+
const amount = BigInt(state.amount).toString();
|
|
506
|
+
const unitPrice = BigInt(state.unitPriceSompi).toString();
|
|
507
|
+
const priceScale = BigInt(state.priceScale ?? protocol_js_1.DEFAULT_KCC20_PRICE_SCALE).toString();
|
|
508
|
+
const mode = Number(state.mode);
|
|
509
|
+
const ownerScheme = Number(state.ownerScheme ?? state.identifierType ?? 0);
|
|
510
|
+
const feeRecipient = protocol_js_1.KCC20_ORDERBOOK_PROTOCOL_FEE_RECIPIENT;
|
|
511
|
+
const fields = {
|
|
512
|
+
ownerIdentifier: owner,
|
|
513
|
+
stateOwner: owner,
|
|
514
|
+
ownerScheme,
|
|
515
|
+
ownerSchemeName: identifierTypeName(ownerScheme),
|
|
516
|
+
tokenAmount: amount,
|
|
517
|
+
stateAmount: amount,
|
|
518
|
+
amount,
|
|
519
|
+
mode,
|
|
520
|
+
stateMode: mode,
|
|
521
|
+
modeName: orderbookModeName(mode),
|
|
522
|
+
unitPriceSompi: unitPrice,
|
|
523
|
+
marketUnitPriceSompi: unitPrice,
|
|
524
|
+
priceScale,
|
|
525
|
+
tokenDisplayScale: priceScale,
|
|
526
|
+
minGrossFillSompi: exports.DEFAULT_KCC20_MIN_GROSS_FILL_SOMPI.toString(),
|
|
527
|
+
minProtocolFeeSompi: protocol_js_1.DEFAULT_KCC20_MIN_PROTOCOL_FEE_SOMPI.toString(),
|
|
528
|
+
feeRecipient,
|
|
529
|
+
protocolFeeRecipient: feeRecipient,
|
|
530
|
+
feeBps: Number(protocol_js_1.DEFAULT_KCC20_FEE_BPS),
|
|
531
|
+
protocolFeeBps: Number(protocol_js_1.DEFAULT_KCC20_FEE_BPS),
|
|
532
|
+
feeTicketId: state.feeTicketId ? bytesToHex(state.feeTicketId) : protocol_js_1.ZERO_HASH,
|
|
533
|
+
};
|
|
534
|
+
if (state.canonicalTokenId) {
|
|
535
|
+
fields.canonicalTokenId = bytesToHex(state.canonicalTokenId);
|
|
536
|
+
fields.tokenId = fields.canonicalTokenId;
|
|
537
|
+
}
|
|
538
|
+
return fields;
|
|
539
|
+
}
|
|
540
|
+
function receiptToKcc20OrderbookState(receipt, fallbackOwnerHex) {
|
|
541
|
+
const state = {
|
|
542
|
+
ownerIdentifier: hexToBytes(requireHex32(receipt.stateOwner || receipt.ownerIdentifier || fallbackOwnerHex, "state owner")),
|
|
543
|
+
ownerScheme: Number(receipt.ownerScheme ??
|
|
544
|
+
receipt.stateOwnerType ??
|
|
545
|
+
receipt.identifierType ??
|
|
546
|
+
0),
|
|
547
|
+
amount: BigInt(receipt.stateAmount || receipt.tokenAmount || receipt.amount || "0"),
|
|
548
|
+
mode: Number(receipt.stateMode ?? receipt.mode ?? 0),
|
|
549
|
+
unitPriceSompi: BigInt(receipt.marketUnitPriceSompi || receipt.unitPriceSompi || "0"),
|
|
550
|
+
priceScale: BigInt(receipt.tokenDisplayScale ||
|
|
551
|
+
receipt.priceScale ||
|
|
552
|
+
protocol_js_1.DEFAULT_KCC20_PRICE_SCALE),
|
|
553
|
+
feeRecipient: hexToBytes(protocol_js_1.KCC20_ORDERBOOK_PROTOCOL_FEE_RECIPIENT),
|
|
554
|
+
feeBps: Number(protocol_js_1.DEFAULT_KCC20_FEE_BPS),
|
|
555
|
+
feeTicketId: hexToBytes(requireHex32(receipt.feeTicketId || protocol_js_1.ZERO_HASH, "fee ticket id")),
|
|
556
|
+
};
|
|
557
|
+
if (receipt.canonicalTokenId || receipt.tokenId) {
|
|
558
|
+
state.canonicalTokenId = hexToBytes(requireHex32(receipt.canonicalTokenId || receipt.tokenId, "canonical token id"));
|
|
559
|
+
}
|
|
560
|
+
return state;
|
|
561
|
+
}
|
|
562
|
+
function buildKcc20V3IssueSigScript(kw, { signature, toOwner, tokenAmount, tokenOutputIndex, rootOutputIndex }) {
|
|
563
|
+
const builder = new kw.ScriptBuilder();
|
|
564
|
+
builder.addData(signature);
|
|
565
|
+
builder.addData(toOwner);
|
|
566
|
+
builder.addI64(BigInt(tokenAmount));
|
|
567
|
+
builder.addI64(BigInt(tokenOutputIndex));
|
|
568
|
+
builder.addI64(BigInt(rootOutputIndex));
|
|
569
|
+
builder.addI64(0n);
|
|
570
|
+
return builder.drain();
|
|
571
|
+
}
|
|
572
|
+
function buildKcc20V3CreateBidSigScript(kw, { buyer, bidAmount, unitPriceSompi, bidOutputIndex, rootOutputIndex }) {
|
|
573
|
+
const builder = new kw.ScriptBuilder();
|
|
574
|
+
builder.addData(buyer);
|
|
575
|
+
builder.addI64(BigInt(bidAmount));
|
|
576
|
+
builder.addI64(BigInt(unitPriceSompi));
|
|
577
|
+
builder.addI64(BigInt(bidOutputIndex));
|
|
578
|
+
builder.addI64(BigInt(rootOutputIndex));
|
|
579
|
+
builder.addData(WRAPPED_ACTION_TAGS.createBidFromRoot);
|
|
580
|
+
return builder.drain();
|
|
581
|
+
}
|
|
582
|
+
function buildKcc20V3CreateAskSigScript(kw, { signature, askAmount, unitPriceSompi, askOutputIndex, changeOutputIndex }) {
|
|
583
|
+
const builder = new kw.ScriptBuilder();
|
|
584
|
+
builder.addData(signature);
|
|
585
|
+
builder.addI64(BigInt(askAmount));
|
|
586
|
+
builder.addI64(BigInt(unitPriceSompi));
|
|
587
|
+
builder.addI64(BigInt(askOutputIndex));
|
|
588
|
+
builder.addI64(BigInt(changeOutputIndex));
|
|
589
|
+
builder.addData(WRAPPED_ACTION_TAGS.createAskByOwnerSig);
|
|
590
|
+
return builder.drain();
|
|
591
|
+
}
|
|
592
|
+
function buildKcc20V3FillAskSigScript(kw, { buyerOwner, fillAmount, buyerTokenOutputIndex, askOutputIndex, sellerOutputIndex, }) {
|
|
593
|
+
const builder = new kw.ScriptBuilder();
|
|
594
|
+
builder.addData(buyerOwner);
|
|
595
|
+
builder.addI64(BigInt(fillAmount));
|
|
596
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
597
|
+
builder.addI64(BigInt(askOutputIndex));
|
|
598
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
599
|
+
builder.addData(WRAPPED_ACTION_TAGS.fillAskPartial);
|
|
600
|
+
return builder.drain();
|
|
601
|
+
}
|
|
602
|
+
function buildKcc20OrderbookFillAskSigScript(kw, { buyerOwner, fillAmount, buyerTokenOutputIndex, askOutputIndex, sellerOutputIndex, ticketInputIndex = -1, }) {
|
|
603
|
+
const builder = new kw.ScriptBuilder();
|
|
604
|
+
builder.addData(buyerOwner);
|
|
605
|
+
builder.addI64(BigInt(fillAmount));
|
|
606
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
607
|
+
builder.addI64(BigInt(askOutputIndex));
|
|
608
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
609
|
+
builder.addI64(BigInt(ticketInputIndex));
|
|
610
|
+
builder.addData(WRAPPED_ACTION_TAGS.fillAskPartial);
|
|
611
|
+
return builder.drain();
|
|
612
|
+
}
|
|
613
|
+
function buildKcc20V3FillBidSigScript(kw, { sellerTokenInputIndex, fillAmount, buyerTokenOutputIndex, bidOutputIndex, sellerOutputIndex, seller, }) {
|
|
614
|
+
const builder = new kw.ScriptBuilder();
|
|
615
|
+
builder.addI64(BigInt(sellerTokenInputIndex));
|
|
616
|
+
builder.addI64(BigInt(fillAmount));
|
|
617
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
618
|
+
builder.addI64(BigInt(bidOutputIndex));
|
|
619
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
620
|
+
builder.addData(seller);
|
|
621
|
+
builder.addData(WRAPPED_ACTION_TAGS.fillBidPartial);
|
|
622
|
+
return builder.drain();
|
|
623
|
+
}
|
|
624
|
+
function buildKcc20OrderbookFillBidSigScript(kw, { sellerTokenInputIndex, fillAmount, buyerTokenOutputIndex, bidOutputIndex, sellerOutputIndex, seller, ticketInputIndex = -1, }) {
|
|
625
|
+
const builder = new kw.ScriptBuilder();
|
|
626
|
+
builder.addI64(BigInt(sellerTokenInputIndex));
|
|
627
|
+
builder.addI64(BigInt(fillAmount));
|
|
628
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
629
|
+
builder.addI64(BigInt(bidOutputIndex));
|
|
630
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
631
|
+
builder.addData(seller);
|
|
632
|
+
builder.addI64(BigInt(ticketInputIndex));
|
|
633
|
+
builder.addData(WRAPPED_ACTION_TAGS.fillBidPartial);
|
|
634
|
+
return builder.drain();
|
|
635
|
+
}
|
|
636
|
+
function buildKcc20V3SellIntoBidSigScript(kw, { signature, buyerOwner, fillAmount, buyerTokenOutputIndex, sellerChangeOutputIndex, sellerRefundOutputIndex = -1, sellerOutputsEndIndex = -1, takesRefundOutput = false, }) {
|
|
637
|
+
const builder = new kw.ScriptBuilder();
|
|
638
|
+
builder.addData(signature);
|
|
639
|
+
builder.addData(buyerOwner);
|
|
640
|
+
builder.addI64(BigInt(fillAmount));
|
|
641
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
642
|
+
builder.addI64(BigInt(sellerChangeOutputIndex));
|
|
643
|
+
if (takesRefundOutput) {
|
|
644
|
+
builder.addI64(BigInt(sellerRefundOutputIndex));
|
|
645
|
+
builder.addI64(BigInt(sellerOutputsEndIndex));
|
|
646
|
+
}
|
|
647
|
+
builder.addData(WRAPPED_ACTION_TAGS.sellIntoBidByOwnerSig);
|
|
648
|
+
return builder.drain();
|
|
649
|
+
}
|
|
650
|
+
function buildKcc20OrderbookCrossAskSigScript(kw, { buyerOwner, bidInputIndex, fillAmount, clearingUnitPriceSompi, buyerTokenOutputIndex, askOutputIndex, bidOutputIndex, sellerOutputIndex, feeOutputIndex, refundOutputIndex, ticketInputIndex = -1, }) {
|
|
651
|
+
const builder = new kw.ScriptBuilder();
|
|
652
|
+
builder.addData(buyerOwner);
|
|
653
|
+
builder.addI64(BigInt(bidInputIndex));
|
|
654
|
+
builder.addI64(BigInt(fillAmount));
|
|
655
|
+
builder.addI64(BigInt(clearingUnitPriceSompi));
|
|
656
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
657
|
+
builder.addI64(BigInt(askOutputIndex));
|
|
658
|
+
builder.addI64(BigInt(bidOutputIndex));
|
|
659
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
660
|
+
builder.addI64(BigInt(feeOutputIndex));
|
|
661
|
+
builder.addI64(BigInt(refundOutputIndex));
|
|
662
|
+
builder.addI64(BigInt(ticketInputIndex));
|
|
663
|
+
builder.addData(WRAPPED_ACTION_TAGS.crossAskSide);
|
|
664
|
+
return builder.drain();
|
|
665
|
+
}
|
|
666
|
+
function buildKcc20OrderbookCrossAskLegacySigScript(kw, { buyerOwner, bidInputIndex, fillAmount, clearingUnitPriceSompi, buyerTokenOutputIndex, askOutputIndex, bidOutputIndex, sellerOutputIndex, feeOutputIndex, refundOutputIndex, }) {
|
|
667
|
+
const builder = new kw.ScriptBuilder();
|
|
668
|
+
builder.addData(buyerOwner);
|
|
669
|
+
builder.addI64(BigInt(bidInputIndex));
|
|
670
|
+
builder.addI64(BigInt(fillAmount));
|
|
671
|
+
builder.addI64(BigInt(clearingUnitPriceSompi));
|
|
672
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
673
|
+
builder.addI64(BigInt(askOutputIndex));
|
|
674
|
+
builder.addI64(BigInt(bidOutputIndex));
|
|
675
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
676
|
+
builder.addI64(BigInt(feeOutputIndex));
|
|
677
|
+
builder.addI64(BigInt(refundOutputIndex));
|
|
678
|
+
builder.addData(WRAPPED_ACTION_TAGS.crossAskSide);
|
|
679
|
+
return builder.drain();
|
|
680
|
+
}
|
|
681
|
+
function buildKcc20OrderbookCrossBidSigScript(kw, { askInputIndex, fillAmount, clearingUnitPriceSompi, buyerTokenOutputIndex, askOutputIndex, bidOutputIndex, sellerOutputIndex, feeOutputIndex, refundOutputIndex, seller, ticketInputIndex = -1, }) {
|
|
682
|
+
const builder = new kw.ScriptBuilder();
|
|
683
|
+
builder.addI64(BigInt(askInputIndex));
|
|
684
|
+
builder.addI64(BigInt(fillAmount));
|
|
685
|
+
builder.addI64(BigInt(clearingUnitPriceSompi));
|
|
686
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
687
|
+
builder.addI64(BigInt(askOutputIndex));
|
|
688
|
+
builder.addI64(BigInt(bidOutputIndex));
|
|
689
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
690
|
+
builder.addI64(BigInt(feeOutputIndex));
|
|
691
|
+
builder.addI64(BigInt(refundOutputIndex));
|
|
692
|
+
builder.addData(seller);
|
|
693
|
+
builder.addI64(BigInt(ticketInputIndex));
|
|
694
|
+
builder.addData(WRAPPED_ACTION_TAGS.crossBidSide);
|
|
695
|
+
return builder.drain();
|
|
696
|
+
}
|
|
697
|
+
function buildKcc20OrderbookCrossBidLegacySigScript(kw, { askInputIndex, fillAmount, clearingUnitPriceSompi, buyerTokenOutputIndex, askOutputIndex, bidOutputIndex, sellerOutputIndex, feeOutputIndex, refundOutputIndex, seller, }) {
|
|
698
|
+
const builder = new kw.ScriptBuilder();
|
|
699
|
+
builder.addI64(BigInt(askInputIndex));
|
|
700
|
+
builder.addI64(BigInt(fillAmount));
|
|
701
|
+
builder.addI64(BigInt(clearingUnitPriceSompi));
|
|
702
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
703
|
+
builder.addI64(BigInt(askOutputIndex));
|
|
704
|
+
builder.addI64(BigInt(bidOutputIndex));
|
|
705
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
706
|
+
builder.addI64(BigInt(feeOutputIndex));
|
|
707
|
+
builder.addI64(BigInt(refundOutputIndex));
|
|
708
|
+
builder.addData(seller);
|
|
709
|
+
builder.addData(WRAPPED_ACTION_TAGS.crossBidSide);
|
|
710
|
+
return builder.drain();
|
|
711
|
+
}
|
|
712
|
+
function buildKcc20OrderbookCancelAskSigScript(kw, { signature, holderOutputIndex }) {
|
|
713
|
+
const builder = new kw.ScriptBuilder();
|
|
714
|
+
builder.addData(signature);
|
|
715
|
+
builder.addI64(BigInt(holderOutputIndex));
|
|
716
|
+
builder.addData(WRAPPED_ACTION_TAGS.cancelAskByOwnerSig);
|
|
717
|
+
return builder.drain();
|
|
718
|
+
}
|
|
719
|
+
function buildKcc20OrderbookCancelBidSigScript(kw, { signature, refundOutputIndex }) {
|
|
720
|
+
const builder = new kw.ScriptBuilder();
|
|
721
|
+
builder.addData(signature);
|
|
722
|
+
builder.addI64(BigInt(refundOutputIndex));
|
|
723
|
+
builder.addData(WRAPPED_ACTION_TAGS.cancelBidByOwnerSig);
|
|
724
|
+
return builder.drain();
|
|
725
|
+
}
|
|
726
|
+
function buildKcc20OrderbookMergePrimarySigScript(kw, { signature }) {
|
|
727
|
+
const builder = new kw.ScriptBuilder();
|
|
728
|
+
builder.addData(signature);
|
|
729
|
+
builder.addData(WRAPPED_ACTION_TAGS.mergeByOwnerSig);
|
|
730
|
+
return builder.drain();
|
|
731
|
+
}
|
|
732
|
+
function buildKcc20OrderbookMergePeerSigScript(kw, { signature, primaryInputIndex, mergedAmount }) {
|
|
733
|
+
const builder = new kw.ScriptBuilder();
|
|
734
|
+
builder.addData(signature);
|
|
735
|
+
builder.addI64(BigInt(primaryInputIndex));
|
|
736
|
+
builder.addI64(BigInt(mergedAmount));
|
|
737
|
+
builder.addData(WRAPPED_ACTION_TAGS.mergePeerByOwnerSig);
|
|
738
|
+
return builder.drain();
|
|
739
|
+
}
|
|
740
|
+
function buildKcc20OrderbookFillBidFromSellerSweepSigScript(kw, { sellerTokenInputIndex, sellerAuthOutputOrdinal, fillAmount, buyerTokenOutputIndex, bidOutputIndex, sellerOutputIndex, seller, ticketInputIndex = -1, }) {
|
|
741
|
+
const builder = new kw.ScriptBuilder();
|
|
742
|
+
builder.addI64(BigInt(sellerTokenInputIndex));
|
|
743
|
+
builder.addI64(BigInt(sellerAuthOutputOrdinal));
|
|
744
|
+
builder.addI64(BigInt(fillAmount));
|
|
745
|
+
builder.addI64(BigInt(buyerTokenOutputIndex));
|
|
746
|
+
builder.addI64(BigInt(bidOutputIndex));
|
|
747
|
+
builder.addI64(BigInt(sellerOutputIndex));
|
|
748
|
+
builder.addData(seller);
|
|
749
|
+
builder.addI64(BigInt(ticketInputIndex));
|
|
750
|
+
builder.addData(WRAPPED_ACTION_TAGS.fillBidFromSellerSweep);
|
|
751
|
+
return builder.drain();
|
|
752
|
+
}
|
|
753
|
+
function buildKcc20OrderbookSellIntoBidsSigScript(kw, { signature, legs, changeOutputIndex, sellerRefundOutputIndex = -1, sellerOutputsEndIndex = -1, takesRefundOutput = false, }) {
|
|
754
|
+
const builder = new kw.ScriptBuilder();
|
|
755
|
+
builder.addData(signature);
|
|
756
|
+
for (let i = 0; i < protocol_js_1.KCC20_ORDERBOOK_MAX_SWEEP_BID_LEGS; i++) {
|
|
757
|
+
const leg = legs[i] || {};
|
|
758
|
+
builder.addData(leg.buyerOwner || hexToBytes(protocol_js_1.ZERO_HASH));
|
|
759
|
+
builder.addI64(BigInt(leg.fillAmount || 0n));
|
|
760
|
+
builder.addI64(BigInt(leg.buyerTokenOutputIndex ?? -1));
|
|
761
|
+
}
|
|
762
|
+
builder.addI64(BigInt(changeOutputIndex));
|
|
763
|
+
if (takesRefundOutput) {
|
|
764
|
+
builder.addI64(BigInt(sellerRefundOutputIndex));
|
|
765
|
+
builder.addI64(BigInt(sellerOutputsEndIndex));
|
|
766
|
+
}
|
|
767
|
+
builder.addData(WRAPPED_ACTION_TAGS.sellIntoBidsByOwnerSig);
|
|
768
|
+
return builder.drain();
|
|
769
|
+
}
|
|
770
|
+
function buildKcc20OrderbookSellIntoBids10SigScript(kw, { signature, legs, changeOutputIndex, sellerRefundOutputIndex = -1, sellerOutputsEndIndex = -1, takesRefundOutput = false, }) {
|
|
771
|
+
const builder = new kw.ScriptBuilder();
|
|
772
|
+
builder.addData(signature);
|
|
773
|
+
for (let i = 0; i < protocol_js_1.KCC20_ORDERBOOK_MAX_EXPANDED_SWEEP_BID_LEGS; i++) {
|
|
774
|
+
const leg = legs[i] || {};
|
|
775
|
+
builder.addData(leg.buyerOwner || hexToBytes(protocol_js_1.ZERO_HASH));
|
|
776
|
+
builder.addI64(BigInt(leg.fillAmount || 0n));
|
|
777
|
+
builder.addI64(BigInt(leg.buyerTokenOutputIndex ?? -1));
|
|
778
|
+
}
|
|
779
|
+
builder.addI64(BigInt(changeOutputIndex));
|
|
780
|
+
if (takesRefundOutput) {
|
|
781
|
+
builder.addI64(BigInt(sellerRefundOutputIndex));
|
|
782
|
+
builder.addI64(BigInt(sellerOutputsEndIndex));
|
|
783
|
+
}
|
|
784
|
+
builder.addData(WRAPPED_ACTION_TAGS.sellIntoBids10ByOwnerSig);
|
|
785
|
+
return builder.drain();
|
|
786
|
+
}
|
|
787
|
+
function argByteArray(hex) {
|
|
788
|
+
const values = hexToBytes(hex);
|
|
789
|
+
return {
|
|
790
|
+
kind: "array",
|
|
791
|
+
data: {
|
|
792
|
+
type_ref: {
|
|
793
|
+
base: "byte",
|
|
794
|
+
array_dims: [{ kind: "fixed", value: values.length }],
|
|
795
|
+
},
|
|
796
|
+
values: Array.from(values, (b) => ({ kind: "byte", data: b })),
|
|
797
|
+
},
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
function argByte(value) {
|
|
801
|
+
return { kind: "byte", data: Number(value) };
|
|
802
|
+
}
|
|
803
|
+
function argInt(value) {
|
|
804
|
+
const n = BigInt(value);
|
|
805
|
+
if (n < 0n || n > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
806
|
+
throw new Error(`SilverScript int constructor arg is outside JS safe integer range: ${value}`);
|
|
807
|
+
}
|
|
808
|
+
return { kind: "int", data: Number(n) };
|
|
809
|
+
}
|
|
810
|
+
function argBool(value) {
|
|
811
|
+
return { kind: "bool", data: Boolean(value) };
|
|
812
|
+
}
|
|
813
|
+
function p2pkScriptPubKey(kw, pubkeyHex) {
|
|
814
|
+
return new kw.ScriptPublicKey(0, `20${requireHex32(pubkeyHex, "pubkey")}ac`);
|
|
815
|
+
}
|
|
816
|
+
function p2shScriptHashHex(kw, script) {
|
|
817
|
+
const spk = kw.payToScriptHashScript(script);
|
|
818
|
+
const hex = String(spk.script);
|
|
819
|
+
if (!/^aa20[0-9a-f]{64}87$/i.test(hex)) {
|
|
820
|
+
throw new Error(`unexpected P2SH script public key: ${hex}`);
|
|
821
|
+
}
|
|
822
|
+
return hex.slice(4, 68).toLowerCase();
|
|
823
|
+
}
|
|
824
|
+
function calculateFeeSplit(grossSompi, feeBps = protocol_js_1.DEFAULT_KCC20_FEE_BPS) {
|
|
825
|
+
const gross = BigInt(grossSompi);
|
|
826
|
+
const fee = calculateFeeSompi(gross, feeBps);
|
|
827
|
+
return { fee, net: gross - fee };
|
|
828
|
+
}
|
|
829
|
+
function exactGrossSompi(rawAmount, unitPriceSompi, priceScale = protocol_js_1.DEFAULT_KCC20_PRICE_SCALE) {
|
|
830
|
+
const raw = BigInt(rawAmount);
|
|
831
|
+
const price = BigInt(unitPriceSompi);
|
|
832
|
+
const scale = BigInt(priceScale ?? protocol_js_1.DEFAULT_KCC20_PRICE_SCALE);
|
|
833
|
+
if (raw < 0n || price < 0n || scale <= 0n)
|
|
834
|
+
throw new Error("invalid exact gross inputs");
|
|
835
|
+
const whole = raw / scale;
|
|
836
|
+
const fraction = raw - whole * scale;
|
|
837
|
+
const fractionProduct = fraction * price;
|
|
838
|
+
if (fractionProduct % scale !== 0n) {
|
|
839
|
+
throw new Error(`gross value is not an exact sompi amount for raw=${raw} unitPriceSompi=${price} priceScale=${scale}`);
|
|
840
|
+
}
|
|
841
|
+
return whole * price + fractionProduct / scale;
|
|
842
|
+
}
|
|
843
|
+
function calculateFeeSompi(grossSompi, feeBps = protocol_js_1.DEFAULT_KCC20_FEE_BPS) {
|
|
844
|
+
const gross = BigInt(grossSompi);
|
|
845
|
+
const bps = BigInt(feeBps);
|
|
846
|
+
if (gross < 0n)
|
|
847
|
+
throw new Error(`grossSompi must be non-negative: ${grossSompi}`);
|
|
848
|
+
if (bps < 0n || bps > 10000n)
|
|
849
|
+
throw new Error(`feeBps must be 0..10000: ${feeBps}`);
|
|
850
|
+
const percentageFee = (gross * bps) / 10000n;
|
|
851
|
+
return percentageFee < protocol_js_1.DEFAULT_KCC20_MIN_PROTOCOL_FEE_SOMPI
|
|
852
|
+
? protocol_js_1.DEFAULT_KCC20_MIN_PROTOCOL_FEE_SOMPI
|
|
853
|
+
: percentageFee;
|
|
854
|
+
}
|
|
855
|
+
function selectFundingEntry(entries, requiredSompi, skippedOutpoints = []) {
|
|
856
|
+
const skipped = new Set(skippedOutpoints);
|
|
857
|
+
const sorted = [...entries].sort((a, b) => {
|
|
858
|
+
const av = utxoAmountSompi(a);
|
|
859
|
+
const bv = utxoAmountSompi(b);
|
|
860
|
+
if (av === bv)
|
|
861
|
+
return 0;
|
|
862
|
+
return av < bv ? 1 : -1;
|
|
863
|
+
});
|
|
864
|
+
const entry = sorted.find((candidate) => {
|
|
865
|
+
const outpoint = `${candidate.outpoint?.transactionId}:${candidate.outpoint?.index}`;
|
|
866
|
+
return (!skipped.has(outpoint) && utxoAmountSompi(candidate) >= requiredSompi);
|
|
867
|
+
});
|
|
868
|
+
if (!entry) {
|
|
869
|
+
throw new Error(`no single funding UTXO can cover ${requiredSompi} sompi`);
|
|
870
|
+
}
|
|
871
|
+
return entry;
|
|
872
|
+
}
|
|
873
|
+
function requiredToccataFeeSompi(kw, network, transaction) {
|
|
874
|
+
if (typeof kw.calculateTransactionFee !== "function" ||
|
|
875
|
+
typeof kw.calculateTransactionMass !== "function") {
|
|
876
|
+
throw new Error("Kaspa transaction fee calculators are unavailable");
|
|
877
|
+
}
|
|
878
|
+
const calculatedFee = BigInt(kw.calculateTransactionFee(network, transaction, 1) || 0n);
|
|
879
|
+
const baseMass = BigInt(kw.calculateTransactionMass(network, transaction, 1));
|
|
880
|
+
const computeBudget = [...transaction.inputs].reduce((sum, input) => sum + BigInt(input.computeBudget || 0), 0n);
|
|
881
|
+
const relayFee = (baseMass + computeBudget * 100n) * 100n;
|
|
882
|
+
return relayFee > calculatedFee ? relayFee : calculatedFee;
|
|
883
|
+
}
|
|
884
|
+
function utxoAmountSompi(entry) {
|
|
885
|
+
const amount = entry?.amount ?? entry?.utxoEntry?.amount;
|
|
886
|
+
if (amount === undefined || amount === null) {
|
|
887
|
+
throw new Error("UTXO entry is missing amount");
|
|
888
|
+
}
|
|
889
|
+
return BigInt(amount);
|
|
890
|
+
}
|
|
891
|
+
function findOutputIndex(tx, address, networkId, addressFromScriptPublicKey) {
|
|
892
|
+
for (let i = 0; i < tx.outputs.length; i++) {
|
|
893
|
+
const outputAddress = addressFromScriptPublicKey(tx.outputs[i].scriptPublicKey, networkId)?.toString();
|
|
894
|
+
if (outputAddress === address)
|
|
895
|
+
return i;
|
|
896
|
+
}
|
|
897
|
+
return -1;
|
|
898
|
+
}
|
|
899
|
+
function setVersionOneInputMassFields(tx, computeBudget = 30) {
|
|
900
|
+
const inputs = tx.inputs;
|
|
901
|
+
for (const input of inputs) {
|
|
902
|
+
input.sigOpCount = 0;
|
|
903
|
+
input.computeBudget = computeBudget;
|
|
904
|
+
}
|
|
905
|
+
tx.inputs = inputs;
|
|
906
|
+
}
|
|
907
|
+
function extractLockedOutput(lockReceipt, saleCovenantId, tokenAmount) {
|
|
908
|
+
const output = lockReceipt.outputs?.find((candidate) => candidate.ownerIdentifier === saleCovenantId &&
|
|
909
|
+
Number(candidate.identifierType) === 4 &&
|
|
910
|
+
BigInt(candidate.tokenAmount) === BigInt(tokenAmount));
|
|
911
|
+
if (!output) {
|
|
912
|
+
throw new Error(`lock receipt does not contain ${tokenAmount} tokens owned by sale covenant ${saleCovenantId}`);
|
|
913
|
+
}
|
|
914
|
+
if (!lockReceipt.txid) {
|
|
915
|
+
throw new Error("lock receipt is missing submitted txid");
|
|
916
|
+
}
|
|
917
|
+
return {
|
|
918
|
+
txid: lockReceipt.txid,
|
|
919
|
+
vout: Number(output.vout),
|
|
920
|
+
tokenAmount: BigInt(output.tokenAmount),
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
function expectPushLen(script, offset, expected, label) {
|
|
924
|
+
if (script[offset] !== expected) {
|
|
925
|
+
throw new Error(`unsupported KCC20 ${label} push length ${script[offset]}; expected ${expected}`);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
//# sourceMappingURL=sale-common.js.map
|