@pioneer-platform/chainflip-client 0.0.9 → 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/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # @pioneer-platform/chainflip-client
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Release: minor version bump for all packages
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @pioneer-platform/maya-network@8.4.0
13
+ - @pioneer-platform/loggerdog@8.4.0
14
+ - @pioneer-platform/pioneer-coins@9.4.0
15
+ - @pioneer-platform/pioneer-caip@9.3.0
package/lib/index.d.ts CHANGED
@@ -1 +1,11 @@
1
- export {};
1
+ declare const TAG = " | chainflip | ";
2
+ type BaseDecimal = string | number;
3
+ declare const uuid: any;
4
+ declare const log: any;
5
+ declare let Chain: any, caipToNetworkId: any, shortListSymbolToCaip: any, ChainToNetworkId: any, NetworkIdToChain: any, getChainEnumValue: any;
6
+ declare const createMemo: any, parseMemo: any;
7
+ declare const axios: any;
8
+ declare let networkSupport: any[];
9
+ declare let assetSupport: any[];
10
+ declare const get_quote_info: (quoteId: string) => Promise<any>;
11
+ declare const get_quote: (quote: any) => Promise<any>;
package/lib/index.js CHANGED
@@ -2,11 +2,7 @@
2
2
  /*
3
3
  chainflip Integration
4
4
  - Highlander
5
-
6
- Notes:
7
- lol polkadot cant ts bro, https://substrate.stackexchange.com/questions/4542/typescript-errors-when-using-polkadot-api
8
-
9
- */
5
+ */
10
6
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
11
7
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
12
8
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -16,12 +12,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
16
12
  step((generator = generator.apply(thisArg, _arguments || [])).next());
17
13
  });
18
14
  };
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
15
  const TAG = " | chainflip | ";
21
16
  const { uuid } = require('uuidv4');
22
17
  const log = require('@pioneer-platform/loggerdog')();
23
18
  let { Chain, caipToNetworkId, shortListSymbolToCaip, ChainToNetworkId, NetworkIdToChain, getChainEnumValue } = require("@pioneer-platform/pioneer-caip");
24
19
  const { createMemo, parseMemo } = require('@pioneer-platform/pioneer-coins');
20
+ // Remove Polkadot API imports
25
21
  const axios = require('axios');
