Skip to content

Commit 794253a

Browse files
authored
sign near tx + examples + cleanup (#122)
* sign near tx + examples + cleanup * lint
1 parent 0f733a8 commit 794253a

File tree

20 files changed

+4684
-3320
lines changed

20 files changed

+4684
-3320
lines changed

README.md

Lines changed: 57 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
## Description
22

3-
Kiln JS SDK makes it easy to interact with the Kiln staking platform.
4-
5-
It provides a simple way of crafting staking transactions as well getting real time and historical data about your stakes.
6-
7-
Check out the [full documentation](https://docs.kiln.fi/v1/connect/overview).
8-
9-
## Supported protocols
10-
11-
- ADA (mainnet)
12-
- ATOM (mainnet and testnet)
13-
- DOT (mainnet)
14-
- DYDX (mainnet)
15-
- ETH (mainnet and holesky)
16-
- FET (mainnet)
17-
- INJ (mainnet)
18-
- KAVA (mainnet)
19-
- KSM (mainnet)
20-
- MATIC (deprecated, use POL instead) (mainnet and sepolia)
21-
- NEAR (mainnet and testnet)
22-
- NOBLE (mainnet)
23-
- OSMO (mainnet)
24-
- POL (mainnet and sepolia)
25-
- SOL (mainnet and testnet)
26-
- TIA (mainnet)
27-
- TON (mainnet and testnet)
28-
- XTZ (mainnet and ghostnet)
29-
- ZETA (mainnet)
30-
- More protocol to come, don't hesitate to contact us ([email protected])
3+
Kiln SDK is a wrapper around the Kiln Connect API, which allows you to craft staking transactions as well getting real time and historical data about your stakes.
4+
5+
On top of that, the SDK provides a way to sign your transactions with Fireblocks using their Contract call and Raw Signing features.
6+
7+
- [Kiln Connect documentation](https://docs.kiln.fi/v1/connect/overview)
8+
- [OpenAPI reference](https://docs.api.kiln.fi/reference/getaccounts)
319

3210
## Installation
3311

34-
You can install the JS SDK with npm:
12+
You can install the JS SDK with your favorite package manager:
3513

3614
```sh
37-
npm install --save @kilnfi/sdk
15+
bun install @kilnfi/sdk
3816
```
3917

4018
## Setup
@@ -43,68 +21,67 @@ In order to use this sdk, you will need a kiln api token.
4321
Please contact [email protected] to get one.
4422

4523
```typescript
46-
import { Kiln } from "../src/kiln";
24+
import { Kiln } from "@kilnfi/sdk";
4725

4826
const k = new Kiln({
49-
testnet: true,
27+
baseUrl: "https://api.kiln.fi",
5028
apiToken: "kiln_xxx",
5129
});
5230
```
5331

54-
## Craft 32 ETH staking transaction, sign it with fireblocks and broadcast it
5532

33+
## Example: Stake 1 NEAR using Fireblocks raw signing
5634
```typescript
5735
import { Kiln } from "@kilnfi/sdk";
58-
import { Integration } from "@kilnfi/sdk/lib/types/integrations";
59-
const fs = require("fs");
60-
61-
const apiSecret = fs.readFileSync(__dirname + "/path_to_fireblocks_secret", "utf8");
62-
63-
const k = new Kiln({
64-
testnet: true,
65-
apiToken: "kiln_xxx",
66-
});
67-
68-
const vault: Integration = {
69-
provider: "fireblocks",
70-
fireblocksApiKey: "YOUR_API_USER_KEY", // your fireblocks API user key
71-
fireblocksSecretKey: apiSecret, // your fireblocks private key (generated with your CSR file and your API user)
72-
vaultId: 7, // your fireblocks vault id
36+
import type { Integration } from "@kilnfi/sdk/lib/types/integrations";
37+
import fs from "node:fs";
38+
import 'dotenv/config'
39+
40+
41+
const apiSecret = fs.readFileSync(__dirname + '/fireblocks_secret.key', 'utf8');
42+
43+
const stake = async () => {
44+
// Kiln client configuration
45+
const k = new Kiln({
46+
baseUrl: 'https://api.testnet.kiln.fi',
47+
apiToken: process.env.KILN_API_KEY,
48+
});
49+
50+
// Fireblocks vault configuration
51+
const vault: Integration = {
52+
provider: 'fireblocks',
53+
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY,
54+
fireblocksSecretKey: apiSecret,
55+
vaultId: 14
56+
};
57+
58+
// Craft staking tx
59+
const tx = await k.client.POST(
60+
'/v1/near/transaction/stake',
61+
{
62+
body: {
63+
account_id: 'd3f1b917-72b1-4982-a4dd-93fce579a708',
64+
wallet: 'c36b1a5da2e60d1fd5d3a6b46f7399eb26571457f3272f3c978bc9527ad2335f',
65+
pool_id: 'kiln.pool.f863973.m0',
66+
amount_yocto: '1000000000000000000000000',
67+
}
68+
}
69+
);
70+
71+
// Sign tx with Fireblocks
72+
const signResponse = await k.fireblocks.signNearTx(vault, tx.data.data, "NEAR_TEST");
73+
74+
// Broadcast tx
75+
const broadcastedTx = await k.client.POST("/v1/near/transaction/broadcast", {
76+
body: {
77+
signed_tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
78+
}
79+
});
7380
};
7481

75-
try {
76-
// Craft 32 ETH staking transaction
77-
const tx = await k.eth.craftStakeTx("kiln_account_id", "withdrawal_address", 32);
78-
79-
// Sign it with your fireblock vault
80-
const txSigned = await k.eth.sign(vault, tx);
81-
82-
// Broadcast it
83-
const hash = await k.eth.broadcast(txSigned);
84-
} catch (err) {
85-
// handle errors
86-
}
87-
```
88-
89-
## Fetch ETH stakes and network stats
90-
91-
```typescript
92-
try {
93-
// Get stakes by accounts
94-
const stakes = await k.eth.getAccountsRewards(["kiln-account-id"]);
95-
96-
// Get stakes by wallets
97-
const stakesByWallet = await k.eth.getWalletRewards(["wallet-address"]);
98-
99-
// Get stakes by validators
100-
const stakesByValidator = await k.eth.getStakesRewards(["validator-address"]);
101-
102-
// Get network stats
103-
const stats = await k.eth.getNetworkStats();
104-
} catch (err) {
105-
// handle errors
106-
}
82+
stake();
10783
```
84+
Find complete examples in the `examples` directory.
10885

10986
## License
11087

bun.lockb

-36.5 KB
Binary file not shown.

examples/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KILN_API_KEY=
2+
FIREBLOCKS_API_KEY=

examples/.gitignore

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store
176+
177+
# Fireblocks keys
178+
*.key

examples/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Examples
2+
3+
### Requirements:
4+
5+
- Get a Kiln API key
6+
- Get a Fireblocks API key & secret key
7+
8+
### Install dependencies:
9+
10+
```bash
11+
bun install
12+
```
13+
14+
### Setup your environment:
15+
Fill your `.env` file with your Kiln and Fireblocks API keys.
16+
```
17+
cp .env.example .env
18+
```
19+
20+
### Run an example:
21+
22+
```bash
23+
bun run near.ts
24+
```

examples/bun.lockb

3.38 KB
Binary file not shown.

0 commit comments

Comments
 (0)