Skip to content

Commit

Permalink
chore: clean constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
thounyy committed Sep 20, 2024
1 parent 33b3ccd commit 9eb4617
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 70 deletions.
30 changes: 15 additions & 15 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getObjectId } from "./lib/utils.js";
import { ConfigDepsProposal, ConfigNameProposal, ConfigRulesProposal } from "./lib/proposal/proposals/config.js";
import { ProposalArgs } from "./types/proposalTypes.js";
import { Proposal } from "./lib/proposal/proposal.js";
import { Extensions } from "./lib/extensions.js";

const proposalRegistry: Record<string, typeof Proposal> = {
[ProposalTypes.configName]: ConfigNameProposal,
Expand All @@ -23,27 +24,26 @@ export class KrakenClient {
* @param client connection to fullnode
*/

public client: SuiClient;
public account: Account;
public multisig: Multisig;

private constructor(
public network: "mainnet" | "testnet" | "devnet" | "localnet" | string,
) {
const url = (network == "mainnet" || network == "testnet" || network == "devnet" || network == "localnet") ? getFullnodeUrl(network) : network;
this.client = new SuiClient({ url });
this.account = new Account(this.client, "");
this.multisig = new Multisig(this.client, "");
}
public client: SuiClient,
public account: Account,
public multisig: Multisig,
public extensions: Extensions,
) {}

static async init(
network: "mainnet" | "testnet" | "devnet" | "localnet" | string,
network: "mainnet" | "testnet" | "devnet" | "localnet" | string,
userAddr: string,
multisigId?: string,
): Promise<KrakenClient> {
const kraken = new KrakenClient(network);
kraken.account = await Account.init(kraken.client, userAddr);
kraken.multisig = await Multisig.init(kraken.client, userAddr, multisigId);
const url = (network == "mainnet" || network == "testnet" || network == "devnet" || network == "localnet") ? getFullnodeUrl(network) : network;
const client = new SuiClient({ url });

const account = await Account.init(client, userAddr);
const multisig = await Multisig.init(client, userAddr, multisigId);
const extensions = await Extensions.init(client);

const kraken = new KrakenClient(client, account, multisig, extensions);
return kraken;
}

Expand Down
14 changes: 6 additions & 8 deletions src/lib/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Transaction, TransactionArgument, TransactionObjectInput, TransactionResult } from "@mysten/sui/transactions";
import { Transaction, TransactionObjectInput, TransactionResult } from "@mysten/sui/transactions";
import { SuiClient, SuiObjectResponse } from "@mysten/sui/client";
import { Account as AccountRaw } from "../.gen/kraken-multisig/account/structs";
import { Multisig as MultisigRaw } from "../.gen/kraken-multisig/multisig/structs";
Expand All @@ -14,17 +14,15 @@ export interface AccountData {
}

export class Account implements AccountData {
id: string = "" ;
username: string = "";
profilePicture: string = "";
multisigs: {id: string, name: string}[] = [];
public id: string = "" ;
public username: string = "";
public profilePicture: string = "";
public multisigs: {id: string, name: string}[] = [];

constructor(
public client: SuiClient,
public userAddr: string,
) {
this.client = client;
}
) {}

