@chainflip/bitcoin 1.2.7 → 2.1.0-beta.1
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/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/address.cjs +93 -0
- package/dist/address.d.cts +29 -0
- package/dist/address.d.mts +29 -0
- package/dist/address.mjs +89 -0
- package/dist/consts.cjs +13 -0
- package/dist/consts.d.cts +7 -0
- package/dist/consts.d.mts +7 -0
- package/dist/consts.mjs +12 -0
- package/dist/deposit.cjs +80 -0
- package/dist/deposit.d.cts +9 -0
- package/dist/deposit.d.mts +9 -0
- package/dist/deposit.mjs +76 -0
- package/dist/index.cjs +6 -318
- package/dist/index.d.cts +4 -35
- package/dist/index.d.mts +4 -0
- package/dist/index.mjs +4 -0
- package/dist/rpc.cjs +86 -0
- package/dist/rpc.d.cts +167 -0
- package/dist/rpc.d.mts +167 -0
- package/dist/rpc.mjs +84 -0
- package/dist/scale.cjs +52 -0
- package/dist/scale.d.cts +44 -0
- package/dist/scale.d.mts +44 -0
- package/dist/scale.mjs +50 -0
- package/package.json +5 -6
- package/dist/index.d.ts +0 -35
- package/dist/index.js +0 -289
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/dist/address.cjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_consts = require('./consts.cjs');
|
|
3
|
+
let _chainflip_utils_assertion = require("@chainflip/utils/assertion");
|
|
4
|
+
let _chainflip_utils_bytes = require("@chainflip/utils/bytes");
|
|
5
|
+
let bitcoinjs_lib = require("bitcoinjs-lib");
|
|
6
|
+
bitcoinjs_lib = require_rolldown_runtime.__toESM(bitcoinjs_lib);
|
|
7
|
+
|
|
8
|
+
//#region src/address.ts
|
|
9
|
+
const p2pkhAddressVersion = {
|
|
10
|
+
mainnet: 0,
|
|
11
|
+
testnet: 111,
|
|
12
|
+
regtest: 111
|
|
13
|
+
};
|
|
14
|
+
const p2shAddressVersion = {
|
|
15
|
+
mainnet: 5,
|
|
16
|
+
testnet: 196,
|
|
17
|
+
regtest: 196
|
|
18
|
+
};
|
|
19
|
+
const networkHrp = {
|
|
20
|
+
mainnet: "bc",
|
|
21
|
+
testnet: "tb",
|
|
22
|
+
regtest: "bcrt"
|
|
23
|
+
};
|
|
24
|
+
const segwitVersions = {
|
|
25
|
+
P2WPKH: 0,
|
|
26
|
+
P2WSH: 0,
|
|
27
|
+
Taproot: 1
|
|
28
|
+
};
|
|
29
|
+
const byteLikeToUint8Array = (data) => typeof data === "string" ? (0, _chainflip_utils_bytes.hexToBytes)(data) : new Uint8Array(data);
|
|
30
|
+
const encodeAddress = (data, kind, cfOrBtcnetwork) => {
|
|
31
|
+
const btcNetwork = require_consts.networkMap[cfOrBtcnetwork];
|
|
32
|
+
(0, _chainflip_utils_assertion.assert)(btcNetwork, `Invalid network: ${cfOrBtcnetwork}`);
|
|
33
|
+
(0, _chainflip_utils_assertion.assert)(data.length % 2 === 0, "bytes must have an even number of characters");
|
|
34
|
+
(0, _chainflip_utils_assertion.assert)(typeof data !== "string" || /^(0x)?[0-9a-f]*$/.test(data), "bytes are not a valid hex string");
|
|
35
|
+
const bytes = byteLikeToUint8Array(data);
|
|
36
|
+
switch (kind) {
|
|
37
|
+
case "P2PKH":
|
|
38
|
+
case "P2SH": {
|
|
39
|
+
const version = (kind === "P2SH" ? p2shAddressVersion : p2pkhAddressVersion)[btcNetwork];
|
|
40
|
+
return bitcoinjs_lib.address.toBase58Check(bytes, version);
|
|
41
|
+
}
|
|
42
|
+
case "P2WPKH":
|
|
43
|
+
case "P2WSH":
|
|
44
|
+
case "Taproot": return bitcoinjs_lib.address.toBech32(bytes, segwitVersions[kind], networkHrp[btcNetwork]);
|
|
45
|
+
default: throw new Error(`Invalid address type: ${kind}`);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const decodeAddress = (address, cfOrBtcNetwork) => {
|
|
49
|
+
const network = require_consts.networkMap[cfOrBtcNetwork];
|
|
50
|
+
if (/^[13mn2]/.test(address)) {
|
|
51
|
+
const { hash, version } = bitcoinjs_lib.address.fromBase58Check(address);
|
|
52
|
+
if (version === p2pkhAddressVersion[network]) return {
|
|
53
|
+
type: "P2PKH",
|
|
54
|
+
data: hash,
|
|
55
|
+
version
|
|
56
|
+
};
|
|
57
|
+
if (version === p2shAddressVersion[network]) return {
|
|
58
|
+
type: "P2SH",
|
|
59
|
+
data: hash,
|
|
60
|
+
version
|
|
61
|
+
};
|
|
62
|
+
throw new TypeError(`Invalid version: ${version}`);
|
|
63
|
+
}
|
|
64
|
+
if (/^(bc|tb|bcrt)1/.test(address)) {
|
|
65
|
+
const { data, prefix, version } = bitcoinjs_lib.address.fromBech32(address);
|
|
66
|
+
(0, _chainflip_utils_assertion.assert)(prefix === networkHrp[network], `Invalid prefix: ${prefix}`);
|
|
67
|
+
let type;
|
|
68
|
+
if (version === 0 && data.length === 20) type = "P2WPKH";
|
|
69
|
+
else if (version === 0) type = "P2WSH";
|
|
70
|
+
else if (version === 1) type = "Taproot";
|
|
71
|
+
else throw new TypeError(`Invalid version: ${version}`);
|
|
72
|
+
return {
|
|
73
|
+
hrp: prefix,
|
|
74
|
+
data,
|
|
75
|
+
type,
|
|
76
|
+
version
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
throw new TypeError(`Invalid address "${address}" for network "${network}"`);
|
|
80
|
+
};
|
|
81
|
+
const isValidAddressForNetwork = (address, cfOrBtcNetwork) => {
|
|
82
|
+
try {
|
|
83
|
+
decodeAddress(address, cfOrBtcNetwork);
|
|
84
|
+
return true;
|
|
85
|
+
} catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
exports.decodeAddress = decodeAddress;
|
|
92
|
+
exports.encodeAddress = encodeAddress;
|
|
93
|
+
exports.isValidAddressForNetwork = isValidAddressForNetwork;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BitcoinNetwork } from "./consts.cjs";
|
|
2
|
+
import { ChainflipNetwork } from "@chainflip/utils/chainflip";
|
|
3
|
+
|
|
4
|
+
//#region src/address.d.ts
|
|
5
|
+
type Bytelike = Uint8Array | number[] | `0x${string}`;
|
|
6
|
+
declare const networkHrp: {
|
|
7
|
+
readonly mainnet: "bc";
|
|
8
|
+
readonly testnet: "tb";
|
|
9
|
+
readonly regtest: "bcrt";
|
|
10
|
+
};
|
|
11
|
+
type HRP = (typeof networkHrp)[keyof typeof networkHrp];
|
|
12
|
+
type Base58AddressType = 'P2SH' | 'P2PKH';
|
|
13
|
+
type DecodedBase58Address = {
|
|
14
|
+
type: Base58AddressType;
|
|
15
|
+
data: Uint8Array;
|
|
16
|
+
version: number;
|
|
17
|
+
};
|
|
18
|
+
type DecodedSegwitAddress = {
|
|
19
|
+
hrp: HRP;
|
|
20
|
+
data: Uint8Array;
|
|
21
|
+
type: SegwitAddressType;
|
|
22
|
+
version: number;
|
|
23
|
+
};
|
|
24
|
+
type SegwitAddressType = 'P2WPKH' | 'P2WSH' | 'Taproot';
|
|
25
|
+
declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
|
|
26
|
+
declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => DecodedBase58Address | DecodedSegwitAddress;
|
|
27
|
+
declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => boolean;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { Base58AddressType, Bytelike, DecodedBase58Address, DecodedSegwitAddress, HRP, SegwitAddressType, decodeAddress, encodeAddress, isValidAddressForNetwork };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BitcoinNetwork } from "./consts.mjs";
|
|
2
|
+
import { ChainflipNetwork } from "@chainflip/utils/chainflip";
|
|
3
|
+
|
|
4
|
+
//#region src/address.d.ts
|
|
5
|
+
type Bytelike = Uint8Array | number[] | `0x${string}`;
|
|
6
|
+
declare const networkHrp: {
|
|
7
|
+
readonly mainnet: "bc";
|
|
8
|
+
readonly testnet: "tb";
|
|
9
|
+
readonly regtest: "bcrt";
|
|
10
|
+
};
|
|
11
|
+
type HRP = (typeof networkHrp)[keyof typeof networkHrp];
|
|
12
|
+
type Base58AddressType = 'P2SH' | 'P2PKH';
|
|
13
|
+
type DecodedBase58Address = {
|
|
14
|
+
type: Base58AddressType;
|
|
15
|
+
data: Uint8Array;
|
|
16
|
+
version: number;
|
|
17
|
+
};
|
|
18
|
+
type DecodedSegwitAddress = {
|
|
19
|
+
hrp: HRP;
|
|
20
|
+
data: Uint8Array;
|
|
21
|
+
type: SegwitAddressType;
|
|
22
|
+
version: number;
|
|
23
|
+
};
|
|
24
|
+
type SegwitAddressType = 'P2WPKH' | 'P2WSH' | 'Taproot';
|
|
25
|
+
declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
|
|
26
|
+
declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => DecodedBase58Address | DecodedSegwitAddress;
|
|
27
|
+
declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => boolean;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { Base58AddressType, Bytelike, DecodedBase58Address, DecodedSegwitAddress, HRP, SegwitAddressType, decodeAddress, encodeAddress, isValidAddressForNetwork };
|
package/dist/address.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { networkMap } from "./consts.mjs";
|
|
2
|
+
import { assert } from "@chainflip/utils/assertion";
|
|
3
|
+
import { hexToBytes } from "@chainflip/utils/bytes";
|
|
4
|
+
import * as bitcoin from "bitcoinjs-lib";
|
|
5
|
+
|
|
6
|
+
//#region src/address.ts
|
|
7
|
+
const p2pkhAddressVersion = {
|
|
8
|
+
mainnet: 0,
|
|
9
|
+
testnet: 111,
|
|
10
|
+
regtest: 111
|
|
11
|
+
};
|
|
12
|
+
const p2shAddressVersion = {
|
|
13
|
+
mainnet: 5,
|
|
14
|
+
testnet: 196,
|
|
15
|
+
regtest: 196
|
|
16
|
+
};
|
|
17
|
+
const networkHrp = {
|
|
18
|
+
mainnet: "bc",
|
|
19
|
+
testnet: "tb",
|
|
20
|
+
regtest: "bcrt"
|
|
21
|
+
};
|
|
22
|
+
const segwitVersions = {
|
|
23
|
+
P2WPKH: 0,
|
|
24
|
+
P2WSH: 0,
|
|
25
|
+
Taproot: 1
|
|
26
|
+
};
|
|
27
|
+
const byteLikeToUint8Array = (data) => typeof data === "string" ? hexToBytes(data) : new Uint8Array(data);
|
|
28
|
+
const encodeAddress = (data, kind, cfOrBtcnetwork) => {
|
|
29
|
+
const btcNetwork = networkMap[cfOrBtcnetwork];
|
|
30
|
+
assert(btcNetwork, `Invalid network: ${cfOrBtcnetwork}`);
|
|
31
|
+
assert(data.length % 2 === 0, "bytes must have an even number of characters");
|
|
32
|
+
assert(typeof data !== "string" || /^(0x)?[0-9a-f]*$/.test(data), "bytes are not a valid hex string");
|
|
33
|
+
const bytes = byteLikeToUint8Array(data);
|
|
34
|
+
switch (kind) {
|
|
35
|
+
case "P2PKH":
|
|
36
|
+
case "P2SH": {
|
|
37
|
+
const version = (kind === "P2SH" ? p2shAddressVersion : p2pkhAddressVersion)[btcNetwork];
|
|
38
|
+
return bitcoin.address.toBase58Check(bytes, version);
|
|
39
|
+
}
|
|
40
|
+
case "P2WPKH":
|
|
41
|
+
case "P2WSH":
|
|
42
|
+
case "Taproot": return bitcoin.address.toBech32(bytes, segwitVersions[kind], networkHrp[btcNetwork]);
|
|
43
|
+
default: throw new Error(`Invalid address type: ${kind}`);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const decodeAddress = (address, cfOrBtcNetwork) => {
|
|
47
|
+
const network = networkMap[cfOrBtcNetwork];
|
|
48
|
+
if (/^[13mn2]/.test(address)) {
|
|
49
|
+
const { hash, version } = bitcoin.address.fromBase58Check(address);
|
|
50
|
+
if (version === p2pkhAddressVersion[network]) return {
|
|
51
|
+
type: "P2PKH",
|
|
52
|
+
data: hash,
|
|
53
|
+
version
|
|
54
|
+
};
|
|
55
|
+
if (version === p2shAddressVersion[network]) return {
|
|
56
|
+
type: "P2SH",
|
|
57
|
+
data: hash,
|
|
58
|
+
version
|
|
59
|
+
};
|
|
60
|
+
throw new TypeError(`Invalid version: ${version}`);
|
|
61
|
+
}
|
|
62
|
+
if (/^(bc|tb|bcrt)1/.test(address)) {
|
|
63
|
+
const { data, prefix, version } = bitcoin.address.fromBech32(address);
|
|
64
|
+
assert(prefix === networkHrp[network], `Invalid prefix: ${prefix}`);
|
|
65
|
+
let type;
|
|
66
|
+
if (version === 0 && data.length === 20) type = "P2WPKH";
|
|
67
|
+
else if (version === 0) type = "P2WSH";
|
|
68
|
+
else if (version === 1) type = "Taproot";
|
|
69
|
+
else throw new TypeError(`Invalid version: ${version}`);
|
|
70
|
+
return {
|
|
71
|
+
hrp: prefix,
|
|
72
|
+
data,
|
|
73
|
+
type,
|
|
74
|
+
version
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
throw new TypeError(`Invalid address "${address}" for network "${network}"`);
|
|
78
|
+
};
|
|
79
|
+
const isValidAddressForNetwork = (address, cfOrBtcNetwork) => {
|
|
80
|
+
try {
|
|
81
|
+
decodeAddress(address, cfOrBtcNetwork);
|
|
82
|
+
return true;
|
|
83
|
+
} catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
export { decodeAddress, encodeAddress, isValidAddressForNetwork };
|
package/dist/consts.cjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ChainflipNetwork } from "@chainflip/utils/chainflip";
|
|
2
|
+
|
|
3
|
+
//#region src/consts.d.ts
|
|
4
|
+
type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
|
|
5
|
+
declare const networkMap: Record<ChainflipNetwork | BitcoinNetwork, BitcoinNetwork>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { BitcoinNetwork, networkMap };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ChainflipNetwork } from "@chainflip/utils/chainflip";
|
|
2
|
+
|
|
3
|
+
//#region src/consts.d.ts
|
|
4
|
+
type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
|
|
5
|
+
declare const networkMap: Record<ChainflipNetwork | BitcoinNetwork, BitcoinNetwork>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { BitcoinNetwork, networkMap };
|
package/dist/consts.mjs
ADDED
package/dist/deposit.cjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_rpc = require('./rpc.cjs');
|
|
3
|
+
const require_scale = require('./scale.cjs');
|
|
4
|
+
let _chainflip_utils_assertion = require("@chainflip/utils/assertion");
|
|
5
|
+
let _chainflip_utils_bytes = require("@chainflip/utils/bytes");
|
|
6
|
+
let _chainflip_utils_base58 = require("@chainflip/utils/base58");
|
|
7
|
+
_chainflip_utils_base58 = require_rolldown_runtime.__toESM(_chainflip_utils_base58);
|
|
8
|
+
let _chainflip_utils_chainflip = require("@chainflip/utils/chainflip");
|
|
9
|
+
let _chainflip_utils_consts = require("@chainflip/utils/consts");
|
|
10
|
+
let _chainflip_utils_ss58 = require("@chainflip/utils/ss58");
|
|
11
|
+
_chainflip_utils_ss58 = require_rolldown_runtime.__toESM(_chainflip_utils_ss58);
|
|
12
|
+
let bignumber_js = require("bignumber.js");
|
|
13
|
+
bignumber_js = require_rolldown_runtime.__toESM(bignumber_js);
|
|
14
|
+
|
|
15
|
+
//#region src/deposit.ts
|
|
16
|
+
const encodeChainAddress = (data, asset) => {
|
|
17
|
+
switch (_chainflip_utils_chainflip.assetConstants[asset].chain) {
|
|
18
|
+
case "Solana": return _chainflip_utils_base58.encode(data);
|
|
19
|
+
case "Assethub":
|
|
20
|
+
case "Polkadot": return _chainflip_utils_ss58.encode({
|
|
21
|
+
data,
|
|
22
|
+
ss58Format: _chainflip_utils_consts.POLKADOT_SS58_PREFIX
|
|
23
|
+
});
|
|
24
|
+
case "Ethereum":
|
|
25
|
+
case "Arbitrum": return (0, _chainflip_utils_bytes.bytesToHex)(data);
|
|
26
|
+
case "Bitcoin": return new TextDecoder().decode(data);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const contractIdToInternalAsset = Object.fromEntries(Object.entries(_chainflip_utils_chainflip.assetContractId).map(([asset, id]) => [id, asset]));
|
|
30
|
+
const parseVaultSwapData = (data) => {
|
|
31
|
+
const version = data[0];
|
|
32
|
+
const outputAsset = contractIdToInternalAsset[data[1]];
|
|
33
|
+
let destinationAddress;
|
|
34
|
+
let parameters;
|
|
35
|
+
if (version === 1) ({destinationAddress, parameters} = require_scale.createSwapDataCodecV1(outputAsset).dec(data));
|
|
36
|
+
else if (version === 0) ({destinationAddress, parameters} = require_scale.createSwapDataCodecV0(outputAsset).dec(data));
|
|
37
|
+
else throw new Error("unsupported version");
|
|
38
|
+
(0, _chainflip_utils_assertion.assert)(outputAsset, "unknown asset contract id");
|
|
39
|
+
return {
|
|
40
|
+
...parameters,
|
|
41
|
+
outputAsset,
|
|
42
|
+
destinationAddress: encodeChainAddress(destinationAddress, outputAsset)
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
const getX128PriceFromAmounts = (depositAmount, minOutputAmount) => BigInt(new bignumber_js.default(minOutputAmount.toString()).div(depositAmount.toString()).multipliedBy(new bignumber_js.default(2).pow(128)).toFixed(0, bignumber_js.default.ROUND_FLOOR));
|
|
46
|
+
const findVaultSwapData = async (url, txId) => {
|
|
47
|
+
const tx = await require_rpc.makeRequest(url, "getrawtransaction", [txId, true]);
|
|
48
|
+
if (!tx) return null;
|
|
49
|
+
const data = parseVaultSwapData(tx.vout[1].scriptPubKey.hex);
|
|
50
|
+
const amount = tx.vout[0].value;
|
|
51
|
+
const block = tx.blockhash ? await require_rpc.makeRequest(url, "getblock", [tx.blockhash, true]).catch(() => null) : null;
|
|
52
|
+
return {
|
|
53
|
+
inputAsset: "Btc",
|
|
54
|
+
amount,
|
|
55
|
+
depositAddress: tx.vout[0].scriptPubKey.address,
|
|
56
|
+
refundParams: {
|
|
57
|
+
refundAddress: tx.vout[2].scriptPubKey.address,
|
|
58
|
+
retryDuration: data.retryDuration,
|
|
59
|
+
minPrice: getX128PriceFromAmounts(amount, data.minOutputAmount),
|
|
60
|
+
maxOraclePriceSlippage: "maxOraclePriceSlippage" in data ? data.maxOraclePriceSlippage : null
|
|
61
|
+
},
|
|
62
|
+
destinationAddress: data.destinationAddress,
|
|
63
|
+
outputAsset: data.outputAsset,
|
|
64
|
+
brokerFee: {
|
|
65
|
+
account: null,
|
|
66
|
+
commissionBps: data.brokerFee
|
|
67
|
+
},
|
|
68
|
+
affiliateFees: data.affiliates,
|
|
69
|
+
ccmDepositMetadata: null,
|
|
70
|
+
maxBoostFee: data.boostFee,
|
|
71
|
+
dcaParams: data.numberOfChunks === 1 && data.chunkInterval === 2 ? null : {
|
|
72
|
+
chunkInterval: data.chunkInterval,
|
|
73
|
+
numberOfChunks: data.numberOfChunks
|
|
74
|
+
},
|
|
75
|
+
depositChainBlockHeight: block && block.height
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
exports.findVaultSwapData = findVaultSwapData;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { VaultSwapData } from "@chainflip/utils/types";
|
|
2
|
+
|
|
3
|
+
//#region src/deposit.d.ts
|
|
4
|
+
type BitcoinVaultSwapData = VaultSwapData<null> & {
|
|
5
|
+
depositAddress: string;
|
|
6
|
+
};
|
|
7
|
+
declare const findVaultSwapData: (url: string, txId: string) => Promise<BitcoinVaultSwapData | null>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { BitcoinVaultSwapData, findVaultSwapData };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { VaultSwapData } from "@chainflip/utils/types";
|
|
2
|
+
|
|
3
|
+
//#region src/deposit.d.ts
|
|
4
|
+
type BitcoinVaultSwapData = VaultSwapData<null> & {
|
|
5
|
+
depositAddress: string;
|
|
6
|
+
};
|
|
7
|
+
declare const findVaultSwapData: (url: string, txId: string) => Promise<BitcoinVaultSwapData | null>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { BitcoinVaultSwapData, findVaultSwapData };
|
package/dist/deposit.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { makeRequest } from "./rpc.mjs";
|
|
2
|
+
import { createSwapDataCodecV0, createSwapDataCodecV1 } from "./scale.mjs";
|
|
3
|
+
import { assert } from "@chainflip/utils/assertion";
|
|
4
|
+
import { bytesToHex } from "@chainflip/utils/bytes";
|
|
5
|
+
import * as base58 from "@chainflip/utils/base58";
|
|
6
|
+
import { assetConstants, assetContractId } from "@chainflip/utils/chainflip";
|
|
7
|
+
import { POLKADOT_SS58_PREFIX } from "@chainflip/utils/consts";
|
|
8
|
+
import * as ss58 from "@chainflip/utils/ss58";
|
|
9
|
+
import BigNumber from "bignumber.js";
|
|
10
|
+
|
|
11
|
+
//#region src/deposit.ts
|
|
12
|
+
const encodeChainAddress = (data, asset) => {
|
|
13
|
+
switch (assetConstants[asset].chain) {
|
|
14
|
+
case "Solana": return base58.encode(data);
|
|
15
|
+
case "Assethub":
|
|
16
|
+
case "Polkadot": return ss58.encode({
|
|
17
|
+
data,
|
|
18
|
+
ss58Format: POLKADOT_SS58_PREFIX
|
|
19
|
+
});
|
|
20
|
+
case "Ethereum":
|
|
21
|
+
case "Arbitrum": return bytesToHex(data);
|
|
22
|
+
case "Bitcoin": return new TextDecoder().decode(data);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const contractIdToInternalAsset = Object.fromEntries(Object.entries(assetContractId).map(([asset, id]) => [id, asset]));
|
|
26
|
+
const parseVaultSwapData = (data) => {
|
|
27
|
+
const version = data[0];
|
|
28
|
+
const outputAsset = contractIdToInternalAsset[data[1]];
|
|
29
|
+
let destinationAddress;
|
|
30
|
+
let parameters;
|
|
31
|
+
if (version === 1) ({destinationAddress, parameters} = createSwapDataCodecV1(outputAsset).dec(data));
|
|
32
|
+
else if (version === 0) ({destinationAddress, parameters} = createSwapDataCodecV0(outputAsset).dec(data));
|
|
33
|
+
else throw new Error("unsupported version");
|
|
34
|
+
assert(outputAsset, "unknown asset contract id");
|
|
35
|
+
return {
|
|
36
|
+
...parameters,
|
|
37
|
+
outputAsset,
|
|
38
|
+
destinationAddress: encodeChainAddress(destinationAddress, outputAsset)
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const getX128PriceFromAmounts = (depositAmount, minOutputAmount) => BigInt(new BigNumber(minOutputAmount.toString()).div(depositAmount.toString()).multipliedBy(new BigNumber(2).pow(128)).toFixed(0, BigNumber.ROUND_FLOOR));
|
|
42
|
+
const findVaultSwapData = async (url, txId) => {
|
|
43
|
+
const tx = await makeRequest(url, "getrawtransaction", [txId, true]);
|
|
44
|
+
if (!tx) return null;
|
|
45
|
+
const data = parseVaultSwapData(tx.vout[1].scriptPubKey.hex);
|
|
46
|
+
const amount = tx.vout[0].value;
|
|
47
|
+
const block = tx.blockhash ? await makeRequest(url, "getblock", [tx.blockhash, true]).catch(() => null) : null;
|
|
48
|
+
return {
|
|
49
|
+
inputAsset: "Btc",
|
|
50
|
+
amount,
|
|
51
|
+
depositAddress: tx.vout[0].scriptPubKey.address,
|
|
52
|
+
refundParams: {
|
|
53
|
+
refundAddress: tx.vout[2].scriptPubKey.address,
|
|
54
|
+
retryDuration: data.retryDuration,
|
|
55
|
+
minPrice: getX128PriceFromAmounts(amount, data.minOutputAmount),
|
|
56
|
+
maxOraclePriceSlippage: "maxOraclePriceSlippage" in data ? data.maxOraclePriceSlippage : null
|
|
57
|
+
},
|
|
58
|
+
destinationAddress: data.destinationAddress,
|
|
59
|
+
outputAsset: data.outputAsset,
|
|
60
|
+
brokerFee: {
|
|
61
|
+
account: null,
|
|
62
|
+
commissionBps: data.brokerFee
|
|
63
|
+
},
|
|
64
|
+
affiliateFees: data.affiliates,
|
|
65
|
+
ccmDepositMetadata: null,
|
|
66
|
+
maxBoostFee: data.boostFee,
|
|
67
|
+
dcaParams: data.numberOfChunks === 1 && data.chunkInterval === 2 ? null : {
|
|
68
|
+
chunkInterval: data.chunkInterval,
|
|
69
|
+
numberOfChunks: data.numberOfChunks
|
|
70
|
+
},
|
|
71
|
+
depositChainBlockHeight: block && block.height
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
export { findVaultSwapData };
|