-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbootstrap.ts
More file actions
201 lines (190 loc) · 6.23 KB
/
bootstrap.ts
File metadata and controls
201 lines (190 loc) · 6.23 KB
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
import * as anchor from "@coral-xyz/anchor";
import { init as initOrg } from "@helium/organization-sdk";
import { init as initProp } from "@helium/proposal-sdk";
import { init as initState, settings } from "@helium/state-controller-sdk";
import { registrarKey } from "@helium/voter-stake-registry-sdk";
import { Connection, PublicKey } from "@solana/web3.js";
import os from "os";
import yargs from "yargs";
import Squads from "@sqds/sdk";
import { loadKeypair, sendInstructionsOrSquads } from "./utils";
import { sendInstructions, withPriorityFees } from "@helium/spl-utils";
export async function run(args: any = process.argv) {
const yarg = yargs(args).options({
wallet: {
alias: "k",
describe: "Anchor wallet keypair",
default: `${os.homedir()}/.config/solana/id.json`,
},
url: {
alias: "u",
default: "http://127.0.0.1:8899",
describe: "The solana url",
},
realmName: {
type: "string",
default: "Helium",
},
name: {
type: "string",
required: true,
},
mint: {
type: "string",
required: true,
},
authority: {
type: "string",
required: true,
describe: "The authority of the organization",
},
multisig: {
type: "string",
describe:
"Address of the squads multisig for subdao authority. If not provided, your wallet will be the authority",
},
authorityIndex: {
type: "number",
describe: "Authority index for squads. Defaults to 1",
default: 1,
},
threshold: {
type: "number",
default: "10000000000000000",
},
ver: {
type: "string",
default: "V1",
},
});
const argv = await yarg.argv;
process.env.ANCHOR_WALLET = argv.wallet;
process.env.ANCHOR_PROVIDER_URL = argv.url;
anchor.setProvider(anchor.AnchorProvider.local(argv.url));
const realmKey = PublicKey.findProgramAddressSync(
[Buffer.from("governance", "utf-8"), Buffer.from(argv.realmName, "utf-8")],
new PublicKey("hgovkRU6Ghe1Qoyb54HdSLdqN7VtxaifBzRmh9jtd3S")
)[0];
console.log("Realm is", realmKey.toBase58());
const registrarK = registrarKey(realmKey, new PublicKey(argv.mint))[0];
console.log("Registrar is", registrarK.toBase58());
const provider = anchor.getProvider() as anchor.AnchorProvider;
const walletKP = loadKeypair(argv.wallet);
const wallet = new anchor.Wallet(walletKP);
const orgProgram = await initOrg(provider);
const proposalProgram = await initProp(provider);
const stateProgram = await initState(provider);
// Must have 100,000,000 veHNT, 67% of the vote. Choose the top one as the winner.
const nodes = settings()
.and(
settings().and(
settings().top(1),
settings().and(
settings().choicePercentage(67),
// 100,000,000 veHNT
settings().choiceVoteWeight(new anchor.BN(argv.threshold))
)
),
settings().offsetFromStartTs(new anchor.BN(60 * 60 * 24 * 7))
)
.build();
const initResolutionSettings =
stateProgram.methods.initializeResolutionSettingsV0({
name: `${argv.name} Single ${argv.ver}`,
settings: {
nodes,
},
});
const resolutionSettings = (await initResolutionSettings.pubkeys())
.resolutionSettings!;
if (!(await exists(provider.connection, resolutionSettings))) {
console.log("Creating resolution settings");
await sendInstructions(
provider,
await withPriorityFees({
connection: provider.connection,
computeUnits: 200000,
instructions: [await initResolutionSettings.instruction()],
})
);
}
const voteController = registrarK;
const initProposalConfig = proposalProgram.methods.initializeProposalConfigV0(
{
name: `${argv.name} Default ${argv.ver}`,
voteController,
stateController: resolutionSettings!,
onVoteHook: stateProgram.programId,
authority: wallet.publicKey,
}
);
const proposalConfig = (await initProposalConfig.pubkeys()).proposalConfig!;
if (!(await exists(provider.connection, proposalConfig))) {
console.log("Creating proposal config");
await sendInstructions(
provider,
await withPriorityFees({
connection: provider.connection,
computeUnits: 200000,
instructions: [await initProposalConfig.instruction()],
})
);
}
const squads = Squads.endpoint(process.env.ANCHOR_PROVIDER_URL, wallet, {
commitmentOrConfig: "finalized",
});
let authority = argv.authority
? new PublicKey(argv.authority)
: provider.wallet.publicKey;
const multisig = argv.multisig ? new PublicKey(argv.multisig) : null;
if (multisig) {
authority = squads.getAuthorityPDA(multisig, argv.authorityIndex);
}
const initOrganization = orgProgram.methods.initializeOrganizationV0({
name: argv.name,
defaultProposalConfig: proposalConfig,
authority,
proposalProgram: proposalProgram.programId,
uri: "https://helium.com",
});
const organization = (await initOrganization.pubkeys()).organization;
if (organization) {
if (!(await exists(provider.connection, organization))) {
console.log("Creating organization");
await sendInstructions(
provider,
await withPriorityFees({
connection: provider.connection,
computeUnits: 200000,
instructions: [await initOrganization.instruction()],
})
);
console.log(`Created org ${organization.toBase58()}`);
} else {
const organizationAcc = await orgProgram.account.organizationV0.fetch(
organization
);
const instruction = await orgProgram.methods
.updateOrganizationV0({
defaultProposalConfig: proposalConfig,
proposalProgram: null,
uri: null,
authority,
})
.accountsPartial({ organization, authority: organizationAcc.authority })
.instruction();
await sendInstructionsOrSquads({
provider,
instructions: [instruction],
executeTransaction: false,
squads,
multisig: argv.multisig ? new PublicKey(argv.multisig) : undefined,
authorityIndex: argv.authorityIndex,
signers: [],
});
}
}
}
async function exists(connection: Connection, pkey: PublicKey) {
return !!(await connection.getAccountInfo(pkey));
}