static async init(
client: SuiClient,
Expand Down
31 changes: 14 additions & 17 deletions src/lib/multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ export interface MultisigData {
}

export class Multisig implements MultisigData {
epoch: number = 0;
id: string = "";
name: string = "";
deps: Dep[] = [];
roles: Map<string, Role> = new Map();
members: MemberAccount[] = [];
proposals: Proposal[] = [];
public epoch: number = 0;
public id: string = "";
public name: string = "";
public deps: Dep[] = [];
public roles: Map<string, Role> = new Map();
public members: MemberAccount[] = [];
public proposals: Proposal[] = [];

constructor(
public client: SuiClient,
public userAddr: string,
) {
this.client = client;
}
) {}

static async init(
client: SuiClient,
Expand Down Expand Up @@ -107,8 +105,8 @@ export class Multisig implements MultisigData {
roles.set(role.name, { threshold: Number(role.threshold), totalWeight: roleWeights.get(role.name) || 0 });
});
// get Proposals with actions
const proposals = await Promise.all(multisigRaw!.proposals.inner.map(async (proposal: ProposalFields) => {
return await this.initProposalWithActions(this.client, proposal);
const proposals = await Promise.all(multisigRaw!.proposals.inner.map(async (fields: ProposalFields) => {
return await this.initProposalWithActions(this.client, fields);
}));

return {
Expand Down Expand Up @@ -446,15 +444,14 @@ export class Multisig implements MultisigData {
// Factory function to create the appropriate proposal type
async initProposalWithActions(
client: SuiClient,
proposalFields: ProposalFields
fields: ProposalFields
): Promise<Proposal> {
switch ("0x" + proposalFields.auth.witness.name) {
switch ("0x" + fields.auth.witness.name) {
case `${KRAKEN_ACTIONS}::config::ConfigNameProposal`:
const proposal = new ConfigNameProposal(client, this.id);
return await ConfigNameProposal.init(proposal, proposalFields);
return await ConfigNameProposal.init(client, this.id, fields);
// ... other cases for different proposal types
default:
throw new Error(`Proposal type ${proposalFields.auth.witness.name} not supported.`);
throw new Error(`Proposal type ${fields.auth.witness.name} not supported.`);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/lib/proposal/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { ProposalFields } from "../../.gen/kraken-multisig/proposals/structs"
import { approveProposal, removeApproval, executeProposal } from "../../.gen/kraken-multisig/multisig/functions"
import { CLOCK } from "src/types/constants";

export interface ProposalData {
}

export abstract class Proposal {
auth?: { witness: string, name: string };
key?: string;
Expand All @@ -26,8 +23,8 @@ export abstract class Proposal {
this.multisig = multisig;
}

init(self: Proposal, proposal: ProposalFields) {
self.setProposalFromFields(proposal);
init(fields: ProposalFields) {
this.setProposalFromFields(fields);
}

abstract propose(tx: Transaction, multisigId: string, ...args: any[]): TransactionResult;
Expand Down
62 changes: 37 additions & 25 deletions src/lib/proposal/proposals/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ export class ConfigNameProposal extends Proposal {
public multisig: string,
) {
super(client, multisig);
this.multisig = multisig;
}

static async init(
self: ConfigNameProposal,
proposal: ProposalFields,
client: SuiClient,
multisig: string,
fields: ProposalFields,
): Promise<ConfigNameProposal> {
self.setProposalFromFields(proposal);
const proposal = new ConfigNameProposal(client, multisig);
proposal.setProposalFromFields(fields);
// resolve actions
const actions = await self.fetchActions(proposal.actions.id);
self.args = { name: actions[0].inner };
const actions = await proposal.fetchActions(fields.actions.id);
if (actions.length === 0) {
throw new Error('No actions found for the ConfigName proposal');
}

return self;
proposal.args = { name: actions[0].inner };
return proposal;
}

propose(
Expand Down Expand Up @@ -64,29 +68,33 @@ export class ConfigNameProposal extends Proposal {
}

export class ConfigRulesProposal extends Proposal {
public args?: ConfigRulesFields;

public args?: ConfigRulesFields;
constructor(
public client: SuiClient,
public multisig: string,
) {
super(client, multisig);
this.multisig = multisig;
}

static async init(
self: ConfigRulesProposal,
proposal: ProposalFields,
client: SuiClient,
multisig: string,
fields: ProposalFields,
): Promise<ConfigRulesProposal> {
self.setProposalFromFields(proposal);
const proposal = new ConfigRulesProposal(client, multisig);
proposal.setProposalFromFields(fields);
// resolve actions
const actions = await self.fetchActions(proposal.actions.id);
self.args = {
const actions = await proposal.fetchActions(fields.actions.id);
if (actions.length === 0) {
throw new Error('No actions found for the ConfigRules proposal');
}

proposal.args = {
members: actions[0].inner,
thresholds: actions[1].inner,
};

return self;
return proposal;
}

propose(
Expand Down Expand Up @@ -158,21 +166,25 @@ export class ConfigDepsProposal extends Proposal {
public multisig: string,
) {
super(client, multisig);
this.multisig = multisig;
}

static async init(
self: ConfigDepsProposal,
proposal: ProposalFields,
client: SuiClient,
multisig: string,
fields: ProposalFields,
): Promise<ConfigDepsProposal> {
self.setProposalFromFields(proposal);
const proposal = new ConfigDepsProposal(client, multisig);
proposal.setProposalFromFields(fields);
// resolve actions
const actions = await self.fetchActions(proposal.actions.id);
self.args = {
const actions = await proposal.fetchActions(fields.actions.id);
if (actions.length === 0) {
throw new Error('No actions found for the ConfigDeps proposal');
}

proposal.args = {
deps: actions[0].inner,
};

return self;
return proposal;
}

propose(
Expand Down

0 comments on commit 9eb4617

Please sign in to comment.