26
22
  let networkSupport = [
27
23
  ChainToNetworkId["BTC"],
package/package.json CHANGED
@@ -1,37 +1,36 @@
1
1
  {
2
2
  "name": "@pioneer-platform/chainflip-client",
3
- "version": "0.0.9",
3
+ "version": "0.1.0",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "dependencies": {
7
- "@pioneer-platform/loggerdog": "^8.3.1",
8
- "@pioneer-platform/maya-network": "^8.3.7",
9
- "@pioneer-platform/pioneer-caip": "^9.2.23",
10
- "@pioneer-platform/pioneer-coins": "^9.2.13",
7
+ "@pioneer-platform/loggerdog": "^8.4.0",
8
+ "@pioneer-platform/maya-network": "^8.4.0",
9
+ "@pioneer-platform/pioneer-caip": "^9.3.0",
10
+ "@pioneer-platform/pioneer-coins": "^9.4.0",
11
11
  "@polkadot/api": "^10.13.1",
12
- "axios": "^1.3.4",
12
+ "axios": "^1.6.0",
13
13
  "dotenv": "^8.2.0",
14
- "ethers": "^6.12.0",
14
+ "ethers": "^6.6.4",
15
15
  "uuidv4": "^6.2.13",
16
16
  "web3": "1.10.3"
17
17
  },
18
- "scripts": {
19
- "npm": "npm i",
20
- "test": "npm run build && node __tests__/test-module.js",
21
- "build": "tsc -p .",
22
- "prepublish": "npm run build",
23
- "refresh": "rm -rf ./node_modules ./package-lock.json && npm install"
24
- },
25
18
  "devDependencies": {
26
19
  "@types/jest": "^25.2.3",
27
- "@types/node": "^13.13.21",
20
+ "@types/node": "^18.16.0",
28
21
  "@types/source-map-support": "^0.5.3",
29
22
  "jest": "^26.4.2",
30
23
  "onchange": "^7.0.2",
31
24
  "serve": "^11.3.2",
32
25
  "ts-jest": "^29.0.5",
33
- "typescript": "^5.0.2"
26
+ "typescript": "^5.0.4"
34
27
  },
35
28
  "gitHead": "a76012f6693a12181c4744e53e977a9eaeef0ed3",
36
- "peerDependencies": {}
37
- }
29
+ "scripts": {
30
+ "npm": "pnpm i",
31
+ "test": "pnpm run build && node __tests__/test-module.js",
32
+ "build": "tsc -p .",
33
+ "prepublish": "pnpm run build",
34
+ "refresh": "rm -rf ./node_modules ./package-lock.json && pnpm install"
35
+ }
36
+ }
package/lib/broker.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import { type AssetValue, type GenericSwapParams } from "@swapkit/helpers";
2
- import type { ETHToolbox } from "@swapkit/toolbox-evm";
3
- import type { ChainflipToolbox } from "@swapkit/toolbox-substrate";
4
- import type { SwapDepositResponse, WithdrawFeeResponse } from "./types.ts";
5
- export declare const ChainflipBroker: (chainflipToolbox: Awaited<ReturnType<typeof ChainflipToolbox>>) => {
6
- registerAsBroker: (address: string) => Promise<string | (() => void)>;
7
- requestSwapDepositAddress: (chainflipTransaction: GenericSwapParams & {
8
- brokerCommissionBPS: number;
9
- }) => Promise<SwapDepositResponse>;
10
- fundStateChainAccount: (stateChainAccount: string, amount: AssetValue, evmToolbox: ReturnType<typeof ETHToolbox>) => Promise<string>;
11
- withdrawFee: (params: {
12
- feeAsset: AssetValue;
13
- recipient: string;
14
- }) => Promise<WithdrawFeeResponse>;
15
- };
package/lib/broker.js DELETED
@@ -1,128 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ChainflipBroker = void 0;
13
- const swap_1 = require("@chainflip/sdk/swap");
14
- const helpers_1 = require("@swapkit/helpers");
15
- const helpers_2 = require("@swapkit/helpers");
16
- const keyring_1 = require("@polkadot/keyring");
17
- const util_1 = require("@polkadot/util");
18
- const chainflipGatewayABI_ts_1 = require("./chainflipGatewayABI.ts");
19
- const chainToChainflipChain = new Map([
20
- [helpers_2.Chain.Ethereum, swap_1.Chains.Ethereum],
21
- [helpers_2.Chain.Bitcoin, swap_1.Chains.Bitcoin],
22
- [helpers_2.Chain.Polkadot, swap_1.Chains.Polkadot],
23
- ]);
24
- const registerAsBroker = (toolbox, address) => {
25
- var _a, _b;
26
- const extrinsic = (_b = (_a = toolbox.api.tx.swapping) === null || _a === void 0 ? void 0 : _a.registerAsBroker) === null || _b === void 0 ? void 0 : _b.call(_a, address);
27
- if (!extrinsic) {
28
- throw new Error("chainflip_broker_register");
29
- }
30
- return toolbox.signAndBroadcast(extrinsic);
31
- };
32
- const requestSwapDepositAddress = (toolbox, { sellAsset, buyAsset, recipient, brokerCommissionBPS, }) => __awaiter(void 0, void 0, void 0, function* () {
33
- const isBuyChainPolkadot = buyAsset.chain === helpers_2.Chain.Polkadot;
34
- const recipientAddress = (0, helpers_1.wrapWithThrow)(() => {
35
- return isBuyChainPolkadot
36
- ? toolbox.encodeAddress(toolbox.decodeAddress(recipient), "hex")
37
- : recipient;
38
- }, "chainflip_broker_recipient_error");
39
- return new Promise((resolve) => {
40
- var _a, _b;
41
- const tx = (_b = (_a = toolbox.api.tx.swapping) === null || _a === void 0 ? void 0 : _a.requestSwapDepositAddress) === null || _b === void 0 ? void 0 : _b.call(_a, sellAsset.ticker.toLowerCase(), buyAsset.ticker.toLowerCase(), { [buyAsset.chain.toLowerCase()]: recipientAddress }, helpers_1.SwapKitNumber.fromBigInt(BigInt(brokerCommissionBPS)).getBaseValue("number"), null, 0);
42
- if (!tx) {
43
- throw new Error("chainflip_broker_tx_error");
44
- }
45
- toolbox.signAndBroadcast(tx, (result) => __awaiter(void 0, void 0, void 0, function* () {
46
- var _c, _d, _e;
47
- if (!((_c = result.status) === null || _c === void 0 ? void 0 : _c.isFinalized)) {
48
- return;
49
- }
50
- const depositChannelEvent = result.events.find((event) => event.event.method === "SwapDepositAddressReady");
51
- if (!depositChannelEvent) {
52
- throw new helpers_1.SwapKitError("chainflip_channel_error", "Could not find 'SwapDepositAddressReady' event");
53
- }
54
- const { event: { data: { depositAddress, sourceChainExpiryBlock, destinationAddress, channelId }, }, } = depositChannelEvent.toHuman();
55
- const hash = (_e = (_d = result.status) === null || _d === void 0 ? void 0 : _d.toJSON) === null || _e === void 0 ? void 0 : _e.call(_d);
56
- const header = yield toolbox.api.rpc.chain.getHeader(hash === null || hash === void 0 ? void 0 : hash.finalized);
57
- const depositChannelId = `${header.number}-${chainToChainflipChain.get(sellAsset.chain)}-${channelId.replaceAll(",", "")}`;
58
- resolve({
59
- brokerCommissionBPS,
60
- buyAsset,
61
- depositAddress: Object.values(depositAddress)[0],
62
- depositChannelId,
63
- recipient: Object.values(destinationAddress)[0],
64
- sellAsset,
65
- srcChainExpiryBlock: Number(sourceChainExpiryBlock.replaceAll(",", "")),
66
- });
67
- }));
68
- });
69
- });
70
- const withdrawFee = (toolbox, { feeAsset, recipient }) => {
71
- const isFeeChainPolkadot = feeAsset.chain === helpers_2.Chain.Polkadot;
72
- const recipientAddress = (0, helpers_1.wrapWithThrow)(() => {
73
- return isFeeChainPolkadot
74
- ? toolbox.encodeAddress(toolbox.decodeAddress(recipient), "hex")
75
- : recipient;
76
- }, "chainflip_broker_recipient_error");
77
- return new Promise((resolve) => {
78
- var _a, _b, _c;
79
- const extrinsic = (_c = (_b = (_a = toolbox.api.tx) === null || _a === void 0 ? void 0 : _a.swapping) === null || _b === void 0 ? void 0 : _b.withdraw) === null || _c === void 0 ? void 0 : _c.call(_b, feeAsset.ticker.toLowerCase(), {
80
- [feeAsset.chain.toLowerCase()]: recipientAddress,
81
- });
82
- if (!extrinsic) {
83
- throw new Error("chainflip_broker_withdraw");
84
- }
85
- toolbox.signAndBroadcast(extrinsic, (result) => __awaiter(void 0, void 0, void 0, function* () {
86
- var _d;
87
- if (!((_d = result.status) === null || _d === void 0 ? void 0 : _d.isFinalized)) {
88
- return;
89
- }
90
- const withdrawEvent = result.events.find((event) => event.event.method === "WithdrawalRequested");
91
- if (!withdrawEvent) {
92
- throw new helpers_1.SwapKitError("chainflip_channel_error", "Could not find 'WithdrawalRequested' event");
93
- }
94
- const { event: { data: { egressId, egressAsset, egressAmount, egressFee, destinationAddress }, }, } = withdrawEvent.toHuman();
95
- resolve({
96
- egressId,
97
- egressAsset,
98
- egressAmount,
99
- egressFee,
100
- destinationAddress,
101
- });
102
- }));
103
- });
104
- };
105
- const fundStateChainAccount = (evmToolbox, chainflipToolbox, stateChainAccount, amount) => {
106
- if (amount.symbol !== "FLIP") {
107
- throw new Error("Only FLIP is supported");
108
- }
109
- if (!chainflipToolbox.validateAddress(stateChainAccount)) {
110
- throw new Error("Invalid address");
111
- }
112
- const hexAddress = (0, util_1.isHex)(stateChainAccount)
113
- ? stateChainAccount
114
- : (0, util_1.u8aToHex)((0, keyring_1.decodeAddress)(stateChainAccount));
115
- return evmToolbox.call({
116
- abi: chainflipGatewayABI_ts_1.chainflipGateway,
117
- contractAddress: "0x6995ab7c4d7f4b03f467cf4c8e920427d9621dbd",
118
- funcName: "fundStateChainAccount",
119
- funcParams: [hexAddress, amount],
120
- });
121
- };
122
- const ChainflipBroker = (chainflipToolbox) => ({
123
- registerAsBroker: (address) => registerAsBroker(chainflipToolbox, address),
124
- requestSwapDepositAddress: (chainflipTransaction) => requestSwapDepositAddress(chainflipToolbox, chainflipTransaction),
125
- fundStateChainAccount: (stateChainAccount, amount, evmToolbox) => fundStateChainAccount(evmToolbox, chainflipToolbox, stateChainAccount, amount),
126
- withdrawFee: (params) => withdrawFee(chainflipToolbox, params),
127
- });
128
- exports.ChainflipBroker = ChainflipBroker;
package/lib/plugin.d.ts DELETED
@@ -1,42 +0,0 @@
1
- import { AssetValue, type BaseWallet } from "@swapkit/helpers";
2
- export declare function confirmSwap({ buyAsset, sellAsset, recipient, brokerEndpoint, }: {
3
- buyAsset: AssetValue;
4
- sellAsset: AssetValue;
5
- recipient: string;
6
- brokerEndpoint: string;
7
- }): Promise<{
8
- channelId: string;
9
- depositAddress: string;
10
- chain: string;
11
- }>;
12
- export declare const ChainflipPlugin: {
13
- readonly chainflip: {
14
- readonly plugin: ({ wallets, config: { brokerEndpoint }, }: {
15
- wallets: BaseWallet<{
16
- [key: string]: NotWorth;
17
- }>;
18
- config: {
19
- brokerEndpoint: string;
20
- };
21
- }) => {
22
- swap: ({ recipient, ...rest }: SwapParams<"chainflip">) => Promise<string>;
23
- };
24
- };
25
- };
26
- /**
27
- * @deprecated Use import { ChainflipPlugin } from "@swapkit/chainflip" instead
28
- */
29
- export declare const ChainflipProvider: {
30
- readonly chainflip: {
31
- readonly plugin: ({ wallets, config: { brokerEndpoint }, }: {
32
- wallets: BaseWallet<{
33
- [key: string]: NotWorth;
34
- }>;
35
- config: {
36
- brokerEndpoint: string;
37
- };
38
- }) => {
39
- swap: ({ recipient, ...rest }: SwapParams<"chainflip">) => Promise<string>;
40
- };
41
- };
42
- };
package/lib/plugin.js DELETED
@@ -1,83 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __rest = (this && this.__rest) || function (s, e) {
12
- var t = {};
13
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
- t[p] = s[p];
15
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
- t[p[i]] = s[p[i]];
19
- }
20
- return t;
21
- };
22
- Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.ChainflipProvider = exports.ChainflipPlugin = exports.confirmSwap = void 0;
24
- const helpers_1 = require("@swapkit/helpers");
25
- function confirmSwap({ buyAsset, sellAsset, recipient, brokerEndpoint, }) {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- try {
28
- const response = yield fetch(brokerEndpoint, {
29
- method: "POST",
30
- headers: { "Content-Type": "application/json" },
31
- body: JSON.stringify({
32
- buyAsset: buyAsset.toString(),
33
- sellAsset: sellAsset.toString(),
34
- destinationAddress: recipient,
35
- }),
36
- }).then((res) => res.json());
37
- return response;
38
- }
39
- catch (error) {
40
- throw new helpers_1.SwapKitError("chainflip_channel_error", error);
41
- }
42
- });
43
- }
44
- exports.confirmSwap = confirmSwap;
45
- const plugin = ({ wallets, config: { brokerEndpoint }, }) => {
46
- function swap(_a) {
47
- var _b, _c;
48
- var { recipient } = _a, rest = __rest(_a, ["recipient"]);
49
- return __awaiter(this, void 0, void 0, function* () {
50
- if (!("route" in rest && ((_b = rest.route) === null || _b === void 0 ? void 0 : _b.buyAsset) && brokerEndpoint)) {
51
- throw new helpers_1.SwapKitError("core_swap_invalid_params");
52
- }
53
- const { buyAsset: buyString, sellAsset: sellString, sellAmount } = rest.route;
54
- if (!(sellString && buyString)) {
55
- throw new helpers_1.SwapKitError("core_swap_asset_not_recognized");
56
- }
57
- const sellAsset = yield helpers_1.AssetValue.fromString(sellString);
58
- if (!(wallets === null || wallets === void 0 ? void 0 : wallets[sellAsset.chain])) {
59
- throw new helpers_1.SwapKitError("core_wallet_connection_not_found");
60
- }
61
- const buyAsset = yield helpers_1.AssetValue.fromString(buyString);
62
- const assetValue = sellAsset.set(sellAmount);
63
- const { depositAddress } = yield confirmSwap({
64
- brokerEndpoint,
65
- buyAsset,
66
- recipient,
67
- sellAsset,
68
- });
69
- const tx = yield wallets[sellAsset.chain].transfer({
70
- assetValue,
71
- from: (_c = wallets[sellAsset.chain]) === null || _c === void 0 ? void 0 : _c.address,
72
- recipient: depositAddress,
73
- });
74
- return tx;
75
- });
76
- }
77
- return { swap };
78
- };
79
- exports.ChainflipPlugin = { chainflip: { plugin } };
80
- /**
81
- * @deprecated Use import { ChainflipPlugin } from "@swapkit/chainflip" instead
82
- */
83
- exports.ChainflipProvider = exports.ChainflipPlugin;
package/lib/types.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import type { AssetValue } from "@swapkit/helpers";
2
- export type WithdrawFeeResponse = {
3
- egressId: string;
4
- egressAsset: string;
5
- egressAmount: string;
6
- egressFee: string;
7
- destinationAddress: string;
8
- };
9
- export type SwapDepositResponse = {
10
- depositChannelId: string;
11
- depositAddress: string;
12
- srcChainExpiryBlock: number;
13
- sellAsset: AssetValue;
14
- buyAsset: AssetValue;
15
- recipient: string;
16
- brokerCommissionBPS: number;
17
- };
package/lib/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });