timefi-sdk 0.1.0 → 0.1.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/README.md +7 -3
- package/dist/index.esm.js +67 -47
- package/dist/index.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -16,14 +16,18 @@ The official JavaScript SDK for interacting with the **TimeFi Protocol** on the
|
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
18
|
npm install timefi-sdk
|
|
19
|
-
```
|
|
20
19
|
|
|
20
|
+
### Running Tests
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm test
|
|
24
|
+
```
|
|
21
25
|
## 🛠️ Quick Start
|
|
22
26
|
|
|
23
27
|
### Fetch Protocol Stats
|
|
24
28
|
|
|
25
29
|
```javascript
|
|
26
|
-
import { TimeFiClient, formatSTX } from '
|
|
30
|
+
import { TimeFiClient, formatSTX } from 'timefi-sdk';
|
|
27
31
|
|
|
28
32
|
// Initialize for Stacks Mainnet
|
|
29
33
|
const client = new TimeFiClient('mainnet');
|
|
@@ -36,7 +40,7 @@ console.log(`Current TVL: ${formatSTX(tvl)} STX`);
|
|
|
36
40
|
### Format a Stacks Address
|
|
37
41
|
|
|
38
42
|
```javascript
|
|
39
|
-
import { formatAddress } from '
|
|
43
|
+
import { formatAddress } from 'timefi-sdk';
|
|
40
44
|
|
|
41
45
|
const shortAddress = formatAddress('SP3...XYZ123');
|
|
42
46
|
console.log(shortAddress); // SP3...Z123
|
package/dist/index.esm.js
CHANGED
|
@@ -1,111 +1,131 @@
|
|
|
1
|
-
import { callReadOnlyFunction as
|
|
2
|
-
import { AnchorMode as
|
|
3
|
-
import { StacksMainnet as
|
|
4
|
-
const
|
|
1
|
+
import { callReadOnlyFunction as l, cvToValue as a, uintCV as i, PostConditionMode as c, AnchorMode as u } from "@stacks/transactions";
|
|
2
|
+
import { AnchorMode as b, PostConditionMode as D, bufferCV as k, noneCV as _, principalCV as S, responseErrorCV as L, responseOkCV as $, someCV as F, stringAsciiCV as v, stringUtf8CV as G, uintCV as P } from "@stacks/transactions";
|
|
3
|
+
import { StacksMainnet as f, StacksTestnet as d } from "@stacks/network";
|
|
4
|
+
const h = "SP3FKNEZ86RG5RT7SZ5FBRGH85FZNG94ZH1MCGG6N", s = {
|
|
5
5
|
VAULT: "timefi-vault-v-A2",
|
|
6
6
|
REWARDS: "timefi-rewards-v-A2",
|
|
7
7
|
GOVERNANCE: "timefi-governance-v-A2",
|
|
8
8
|
EMERGENCY: "timefi-emergency-v-A2"
|
|
9
|
-
},
|
|
9
|
+
}, N = {
|
|
10
10
|
MONTH_1: { label: "1 Month", blocks: 4320, apy: 1 },
|
|
11
11
|
MONTH_3: { label: "3 Months", blocks: 12960, apy: 3 },
|
|
12
12
|
MONTH_6: { label: "6 Months", blocks: 25920, apy: 6 },
|
|
13
13
|
MONTH_9: { label: "9 Months", blocks: 38880, apy: 9 },
|
|
14
14
|
YEAR_1: { label: "1 Year", blocks: 52560, apy: 12 }
|
|
15
|
-
}, M = 0.01,
|
|
15
|
+
}, M = 0.01, g = 1e6, y = (t) => {
|
|
16
16
|
if (t == null) return "0.000000";
|
|
17
17
|
try {
|
|
18
|
-
const n = typeof t == "object" && t !== null && "value" in t ? t.value : t;
|
|
19
|
-
return (
|
|
18
|
+
const n = typeof t == "object" && t !== null && "value" in t ? t.value : t, e = Number(n);
|
|
19
|
+
return isNaN(e) ? "0.000000" : (e / 1e6).toLocaleString(void 0, {
|
|
20
20
|
minimumFractionDigits: 0,
|
|
21
21
|
maximumFractionDigits: 6
|
|
22
22
|
});
|
|
23
23
|
} catch (n) {
|
|
24
24
|
return console.error("Error formatting STX:", n), "0.000000";
|
|
25
25
|
}
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
26
|
+
}, C = (t, n = 4, e = 4) => t ? t.length <= n + e ? t : `${t.slice(0, n + 2)}...${t.slice(-e)}` : "", T = (t) => {
|
|
27
|
+
if (t == null) return "0";
|
|
28
|
+
const n = Number(t);
|
|
29
|
+
return isNaN(n) ? "0" : n.toLocaleString();
|
|
30
|
+
}, p = (t, n = 2) => {
|
|
31
|
+
if (t == null) return "0%";
|
|
32
|
+
const e = Number(t);
|
|
33
|
+
return isNaN(e) ? "0%" : (e * 100).toFixed(n) + "%";
|
|
34
|
+
}, m = (t) => {
|
|
31
35
|
if (!t) return "--";
|
|
32
|
-
const n =
|
|
33
|
-
return
|
|
36
|
+
const n = new Date(t);
|
|
37
|
+
return isNaN(n.getTime()) ? "--" : n.toLocaleDateString(void 0, {
|
|
38
|
+
year: "numeric",
|
|
39
|
+
month: "short",
|
|
40
|
+
day: "numeric"
|
|
41
|
+
});
|
|
42
|
+
}, O = (t) => {
|
|
43
|
+
if (!t) return "--";
|
|
44
|
+
const n = /* @__PURE__ */ new Date(), e = new Date(t), r = Math.floor((n - e) / 1e3);
|
|
45
|
+
if (r < -1) {
|
|
46
|
+
const o = Math.abs(r);
|
|
47
|
+
return o < 60 ? "in a few seconds" : o < 3600 ? `in ${Math.floor(o / 60)}m` : o < 86400 ? `in ${Math.floor(o / 3600)}h` : `in ${Math.floor(o / 86400)}d`;
|
|
48
|
+
}
|
|
49
|
+
return r < 5 ? "just now" : r < 60 ? `${r}s ago` : r < 3600 ? `${Math.floor(r / 60)}m ago` : r < 86400 ? `${Math.floor(r / 3600)}h ago` : r < 604800 ? `${Math.floor(r / 86400)}d ago` : m(t);
|
|
34
50
|
};
|
|
35
51
|
class R {
|
|
36
52
|
constructor(n = "mainnet") {
|
|
37
|
-
this.network = n === "mainnet" ? new
|
|
53
|
+
this.network = n === "mainnet" ? new f() : new d(), this.contractAddress = h;
|
|
38
54
|
}
|
|
39
55
|
// --- Read-only Methods ---
|
|
40
|
-
async callReadOnly(n,
|
|
41
|
-
const
|
|
56
|
+
async callReadOnly(n, e = [], r) {
|
|
57
|
+
const o = await l({
|
|
42
58
|
contractAddress: this.contractAddress,
|
|
43
|
-
contractName:
|
|
59
|
+
contractName: s.VAULT,
|
|
44
60
|
functionName: n,
|
|
45
|
-
functionArgs:
|
|
61
|
+
functionArgs: e,
|
|
46
62
|
network: this.network,
|
|
47
|
-
senderAddress:
|
|
63
|
+
senderAddress: r || this.contractAddress
|
|
48
64
|
});
|
|
49
|
-
return
|
|
65
|
+
return o.type === 7 || o.type === 8 ? a(o.value) : a(o);
|
|
50
66
|
}
|
|
51
67
|
async getVault(n) {
|
|
52
|
-
|
|
68
|
+
if (n == null) throw new Error("vaultId is required");
|
|
69
|
+
return this.callReadOnly("get-vault", [i(n)]);
|
|
53
70
|
}
|
|
54
71
|
async getTimeRemaining(n) {
|
|
55
|
-
|
|
72
|
+
if (n == null) throw new Error("vaultId is required");
|
|
73
|
+
return this.callReadOnly("get-time-remaining", [i(n)]);
|
|
56
74
|
}
|
|
57
75
|
async canWithdraw(n) {
|
|
58
|
-
|
|
76
|
+
if (n == null) throw new Error("vaultId is required");
|
|
77
|
+
return this.callReadOnly("can-withdraw", [i(n)]);
|
|
59
78
|
}
|
|
60
79
|
async getTVL() {
|
|
61
80
|
return this.callReadOnly("get-tvl", []);
|
|
62
81
|
}
|
|
63
82
|
// --- Transaction Signing Options Helpers ---
|
|
64
|
-
getCreateVaultOptions(n,
|
|
83
|
+
getCreateVaultOptions(n, e) {
|
|
65
84
|
return {
|
|
66
85
|
contractAddress: this.contractAddress,
|
|
67
|
-
contractName:
|
|
86
|
+
contractName: s.VAULT,
|
|
68
87
|
functionName: "create-vault",
|
|
69
|
-
functionArgs: [
|
|
88
|
+
functionArgs: [i(n * 1e6), i(e)],
|
|
70
89
|
network: this.network,
|
|
71
|
-
anchorMode:
|
|
90
|
+
anchorMode: u.Any,
|
|
72
91
|
postConditionMode: c.Deny
|
|
73
92
|
};
|
|
74
93
|
}
|
|
75
94
|
getWithdrawOptions(n) {
|
|
95
|
+
if (n == null) throw new Error("vaultId is required");
|
|
76
96
|
return {
|
|
77
97
|
contractAddress: this.contractAddress,
|
|
78
|
-
contractName:
|
|
98
|
+
contractName: s.VAULT,
|
|
79
99
|
functionName: "request-withdraw",
|
|
80
|
-
functionArgs: [
|
|
100
|
+
functionArgs: [i(n)],
|
|
81
101
|
network: this.network,
|
|
82
|
-
anchorMode:
|
|
102
|
+
anchorMode: u.Any,
|
|
83
103
|
postConditionMode: c.Deny
|
|
84
104
|
};
|
|
85
105
|
}
|
|
86
106
|
}
|
|
87
107
|
export {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
108
|
+
b as AnchorMode,
|
|
109
|
+
h as CONTRACT_ADDRESS,
|
|
110
|
+
s as CONTRACT_NAMES,
|
|
111
|
+
N as LOCK_PERIODS,
|
|
112
|
+
g as MAX_DEPOSIT,
|
|
93
113
|
M as MIN_DEPOSIT,
|
|
94
114
|
D as PostConditionMode,
|
|
95
115
|
R as TimeFiClient,
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
116
|
+
k as bufferCV,
|
|
117
|
+
C as formatAddress,
|
|
118
|
+
m as formatDate,
|
|
99
119
|
T as formatNumber,
|
|
100
120
|
p as formatPercent,
|
|
101
121
|
O as formatRelativeTime,
|
|
102
|
-
|
|
122
|
+
y as formatSTX,
|
|
103
123
|
_ as noneCV,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
124
|
+
S as principalCV,
|
|
125
|
+
L as responseErrorCV,
|
|
126
|
+
$ as responseOkCV,
|
|
107
127
|
F as someCV,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
128
|
+
v as stringAsciiCV,
|
|
129
|
+
G as stringUtf8CV,
|
|
130
|
+
P as uintCV
|
|
111
131
|
};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("@stacks/transactions"),s=require("@stacks/network"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("@stacks/transactions"),s=require("@stacks/network"),u="SP3FKNEZ86RG5RT7SZ5FBRGH85FZNG94ZH1MCGG6N",a={VAULT:"timefi-vault-v-A2",REWARDS:"timefi-rewards-v-A2",GOVERNANCE:"timefi-governance-v-A2",EMERGENCY:"timefi-emergency-v-A2"},l={MONTH_1:{label:"1 Month",blocks:4320,apy:1},MONTH_3:{label:"3 Months",blocks:12960,apy:3},MONTH_6:{label:"6 Months",blocks:25920,apy:6},MONTH_9:{label:"9 Months",blocks:38880,apy:9},YEAR_1:{label:"1 Year",blocks:52560,apy:12}},f=.01,d=1e6,m=t=>{if(t==null)return"0.000000";try{const e=typeof t=="object"&&t!==null&&"value"in t?t.value:t,r=Number(e);return isNaN(r)?"0.000000":(r/1e6).toLocaleString(void 0,{minimumFractionDigits:0,maximumFractionDigits:6})}catch(e){return console.error("Error formatting STX:",e),"0.000000"}},C=(t,e=4,r=4)=>t?t.length<=e+r?t:`${t.slice(0,e+2)}...${t.slice(-r)}`:"",h=t=>{if(t==null)return"0";const e=Number(t);return isNaN(e)?"0":e.toLocaleString()},b=(t,e=2)=>{if(t==null)return"0%";const r=Number(t);return isNaN(r)?"0%":(r*100).toFixed(e)+"%"},c=t=>{if(!t)return"--";const e=new Date(t);return isNaN(e.getTime())?"--":e.toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"})},g=t=>{if(!t)return"--";const e=new Date,r=new Date(t),o=Math.floor((e-r)/1e3);if(o<-1){const i=Math.abs(o);return i<60?"in a few seconds":i<3600?`in ${Math.floor(i/60)}m`:i<86400?`in ${Math.floor(i/3600)}h`:`in ${Math.floor(i/86400)}d`}return o<5?"just now":o<60?`${o}s ago`:o<3600?`${Math.floor(o/60)}m ago`:o<86400?`${Math.floor(o/3600)}h ago`:o<604800?`${Math.floor(o/86400)}d ago`:c(t)};class A{constructor(e="mainnet"){this.network=e==="mainnet"?new s.StacksMainnet:new s.StacksTestnet,this.contractAddress=u}async callReadOnly(e,r=[],o){const i=await n.callReadOnlyFunction({contractAddress:this.contractAddress,contractName:a.VAULT,functionName:e,functionArgs:r,network:this.network,senderAddress:o||this.contractAddress});return i.type===7||i.type===8?n.cvToValue(i.value):n.cvToValue(i)}async getVault(e){if(e==null)throw new Error("vaultId is required");return this.callReadOnly("get-vault",[n.uintCV(e)])}async getTimeRemaining(e){if(e==null)throw new Error("vaultId is required");return this.callReadOnly("get-time-remaining",[n.uintCV(e)])}async canWithdraw(e){if(e==null)throw new Error("vaultId is required");return this.callReadOnly("can-withdraw",[n.uintCV(e)])}async getTVL(){return this.callReadOnly("get-tvl",[])}getCreateVaultOptions(e,r){return{contractAddress:this.contractAddress,contractName:a.VAULT,functionName:"create-vault",functionArgs:[n.uintCV(e*1e6),n.uintCV(r)],network:this.network,anchorMode:n.AnchorMode.Any,postConditionMode:n.PostConditionMode.Deny}}getWithdrawOptions(e){if(e==null)throw new Error("vaultId is required");return{contractAddress:this.contractAddress,contractName:a.VAULT,functionName:"request-withdraw",functionArgs:[n.uintCV(e)],network:this.network,anchorMode:n.AnchorMode.Any,postConditionMode:n.PostConditionMode.Deny}}}Object.defineProperty(exports,"AnchorMode",{enumerable:!0,get:()=>n.AnchorMode});Object.defineProperty(exports,"PostConditionMode",{enumerable:!0,get:()=>n.PostConditionMode});Object.defineProperty(exports,"bufferCV",{enumerable:!0,get:()=>n.bufferCV});Object.defineProperty(exports,"noneCV",{enumerable:!0,get:()=>n.noneCV});Object.defineProperty(exports,"principalCV",{enumerable:!0,get:()=>n.principalCV});Object.defineProperty(exports,"responseErrorCV",{enumerable:!0,get:()=>n.responseErrorCV});Object.defineProperty(exports,"responseOkCV",{enumerable:!0,get:()=>n.responseOkCV});Object.defineProperty(exports,"someCV",{enumerable:!0,get:()=>n.someCV});Object.defineProperty(exports,"stringAsciiCV",{enumerable:!0,get:()=>n.stringAsciiCV});Object.defineProperty(exports,"stringUtf8CV",{enumerable:!0,get:()=>n.stringUtf8CV});Object.defineProperty(exports,"uintCV",{enumerable:!0,get:()=>n.uintCV});exports.CONTRACT_ADDRESS=u;exports.CONTRACT_NAMES=a;exports.LOCK_PERIODS=l;exports.MAX_DEPOSIT=d;exports.MIN_DEPOSIT=f;exports.TimeFiClient=A;exports.formatAddress=C;exports.formatDate=c;exports.formatNumber=h;exports.formatPercent=b;exports.formatRelativeTime=g;exports.formatSTX=m;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "timefi-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Core SDK for interacting with TimeFi Protocol on Stacks",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -11,12 +11,12 @@
|
|
|
11
11
|
],
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
|
-
"url": "git+https://github.com/AdekunleBamz/
|
|
14
|
+
"url": "git+https://github.com/AdekunleBamz/timefi-sdk.git"
|
|
15
15
|
},
|
|
16
16
|
"bugs": {
|
|
17
|
-
"url": "https://github.com/AdekunleBamz/
|
|
17
|
+
"url": "https://github.com/AdekunleBamz/timefi-sdk/issues"
|
|
18
18
|
},
|
|
19
|
-
"homepage": "https://github.com/AdekunleBamz/
|
|
19
|
+
"homepage": "https://github.com/AdekunleBamz/timefi-sdk#readme",
|
|
20
20
|
"publishConfig": {
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|