stx-vault-sdk 1.0.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/.github/workflows/publish.yml +28 -0
- package/LICENSE +7 -0
- package/README.md +180 -0
- package/examples/react-demo/app.js +15 -0
- package/package.json +44 -0
- package/src/analytics/tvl.ts +4 -0
- package/src/analytics/vault-stats.ts +4 -0
- package/src/cli.ts +75 -0
- package/src/events/vault-events.ts +4 -0
- package/src/react/useVault.ts +13 -0
- package/src/utils/network.ts +3 -0
- package/src/vault/balance.ts +17 -0
- package/src/vault/create-vault.ts +18 -0
- package/src/vault/history.ts +4 -0
- package/src/vault/lock.ts +22 -0
- package/src/vault/withdraw.ts +17 -0
- package/src/wallet/connect.ts +16 -0
- package/src/wallet/session.ts +11 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Publish SDK to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
|
|
12
|
+
steps:
|
|
13
|
+
- name: Checkout repo
|
|
14
|
+
uses: actions/checkout@v3
|
|
15
|
+
|
|
16
|
+
- name: Setup Node
|
|
17
|
+
uses: actions/setup-node@v3
|
|
18
|
+
with:
|
|
19
|
+
node-version: 18
|
|
20
|
+
registry-url: https://registry.npmjs.org/
|
|
21
|
+
|
|
22
|
+
- name: Install dependencies
|
|
23
|
+
run: npm install
|
|
24
|
+
|
|
25
|
+
- name: Publish to npm
|
|
26
|
+
run: npm publish --access public
|
|
27
|
+
env:
|
|
28
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
STX Vault SDK
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
Overview
|
|
9
|
+
|
|
10
|
+
STX Vault SDK is a production-ready JavaScript/TypeScript SDK for building STX vaults and time-lock applications on the Stacks blockchain.
|
|
11
|
+
|
|
12
|
+
It allows developers to:
|
|
13
|
+
|
|
14
|
+
Lock STX in smart contract vaults
|
|
15
|
+
|
|
16
|
+
Withdraw funds after the unlock period
|
|
17
|
+
|
|
18
|
+
Create new vaults (multi-pool support)
|
|
19
|
+
|
|
20
|
+
Track real-time vault events
|
|
21
|
+
|
|
22
|
+
Get vault analytics & TVL
|
|
23
|
+
|
|
24
|
+
Connect wallets like Xverse and Leather
|
|
25
|
+
|
|
26
|
+
Use React hooks for front-end apps
|
|
27
|
+
|
|
28
|
+
Interact via CLI commands
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
Installation
|
|
37
|
+
|
|
38
|
+
npm install stx-vault-sdk
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
Quick Start
|
|
44
|
+
|
|
45
|
+
Connect Wallet
|
|
46
|
+
|
|
47
|
+
import { connectWallet } from "stx-vault-sdk"
|
|
48
|
+
|
|
49
|
+
await connectWallet()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
Lock STX
|
|
55
|
+
|
|
56
|
+
import { lockSTX } from "stx-vault-sdk"
|
|
57
|
+
|
|
58
|
+
await lockSTX(
|
|
59
|
+
100000000, // Amount in micro-STX
|
|
60
|
+
900000, // Unlock block
|
|
61
|
+
PRIVATE_KEY, // Your private key
|
|
62
|
+
CONTRACT_ADDRESS, // Smart contract address
|
|
63
|
+
"stx-vault" // Contract name
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
Withdraw STX
|
|
70
|
+
|
|
71
|
+
import { withdrawSTX } from "stx-vault-sdk"
|
|
72
|
+
|
|
73
|
+
await withdrawSTX(
|
|
74
|
+
PRIVATE_KEY,
|
|
75
|
+
CONTRACT_ADDRESS,
|
|
76
|
+
"stx-vault"
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
Create Vault
|
|
83
|
+
|
|
84
|
+
import { createVault } from "stx-vault-sdk"
|
|
85
|
+
|
|
86
|
+
await createVault(
|
|
87
|
+
900000, // Unlock block
|
|
88
|
+
PRIVATE_KEY,
|
|
89
|
+
CONTRACT_ADDRESS,
|
|
90
|
+
"stx-vault"
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
Get Vault Balance
|
|
97
|
+
|
|
98
|
+
import { getVaultBalance } from "stx-vault-sdk"
|
|
99
|
+
|
|
100
|
+
const balance = await getVaultBalance(
|
|
101
|
+
USER_ADDRESS,
|
|
102
|
+
CONTRACT_ADDRESS,
|
|
103
|
+
"stx-vault"
|
|
104
|
+
)
|
|
105
|
+
console.log("Vault Balance:", balance)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
React Hook Example
|
|
111
|
+
|
|
112
|
+
import { useVault } from "stx-vault-sdk"
|
|
113
|
+
|
|
114
|
+
const { balance, refresh } = useVault(USER_ADDRESS, CONTRACT_ADDRESS, "stx-vault")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
CLI Usage
|
|
120
|
+
|
|
121
|
+
After installing and building the SDK:
|
|
122
|
+
|
|
123
|
+
npx stx-vault connect
|
|
124
|
+
npx stx-vault lock -a 1000000 -b 900000 -k YOUR_KEY -c SPXXXX -n stx-vault
|
|
125
|
+
npx stx-vault withdraw -k YOUR_KEY -c SPXXXX -n stx-vault
|
|
126
|
+
npx stx-vault create -b 900000 -k YOUR_KEY -c SPXXXX -n stx-vault
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
Analytics & TVL
|
|
132
|
+
|
|
133
|
+
import { getVaultStats, getVaultTVL } from "stx-vault-sdk"
|
|
134
|
+
|
|
135
|
+
const stats = await getVaultStats("https://api.yourvault.com")
|
|
136
|
+
const tvl = await getVaultTVL("https://api.yourvault.com")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
Real-time Vault Events
|
|
142
|
+
|
|
143
|
+
import { watchVaultEvents } from "stx-vault-sdk"
|
|
144
|
+
|
|
145
|
+
const events = await watchVaultEvents(
|
|
146
|
+
"https://api.mainnet.hiro.so",
|
|
147
|
+
"SPXXXX.stx-vault"
|
|
148
|
+
)
|
|
149
|
+
console.log(events)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
Supported Wallets
|
|
155
|
+
|
|
156
|
+
Xverse Wallet
|
|
157
|
+
|
|
158
|
+
Leather Wallet
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
Badges & Ecosystem
|
|
165
|
+
|
|
166
|
+
Works on Stacks Mainnet
|
|
167
|
+
|
|
168
|
+
Supports mobile-first wallets
|
|
169
|
+
|
|
170
|
+
TypeScript-ready for front-end & back-end apps
|
|
171
|
+
|
|
172
|
+
Fully DeFi protocol-ready SDK
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
License
|
|
179
|
+
|
|
180
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "stx-vault-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Production SDK for STX vault and time-lock applications on the Stacks blockchain, with CLI, React hooks, analytics, and wallet support.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"stacks",
|
|
10
|
+
"stx",
|
|
11
|
+
"vault",
|
|
12
|
+
"defi",
|
|
13
|
+
"web3",
|
|
14
|
+
"time-lock",
|
|
15
|
+
"stacks-sdk",
|
|
16
|
+
"cli",
|
|
17
|
+
"react-hook",
|
|
18
|
+
"analytics"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/investorphem/stx-vault-sdk"
|
|
23
|
+
},
|
|
24
|
+
"author": "Oluwafemi Olagoke",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup src/index.ts src/cli.ts --format esm,cjs --dts",
|
|
28
|
+
"dev": "tsup src/index.ts src/cli.ts --watch",
|
|
29
|
+
"clean": "rm -rf dist"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@stacks/connect": "^7.0.1",
|
|
33
|
+
"@stacks/network": "^7.0.1",
|
|
34
|
+
"@stacks/transactions": "^7.0.1",
|
|
35
|
+
"commander": "^11.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"tsup": "^7.2.0",
|
|
39
|
+
"typescript": "^5.3.3"
|
|
40
|
+
},
|
|
41
|
+
"bin": {
|
|
42
|
+
"stx-vault": "./dist/cli.js"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { program } from "commander"
|
|
3
|
+
import { lockSTX, withdrawSTX, createVault } from "./vault"
|
|
4
|
+
import { connectWallet } from "./wallet/connect"
|
|
5
|
+
|
|
6
|
+
program.version("1.0.0").description("STX Vault CLI - interact with STX vaults")
|
|
7
|
+
|
|
8
|
+
// LOCK STX
|
|
9
|
+
program
|
|
10
|
+
.command("lock")
|
|
11
|
+
.description("Lock STX into a vault")
|
|
12
|
+
.requiredOption("-a, --amount <number>", "Amount of STX to lock (in micro-STX)")
|
|
13
|
+
.requiredOption("-b, --unlockBlock <number>", "Unlock block height")
|
|
14
|
+
.requiredOption("-k, --key <string>", "Your private key")
|
|
15
|
+
.requiredOption("-c, --contractAddress <string>", "Contract address")
|
|
16
|
+
.requiredOption("-n, --contractName <string>", "Contract name")
|
|
17
|
+
.action(async (opts) => {
|
|
18
|
+
console.log("Locking STX...")
|
|
19
|
+
const tx = await lockSTX(
|
|
20
|
+
Number(opts.amount),
|
|
21
|
+
Number(opts.unlockBlock),
|
|
22
|
+
opts.key,
|
|
23
|
+
opts.contractAddress,
|
|
24
|
+
opts.contractName
|
|
25
|
+
)
|
|
26
|
+
console.log("Transaction:", tx)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// WITHDRAW STX
|
|
30
|
+
program
|
|
31
|
+
.command("withdraw")
|
|
32
|
+
.description("Withdraw STX from your vault")
|
|
33
|
+
.requiredOption("-k, --key <string>", "Your private key")
|
|
34
|
+
.requiredOption("-c, --contractAddress <string>", "Contract address")
|
|
35
|
+
.requiredOption("-n, --contractName <string>", "Contract name")
|
|
36
|
+
.action(async (opts) => {
|
|
37
|
+
console.log("Withdrawing STX...")
|
|
38
|
+
const tx = await withdrawSTX(
|
|
39
|
+
opts.key,
|
|
40
|
+
opts.contractAddress,
|
|
41
|
+
opts.contractName
|
|
42
|
+
)
|
|
43
|
+
console.log("Transaction:", tx)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// CREATE VAULT
|
|
47
|
+
program
|
|
48
|
+
.command("create")
|
|
49
|
+
.description("Create a new vault with a specific unlock block")
|
|
50
|
+
.requiredOption("-b, --unlockBlock <number>", "Unlock block height")
|
|
51
|
+
.requiredOption("-k, --key <string>", "Your private key")
|
|
52
|
+
.requiredOption("-c, --contractAddress <string>", "Contract address")
|
|
53
|
+
.requiredOption("-n, --contractName <string>", "Contract name")
|
|
54
|
+
.action(async (opts) => {
|
|
55
|
+
console.log("Creating vault...")
|
|
56
|
+
const tx = await createVault(
|
|
57
|
+
Number(opts.unlockBlock),
|
|
58
|
+
opts.key,
|
|
59
|
+
opts.contractAddress,
|
|
60
|
+
opts.contractName
|
|
61
|
+
)
|
|
62
|
+
console.log("Vault created:", tx)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// WALLET CONNECT (optional CLI)
|
|
66
|
+
program
|
|
67
|
+
.command("connect")
|
|
68
|
+
.description("Connect a wallet")
|
|
69
|
+
.action(async () => {
|
|
70
|
+
console.log("Connecting wallet...")
|
|
71
|
+
await connectWallet()
|
|
72
|
+
console.log("Wallet connected!")
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
program.parse(process.argv)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useState } from "react"
|
|
2
|
+
import { getVaultBalance } from "../vault/balance"
|
|
3
|
+
|
|
4
|
+
export function useVault(address: string, contractAddress: string, contractName: string) {
|
|
5
|
+
const [balance, setBalance] = useState<number | null>(null)
|
|
6
|
+
|
|
7
|
+
async function refresh() {
|
|
8
|
+
const result = await getVaultBalance(address, contractAddress, contractName)
|
|
9
|
+
setBalance(result)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return { balance, refresh }
|
|
13
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { callReadOnlyFunction } from "@stacks/transactions"
|
|
2
|
+
import { network } from "../utils/network"
|
|
3
|
+
|
|
4
|
+
export async function getVaultBalance(
|
|
5
|
+
userAddress: string,
|
|
6
|
+
contractAddress: string,
|
|
7
|
+
contractName: string
|
|
8
|
+
) {
|
|
9
|
+
return callReadOnlyFunction({
|
|
10
|
+
contractAddress,
|
|
11
|
+
contractName,
|
|
12
|
+
functionName: "get-balance",
|
|
13
|
+
functionArgs: [],
|
|
14
|
+
senderAddress: userAddress,
|
|
15
|
+
network
|
|
16
|
+
})
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { makeContractCall, uintCV } from "@stacks/transactions"
|
|
2
|
+
import { network } from "../utils/network"
|
|
3
|
+
|
|
4
|
+
export async function createVault(
|
|
5
|
+
unlockBlock: number,
|
|
6
|
+
senderKey: string,
|
|
7
|
+
contractAddress: string,
|
|
8
|
+
contractName: string
|
|
9
|
+
) {
|
|
10
|
+
return makeContractCall({
|
|
11
|
+
contractAddress,
|
|
12
|
+
contractName,
|
|
13
|
+
functionName: "create-vault",
|
|
14
|
+
functionArgs: [uintCV(unlockBlock)],
|
|
15
|
+
senderKey,
|
|
16
|
+
network
|
|
17
|
+
})
|
|
18
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { makeContractCall, uintCV } from "@stacks/transactions"
|
|
2
|
+
import { network } from "../utils/network"
|
|
3
|
+
|
|
4
|
+
export async function lockSTX(
|
|
5
|
+
amount: number,
|
|
6
|
+
unlockBlock: number,
|
|
7
|
+
senderKey: string,
|
|
8
|
+
contractAddress: string,
|
|
9
|
+
contractName: string
|
|
10
|
+
) {
|
|
11
|
+
return makeContractCall({
|
|
12
|
+
contractAddress,
|
|
13
|
+
contractName,
|
|
14
|
+
functionName: "lock-stx",
|
|
15
|
+
functionArgs: [
|
|
16
|
+
uintCV(amount),
|
|
17
|
+
uintCV(unlockBlock)
|
|
18
|
+
],
|
|
19
|
+
senderKey,
|
|
20
|
+
network
|
|
21
|
+
})
|
|
22
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { makeContractCall } from "@stacks/transactions"
|
|
2
|
+
import { network } from "../utils/network"
|
|
3
|
+
|
|
4
|
+
export async function withdrawSTX(
|
|
5
|
+
senderKey: string,
|
|
6
|
+
contractAddress: string,
|
|
7
|
+
contractName: string
|
|
8
|
+
) {
|
|
9
|
+
return makeContractCall({
|
|
10
|
+
contractAddress,
|
|
11
|
+
contractName,
|
|
12
|
+
functionName: "withdraw",
|
|
13
|
+
functionArgs: [],
|
|
14
|
+
senderKey,
|
|
15
|
+
network
|
|
16
|
+
})
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AppConfig, UserSession, showConnect } from "@stacks/connect"
|
|
2
|
+
|
|
3
|
+
const appConfig = new AppConfig(["store_write"])
|
|
4
|
+
|
|
5
|
+
export const userSession = new UserSession({ appConfig })
|
|
6
|
+
|
|
7
|
+
export function connectWallet() {
|
|
8
|
+
showConnect({
|
|
9
|
+
userSession,
|
|
10
|
+
appDetails: {
|
|
11
|
+
name: "STX Vault",
|
|
12
|
+
icon: "/logo.png"
|
|
13
|
+
},
|
|
14
|
+
onFinish: () => window.location.reload()
|
|
15
|
+
})
|
|
16
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AppConfig, UserSession } from "@stacks/connect"
|
|
2
|
+
|
|
3
|
+
const appConfig = new AppConfig(["store_write"])
|
|
4
|
+
|
|
5
|
+
export const userSession = new UserSession({ appConfig })
|
|
6
|
+
|
|
7
|
+
export function getUserData() {
|
|
8
|
+
if (userSession.isUserSignedIn()) {
|
|
9
|
+
return userSession.loadUserData()
|
|
10
|
+
}
|
|
11
|
+
}
|
package/tsconfig.json
ADDED