@gardenfi/core 0.1.14 → 0.1.15
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
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 f=require("ethers"),c=require("@gardenfi/orderbook"),h=require("@catalogfi/utils"),y=require("@catalogfi/wallets"),N=async(e,t,r,s)=>{const i=r[e],a=r[t];if(!a)throw new Error(`No ${e} wallet found`);if(!i)throw new Error(`No ${t} wallet found`);let d;if(w(e)){const o=f.sha256(h.with0x(Buffer.from("catalog.js"+s+await a.getAddress()).toString("hex"))).slice(2);d=await i.sign(o)}else{const o=f.sha256(h.with0x(Buffer.from("catalog.js"+s+await i.getAddress()).toString("hex"))).slice(2);d=await a.sign(o)}return h.trim0x(f.sha256(h.with0x(d)))},w=e=>e==="bitcoin"||e==="bitcoin_testnet"||e==="bitcoin_regtest",m={WALLET_NOT_FOUND:e=>`${e?"from":"to"} asset wallet not found; please pass the wallet associated with the chain of the asset swapping`,CHAIN_WALLET_NOT_FOUND:e=>`no ${e} wallet found`},S={NO_ACTION:"no actions can be performed in this state",NO_SECRET:"secret not found in order",INVALID_ACTION:(e,t)=>`can not ${e} on status: ${t}`};class L{constructor(t,r){this.order=t,this.wallets=r}get action(){return c.parseStatus(this.order)}get status(){return+`${this.order.status}${this.order.initiatorAtomicSwap.swapStatus}${this.order.followerAtomicSwap.swapStatus}`}async next(){switch(v[this.action]){case"Initiate":return await this.init();case"Redeem":return await this.redeem();case"Refund":return await this.refund();default:return U}}async init(){switch(this.action){case c.Actions.UserCanInitiate:{const t=this.wallets[this.order.initiatorAtomicSwap.chain];if(!t)throw new Error(`No ${this.order.initiatorAtomicSwap.chain} wallet found`);return{user:"initiator",action:"Initiate",output:await(await l(t,this.order,"native",this.order.initiatorAtomicSwap.asset)).init()}}case c.Actions.CounterpartyCanInitiate:{const t=this.wallets[this.order.followerAtomicSwap.chain];if(!t)throw new Error(`No ${this.order.followerAtomicSwap.chain} wallet`);return{user:"redeemer",action:"Initiate",output:await(await l(t,this.order,"foreign",this.order.followerAtomicSwap.asset)).init()}}default:throw new Error(S.INVALID_ACTION("init",this.status))}}async redeem(){const t=this.getWallet(this.order.followerAtomicSwap.chain),r=this.getWallet(this.order.initiatorAtomicSwap.chain);switch(this.action){case c.Actions.UserCanRedeem:{const s=await l(t,this.order,"foreign",this.order.followerAtomicSwap.asset),i=await N(this.order.initiatorAtomicSwap.chain,this.order.followerAtomicSwap.chain,this.wallets,this.order.secretNonce);return{user:"initiator",action:"Redeem",output:await s.redeem(i,this.order.userBtcWalletAddress)}}case c.Actions.CounterpartyCanRedeem:{if(!this.order.secret)throw new Error("Secret not found in order");return{user:"redeemer",action:"Redeem",output:await(await l(r,this.order,"native",this.order.initiatorAtomicSwap.asset)).redeem(this.order.secret)}}default:throw new Error(S.INVALID_ACTION("redeem",this.status))}}async refund(){const t=this.getWallet(this.order.initiatorAtomicSwap.chain),r=this.getWallet(this.order.followerAtomicSwap.chain);if(this.order.initiatorAtomicSwap.swapStatus===3)return{user:"initiator",action:"Refund",output:await(await l(t,this.order,"native",this.order.initiatorAtomicSwap.asset)).refund(this.order.userBtcWalletAddress)};if(this.order.followerAtomicSwap.swapStatus===3)return{user:"redeemer",action:"Refund",output:await(await l(r,this.order,"foreign",this.order.followerAtomicSwap.asset)).refund(this.order.userBtcWalletAddress)};throw new Error(S.INVALID_ACTION("refund",this.status))}id(){throw new Error("Method not implemented.")}getWallet(t){const r=this.wallets[t];if(!r)throw new Error(`No ${t} wallet found`);return r}}const l=async(e,t,r,s)=>{const i=r==="native"?t.initiatorAtomicSwap:t.followerAtomicSwap;return e.newSwap({recipientAddress:i.redeemerAddress,refundAddress:i.initiatorAddress,initiatorAddress:i.initiatorAddress,chain:c.chainToId[i.chain],expiryBlocks:+i.timelock,secretHash:t.secretHash,amount:+i.amount,contractAddress:s})};var I=(e=>(e.INITIATOR="initiator",e.REDEEMER="redeemer",e))(I||{}),E=(e=>(e.Init="Initiate",e.Redeem="Redeem",e.Refund="Refund",e.None="None",e))(E||{});const v={[c.Actions.UserCanInitiate]:"Initiate",[c.Actions.CounterpartyCanInitiate]:"Initiate",[c.Actions.UserCanRedeem]:"Redeem",[c.Actions.CounterpartyCanRedeem]:"Redeem",[c.Actions.UserCanRefund]:"Refund",[c.Actions.CounterpartyCanRefund]:"Refund",[c.Actions.NoAction]:"None"},U={user:"initiator",action:"None",output:""};class ${constructor(t){if(t)this.extensionId=t.id;else if(x())this.extensionId=g("chrome");else if(O())this.extensionId=g("window");else throw new Error("Catalog wallet extension not found")}send(t,r){return new Promise((s,i)=>{const a={url:"",favicon:""},d=window.location.href.split("://")[1].replace(/\/$/,"");a.url=d;let o=document.querySelector("link[rel='icon']");if(o||(o=document.querySelector("link[rel='shortcut icon']")),o||(o=document.querySelector("link[rel='apple-touch-icon']")),o||(o=document.querySelector("meta[property='og:image']")),o)a.favicon=o instanceof HTMLMetaElement?o.content:o.href;else{const n=new URL(window.location.href);a.favicon=`${n.protocol}//${n.hostname}/favicon.ico`}chrome.runtime.sendMessage(this.extensionId,{method:t,params:r,metadata:a},n=>chrome.runtime.lastError?i(chrome.runtime.lastError):typeof n=="object"&&n&&"error"in n?i(n.error):s(n))})}}const O=()=>{try{const e=window;return e&&e.catalog&&e.catalog.id}catch{return!1}},g=e=>{if(e==="chrome")return chrome.runtime.id;if(e==="window")return window.catalog.id;throw new Error("Invalid medium to get wallet id")},x=()=>{try{return chrome&&!!chrome.runtime&&!!chrome.runtime.id}catch{return!1}},W={createOrderAndSwap:"createOrderAndSwap"};class B{constructor(t,r){this.orderbook=t,this.wallets=r}subscribeOrders(t,r){this.orderbook.subscribeOrders(t,r)}unsubscribeOrders(){return this.orderbook.unsubscribeOrders()}async swap(t,r,s,i,a){if(O()){const u=await new $().send(W.createOrderAndSwap,{from:t,to:r,amt:s,receiveAmount:i,opts:a});return Number(u)}const d=this.wallets[t.chain],o=this.wallets[r.chain];if(!d)throw new Error(m.WALLET_NOT_FOUND(!0));if(!o)throw new Error(m.WALLET_NOT_FOUND(!1));if(D(t.chain,r.chain),a&&!a.btcUserAddress||!a){a=a||{};const u=this.getBitcoinWallet();if(!u||!(u instanceof y.AbstractBitcoinWallet))throw new Error(m.CHAIN_WALLET_NOT_FOUND("Bitcoin"));a.btcUserAddress=await u.getAddress()}const n=w(t.chain),A=n?o:d,p=n?d:o,b=await A.getAddress(),R=await this.orderbook.getOrders(b),_=n?await p.getAddress():await A.getAddress(),C=n?await A.getAddress():await p.getAddress(),T=await N(t.chain,r.chain,this.wallets,R.length+1);return await this.orderbook.createOrder({fromAsset:t,toAsset:r,sendAmount:s.toString(),receiveAmount:i.toString(),receiveAddress:C,sendAddress:_,secretHash:f.sha256(h.with0x(T)),btcInputAddress:a.btcUserAddress??await p.getAddress()})}getSwap(t){return new L(t,this.wallets)}calculateReceiveAmt(t,r,s){return Promise.resolve(Math.floor(s-s*.003))}getBitcoinWallet(){for(const t in this.wallets)if(w(t))return this.wallets[t];return null}}const D=(e,t)=>{if(w(e)===w(t))throw new Error(w(e)?m.CHAIN_WALLET_NOT_FOUND("Bitcoin"):m.CHAIN_WALLET_NOT_FOUND("EVM"))};exports.GardenJS=B;exports.SwapperActions=E;exports.SwapperRole=I;exports.catalogWalletActions=W;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{sha256 as
|
|
1
|
+
import{sha256 as m}from"ethers";import{Actions as c,parseStatus as L,chainToId as W}from"@gardenfi/orderbook";import{with0x as f,trim0x as U}from"@catalogfi/utils";import{AbstractBitcoinWallet as v}from"@catalogfi/wallets";const g=async(e,t,r,a)=>{const i=r[e],s=r[t];if(!s)throw new Error(`No ${e} wallet found`);if(!i)throw new Error(`No ${t} wallet found`);let d;if(w(e)){const o=m(f(Buffer.from("catalog.js"+a+await s.getAddress()).toString("hex"))).slice(2);d=await i.sign(o)}else{const o=m(f(Buffer.from("catalog.js"+a+await i.getAddress()).toString("hex"))).slice(2);d=await s.sign(o)}return U(m(f(d)))},w=e=>e==="bitcoin"||e==="bitcoin_testnet"||e==="bitcoin_regtest",u={WALLET_NOT_FOUND:e=>`${e?"from":"to"} asset wallet not found; please pass the wallet associated with the chain of the asset swapping`,CHAIN_WALLET_NOT_FOUND:e=>`no ${e} wallet found`},S={NO_ACTION:"no actions can be performed in this state",NO_SECRET:"secret not found in order",INVALID_ACTION:(e,t)=>`can not ${e} on status: ${t}`};class ${constructor(t,r){this.order=t,this.wallets=r}get action(){return L(this.order)}get status(){return+`${this.order.status}${this.order.initiatorAtomicSwap.swapStatus}${this.order.followerAtomicSwap.swapStatus}`}async next(){switch(x[this.action]){case"Initiate":return await this.init();case"Redeem":return await this.redeem();case"Refund":return await this.refund();default:return D}}async init(){switch(this.action){case c.UserCanInitiate:{const t=this.wallets[this.order.initiatorAtomicSwap.chain];if(!t)throw new Error(`No ${this.order.initiatorAtomicSwap.chain} wallet found`);return{user:"initiator",action:"Initiate",output:await(await l(t,this.order,"native",this.order.initiatorAtomicSwap.asset)).init()}}case c.CounterpartyCanInitiate:{const t=this.wallets[this.order.followerAtomicSwap.chain];if(!t)throw new Error(`No ${this.order.followerAtomicSwap.chain} wallet`);return{user:"redeemer",action:"Initiate",output:await(await l(t,this.order,"foreign",this.order.followerAtomicSwap.asset)).init()}}default:throw new Error(S.INVALID_ACTION("init",this.status))}}async redeem(){const t=this.getWallet(this.order.followerAtomicSwap.chain),r=this.getWallet(this.order.initiatorAtomicSwap.chain);switch(this.action){case c.UserCanRedeem:{const a=await l(t,this.order,"foreign",this.order.followerAtomicSwap.asset),i=await g(this.order.initiatorAtomicSwap.chain,this.order.followerAtomicSwap.chain,this.wallets,this.order.secretNonce);return{user:"initiator",action:"Redeem",output:await a.redeem(i,this.order.userBtcWalletAddress)}}case c.CounterpartyCanRedeem:{if(!this.order.secret)throw new Error("Secret not found in order");return{user:"redeemer",action:"Redeem",output:await(await l(r,this.order,"native",this.order.initiatorAtomicSwap.asset)).redeem(this.order.secret)}}default:throw new Error(S.INVALID_ACTION("redeem",this.status))}}async refund(){const t=this.getWallet(this.order.initiatorAtomicSwap.chain),r=this.getWallet(this.order.followerAtomicSwap.chain);if(this.order.initiatorAtomicSwap.swapStatus===3)return{user:"initiator",action:"Refund",output:await(await l(t,this.order,"native",this.order.initiatorAtomicSwap.asset)).refund(this.order.userBtcWalletAddress)};if(this.order.followerAtomicSwap.swapStatus===3)return{user:"redeemer",action:"Refund",output:await(await l(r,this.order,"foreign",this.order.followerAtomicSwap.asset)).refund(this.order.userBtcWalletAddress)};throw new Error(S.INVALID_ACTION("refund",this.status))}id(){throw new Error("Method not implemented.")}getWallet(t){const r=this.wallets[t];if(!r)throw new Error(`No ${t} wallet found`);return r}}const l=async(e,t,r,a)=>{const i=r==="native"?t.initiatorAtomicSwap:t.followerAtomicSwap;return e.newSwap({recipientAddress:i.redeemerAddress,refundAddress:i.initiatorAddress,initiatorAddress:i.initiatorAddress,chain:W[i.chain],expiryBlocks:+i.timelock,secretHash:t.secretHash,amount:+i.amount,contractAddress:a})};var N=(e=>(e.INITIATOR="initiator",e.REDEEMER="redeemer",e))(N||{}),I=(e=>(e.Init="Initiate",e.Redeem="Redeem",e.Refund="Refund",e.None="None",e))(I||{});const x={[c.UserCanInitiate]:"Initiate",[c.CounterpartyCanInitiate]:"Initiate",[c.UserCanRedeem]:"Redeem",[c.CounterpartyCanRedeem]:"Redeem",[c.UserCanRefund]:"Refund",[c.CounterpartyCanRefund]:"Refund",[c.NoAction]:"None"},D={user:"initiator",action:"None",output:""};class B{constructor(t){if(t)this.extensionId=t.id;else if(k())this.extensionId=O("chrome");else if(E())this.extensionId=O("window");else throw new Error("Catalog wallet extension not found")}send(t,r){return new Promise((a,i)=>{const s={url:"",favicon:""},d=window.location.href.split("://")[1].replace(/\/$/,"");s.url=d;let o=document.querySelector("link[rel='icon']");if(o||(o=document.querySelector("link[rel='shortcut icon']")),o||(o=document.querySelector("link[rel='apple-touch-icon']")),o||(o=document.querySelector("meta[property='og:image']")),o)s.favicon=o instanceof HTMLMetaElement?o.content:o.href;else{const n=new URL(window.location.href);s.favicon=`${n.protocol}//${n.hostname}/favicon.ico`}chrome.runtime.sendMessage(this.extensionId,{method:t,params:r,metadata:s},n=>chrome.runtime.lastError?i(chrome.runtime.lastError):typeof n=="object"&&n&&"error"in n?i(n.error):a(n))})}}const E=()=>{try{const e=window;return e&&e.catalog&&e.catalog.id}catch{return!1}},O=e=>{if(e==="chrome")return chrome.runtime.id;if(e==="window")return window.catalog.id;throw new Error("Invalid medium to get wallet id")},k=()=>{try{return chrome&&!!chrome.runtime&&!!chrome.runtime.id}catch{return!1}},R={createOrderAndSwap:"createOrderAndSwap"};class F{constructor(t,r){this.orderbook=t,this.wallets=r}subscribeOrders(t,r){this.orderbook.subscribeOrders(t,r)}unsubscribeOrders(){return this.orderbook.unsubscribeOrders()}async swap(t,r,a,i,s){if(E()){const h=await new B().send(R.createOrderAndSwap,{from:t,to:r,amt:a,receiveAmount:i,opts:s});return Number(h)}const d=this.wallets[t.chain],o=this.wallets[r.chain];if(!d)throw new Error(u.WALLET_NOT_FOUND(!0));if(!o)throw new Error(u.WALLET_NOT_FOUND(!1));if(H(t.chain,r.chain),s&&!s.btcUserAddress||!s){s=s||{};const h=this.getBitcoinWallet();if(!h||!(h instanceof v))throw new Error(u.CHAIN_WALLET_NOT_FOUND("Bitcoin"));s.btcUserAddress=await h.getAddress()}const n=w(t.chain),A=n?o:d,p=n?d:o,C=await A.getAddress(),_=await this.orderbook.getOrders(C),b=n?await p.getAddress():await A.getAddress(),T=n?await A.getAddress():await p.getAddress(),y=await g(t.chain,r.chain,this.wallets,_.length+1);return await this.orderbook.createOrder({fromAsset:t,toAsset:r,sendAmount:a.toString(),receiveAmount:i.toString(),receiveAddress:T,sendAddress:b,secretHash:m(f(y)),btcInputAddress:s.btcUserAddress??await p.getAddress()})}getSwap(t){return new $(t,this.wallets)}calculateReceiveAmt(t,r,a){return Promise.resolve(Math.floor(a-a*.003))}getBitcoinWallet(){for(const t in this.wallets)if(w(t))return this.wallets[t];return null}}const H=(e,t)=>{if(w(e)===w(t))throw new Error(w(e)?u.CHAIN_WALLET_NOT_FOUND("Bitcoin"):u.CHAIN_WALLET_NOT_FOUND("EVM"))};export{F as GardenJS,I as SwapperActions,N as SwapperRole,R as catalogWalletActions};
|
package/dist/src/lib/errors.d.ts
CHANGED
package/dist/src/lib/garden.d.ts
CHANGED
|
@@ -40,7 +40,8 @@ export declare class GardenJS implements IGardenJS {
|
|
|
40
40
|
*/
|
|
41
41
|
getSwap(order: Order): ISwapper;
|
|
42
42
|
/**
|
|
43
|
-
* Calculates the receive amount for the given send amount
|
|
43
|
+
* Calculates the receive amount for the given send amount. The send amount should be in it's lowest denomination
|
|
44
44
|
*/
|
|
45
45
|
calculateReceiveAmt(from: Asset, to: Asset, sendAmt: number): Promise<number>;
|
|
46
|
+
private getBitcoinWallet;
|
|
46
47
|
}
|
|
@@ -27,7 +27,7 @@ export interface IGardenJS {
|
|
|
27
27
|
* Subscribes to order updates
|
|
28
28
|
*
|
|
29
29
|
* @method
|
|
30
|
-
* @param {string} address - The address to subscribe to, currently each
|
|
30
|
+
* @param {string} address - The address to subscribe to, currently each GardenJS instance can only connect to a single address
|
|
31
31
|
* @param {(orders: Orders) => void} callback - The callback to call when the orders are updated. The first response are all the orders created by a given address
|
|
32
32
|
*
|
|
33
33
|
* @returns {void}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gardenfi/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@catalogfi/utils": "^0.1.6",
|
|
7
7
|
"@catalogfi/wallets": "^0.2.45",
|
|
8
|
-
"@gardenfi/orderbook": "^0.1.
|
|
8
|
+
"@gardenfi/orderbook": "^0.1.10",
|
|
9
9
|
"ethers": "6.8.0"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
],
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "vite build",
|
|
16
|
+
"test": "vitest run",
|
|
16
17
|
"dev": "vite build --watch"
|
|
17
18
|
},
|
|
18
19
|
"main": "./dist/index.cjs",
|
|
@@ -31,14 +32,13 @@
|
|
|
31
32
|
"registry": "https://registry.npmjs.org/"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
|
-
"@types/jest": "^29.5.12",
|
|
35
35
|
"dotenv": "^16.3.1",
|
|
36
|
-
"jest": "^29.7.0",
|
|
37
36
|
"typescript": "^5.2.2",
|
|
38
37
|
"vite": "^5.1.6",
|
|
39
38
|
"vite-plugin-dts": "^3.7.3",
|
|
40
39
|
"vite-plugin-top-level-await": "^1.4.1",
|
|
41
|
-
"vite-plugin-wasm": "^3.3.0"
|
|
40
|
+
"vite-plugin-wasm": "^3.3.0",
|
|
41
|
+
"vitest": "^1.6.0"
|
|
42
42
|
},
|
|
43
43
|
"sideEffects": false
|
|
44
44
|
}
|