-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.ts
187 lines (162 loc) · 6.69 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import inquirer = require("inquirer");
import { SolStaker } from "./src/sol-staker";
import { FireblocksSDK } from "fireblocks-sdk";
import * as fs from 'fs'
import { LAMPORTS_TO_SOL } from "./src/utils";
require('dotenv').config();
var DEVNET = true;
const apiSecret = fs.readFileSync((process.env.API_SECRET_PATH), "utf8");
const fireblocks = new FireblocksSDK(apiSecret, process.env.API_KEY, "https://sandbox-api.fireblocks.io");
async function waitToProceed(){
await inquirer.prompt(
{
type: "input",
message: "Press 'Enter' to continue",
name: "doesNotMatter"
}
);
}
(async () => {
let exit = false;
console.log("Welcome to Fireblocks Solana Staking CLI!")
const vault = await inquirer.prompt<any>({
type: "input",
name: "vaId",
message: 'Enter Vault Account ID'
});
if(!Number(vault.vaId) && Number(vault.vaId)!= 0){
console.log(Number(vault.vaId))
throw new Error("Wrong vault account ID input - should be a number ");
}
else if((Number(vault.vaId) < 0 || Number(vault.vaId) > 4000000000))
throw new Error("ID should be an integer between 0 to N.")
const mainnet = await inquirer.prompt<any>({
type: "list",
name: "confirm",
message: "Do you want to stake on mainnet?",
choices: [
'No',
'Yes'
]
});
if(mainnet.confirm == "Yes")
DEVNET = false;
while(!exit){
const operationsList: inquirer.QuestionCollection = {
type: "list",
name: "staking",
message: 'Choose Staking Operation',
choices: [
'Create Stake Account',
'Delegate',
'Deactivate',
'Withdraw staked amount',
'Get Rewards info',
'Exit'
]
};
const solStaker = new SolStaker(fireblocks, vault.vaId, DEVNET);
const operation = await inquirer.prompt<any>(operationsList);
switch(operation.staking){
case 'Create Stake Account':
const amount = await inquirer.prompt<any>({
type: "input",
name: "amount",
message: 'Enter the amount to stake'
});
if(!Number(amount.amount))
throw new Error("Wrong amount input");
const confirmStakeAccount = await inquirer.prompt<any>({
type: "list",
name: "confirm",
message: `Going to create a new stake account from vault account ${vault.vaId} with an amount of ${amount.amount}\n Please confirm:`,
choices: [
'No',
'Yes'
]
});
if(confirmStakeAccount.confirm == "Yes")
await solStaker.createStakeAccount(amount.amount)
else
console.log("Cancelling the operation")
await waitToProceed();
break;
case 'Delegate':
const delegate = await inquirer.prompt<any>({
type: "input",
name: "validator",
message: 'Enter the validator address'
});
if((delegate.validator).length > 44 &&(delegate.validator).length < 32)
throw new Error("Wrong validator address input")
const confirmDelegate = await inquirer.prompt<any>({
type: "list",
name: "confirm",
message: `Going to delegate from vault ${vault.vaId} to ${delegate.validator}\n Please confirm:`,
choices: [
'No',
'Yes'
]
});
if(confirmDelegate.confirm == "Yes")
await solStaker.delegate(delegate.validator);
else
console.log("Cancelling the operation")
await waitToProceed();
break;
case 'Deactivate':
const confirmDeactivate = await inquirer.prompt<any>({
type: "list",
name: "confirm",
message: `Going to deactivate your stake account in vault ${vault.vaId}\n Please confirm:`,
choices: [
'No',
'Yes'
]
});
if(confirmDeactivate.confirm == "Yes")
await solStaker.deactivate();
else
console.log("Cancelling the operation")
await waitToProceed();
break;
case 'Withdraw staked amount':
const stakeBalance = await solStaker.getStakedBalance()
const amountToWithdraw = await inquirer.prompt<any>({
type: "input",
name: "amount",
message: 'Enter the amount to withdraw'
});
if(Number(amountToWithdraw.amount) <= stakeBalance.total/LAMPORTS_TO_SOL){
const confirmWithdraw = await inquirer.prompt<any>({
type: "list",
name: "confirm",
message: `Going to withdraw ${amountToWithdraw.amount} out of ${stakeBalance.total/LAMPORTS_TO_SOL} SOL\n Please confirm:`,
choices: [
'No',
'Yes'
]
});
if(confirmWithdraw.confirm == "Yes")
await solStaker.withdrawStakedBalance(amountToWithdraw.amount)
else
console.log("Cancelling the operation")
}else{
throw new Error("Cannot withdraw an amount greater than the actual balance")
}
await waitToProceed();
break;
case("Get Rewards info"):
await solStaker.getStakedBalance();
await waitToProceed();
break;
case("Exit"):
exit = true;
console.log("Thanks for using Fireblocks Solana Staking CLI")
break;
default:
console.log("Wrong Choice")
break;
}
}
})();