-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
258 lines (229 loc) · 10.5 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { program } from 'commander';
import log from 'loglevel';
import * as web3 from '@solana/web3.js';
import * as splToken from '@solana/spl-token';
import { readFileSync, writeFileSync } from 'fs';
import { FanoutClient, MembershipModel } from "@glasseaters/hydra-sdk";
import { NodeWallet } from "@project-serum/common"; //TODO remove this
//import { BorshAccountsCoder, Program, Provider } from "@project-serum/anchor";
import { parseTokenAccount, sendMultipleInstructions } from '@strata-foundation/spl-utils';
programCommand('create')
.option('-s, --shares <string>', 'The total number of shares')
.option('-n, --hydraName <string>', 'The name of the hydra wallet')
.action(async (directory, cmd) => {
const { keypair, env, rpc, shares, hydraName } = cmd.opts();
//console.log(cmd.opts());
const walletKeyPair = loadWalletKey(keypair);
let connection;
if (rpc === 'undefined') {
console.log(rpc);
connection = new web3.Connection(rpc);
}
else {
connection = new web3.Connection(web3.clusterApiUrl(env));
}
let fanoutClient = new FanoutClient(connection, new NodeWallet(new web3.Account(walletKeyPair.secretKey)));
const init = await fanoutClient.initializeFanout({
totalShares: shares,
name: hydraName,
membershipModel: MembershipModel.NFT,
})
console.log("Fanout: %s\nNativeAccount: %s", init.fanout, init.nativeAccount);
});
programCommand('add_members')
.option('-f, --fanout <string>', 'The fanout public key')
.option('-n, --nativeAccount <string>', 'The native account of the fanout')
.option('-s, --shares <string>', 'The number of shares to give each member')
.option('-o, --holders <string>', 'The JSON file containing the holder list')
.action(async (directory, cmd) => {
const { keypair, env, rpc, fanout, nativeAccount, shares, holders } = cmd.opts();
const fanoutPubkey = new web3.PublicKey(fanout);
const nativeAccountPubkey = new web3.PublicKey(nativeAccount);
const walletKeyPair = loadWalletKey(keypair);
let connection;
if (rpc === 'undefined') {
console.log(rpc);
connection = new web3.Connection(rpc);
}
else {
connection = new web3.Connection(web3.clusterApiUrl(env));
}
let fanoutClient = new FanoutClient(connection, new NodeWallet(new web3.Account(walletKeyPair.secretKey)));
let holdersJson = JSON.parse(readFileSync(holders).toString());
//let receivers: String[] = [];
for (let holder of holdersJson) {
//receivers.push(holder.owner_wallet);
console.log("Adding %s", holder);
await fanoutClient.addMemberNft({
fanout: fanoutPubkey,
fanoutNativeAccount: nativeAccountPubkey,
membershipKey: new web3.PublicKey(holder.mint_account),
shares: shares
});
}
});
// programCommand('fund')
// .option('-f, --fanout <string>', 'The fanout public key')
// .option('-n, --nativeAccount <string>', 'The native account of the fanout')
// .option('-m --mint <string>', 'The mint of what to distribute')
// .option('-a --amount <string>', 'The amount to transfer')
// .action(async (directory, cmd) => {
// const { keypair, env, rpc, fanout, nativeAccount, mint, amount } = cmd.opts();
// const fanoutPubkey = new web3.PublicKey(fanout);
// const nativeAccountPubkey = new web3.PublicKey(nativeAccount);
// const mintPubkey = new web3.PublicKey(mint);
// const walletKeyPair = loadWalletKey(keypair);
// let connection;
// if (rpc === 'undefined') {
// console.log(rpc);
// connection = new web3.Connection(rpc);
// }
// else {
// connection = new web3.Connection(web3.clusterApiUrl(env));
// }
// let fanoutClient = new FanoutClient(connection, new NodeWallet(new web3.Account(walletKeyPair.secretKey)));
// transfer(connection, walletKeyPair, walletKeyPair.publicKey, fanoutPubkey, mintPubkey, +amount);
// });
programCommand('distribute')
.option('-f, --fanout <string>', 'The fanout public key')
.option('-n, --nativeAccount <string>', 'The native account of the fanout')
.option('-m --mint <string>', 'The mint of what to distribute')
.action(async (directory, cmd) => {
const { keypair, env, rpc, fanout, nativeAccount, mint } = cmd.opts();
const fanoutPubkey = new web3.PublicKey(fanout);
const nativeAccountPubkey = new web3.PublicKey(nativeAccount);
const mintPubkey = new web3.PublicKey(mint);
const walletKeyPair = loadWalletKey(keypair);
let connection;
if (rpc === 'undefined') {
console.log(rpc);
connection = new web3.Connection(rpc);
}
else {
connection = new web3.Connection(web3.clusterApiUrl(env));
}
let fanoutClient = new FanoutClient(connection, new NodeWallet(new web3.Account(walletKeyPair.secretKey)));
// console.log(await fanoutClient.getMembers({ fanout: fanoutPubkey }));
let members = await fanoutClient.getMembers({ fanout: fanoutPubkey });
// console.log(fanoutPubkey.toString());
// console.log(mintPubkey.toString());
// console.log(walletKeyPair.publicKey.toString());
await fanoutClient.distributeAll({
fanout: fanoutPubkey,
mint: mintPubkey,
payer: fanoutClient.wallet.publicKey,
});
// const errorMap = new Map<number, string>();
// const instructionResultsTokens = await fanoutClient.distributeAllInstructions({ fanout: fanoutPubkey, mint: mintPubkey, payer: walletKeyPair.publicKey });
// console.log(instructionResultsTokens.instructions);
// await sendMultipleInstructions(
// errorMap,
// fanoutClient.provider,
// instructionResultsTokens.instructions,
// instructionResultsTokens.signers,
// walletKeyPair.publicKey,
// )
// This is the caller of the Distribute method, it can be a bot or a user,
// they just need enough funds to pay for the transaction fee. If you're using
// this code, airdrop a sol to distributionBot.
//const member1.mint = "NFT Mint for Member 1";
// for (let member of members) {
// console.log(member.toString());
// let atas = await connection.getTokenLargestAccounts(member);
// let tokenAccount;
// for (let ata of atas.value) {
// if (ata.uiAmount === 1) {
// tokenAccount = ata.address;
// }
// }
// console.log(tokenAccount.toString());
// let owner = parseTokenAccount((await connection.getAccountInfo(tokenAccount)).data).owner;
// //console.log(owner.owner.toString());
// //splToken.Token.account
// let distributeToMember = await fanoutClient.distributeNftMemberInstructions(
// {
// distributeForMint: true,
// member: owner,
// membershipKey: member,
// fanout: fanoutPubkey,
// payer: fanoutClient.wallet.publicKey,
// },
// );
// const tx = await fanoutClient.sendInstructions(
// [...distributeToMember.instructions],
// [walletKeyPair],
// walletKeyPair.publicKey
// );
// if (!!tx.RpcResponseAndContext.value.err) {
// const txdetails = await connection.getConfirmedTransaction(tx.TransactionSignature);
// console.log(txdetails, tx.RpcResponseAndContext.value.err);
// }
// }
});
function programCommand(name: string) {
return program
.command(name)
.option(
'-e, --env <string>',
'Solana cluster env name',
'devnet', //mainnet-beta, testnet, devnet
)
.option(
'-r, --rpc <string>',
"The endpoint to connect to.",
)
.option(
'-k, --keypair <path>',
`Solana wallet location`,
'--keypair not provided',
)
.option('-l, --log-level <string>', 'log level', setLogLevel);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function setLogLevel(value, prev) {
if (value === undefined || value === null) {
return;
}
log.info('setting the log value to: ' + value);
log.setLevel(value);
}
program.parse(process.argv);
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function loadWalletKey(keypair: string): web3.Keypair {
if (!keypair || keypair == '') {
throw new Error('Keypair is required!');
}
const loaded = web3.Keypair.fromSecretKey(
new Uint8Array(JSON.parse(readFileSync(keypair).toString())),
);
log.info(`wallet public key: ${loaded.publicKey}`);
return loaded;
}
// async function transfer(connection: web3.Connection, payer: web3.Signer, from: web3.PublicKey, to: web3.PublicKey, token: web3.PublicKey, amount: Number) {
// console.log("Transfering %d %s to %s", token.toString(), to.toString());
// let fromATA = await splToken.Token.getAssociatedTokenAddress(token, from);
// let fromBalance = (await connection.getTokenAccountBalance(fromATA)).value.amount;
// let toBalance = "0";
// while (toBalance !== amount.toString()) {
// let toATA: web3.PublicKey;
// try {
// toATA = (await splToken.getOrCreateAssociatedTokenAccount(connection, payer, token, to)).address;
// //console.log("To: %d", (await connection.getTokenAccountBalance(toATA)).value.amount);
// await splToken.transferChecked(connection, payer, fromATA, token, toATA, from, amount, 0);
// fromBalance = (await connection.getTokenAccountBalance(fromATA)).value.amount;
// toBalance = (await connection.getTokenAccountBalance(toATA)).value.amount;
// }
// catch (e) {
// console.log(e);
// try {
// fromBalance = (await connection.getTokenAccountBalance(fromATA)).value.amount;
// toBalance = (await connection.getTokenAccountBalance(toATA)).value.amount;
// }
// catch (e) {
// console.log(e);
// }
// }
// }
// }