@tribecahq/tribeca-sdk
Version:
The TypeScript SDK for Tribeca, an open standard and toolkit for launching DAOs on Solana.
177 lines • 6.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GovernorWrapper = void 0;
const client_1 = require("@gokiprotocol/client");
const solana_contrib_1 = require("@saberhq/solana-contrib");
const token_utils_1 = require("@saberhq/token-utils");
const web3_js_1 = require("@solana/web3.js");
const pda_1 = require("./pda");
/**
* Wrapper around a Governor.
*/
class GovernorWrapper {
constructor(sdk, governorKey) {
this.sdk = sdk;
this.governorKey = governorKey;
this._governor = null;
}
get provider() {
return this.sdk.provider;
}
get program() {
return this.sdk.programs.Govern;
}
async reload() {
return await this.program.account.governor.fetch(this.governorKey);
}
async data() {
if (!this._governor) {
this._governor = await this.reload();
}
return this._governor;
}
async findProposalAddress(index) {
const [key] = await (0, pda_1.findProposalAddress)(this.governorKey, index);
return key;
}
async fetchProposalByKey(key) {
return await this.program.account.proposal.fetch(key);
}
async fetchProposal(index) {
const key = await this.findProposalAddress(index);
return await this.fetchProposalByKey(key);
}
async fetchProposalMeta(proposalKey) {
const [key] = await (0, pda_1.findProposalMetaAddress)(proposalKey);
return await this.program.account.proposalMeta.fetch(key);
}
/**
* Creates a ProposalMeta for a proposal.
* Only the Proposer may call this.
*
* @returns
*/
async createProposalMeta({ proposal, proposer = this.sdk.provider.wallet.publicKey, title, descriptionLink, }) {
const [proposalMetaKey, bump] = await (0, pda_1.findProposalMetaAddress)(proposal);
const ix = this.sdk.programs.Govern.instruction.createProposalMeta(bump, title, descriptionLink, {
accounts: {
proposal,
proposer,
proposalMeta: proposalMetaKey,
payer: this.provider.wallet.publicKey,
systemProgram: web3_js_1.SystemProgram.programId,
},
});
return this.provider.newTX([ix]);
}
/**
* Creates a new Proposal.
* @returns
*/
async createProposal({ proposer = this.sdk.provider.wallet.publicKey, instructions, }) {
const { provider } = this.sdk;
const governorData = await this.reload();
const index = new token_utils_1.u64(governorData.proposalCount);
const [proposal, bump] = await (0, pda_1.findProposalAddress)(this.governorKey, index);
const ixs = [];
ixs.push(this.sdk.programs.Govern.instruction.createProposal(bump, instructions, {
accounts: {
governor: this.governorKey,
proposal,
proposer,
payer: provider.wallet.publicKey,
systemProgram: web3_js_1.SystemProgram.programId,
},
}));
return {
proposal,
index,
tx: this.provider.newTX(ixs),
};
}
/**
* Queues a Proposal for execution by the Smart Wallet.
* @returns
*/
async queueProposal({ index, smartWalletProgram = client_1.GOKI_ADDRESSES.SmartWallet, payer = this.provider.wallet.publicKey, }) {
const governorData = await this.data();
const [proposal] = await (0, pda_1.findProposalAddress)(this.governorKey, index);
const smartWalletDataRaw = await this.program.provider.connection.getAccountInfo(governorData.smartWallet);
if (!smartWalletDataRaw) {
throw new Error("smart wallet not found");
}
const smartWalletData = client_1.GOKI_CODERS.SmartWallet.accountParsers.smartWallet(smartWalletDataRaw.data);
const [txKey, txBump] = await (0, client_1.findTransactionAddress)(governorData.smartWallet, smartWalletData.numTransactions.toNumber());
return new solana_contrib_1.TransactionEnvelope(this.sdk.provider, [
this.program.instruction.queueProposal(txBump, {
accounts: {
governor: this.governorKey,
proposal,
smartWallet: governorData.smartWallet,
smartWalletProgram,
transaction: txKey,
payer,
systemProgram: web3_js_1.SystemProgram.programId,
},
}),
]);
}
/**
* Cancel a new Proposal.
* @returns
*/
cancelProposal({ proposal, proposer = this.sdk.provider.wallet.publicKey, }) {
return new solana_contrib_1.TransactionEnvelope(this.sdk.provider, [
this.sdk.programs.Govern.instruction.cancelProposal({
accounts: {
governor: this.governorKey,
proposal,
proposer,
},
}),
]);
}
async getOrCreateVote({ proposal, voter = this.sdk.provider.wallet.publicKey, payer = this.sdk.provider.wallet.publicKey, }) {
const [voteKey, bump] = await (0, pda_1.findVoteAddress)(proposal, voter);
try {
await this.program.account.vote.fetch(voteKey);
return { voteKey, instruction: null };
}
catch {
return {
voteKey,
instruction: await this.createVoteIx({
proposal,
voteKeyAndBump: [voteKey, bump],
voter,
payer,
}),
};
}
}
async createVoteIx({ proposal, voteKeyAndBump, voter = this.sdk.provider.wallet.publicKey, payer = this.sdk.provider.wallet.publicKey, }) {
if (!voteKeyAndBump) {
voteKeyAndBump = await (0, pda_1.findVoteAddress)(proposal, voter);
}
const [voteKey, bump] = voteKeyAndBump;
return this.program.instruction.newVote(bump, voter, {
accounts: {
vote: voteKey,
proposal,
payer,
systemProgram: web3_js_1.SystemProgram.programId,
},
});
}
async setGovernanceParamsIx(newParams) {
const { smartWallet } = await this.data();
return this.program.instruction.setGovernanceParams(newParams, {
accounts: {
governor: this.governorKey,
smartWallet,
},
});
}
}
exports.GovernorWrapper = GovernorWrapper;
//# sourceMappingURL=governor.js.map