@chainflip/bitcoin 1.2.2 → 1.2.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/dist/index.cjs +283 -4
- package/dist/index.d.cts +36 -5
- package/dist/index.d.ts +36 -5
- package/dist/index.js +255 -0
- package/package.json +5 -15
- package/dist/address.cjs +0 -91
- package/dist/address.d.cts +0 -28
- package/dist/address.d.ts +0 -28
- package/dist/address.mjs +0 -91
- package/dist/consts.cjs +0 -12
- package/dist/consts.d.cts +0 -5
- package/dist/consts.d.ts +0 -5
- package/dist/consts.mjs +0 -12
- package/dist/deposit.cjs +0 -75
- package/dist/deposit.d.cts +0 -8
- package/dist/deposit.d.ts +0 -8
- package/dist/deposit.mjs +0 -75
- package/dist/index.mjs +0 -6
- package/dist/rpc.cjs +0 -75
- package/dist/rpc.d.cts +0 -166
- package/dist/rpc.d.ts +0 -166
- package/dist/rpc.mjs +0 -75
- package/dist/scale.cjs +0 -29
- package/dist/scale.d.cts +0 -22
- package/dist/scale.d.ts +0 -22
- package/dist/scale.mjs +0 -29
package/dist/address.cjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }// src/address.ts
|
|
2
|
-
var _assertion = require('@chainflip/utils/assertion');
|
|
3
|
-
var _bytes = require('@chainflip/utils/bytes');
|
|
4
|
-
var _bitcoinjslib = require('bitcoinjs-lib'); var bitcoin = _interopRequireWildcard(_bitcoinjslib);
|
|
5
|
-
var _constscjs = require('./consts.cjs');
|
|
6
|
-
var p2pkhAddressVersion = {
|
|
7
|
-
mainnet: 0,
|
|
8
|
-
testnet: 111,
|
|
9
|
-
regtest: 111
|
|
10
|
-
};
|
|
11
|
-
var p2shAddressVersion = {
|
|
12
|
-
mainnet: 5,
|
|
13
|
-
testnet: 196,
|
|
14
|
-
regtest: 196
|
|
15
|
-
};
|
|
16
|
-
var networkHrp = {
|
|
17
|
-
mainnet: "bc",
|
|
18
|
-
testnet: "tb",
|
|
19
|
-
regtest: "bcrt"
|
|
20
|
-
};
|
|
21
|
-
var segwitVersions = {
|
|
22
|
-
P2WPKH: 0,
|
|
23
|
-
P2WSH: 0,
|
|
24
|
-
Taproot: 1
|
|
25
|
-
};
|
|
26
|
-
var byteLikeToUint8Array = (data) => typeof data === "string" ? _bytes.hexToBytes.call(void 0, data) : new Uint8Array(data);
|
|
27
|
-
var encodeAddress = (data, kind, cfOrBtcnetwork) => {
|
|
28
|
-
const btcNetwork = _constscjs.networkMap[cfOrBtcnetwork];
|
|
29
|
-
_assertion.assert.call(void 0, btcNetwork, `Invalid network: ${cfOrBtcnetwork}`);
|
|
30
|
-
_assertion.assert.call(void 0, data.length % 2 === 0, "bytes must have an even number of characters");
|
|
31
|
-
_assertion.assert.call(void 0,
|
|
32
|
-
typeof data !== "string" || /^(0x)?[0-9a-f]*$/.test(data),
|
|
33
|
-
"bytes are not a valid hex string"
|
|
34
|
-
);
|
|
35
|
-
const bytes = byteLikeToUint8Array(data);
|
|
36
|
-
switch (kind) {
|
|
37
|
-
case "P2PKH":
|
|
38
|
-
case "P2SH": {
|
|
39
|
-
const version = (kind === "P2SH" ? p2shAddressVersion : p2pkhAddressVersion)[btcNetwork];
|
|
40
|
-
return bitcoin.address.toBase58Check(bytes, version);
|
|
41
|
-
}
|
|
42
|
-
case "P2WPKH":
|
|
43
|
-
case "P2WSH":
|
|
44
|
-
case "Taproot":
|
|
45
|
-
return bitcoin.address.toBech32(bytes, segwitVersions[kind], networkHrp[btcNetwork]);
|
|
46
|
-
default:
|
|
47
|
-
throw new Error(`Invalid address type: ${kind}`);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
var decodeAddress = (address2, cfOrBtcNetwork) => {
|
|
51
|
-
const network = _constscjs.networkMap[cfOrBtcNetwork];
|
|
52
|
-
if (/^[13mn2]/.test(address2)) {
|
|
53
|
-
const { hash, version } = bitcoin.address.fromBase58Check(address2);
|
|
54
|
-
if (version === p2pkhAddressVersion[network]) {
|
|
55
|
-
return { type: "P2PKH", data: hash, version };
|
|
56
|
-
}
|
|
57
|
-
if (version === p2shAddressVersion[network]) {
|
|
58
|
-
return { type: "P2SH", data: hash, version };
|
|
59
|
-
}
|
|
60
|
-
throw new TypeError(`Invalid version: ${version}`);
|
|
61
|
-
}
|
|
62
|
-
if (/^(bc|tb|bcrt)1/.test(address2)) {
|
|
63
|
-
const { data, prefix, version } = bitcoin.address.fromBech32(address2);
|
|
64
|
-
_assertion.assert.call(void 0, prefix === networkHrp[network], `Invalid prefix: ${prefix}`);
|
|
65
|
-
let type;
|
|
66
|
-
if (version === 0 && data.length === 20) {
|
|
67
|
-
type = "P2WPKH";
|
|
68
|
-
} else if (version === 0) {
|
|
69
|
-
type = "P2WSH";
|
|
70
|
-
} else if (version === 1) {
|
|
71
|
-
type = "Taproot";
|
|
72
|
-
} else {
|
|
73
|
-
throw new TypeError(`Invalid version: ${version}`);
|
|
74
|
-
}
|
|
75
|
-
return { hrp: prefix, data, type, version };
|
|
76
|
-
}
|
|
77
|
-
throw new TypeError(`Invalid address "${address2}" for network "${network}"`);
|
|
78
|
-
};
|
|
79
|
-
var isValidAddressForNetwork = (address2, cfOrBtcNetwork) => {
|
|
80
|
-
try {
|
|
81
|
-
decodeAddress(address2, cfOrBtcNetwork);
|
|
82
|
-
return true;
|
|
83
|
-
} catch (e) {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
exports.decodeAddress = decodeAddress; exports.encodeAddress = encodeAddress; exports.isValidAddressForNetwork = isValidAddressForNetwork;
|
package/dist/address.d.cts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ChainflipNetwork } from '@chainflip/utils/chainflip';
|
|
2
|
-
import { BitcoinNetwork } from './consts.cjs';
|
|
3
|
-
|
|
4
|
-
type Bytelike = Uint8Array | number[] | `0x${string}`;
|
|
5
|
-
declare const networkHrp: {
|
|
6
|
-
readonly mainnet: "bc";
|
|
7
|
-
readonly testnet: "tb";
|
|
8
|
-
readonly regtest: "bcrt";
|
|
9
|
-
};
|
|
10
|
-
type HRP = (typeof networkHrp)[keyof typeof networkHrp];
|
|
11
|
-
type Base58AddressType = 'P2SH' | 'P2PKH';
|
|
12
|
-
type DecodedBase58Address = {
|
|
13
|
-
type: Base58AddressType;
|
|
14
|
-
data: Uint8Array;
|
|
15
|
-
version: number;
|
|
16
|
-
};
|
|
17
|
-
type DecodedSegwitAddress = {
|
|
18
|
-
hrp: HRP;
|
|
19
|
-
data: Uint8Array;
|
|
20
|
-
type: SegwitAddressType;
|
|
21
|
-
version: number;
|
|
22
|
-
};
|
|
23
|
-
type SegwitAddressType = 'P2WPKH' | 'P2WSH' | 'Taproot';
|
|
24
|
-
declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
|
|
25
|
-
declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => DecodedBase58Address | DecodedSegwitAddress;
|
|
26
|
-
declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => boolean;
|
|
27
|
-
|
|
28
|
-
export { type Base58AddressType, type Bytelike, type DecodedBase58Address, type DecodedSegwitAddress, type HRP, type SegwitAddressType, decodeAddress, encodeAddress, isValidAddressForNetwork };
|
package/dist/address.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ChainflipNetwork } from '@chainflip/utils/chainflip';
|
|
2
|
-
import { BitcoinNetwork } from './consts.js';
|
|
3
|
-
|
|
4
|
-
type Bytelike = Uint8Array | number[] | `0x${string}`;
|
|
5
|
-
declare const networkHrp: {
|
|
6
|
-
readonly mainnet: "bc";
|
|
7
|
-
readonly testnet: "tb";
|
|
8
|
-
readonly regtest: "bcrt";
|
|
9
|
-
};
|
|
10
|
-
type HRP = (typeof networkHrp)[keyof typeof networkHrp];
|
|
11
|
-
type Base58AddressType = 'P2SH' | 'P2PKH';
|
|
12
|
-
type DecodedBase58Address = {
|
|
13
|
-
type: Base58AddressType;
|
|
14
|
-
data: Uint8Array;
|
|
15
|
-
version: number;
|
|
16
|
-
};
|
|
17
|
-
type DecodedSegwitAddress = {
|
|
18
|
-
hrp: HRP;
|
|
19
|
-
data: Uint8Array;
|
|
20
|
-
type: SegwitAddressType;
|
|
21
|
-
version: number;
|
|
22
|
-
};
|
|
23
|
-
type SegwitAddressType = 'P2WPKH' | 'P2WSH' | 'Taproot';
|
|
24
|
-
declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
|
|
25
|
-
declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => DecodedBase58Address | DecodedSegwitAddress;
|
|
26
|
-
declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => boolean;
|
|
27
|
-
|
|
28
|
-
export { type Base58AddressType, type Bytelike, type DecodedBase58Address, type DecodedSegwitAddress, type HRP, type SegwitAddressType, decodeAddress, encodeAddress, isValidAddressForNetwork };
|
package/dist/address.mjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
// src/address.ts
|
|
2
|
-
import { assert } from "@chainflip/utils/assertion";
|
|
3
|
-
import { hexToBytes } from "@chainflip/utils/bytes";
|
|
4
|
-
import * as bitcoin from "bitcoinjs-lib";
|
|
5
|
-
import { networkMap } from "./consts.mjs";
|
|
6
|
-
var p2pkhAddressVersion = {
|
|
7
|
-
mainnet: 0,
|
|
8
|
-
testnet: 111,
|
|
9
|
-
regtest: 111
|
|
10
|
-
};
|
|
11
|
-
var p2shAddressVersion = {
|
|
12
|
-
mainnet: 5,
|
|
13
|
-
testnet: 196,
|
|
14
|
-
regtest: 196
|
|
15
|
-
};
|
|
16
|
-
var networkHrp = {
|
|
17
|
-
mainnet: "bc",
|
|
18
|
-
testnet: "tb",
|
|
19
|
-
regtest: "bcrt"
|
|
20
|
-
};
|
|
21
|
-
var segwitVersions = {
|
|
22
|
-
P2WPKH: 0,
|
|
23
|
-
P2WSH: 0,
|
|
24
|
-
Taproot: 1
|
|
25
|
-
};
|
|
26
|
-
var byteLikeToUint8Array = (data) => typeof data === "string" ? hexToBytes(data) : new Uint8Array(data);
|
|
27
|
-
var encodeAddress = (data, kind, cfOrBtcnetwork) => {
|
|
28
|
-
const btcNetwork = networkMap[cfOrBtcnetwork];
|
|
29
|
-
assert(btcNetwork, `Invalid network: ${cfOrBtcnetwork}`);
|
|
30
|
-
assert(data.length % 2 === 0, "bytes must have an even number of characters");
|
|
31
|
-
assert(
|
|
32
|
-
typeof data !== "string" || /^(0x)?[0-9a-f]*$/.test(data),
|
|
33
|
-
"bytes are not a valid hex string"
|
|
34
|
-
);
|
|
35
|
-
const bytes = byteLikeToUint8Array(data);
|
|
36
|
-
switch (kind) {
|
|
37
|
-
case "P2PKH":
|
|
38
|
-
case "P2SH": {
|
|
39
|
-
const version = (kind === "P2SH" ? p2shAddressVersion : p2pkhAddressVersion)[btcNetwork];
|
|
40
|
-
return bitcoin.address.toBase58Check(bytes, version);
|
|
41
|
-
}
|
|
42
|
-
case "P2WPKH":
|
|
43
|
-
case "P2WSH":
|
|
44
|
-
case "Taproot":
|
|
45
|
-
return bitcoin.address.toBech32(bytes, segwitVersions[kind], networkHrp[btcNetwork]);
|
|
46
|
-
default:
|
|
47
|
-
throw new Error(`Invalid address type: ${kind}`);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
var decodeAddress = (address2, cfOrBtcNetwork) => {
|
|
51
|
-
const network = networkMap[cfOrBtcNetwork];
|
|
52
|
-
if (/^[13mn2]/.test(address2)) {
|
|
53
|
-
const { hash, version } = bitcoin.address.fromBase58Check(address2);
|
|
54
|
-
if (version === p2pkhAddressVersion[network]) {
|
|
55
|
-
return { type: "P2PKH", data: hash, version };
|
|
56
|
-
}
|
|
57
|
-
if (version === p2shAddressVersion[network]) {
|
|
58
|
-
return { type: "P2SH", data: hash, version };
|
|
59
|
-
}
|
|
60
|
-
throw new TypeError(`Invalid version: ${version}`);
|
|
61
|
-
}
|
|
62
|
-
if (/^(bc|tb|bcrt)1/.test(address2)) {
|
|
63
|
-
const { data, prefix, version } = bitcoin.address.fromBech32(address2);
|
|
64
|
-
assert(prefix === networkHrp[network], `Invalid prefix: ${prefix}`);
|
|
65
|
-
let type;
|
|
66
|
-
if (version === 0 && data.length === 20) {
|
|
67
|
-
type = "P2WPKH";
|
|
68
|
-
} else if (version === 0) {
|
|
69
|
-
type = "P2WSH";
|
|
70
|
-
} else if (version === 1) {
|
|
71
|
-
type = "Taproot";
|
|
72
|
-
} else {
|
|
73
|
-
throw new TypeError(`Invalid version: ${version}`);
|
|
74
|
-
}
|
|
75
|
-
return { hrp: prefix, data, type, version };
|
|
76
|
-
}
|
|
77
|
-
throw new TypeError(`Invalid address "${address2}" for network "${network}"`);
|
|
78
|
-
};
|
|
79
|
-
var isValidAddressForNetwork = (address2, cfOrBtcNetwork) => {
|
|
80
|
-
try {
|
|
81
|
-
decodeAddress(address2, cfOrBtcNetwork);
|
|
82
|
-
return true;
|
|
83
|
-
} catch {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
export {
|
|
88
|
-
decodeAddress,
|
|
89
|
-
encodeAddress,
|
|
90
|
-
isValidAddressForNetwork
|
|
91
|
-
};
|
package/dist/consts.cjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/consts.ts
|
|
2
|
-
var networkMap = {
|
|
3
|
-
mainnet: "mainnet",
|
|
4
|
-
perseverance: "testnet",
|
|
5
|
-
sisyphos: "testnet",
|
|
6
|
-
testnet: "testnet",
|
|
7
|
-
backspin: "regtest",
|
|
8
|
-
regtest: "regtest"
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
exports.networkMap = networkMap;
|
package/dist/consts.d.cts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
type ChainflipNetwork = 'mainnet' | 'perseverance' | 'sisyphos' | 'backspin';
|
|
2
|
-
type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
|
|
3
|
-
declare const networkMap: Record<ChainflipNetwork | BitcoinNetwork, BitcoinNetwork>;
|
|
4
|
-
|
|
5
|
-
export { type BitcoinNetwork, type ChainflipNetwork, networkMap };
|
package/dist/consts.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
type ChainflipNetwork = 'mainnet' | 'perseverance' | 'sisyphos' | 'backspin';
|
|
2
|
-
type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
|
|
3
|
-
declare const networkMap: Record<ChainflipNetwork | BitcoinNetwork, BitcoinNetwork>;
|
|
4
|
-
|
|
5
|
-
export { type BitcoinNetwork, type ChainflipNetwork, networkMap };
|
package/dist/consts.mjs
DELETED
package/dist/deposit.cjs
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/deposit.ts
|
|
2
|
-
var _assertion = require('@chainflip/utils/assertion');
|
|
3
|
-
var _base58 = require('@chainflip/utils/base58'); var base58 = _interopRequireWildcard(_base58);
|
|
4
|
-
var _bytes = require('@chainflip/utils/bytes');
|
|
5
|
-
var _chainflip = require('@chainflip/utils/chainflip');
|
|
6
|
-
var _consts = require('@chainflip/utils/consts');
|
|
7
|
-
var _ss58 = require('@chainflip/utils/ss58'); var ss58 = _interopRequireWildcard(_ss58);
|
|
8
|
-
var _bignumberjs = require('bignumber.js');
|
|
9
|
-
var _rpccjs = require('./rpc.cjs'); var rpc = _interopRequireWildcard(_rpccjs);
|
|
10
|
-
var _scalecjs = require('./scale.cjs');
|
|
11
|
-
var encodeChainAddress = (data, asset) => {
|
|
12
|
-
switch (_chainflip.assetConstants[asset].chain) {
|
|
13
|
-
case "Solana":
|
|
14
|
-
return base58.encode(data);
|
|
15
|
-
case "Polkadot":
|
|
16
|
-
return ss58.encode({ data, ss58Format: _consts.POLKADOT_SS58_PREFIX });
|
|
17
|
-
case "Ethereum":
|
|
18
|
-
case "Arbitrum":
|
|
19
|
-
return _bytes.bytesToHex.call(void 0, data);
|
|
20
|
-
case "Bitcoin":
|
|
21
|
-
return new TextDecoder().decode(data);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
var contractIdToInternalAsset = Object.fromEntries(
|
|
25
|
-
Object.entries(_chainflip.assetContractId).map(([asset, id]) => [id, asset])
|
|
26
|
-
);
|
|
27
|
-
var parseVaultSwapData = (data) => {
|
|
28
|
-
const version = data[0];
|
|
29
|
-
_assertion.assert.call(void 0, version === 0, "unsupported version");
|
|
30
|
-
const contractId = data[1];
|
|
31
|
-
const outputAsset = contractIdToInternalAsset[contractId];
|
|
32
|
-
_assertion.assert.call(void 0, outputAsset, "unknown asset contract id");
|
|
33
|
-
const { destinationAddress, parameters } = _scalecjs.createSwapDataCodec.call(void 0, outputAsset).dec(data);
|
|
34
|
-
return {
|
|
35
|
-
...parameters,
|
|
36
|
-
outputAsset,
|
|
37
|
-
destinationAddress: encodeChainAddress(destinationAddress, outputAsset)
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
var getX128PriceFromAmounts = (depositAmount, minOutputAmount) => BigInt(
|
|
41
|
-
new (0, _bignumberjs.BigNumber)(minOutputAmount.toString()).div(depositAmount.toString()).multipliedBy(new (0, _bignumberjs.BigNumber)(2).pow(128)).toFixed(0, _bignumberjs.BigNumber.ROUND_FLOOR)
|
|
42
|
-
);
|
|
43
|
-
var findVaultSwapData = async (url, txId) => {
|
|
44
|
-
const tx = await rpc.makeRequest(url, "getrawtransaction", [txId, true]);
|
|
45
|
-
if (!tx) return null;
|
|
46
|
-
const data = parseVaultSwapData(tx.vout[1].scriptPubKey.hex);
|
|
47
|
-
const amount = tx.vout[0].value;
|
|
48
|
-
const block = await rpc.makeRequest(url, "getblock", [tx.blockhash, true]).catch(() => ({
|
|
49
|
-
height: 0
|
|
50
|
-
}));
|
|
51
|
-
return {
|
|
52
|
-
inputAsset: "Btc",
|
|
53
|
-
amount,
|
|
54
|
-
depositAddress: tx.vout[0].scriptPubKey.address,
|
|
55
|
-
refundParams: {
|
|
56
|
-
refundAddress: tx.vout[2].scriptPubKey.address,
|
|
57
|
-
retryDuration: data.retryDuration,
|
|
58
|
-
minPrice: getX128PriceFromAmounts(amount, data.minOutputAmount)
|
|
59
|
-
},
|
|
60
|
-
destinationAddress: data.destinationAddress,
|
|
61
|
-
outputAsset: data.outputAsset,
|
|
62
|
-
brokerFee: { account: null, commissionBps: data.brokerFee },
|
|
63
|
-
affiliateFees: data.affiliates,
|
|
64
|
-
ccmDepositMetadata: null,
|
|
65
|
-
maxBoostFee: data.boostFee,
|
|
66
|
-
dcaParams: data.numberOfChunks > 0 && data.chunkInterval > 0 ? {
|
|
67
|
-
chunkInterval: data.chunkInterval,
|
|
68
|
-
numberOfChunks: data.numberOfChunks
|
|
69
|
-
} : null,
|
|
70
|
-
depositChainBlockHeight: _nullishCoalesce(_optionalChain([block, 'optionalAccess', _ => _.height]), () => ( 0))
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
exports.findVaultSwapData = findVaultSwapData;
|
package/dist/deposit.d.cts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { VaultSwapData } from '@chainflip/utils/types';
|
|
2
|
-
|
|
3
|
-
type BitcoinVaultSwapData = VaultSwapData & {
|
|
4
|
-
depositAddress: string;
|
|
5
|
-
};
|
|
6
|
-
declare const findVaultSwapData: (url: string, txId: string) => Promise<BitcoinVaultSwapData | null>;
|
|
7
|
-
|
|
8
|
-
export { type BitcoinVaultSwapData, findVaultSwapData };
|
package/dist/deposit.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { VaultSwapData } from '@chainflip/utils/types';
|
|
2
|
-
|
|
3
|
-
type BitcoinVaultSwapData = VaultSwapData & {
|
|
4
|
-
depositAddress: string;
|
|
5
|
-
};
|
|
6
|
-
declare const findVaultSwapData: (url: string, txId: string) => Promise<BitcoinVaultSwapData | null>;
|
|
7
|
-
|
|
8
|
-
export { type BitcoinVaultSwapData, findVaultSwapData };
|
package/dist/deposit.mjs
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
// src/deposit.ts
|
|
2
|
-
import { assert } from "@chainflip/utils/assertion";
|
|
3
|
-
import * as base58 from "@chainflip/utils/base58";
|
|
4
|
-
import { bytesToHex } from "@chainflip/utils/bytes";
|
|
5
|
-
import { assetConstants, assetContractId } from "@chainflip/utils/chainflip";
|
|
6
|
-
import { POLKADOT_SS58_PREFIX } from "@chainflip/utils/consts";
|
|
7
|
-
import * as ss58 from "@chainflip/utils/ss58";
|
|
8
|
-
import { BigNumber } from "bignumber.js";
|
|
9
|
-
import * as rpc from "./rpc.mjs";
|
|
10
|
-
import { createSwapDataCodec } from "./scale.mjs";
|
|
11
|
-
var encodeChainAddress = (data, asset) => {
|
|
12
|
-
switch (assetConstants[asset].chain) {
|
|
13
|
-
case "Solana":
|
|
14
|
-
return base58.encode(data);
|
|
15
|
-
case "Polkadot":
|
|
16
|
-
return ss58.encode({ data, ss58Format: POLKADOT_SS58_PREFIX });
|
|
17
|
-
case "Ethereum":
|
|
18
|
-
case "Arbitrum":
|
|
19
|
-
return bytesToHex(data);
|
|
20
|
-
case "Bitcoin":
|
|
21
|
-
return new TextDecoder().decode(data);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
var contractIdToInternalAsset = Object.fromEntries(
|
|
25
|
-
Object.entries(assetContractId).map(([asset, id]) => [id, asset])
|
|
26
|
-
);
|
|
27
|
-
var parseVaultSwapData = (data) => {
|
|
28
|
-
const version = data[0];
|
|
29
|
-
assert(version === 0, "unsupported version");
|
|
30
|
-
const contractId = data[1];
|
|
31
|
-
const outputAsset = contractIdToInternalAsset[contractId];
|
|
32
|
-
assert(outputAsset, "unknown asset contract id");
|
|
33
|
-
const { destinationAddress, parameters } = createSwapDataCodec(outputAsset).dec(data);
|
|
34
|
-
return {
|
|
35
|
-
...parameters,
|
|
36
|
-
outputAsset,
|
|
37
|
-
destinationAddress: encodeChainAddress(destinationAddress, outputAsset)
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
var getX128PriceFromAmounts = (depositAmount, minOutputAmount) => BigInt(
|
|
41
|
-
new BigNumber(minOutputAmount.toString()).div(depositAmount.toString()).multipliedBy(new BigNumber(2).pow(128)).toFixed(0, BigNumber.ROUND_FLOOR)
|
|
42
|
-
);
|
|
43
|
-
var findVaultSwapData = async (url, txId) => {
|
|
44
|
-
const tx = await rpc.makeRequest(url, "getrawtransaction", [txId, true]);
|
|
45
|
-
if (!tx) return null;
|
|
46
|
-
const data = parseVaultSwapData(tx.vout[1].scriptPubKey.hex);
|
|
47
|
-
const amount = tx.vout[0].value;
|
|
48
|
-
const block = await rpc.makeRequest(url, "getblock", [tx.blockhash, true]).catch(() => ({
|
|
49
|
-
height: 0
|
|
50
|
-
}));
|
|
51
|
-
return {
|
|
52
|
-
inputAsset: "Btc",
|
|
53
|
-
amount,
|
|
54
|
-
depositAddress: tx.vout[0].scriptPubKey.address,
|
|
55
|
-
refundParams: {
|
|
56
|
-
refundAddress: tx.vout[2].scriptPubKey.address,
|
|
57
|
-
retryDuration: data.retryDuration,
|
|
58
|
-
minPrice: getX128PriceFromAmounts(amount, data.minOutputAmount)
|
|
59
|
-
},
|
|
60
|
-
destinationAddress: data.destinationAddress,
|
|
61
|
-
outputAsset: data.outputAsset,
|
|
62
|
-
brokerFee: { account: null, commissionBps: data.brokerFee },
|
|
63
|
-
affiliateFees: data.affiliates,
|
|
64
|
-
ccmDepositMetadata: null,
|
|
65
|
-
maxBoostFee: data.boostFee,
|
|
66
|
-
dcaParams: data.numberOfChunks > 0 && data.chunkInterval > 0 ? {
|
|
67
|
-
chunkInterval: data.chunkInterval,
|
|
68
|
-
numberOfChunks: data.numberOfChunks
|
|
69
|
-
} : null,
|
|
70
|
-
depositChainBlockHeight: block?.height ?? 0
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
export {
|
|
74
|
-
findVaultSwapData
|
|
75
|
-
};
|
package/dist/index.mjs
DELETED
package/dist/rpc.cjs
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/rpc.ts
|
|
2
|
-
var _bytes = require('@chainflip/utils/bytes');
|
|
3
|
-
var _url = require('@chainflip/utils/url');
|
|
4
|
-
var _bignumberjs = require('bignumber.js'); var _bignumberjs2 = _interopRequireDefault(_bignumberjs);
|
|
5
|
-
var _zod = require('zod');
|
|
6
|
-
var hexString = _zod.z.string().regex(/^([0-9a-f]{2})+$/, { message: "expected hex string" });
|
|
7
|
-
var vout = _zod.z.object({
|
|
8
|
-
value: _zod.z.number().transform((n) => BigInt(new (0, _bignumberjs2.default)(n).shiftedBy(8).toFixed(0))),
|
|
9
|
-
n: _zod.z.number()
|
|
10
|
-
});
|
|
11
|
-
var nulldataVout = _zod.z.object({
|
|
12
|
-
scriptPubKey: _zod.z.object({
|
|
13
|
-
type: _zod.z.literal("nulldata"),
|
|
14
|
-
// remove OP_RETURN and PUSH_BYTES_XX
|
|
15
|
-
hex: hexString.transform((x) => _bytes.hexToBytes.call(void 0, `0x${x.slice(4)}`))
|
|
16
|
-
})
|
|
17
|
-
}).and(vout);
|
|
18
|
-
var addressVout = _zod.z.object({
|
|
19
|
-
scriptPubKey: _zod.z.object({
|
|
20
|
-
type: _zod.z.enum([
|
|
21
|
-
"witness_v1_taproot",
|
|
22
|
-
"witness_v0_scripthash",
|
|
23
|
-
"witness_v0_keyhash",
|
|
24
|
-
"pubkeyhash",
|
|
25
|
-
"scripthash"
|
|
26
|
-
]),
|
|
27
|
-
address: _zod.z.string()
|
|
28
|
-
})
|
|
29
|
-
}).and(vout);
|
|
30
|
-
var txSchema = _zod.z.object({
|
|
31
|
-
vout: _zod.z.tuple([addressVout, nulldataVout, addressVout]),
|
|
32
|
-
blockhash: hexString
|
|
33
|
-
});
|
|
34
|
-
var blockSchema = _zod.z.object({
|
|
35
|
-
height: _zod.z.number()
|
|
36
|
-
});
|
|
37
|
-
var responseSchemas = {
|
|
38
|
-
getrawtransaction: txSchema,
|
|
39
|
-
getblock: blockSchema
|
|
40
|
-
};
|
|
41
|
-
var rpcResponse = _zod.z.union([
|
|
42
|
-
_zod.z.object({ result: _zod.z.null(), error: _zod.z.object({ code: _zod.z.number(), message: _zod.z.string() }) }),
|
|
43
|
-
_zod.z.object({ result: _zod.z.unknown(), error: _zod.z.null() })
|
|
44
|
-
]);
|
|
45
|
-
var makeRequest = async (rpcUrl, method, params) => {
|
|
46
|
-
const { url, headers } = _url.parseUrlWithBasicAuth.call(void 0, rpcUrl);
|
|
47
|
-
const res = await fetch(url, {
|
|
48
|
-
method: "POST",
|
|
49
|
-
headers: {
|
|
50
|
-
...headers,
|
|
51
|
-
"Content-Type": "application/json"
|
|
52
|
-
},
|
|
53
|
-
body: JSON.stringify({
|
|
54
|
-
jsonrpc: "2.0",
|
|
55
|
-
id: 1,
|
|
56
|
-
method,
|
|
57
|
-
params
|
|
58
|
-
})
|
|
59
|
-
});
|
|
60
|
-
const json = await res.json();
|
|
61
|
-
const result = rpcResponse.parse(json);
|
|
62
|
-
if (result.error) {
|
|
63
|
-
if (result.error.code === -5) return null;
|
|
64
|
-
throw new Error(`RPC error [${result.error.code}]: ${result.error.message}`);
|
|
65
|
-
}
|
|
66
|
-
const parseResult = responseSchemas[method].safeParse(result.result);
|
|
67
|
-
if (!parseResult.success) {
|
|
68
|
-
if (method === "getrawtransaction") return null;
|
|
69
|
-
throw parseResult.error;
|
|
70
|
-
}
|
|
71
|
-
return parseResult.data;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
exports.makeRequest = makeRequest;
|