@gardenfi/orderbook 0.1.1 → 0.1.3
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/index13.cjs +1 -0
- package/dist/index13.js +18 -0
- package/dist/index2.cjs +1 -1
- package/dist/index2.js +45 -44
- package/dist/index5.cjs +1 -1
- package/dist/index5.js +1 -1
- package/dist/index6.cjs +1 -1
- package/dist/index6.js +6 -7
- package/dist/index8.cjs +1 -1
- package/dist/index8.js +18 -18
- package/dist/src/lib/auth/siwe.d.ts +5 -4
- package/dist/src/lib/orderbook.d.ts +1 -1
- package/dist/src/lib/orderbook.types.d.ts +9 -30
- package/dist/src/lib/pws/index.d.ts +3 -0
- package/dist/src/lib/pws/interface.d.ts +78 -0
- package/dist/src/lib/pws/service.d.ts +32 -0
- package/dist/src/lib/pws/utils.d.ts +6 -0
- package/dist/src/lib/url/Url.d.ts +5 -0
- package/dist/src/lib/url/index.d.ts +1 -0
- package/dist/src/lib/utils.d.ts +1 -2
- package/package.json +6 -4
package/dist/index13.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class e extends URL{constructor(t,r){super(t,r)}endpoint(t){return new e(t,this)}socket(){if(this.protocol==="https:")return this.origin.replace("https","wss");if(this.protocol==="http:")return this.origin.replace("http","ws");throw new Error("Invalid protocol")}}exports.Url=e;
|
package/dist/index13.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
class r extends URL {
|
|
2
|
+
constructor(t, e) {
|
|
3
|
+
super(t, e);
|
|
4
|
+
}
|
|
5
|
+
endpoint(t) {
|
|
6
|
+
return new r(t, this);
|
|
7
|
+
}
|
|
8
|
+
socket() {
|
|
9
|
+
if (this.protocol === "https:")
|
|
10
|
+
return this.origin.replace("https", "wss");
|
|
11
|
+
if (this.protocol === "http:")
|
|
12
|
+
return this.origin.replace("http", "ws");
|
|
13
|
+
throw new Error("Invalid protocol");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
r as Url
|
|
18
|
+
};
|
package/dist/index2.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@catalogfi/utils"),O=require("./index7.cjs"),i=require("./index8.cjs"),y=require("./index3.cjs"),h=require("./index9.cjs"),I=require("./index10.cjs"),l=require("./index11.cjs"),o=require("./index12.cjs"),w=require("./index13.cjs");class a{constructor(t){var e;this.url=new w.Url("/",t.url??o.API),this.orderSocket=new O.OrdersSocket(this.url.socket()),this.auth=new i.Siwe(this.url,t.signer,{...t.opts,store:((e=t.opts)==null?void 0:e.store)||new l.MemoryStorage})}static async init(t){var u;const s=await new i.Siwe(new w.Url("/",t.url??o.API),t.signer,t.opts).getToken(),r=((u=t.opts)==null?void 0:u.store)??new l.MemoryStorage;return t.opts={...t.opts,store:r},r.setItem(I.StoreKeys.AUTH_TOKEN,s),new a(t)}async createOrder(t){const{sendAmount:e,secretHash:s,receiveAmount:r,fromAsset:u,feeInSeed:N,toAsset:A,...n}=t;this.validateConfig(t);const S=y.orderPairGenerator(u,A),m=this.url.endpoint("orders"),{orderId:p}=await c.Fetcher.post(m,{body:JSON.stringify({...n,sendAmount:e,receiveAmount:r,secretHash:c.trim0x(s),orderPair:S,userWalletBTCAddress:n.btcInputAddress}),headers:{Authorization:await this.auth.getToken()}});return p}async getOrders(t,e){const s=await c.Fetcher.get(this.url+"orders?"+new URLSearchParams({...e!=null&&e.taker?{taker:t}:{maker:t},verbose:e!=null&&e.verbose?"true":"false",...e!=null&&e.pending?{status:"2"}:{}}));return e!=null&&e.verbose,s}subscribeOrders(t,e){this.orderSocket.subscribe(t,e)}unsubscribeOrders(){this.orderSocket.unsubscribe()}validateConfig(t){const{sendAmount:e,receiveAmount:s}=t,r=+e,u=+s;if(isNaN(r)||r<=0||e.includes("."))throw new Error(h.OrderbookErrors.INVALID_SEND_AMOUNT);if(isNaN(u)||u<=0||s.includes("."))throw new Error(h.OrderbookErrors.INVALID_RECEIVE_AMOUNT)}}exports.Orderbook=a;
|
package/dist/index2.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { Fetcher as c, trim0x as S } from "@catalogfi/utils";
|
|
2
2
|
import { OrdersSocket as I } from "./index7.js";
|
|
3
|
-
import { Siwe as
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
class
|
|
3
|
+
import { Siwe as n } from "./index8.js";
|
|
4
|
+
import { orderPairGenerator as O } from "./index3.js";
|
|
5
|
+
import { OrderbookErrors as m } from "./index9.js";
|
|
6
|
+
import { StoreKeys as E } from "./index10.js";
|
|
7
|
+
import { MemoryStorage as i } from "./index11.js";
|
|
8
|
+
import { API as h } from "./index12.js";
|
|
9
|
+
import { Url as p } from "./index13.js";
|
|
10
|
+
class w {
|
|
11
11
|
/**
|
|
12
12
|
* Creates an instance of Orderbook. Does not login to the orderbook backend
|
|
13
13
|
* @constructor
|
|
14
14
|
* @param {OrderbookConfig} orderbookConfig - The configuration object for the orderbook.
|
|
15
15
|
*
|
|
16
16
|
*/
|
|
17
|
-
constructor(
|
|
18
|
-
var
|
|
19
|
-
this.url =
|
|
20
|
-
...
|
|
21
|
-
store: ((
|
|
17
|
+
constructor(s) {
|
|
18
|
+
var t;
|
|
19
|
+
this.url = new p("/", s.url ?? h), this.orderSocket = new I(this.url.socket()), this.auth = new n(this.url, s.signer, {
|
|
20
|
+
...s.opts,
|
|
21
|
+
store: ((t = s.opts) == null ? void 0 : t.store) || new i()
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
@@ -26,35 +26,36 @@ class n {
|
|
|
26
26
|
*
|
|
27
27
|
* @param {OrderbookConfig} orderbookConfig - The configuration object for the orderbook.
|
|
28
28
|
*/
|
|
29
|
-
static async init(
|
|
30
|
-
var
|
|
31
|
-
const e = await new
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
).getToken();
|
|
36
|
-
return
|
|
37
|
-
...
|
|
38
|
-
store:
|
|
39
|
-
},
|
|
29
|
+
static async init(s) {
|
|
30
|
+
var a;
|
|
31
|
+
const e = await new n(
|
|
32
|
+
new p("/", s.url ?? h),
|
|
33
|
+
s.signer,
|
|
34
|
+
s.opts
|
|
35
|
+
).getToken(), r = ((a = s.opts) == null ? void 0 : a.store) ?? new i();
|
|
36
|
+
return s.opts = {
|
|
37
|
+
...s.opts,
|
|
38
|
+
store: r
|
|
39
|
+
}, r.setItem(E.AUTH_TOKEN, e), new w(s);
|
|
40
40
|
}
|
|
41
|
-
async createOrder(
|
|
41
|
+
async createOrder(s) {
|
|
42
42
|
const {
|
|
43
|
-
sendAmount:
|
|
43
|
+
sendAmount: t,
|
|
44
44
|
secretHash: e,
|
|
45
45
|
receiveAmount: r,
|
|
46
46
|
fromAsset: a,
|
|
47
|
+
feeInSeed: T,
|
|
47
48
|
toAsset: l,
|
|
48
49
|
...u
|
|
49
|
-
} =
|
|
50
|
-
this.validateConfig(
|
|
51
|
-
const
|
|
50
|
+
} = s;
|
|
51
|
+
this.validateConfig(s);
|
|
52
|
+
const A = O(a, l), o = this.url.endpoint("orders"), { orderId: N } = await c.post(o, {
|
|
52
53
|
body: JSON.stringify({
|
|
53
54
|
...u,
|
|
54
|
-
sendAmount:
|
|
55
|
+
sendAmount: t,
|
|
55
56
|
receiveAmount: r,
|
|
56
57
|
secretHash: S(e),
|
|
57
|
-
orderPair:
|
|
58
|
+
orderPair: A,
|
|
58
59
|
userWalletBTCAddress: u.btcInputAddress
|
|
59
60
|
}),
|
|
60
61
|
headers: {
|
|
@@ -63,30 +64,30 @@ class n {
|
|
|
63
64
|
});
|
|
64
65
|
return N;
|
|
65
66
|
}
|
|
66
|
-
async getOrders(
|
|
67
|
+
async getOrders(s, t) {
|
|
67
68
|
const e = await c.get(
|
|
68
69
|
this.url + "orders?" + new URLSearchParams({
|
|
69
|
-
...
|
|
70
|
-
verbose:
|
|
71
|
-
...
|
|
70
|
+
...t != null && t.taker ? { taker: s } : { maker: s },
|
|
71
|
+
verbose: t != null && t.verbose ? "true" : "false",
|
|
72
|
+
...t != null && t.pending ? { status: "2" } : {}
|
|
72
73
|
})
|
|
73
74
|
);
|
|
74
|
-
return
|
|
75
|
+
return t != null && t.verbose, e;
|
|
75
76
|
}
|
|
76
|
-
subscribeOrders(
|
|
77
|
-
this.orderSocket.subscribe(
|
|
77
|
+
subscribeOrders(s, t) {
|
|
78
|
+
this.orderSocket.subscribe(s, t);
|
|
78
79
|
}
|
|
79
80
|
unsubscribeOrders() {
|
|
80
81
|
this.orderSocket.unsubscribe();
|
|
81
82
|
}
|
|
82
|
-
validateConfig(
|
|
83
|
-
const { sendAmount:
|
|
84
|
-
if (isNaN(r) || r <= 0 ||
|
|
85
|
-
throw new Error(
|
|
83
|
+
validateConfig(s) {
|
|
84
|
+
const { sendAmount: t, receiveAmount: e } = s, r = +t, a = +e;
|
|
85
|
+
if (isNaN(r) || r <= 0 || t.includes("."))
|
|
86
|
+
throw new Error(m.INVALID_SEND_AMOUNT);
|
|
86
87
|
if (isNaN(a) || a <= 0 || e.includes("."))
|
|
87
|
-
throw new Error(
|
|
88
|
+
throw new Error(m.INVALID_RECEIVE_AMOUNT);
|
|
88
89
|
}
|
|
89
90
|
}
|
|
90
91
|
export {
|
|
91
|
-
|
|
92
|
+
w as Orderbook
|
|
92
93
|
};
|
package/dist/index5.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A={1:{AtomicSwap:"0xA5E38d098b54C00F10e32E51647086232a9A0afD"},11155111:{AtomicSwap:"
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A={1:{AtomicSwap:"0xA5E38d098b54C00F10e32E51647086232a9A0afD"},11155111:{AtomicSwap:"0x9ceD08aeE17Fbc333BB7741Ec5eB2907b0CA4241"},42161:{AtomicSwap:"0x203DAC25763aE783Ad532A035FfF33d8df9437eE"}};exports.CONTRACT_ADDRESS=A;
|
package/dist/index5.js
CHANGED
|
@@ -3,7 +3,7 @@ const A = {
|
|
|
3
3
|
AtomicSwap: "0xA5E38d098b54C00F10e32E51647086232a9A0afD"
|
|
4
4
|
},
|
|
5
5
|
11155111: {
|
|
6
|
-
AtomicSwap: "
|
|
6
|
+
AtomicSwap: "0x9ceD08aeE17Fbc333BB7741Ec5eB2907b0CA4241"
|
|
7
7
|
},
|
|
8
8
|
42161: {
|
|
9
9
|
AtomicSwap: "0x203DAC25763aE783Ad532A035FfF33d8df9437eE"
|
package/dist/index6.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=e=>{const t=e.status*100+e.initiatorAtomicSwap.swapStatus*10+e.followerAtomicSwap.swapStatus;return t===200?"user can initiate":t===222?"user can redeem":/\d3\d/.test(`${t}`)?"user can refund":t===220?"counterparty can initiate":t===224||t===226?"counterparty can redeem":/\d\d3/.test(`${t}`)?"counterparty can refund":"no action can be performed"}
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=e=>{const t=e.status*100+e.initiatorAtomicSwap.swapStatus*10+e.followerAtomicSwap.swapStatus;return t===200?"user can initiate":t===222?"user can redeem":/\d3\d/.test(`${t}`)?"user can refund":t===220?"counterparty can initiate":t===224||t===226?"counterparty can redeem":/\d\d3/.test(`${t}`)?"counterparty can refund":"no action can be performed"};var r=(e=>(e.UserCanInitiate="user can initiate",e.UserCanRedeem="user can redeem",e.UserCanRefund="user can refund",e.CounterpartyCanInitiate="counterparty can initiate",e.CounterpartyCanRedeem="counterparty can redeem",e.CounterpartyCanRefund="counterparty can refund",e.NoAction="no action can be performed",e))(r||{});exports.Actions=r;exports.parseStatus=n;
|
package/dist/index6.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
const n = (e) => {
|
|
2
|
-
const
|
|
3
|
-
return
|
|
4
|
-
}
|
|
5
|
-
var
|
|
2
|
+
const r = e.status * 100 + e.initiatorAtomicSwap.swapStatus * 10 + e.followerAtomicSwap.swapStatus;
|
|
3
|
+
return r === 200 ? "user can initiate" : r === 222 ? "user can redeem" : /\d3\d/.test(`${r}`) ? "user can refund" : r === 220 ? "counterparty can initiate" : r === 224 || r === 226 ? "counterparty can redeem" : /\d\d3/.test(`${r}`) ? "counterparty can refund" : "no action can be performed";
|
|
4
|
+
};
|
|
5
|
+
var t = /* @__PURE__ */ ((e) => (e.UserCanInitiate = "user can initiate", e.UserCanRedeem = "user can redeem", e.UserCanRefund = "user can refund", e.CounterpartyCanInitiate = "counterparty can initiate", e.CounterpartyCanRedeem = "counterparty can redeem", e.CounterpartyCanRefund = "counterparty can refund", e.NoAction = "no action can be performed", e))(t || {});
|
|
6
6
|
export {
|
|
7
|
-
|
|
8
|
-
n as parseStatus
|
|
9
|
-
a as parseURL
|
|
7
|
+
t as Actions,
|
|
8
|
+
n as parseStatus
|
|
10
9
|
};
|
package/dist/index8.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("siwe"),a=require("@catalogfi/utils"),o=require("./index10.cjs"),h=require("./index11.cjs"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("siwe"),a=require("@catalogfi/utils"),o=require("./index10.cjs"),h=require("./index11.cjs"),d=require("./index12.cjs"),u=require("./index13.cjs");class m{constructor(t,i,e){this.signingStatement="I'm signing in to Catalog",this.url=new u.Url("/",t??d.API),this.signer=i,this.domain=(e==null?void 0:e.domain)||"catalog.fi",this.domain.startsWith("https://")&&(this.domain=this.domain.split("https://")[1]),this.store=(e==null?void 0:e.store)??new h.MemoryStorage}verifyToken(t,i){const e=w(t);if(!e)return!1;try{const r=Math.floor(Date.now()/1e3)+120;return e.exp>r&&e.userWallet.toLowerCase()===i.toLowerCase()}catch{return!1}}async getToken(){const t=this.store.getItem(o.StoreKeys.AUTH_TOKEN);if(t&&this.verifyToken(t,this.signer.address))return t;const{message:i,signature:e}=await this.signStatement(),{token:r}=await a.Fetcher.post(this.url.endpoint("verify"),{body:JSON.stringify({message:i,signature:e})});if(!this.verifyToken(r,await this.signer.getAddress()))throw new Error("Token verification failed");return this.store.setItem(o.StoreKeys.AUTH_TOKEN,r),r}async signStatement(){if(!this.signer.provider)throw new Error("signer does not have a provider");const t=new Date,i=new Date(t.getTime()+300*1e3),{nonce:e}=await a.Fetcher.get(this.url.endpoint("nonce")),r=await this.signer.provider.getNetwork(),n=new c.SiweMessage({domain:this.domain,address:await this.signer.getAddress(),statement:this.signingStatement,nonce:e,uri:"https://"+this.domain,version:"1",chainId:+r.chainId.toString(),expirationTime:i.toISOString()}).prepareMessage(),g=await this.signer.signMessage(n);return{message:n,signature:g}}}const w=s=>{try{if(s.split(".").length<3)return;const t=s.split(".")[1];return t?JSON.parse(Buffer.from(t,"base64").toString("latin1")):void 0}catch{return}};exports.Siwe=m;
|
package/dist/index8.js
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
1
|
import { SiweMessage as h } from "siwe";
|
|
2
|
-
import { Fetcher as
|
|
3
|
-
import { StoreKeys as
|
|
2
|
+
import { Fetcher as o } from "@catalogfi/utils";
|
|
3
|
+
import { StoreKeys as a } from "./index10.js";
|
|
4
4
|
import { MemoryStorage as m } from "./index11.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { API as c } from "./index12.js";
|
|
6
|
+
import { Url as d } from "./index13.js";
|
|
7
7
|
class v {
|
|
8
|
-
constructor(e,
|
|
9
|
-
this.signingStatement = "I'm signing in to Catalog", this.url =
|
|
8
|
+
constructor(e, r, t) {
|
|
9
|
+
this.signingStatement = "I'm signing in to Catalog", this.url = new d("/", e ?? c), this.signer = r, this.domain = (t == null ? void 0 : t.domain) || "catalog.fi", this.domain.startsWith("https://") && (this.domain = this.domain.split("https://")[1]), this.store = (t == null ? void 0 : t.store) ?? new m();
|
|
10
10
|
}
|
|
11
|
-
verifyToken(e,
|
|
11
|
+
verifyToken(e, r) {
|
|
12
12
|
const t = f(e);
|
|
13
13
|
if (!t)
|
|
14
14
|
return !1;
|
|
15
15
|
try {
|
|
16
|
-
const
|
|
17
|
-
return t.exp >
|
|
16
|
+
const i = Math.floor(Date.now() / 1e3) + 120;
|
|
17
|
+
return t.exp > i && t.userWallet.toLowerCase() === r.toLowerCase();
|
|
18
18
|
} catch {
|
|
19
19
|
return !1;
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
async getToken() {
|
|
23
|
-
const e = this.store.getItem(
|
|
23
|
+
const e = this.store.getItem(a.AUTH_TOKEN);
|
|
24
24
|
if (e && this.verifyToken(e, this.signer.address))
|
|
25
25
|
return e;
|
|
26
|
-
const { message:
|
|
27
|
-
this.url
|
|
26
|
+
const { message: r, signature: t } = await this.signStatement(), { token: i } = await o.post(
|
|
27
|
+
this.url.endpoint("verify"),
|
|
28
28
|
{
|
|
29
29
|
body: JSON.stringify({
|
|
30
|
-
message:
|
|
30
|
+
message: r,
|
|
31
31
|
signature: t
|
|
32
32
|
})
|
|
33
33
|
}
|
|
34
34
|
);
|
|
35
|
-
if (!this.verifyToken(
|
|
35
|
+
if (!this.verifyToken(i, await this.signer.getAddress()))
|
|
36
36
|
throw new Error("Token verification failed");
|
|
37
|
-
return this.store.setItem(
|
|
37
|
+
return this.store.setItem(a.AUTH_TOKEN, i), i;
|
|
38
38
|
}
|
|
39
39
|
async signStatement() {
|
|
40
40
|
if (!this.signer.provider)
|
|
41
41
|
throw new Error("signer does not have a provider");
|
|
42
|
-
const e = /* @__PURE__ */ new Date(),
|
|
42
|
+
const e = /* @__PURE__ */ new Date(), r = new Date(e.getTime() + 300 * 1e3), { nonce: t } = await o.get(this.url.endpoint("nonce")), i = await this.signer.provider.getNetwork(), n = new h({
|
|
43
43
|
domain: this.domain,
|
|
44
44
|
address: await this.signer.getAddress(),
|
|
45
45
|
statement: this.signingStatement,
|
|
46
46
|
nonce: t,
|
|
47
47
|
uri: "https://" + this.domain,
|
|
48
48
|
version: "1",
|
|
49
|
-
chainId: +
|
|
50
|
-
expirationTime:
|
|
49
|
+
chainId: +i.chainId.toString(),
|
|
50
|
+
expirationTime: r.toISOString()
|
|
51
51
|
}).prepareMessage(), g = await this.signer.signMessage(n);
|
|
52
52
|
return {
|
|
53
53
|
message: n,
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { JsonRpcSigner, Wallet } from
|
|
2
|
-
import { IAuth } from
|
|
3
|
-
import { OrderbookOpts } from
|
|
1
|
+
import { JsonRpcSigner, Wallet } from "ethers";
|
|
2
|
+
import { IAuth } from "./auth.interface";
|
|
3
|
+
import { OrderbookOpts } from "../orderbook.types";
|
|
4
|
+
import { Url } from "../url";
|
|
4
5
|
export declare class Siwe implements IAuth {
|
|
5
6
|
private readonly url;
|
|
6
7
|
private store;
|
|
7
8
|
private readonly signer;
|
|
8
9
|
private readonly signingStatement;
|
|
9
10
|
private readonly domain;
|
|
10
|
-
constructor(url:
|
|
11
|
+
constructor(url: Url, signer: JsonRpcSigner | Wallet, opts?: OrderbookOpts);
|
|
11
12
|
verifyToken(token: string, account: string): boolean;
|
|
12
13
|
getToken(): Promise<string>;
|
|
13
14
|
private signStatement;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CreateOrderConfig, IOrderbook, Order, OrderConfig, OrderNonVerbose, OrderbookConfig } from
|
|
1
|
+
import { CreateOrderConfig, IOrderbook, Order, OrderConfig, OrderNonVerbose, OrderbookConfig } from "./orderbook.types";
|
|
2
2
|
/**
|
|
3
3
|
* A class that allows you to create and manage orders with the backend url.
|
|
4
4
|
*
|
|
@@ -1,19 +1,7 @@
|
|
|
1
|
-
import { MarkNonNullable } from
|
|
2
|
-
import { Asset } from
|
|
3
|
-
import { JsonRpcSigner, Wallet } from
|
|
4
|
-
import { IStore } from
|
|
5
|
-
/**
|
|
6
|
-
* Type for the configuration of an Order.
|
|
7
|
-
*
|
|
8
|
-
* @typedef {Object} OrderConfig
|
|
9
|
-
*
|
|
10
|
-
* @template T extends boolean
|
|
11
|
-
*
|
|
12
|
-
* @property {T} verbose - Determines the verbosity of the order.
|
|
13
|
-
* @property {boolean} taker - True if the order is a taker order, false otherwise.
|
|
14
|
-
* @property {boolean} [pending] - True if the order is pending, false otherwise. Optional.
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
1
|
+
import { MarkNonNullable } from "@catalogfi/utils";
|
|
2
|
+
import { Asset } from "./asset";
|
|
3
|
+
import { JsonRpcSigner, Wallet } from "ethers";
|
|
4
|
+
import { IStore } from "./store/store.interface";
|
|
17
5
|
/**
|
|
18
6
|
* Configuration for the orders you want to receive
|
|
19
7
|
*
|
|
@@ -70,6 +58,10 @@ export interface CreateOrderConfig {
|
|
|
70
58
|
* The funds are received at this address if specified, otherwise the funds are sent to the receive address.
|
|
71
59
|
*/
|
|
72
60
|
btcInputAddress: string;
|
|
61
|
+
/**
|
|
62
|
+
* Pay with seed
|
|
63
|
+
*/
|
|
64
|
+
feeInSeed?: string;
|
|
73
65
|
}
|
|
74
66
|
/**
|
|
75
67
|
* Additional configuration options for the orderbook
|
|
@@ -90,23 +82,10 @@ export type OrderbookOpts = {
|
|
|
90
82
|
*
|
|
91
83
|
*/
|
|
92
84
|
export interface OrderbookConfig {
|
|
93
|
-
/**
|
|
94
|
-
*
|
|
95
|
-
*/
|
|
96
85
|
url?: string;
|
|
97
|
-
/**
|
|
98
|
-
*
|
|
99
|
-
*/
|
|
100
86
|
signer: JsonRpcSigner | Wallet;
|
|
101
|
-
/**
|
|
102
|
-
*
|
|
103
|
-
*/
|
|
104
87
|
opts?: OrderbookOpts;
|
|
105
88
|
}
|
|
106
|
-
/**
|
|
107
|
-
* @interface IOrderbook
|
|
108
|
-
*
|
|
109
|
-
*/
|
|
110
89
|
export interface IOrderbook {
|
|
111
90
|
/**
|
|
112
91
|
* Creates an order
|
|
@@ -188,7 +167,7 @@ export type DecodedAuthToken = {
|
|
|
188
167
|
exp: number;
|
|
189
168
|
iat: number;
|
|
190
169
|
};
|
|
191
|
-
export type Order = MarkNonNullable<OrderNonVerbose,
|
|
170
|
+
export type Order = MarkNonNullable<OrderNonVerbose, "initiatorAtomicSwap" | "followerAtomicSwap">;
|
|
192
171
|
export type Orders = Order[];
|
|
193
172
|
export type GetOrdersOutput<T extends boolean> = (T extends true ? Order : OrderNonVerbose)[];
|
|
194
173
|
export type CreateOrderResponse = {
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { AsyncResult } from "@catalogfi/utils";
|
|
2
|
+
declare enum HTLCStatus {
|
|
3
|
+
Pending = "pending",
|
|
4
|
+
Ready = "ready",
|
|
5
|
+
Failed = "failed",
|
|
6
|
+
Expired = "expired",
|
|
7
|
+
RelayerInitiated = "relayer-initiated",
|
|
8
|
+
RelayerRefunded = "relayer-refunded",
|
|
9
|
+
Refunded = "refunded",
|
|
10
|
+
Redeemed = "redeemed",
|
|
11
|
+
Resolved = "resolved"
|
|
12
|
+
}
|
|
13
|
+
type PendingHTLC = {
|
|
14
|
+
htlcId: number;
|
|
15
|
+
channelId: number;
|
|
16
|
+
processed: boolean;
|
|
17
|
+
};
|
|
18
|
+
type HTLC = {
|
|
19
|
+
ID: number;
|
|
20
|
+
secretHash: string;
|
|
21
|
+
secret: string;
|
|
22
|
+
timeLock: number;
|
|
23
|
+
sendAmount: string;
|
|
24
|
+
receiveAmount: string;
|
|
25
|
+
pendingHTLC: PendingHTLC;
|
|
26
|
+
isWithdraw: boolean;
|
|
27
|
+
status: HTLCStatus;
|
|
28
|
+
withdrawNonce: number;
|
|
29
|
+
orderId: string;
|
|
30
|
+
initiateTxHash: string;
|
|
31
|
+
redeemTxHash: string;
|
|
32
|
+
refundTxHash: string;
|
|
33
|
+
};
|
|
34
|
+
export type PaymentChannelState = {
|
|
35
|
+
ID: number;
|
|
36
|
+
CreatedAt: string;
|
|
37
|
+
UpdatedAt: string;
|
|
38
|
+
DeletedAt: any;
|
|
39
|
+
latestState: LatestState;
|
|
40
|
+
address: string;
|
|
41
|
+
userId: string;
|
|
42
|
+
feehub: string;
|
|
43
|
+
Balance: string;
|
|
44
|
+
status: string;
|
|
45
|
+
editStatus: string;
|
|
46
|
+
lockedAmount: string;
|
|
47
|
+
};
|
|
48
|
+
type LatestState = {
|
|
49
|
+
ID: number;
|
|
50
|
+
CreatedAt: string;
|
|
51
|
+
UpdatedAt: string;
|
|
52
|
+
DeletedAt: any;
|
|
53
|
+
channelId: number;
|
|
54
|
+
htlcs: HTLC[];
|
|
55
|
+
nonce: number;
|
|
56
|
+
amount: string;
|
|
57
|
+
type: string;
|
|
58
|
+
userSignature: string;
|
|
59
|
+
feehubSignature: string;
|
|
60
|
+
};
|
|
61
|
+
export type ConditionalPaymentInitialRequest = {
|
|
62
|
+
sendAmount: string;
|
|
63
|
+
receiveAmount: string;
|
|
64
|
+
timeLock: number;
|
|
65
|
+
secretHash: string;
|
|
66
|
+
};
|
|
67
|
+
export type ConditionalPaymentFinalRequest = {
|
|
68
|
+
userSig: string;
|
|
69
|
+
channelId: number;
|
|
70
|
+
htlc: ConditionalPaymentInitialRequest;
|
|
71
|
+
};
|
|
72
|
+
export interface IPaymentChannelService {
|
|
73
|
+
getChannel(): AsyncResult<PaymentChannelState, string>;
|
|
74
|
+
createChannel(address: string): AsyncResult<PaymentChannelState, string>;
|
|
75
|
+
createConditionalPayment(paymentRequest: Omit<ConditionalPaymentInitialRequest, "timelock">): AsyncResult<ConditionalPaymentFinalRequest | null, string>;
|
|
76
|
+
payConditionally(paymentRequest: Omit<ConditionalPaymentInitialRequest, "timelock">): AsyncResult<void, string>;
|
|
77
|
+
}
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { JsonRpcSigner, Wallet } from "ethers";
|
|
2
|
+
import { ConditionalPaymentFinalRequest, ConditionalPaymentInitialRequest, IPaymentChannelService as IPaymentChannelService, PaymentChannelState } from "./interface";
|
|
3
|
+
import { AsyncResult } from "@catalogfi/utils";
|
|
4
|
+
import { IAuth } from "../auth/auth.interface";
|
|
5
|
+
export declare class PaymentChannelService implements IPaymentChannelService {
|
|
6
|
+
private api;
|
|
7
|
+
private signer;
|
|
8
|
+
private provider;
|
|
9
|
+
private auth;
|
|
10
|
+
private constructor();
|
|
11
|
+
static init(api: string, signer: JsonRpcSigner | Wallet, auth: IAuth): PaymentChannelService;
|
|
12
|
+
/**
|
|
13
|
+
* creates a new payment channel with the specified deposit amount
|
|
14
|
+
*/
|
|
15
|
+
createChannel(depositAmt?: string): AsyncResult<PaymentChannelState, string>;
|
|
16
|
+
/**
|
|
17
|
+
* gets the latest state of the payment channel associated with the address
|
|
18
|
+
*
|
|
19
|
+
* Note: Currently only returns the first channel
|
|
20
|
+
*/
|
|
21
|
+
getChannel(): AsyncResult<PaymentChannelState, string>;
|
|
22
|
+
private _getAuthToken;
|
|
23
|
+
private getSignatureForConditionalPayment;
|
|
24
|
+
private _lockChannel;
|
|
25
|
+
payConditionally(request: Omit<ConditionalPaymentInitialRequest, "timeLock">): AsyncResult<void, string>;
|
|
26
|
+
/**
|
|
27
|
+
* Constructs a conditional payment request for backend to accept.
|
|
28
|
+
*
|
|
29
|
+
* Use .payConditionally() to actually pay conditionally.
|
|
30
|
+
*/
|
|
31
|
+
createConditionalPayment(paymentRequest: Omit<ConditionalPaymentInitialRequest, "timeLock">): AsyncResult<ConditionalPaymentFinalRequest, string>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { JsonRpcApiProvider, JsonRpcSigner, Wallet } from "ethers";
|
|
2
|
+
import { ConditionalPaymentInitialRequest, PaymentChannelState } from "./interface";
|
|
3
|
+
export declare function getTimelock(provider: JsonRpcApiProvider): Promise<number>;
|
|
4
|
+
export declare const parseError: (error: unknown) => string;
|
|
5
|
+
export declare const getProviderOrThrow: (signer: JsonRpcSigner | Wallet) => JsonRpcApiProvider;
|
|
6
|
+
export declare const signPayment: (channel: PaymentChannelState, paymentRequest: ConditionalPaymentInitialRequest, signer: JsonRpcSigner | Wallet) => Promise<string>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Url";
|
package/dist/src/lib/utils.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Order } from
|
|
1
|
+
import { Order } from "./orderbook.types";
|
|
2
2
|
export declare const parseStatus: (order: Order) => Actions;
|
|
3
|
-
export declare const parseURL: (url: string) => string;
|
|
4
3
|
export declare enum Actions {
|
|
5
4
|
UserCanInitiate = "user can initiate",
|
|
6
5
|
UserCanRedeem = "user can redeem",
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gardenfi/orderbook",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@catalogfi/utils": "^0.1.
|
|
6
|
+
"@catalogfi/utils": "^0.1.5",
|
|
7
|
+
"@catalogfi/wallets": "0.2.30",
|
|
7
8
|
"bufferutil": "^4.0.8",
|
|
8
9
|
"ethers": "6.8.0",
|
|
9
10
|
"siwe": "^2.1.4",
|
|
@@ -13,7 +14,7 @@
|
|
|
13
14
|
},
|
|
14
15
|
"scripts": {
|
|
15
16
|
"build": "vite build",
|
|
16
|
-
"test": "
|
|
17
|
+
"test": "vitest run",
|
|
17
18
|
"dev": "vite build --watch"
|
|
18
19
|
},
|
|
19
20
|
"files": [
|
|
@@ -41,7 +42,8 @@
|
|
|
41
42
|
"jest": "^29.7.0",
|
|
42
43
|
"typescript": "^5.2.2",
|
|
43
44
|
"vite": "^5.1.6",
|
|
44
|
-
"vite-plugin-dts": "^3.7.3"
|
|
45
|
+
"vite-plugin-dts": "^3.7.3",
|
|
46
|
+
"vitest": "^1.6.0"
|
|
45
47
|
},
|
|
46
48
|
"sideEffects": false
|
|
47
49
|
}
|