Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mesh-common/src/constants/protocol-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const DEFAULT_PROTOCOL_PARAMETERS: Protocol = {
};

export const DREP_DEPOSIT = "500000000";
export const VOTING_PROPOSAL_DEPOSIT = "100000000000";

export const resolveTxFees = (
txSize: number,
Expand Down
149 changes: 149 additions & 0 deletions packages/mesh-common/src/types/governance.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { ScriptHash } from "../data";
import { Anchor } from "./transaction-builder/certificate";
import { Credential } from "./transaction-builder/credential";

export type GovernanceProposalInfo = {
txHash: string;
certIndex: number;
Expand All @@ -12,3 +16,148 @@ export type GovernanceProposalInfo = {
expiration: number;
metadata: object;
};

export type Rational = {
numerator: string;
denominator: string;
};

export type RewardAddress = string;

export type GovernanceActionId = {
transactionId: string;
govActionIndex: number;
};

export type ProtocolVersion = {
major: number;
minor: number;
};

export type ProtocolParamUpdate = {
minFeeA?: string;
minFeeB?: string;
maxBlockBodySize?: number;
maxTxSize?: number;
maxBlockHeaderSize?: number;
keyDeposit?: string;
poolDeposit?: string;
maxEpoch?: number;
nOpt?: number;
poolPledgeInfluence?: Rational;
expansionRate?: Rational;
treasuryGrowthRate?: Rational;
minPoolCost?: string;
adaPerUtxoByte?: string;
costModels?: Record<string, number[]>;
executionCosts?: {
memPrice?: Rational;
stepPrice?: Rational;
};
maxTxExUnits?: {
mem: string;
steps: string;
};
maxBlockExUnits?: {
mem: string;
steps: string;
};
maxValueSize?: number;
collateralPercentage?: number;
maxCollateralInputs?: number;
poolVotingThresholds?: {
motionNoConfidence?: Rational;
committeeNormal?: Rational;
committeeNoConfidence?: Rational;
hardForkInitiation?: Rational;
ppSecurityGroup?: Rational;
};
drepVotingThresholds?: {
motionNoConfidence?: Rational;
committeeNormal?: Rational;
committeeNoConfidence?: Rational;
updateConstitution?: Rational;
hardForkInitiation?: Rational;
ppNetworkGroup?: Rational;
ppEconomicGroup?: Rational;
ppTechnicalGroup?: Rational;
ppGovGroup?: Rational;
treasuryWithdrawal?: Rational;
};
minCommitteeSize?: number;
committeeTermLimit?: number;
govActionValidityPeriod?: number;
govActionDeposit?: string;
drepDeposit?: string;
drepInactivityPeriod?: number;
refScriptCostPerByte?: Rational;
};

export type CommitteeMember = {
stakeCredential: Credential;
termLimit: number;
};

export type Committee = {
members: CommitteeMember[];
quorumThreshold: Rational;
};

export type Constitution = {
anchor: Anchor;
scriptHash?: ScriptHash;
};

export type TreasuryWithdrawals = Record<RewardAddress, string>;

export type ParameterChangeAction = {
govActionId?: GovernanceActionId;
protocolParamUpdates: ProtocolParamUpdate;
policyHash?: ScriptHash;
};

export type HardForkInitiationAction = {
govActionId?: GovernanceActionId;
protocolVersion: ProtocolVersion;
};

export type TreasuryWithdrawalsAction = {
withdrawals: TreasuryWithdrawals;
policyHash?: ScriptHash;
};

export type NoConfidenceAction = {
govActionId?: GovernanceActionId;
};

export type UpdateCommitteeAction = {
govActionId?: GovernanceActionId;
committee: Committee;
membersToRemove: Credential[];
};

export type NewConstitutionAction = {
govActionId?: GovernanceActionId;
constitution: Constitution;
};

export type InfoAction = {};

export enum GovernanceActionKind {
ParameterChangeAction = "ParameterChangeAction",
HardForkInitiationAction = "HardForkInitiationAction",
TreasuryWithdrawalsAction = "TreasuryWithdrawalsAction",
NoConfidenceAction = "NoConfidenceAction",
UpdateCommitteeAction = "UpdateCommitteeAction",
NewConstitutionAction = "NewConstitutionAction",
InfoAction = "InfoAction",
}

export type GovernanceAction =
| { kind: "ParameterChangeAction"; action: ParameterChangeAction }
| { kind: "HardForkInitiationAction"; action: HardForkInitiationAction }
| { kind: "TreasuryWithdrawalsAction"; action: TreasuryWithdrawalsAction }
| { kind: "NoConfidenceAction"; action: NoConfidenceAction }
| { kind: "UpdateCommitteeAction"; action: UpdateCommitteeAction }
| { kind: "NewConstitutionAction"; action: NewConstitutionAction }
| { kind: "InfoAction"; action: InfoAction };
4 changes: 4 additions & 0 deletions packages/mesh-common/src/types/transaction-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UTxO } from "../utxo";
import { Certificate } from "./certificate";
import { MintParam } from "./mint";
import { Output } from "./output";
import { Proposal } from "./proposal";
import { PubKeyTxIn, RefTxIn, TxIn } from "./txin";
import { Vote } from "./vote";
import { Withdrawal } from "./withdrawal";
Expand All @@ -17,6 +18,7 @@ export * from "./txin";
export * from "./withdrawal";
export * from "./certificate";
export * from "./vote";
export * from "./proposal";

export type MeshTxBuilderBody = {
inputs: TxIn[];
Expand All @@ -33,6 +35,7 @@ export type MeshTxBuilderBody = {
certificates: Certificate[];
withdrawals: Withdrawal[];
votes: Vote[];
proposals: Proposal[];
signingKey: string[];
extraInputs: UTxO[];
chainedTxs: string[];
Expand Down Expand Up @@ -60,6 +63,7 @@ export const emptyTxBuilderBody = (): MeshTxBuilderBody => ({
certificates: [],
withdrawals: [],
votes: [],
proposals: [],
signingKey: [],
chainedTxs: [],
inputsForEvaluation: {},
Expand Down
35 changes: 35 additions & 0 deletions packages/mesh-common/src/types/transaction-builder/proposal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Anchor } from "./certificate";
import { Redeemer } from "./data";
import { ScriptSource, SimpleScriptSourceInfo } from "./script";
import {
GovernanceAction,
RewardAddress,
} from "../governance";

export type ProposalType = {
governanceAction: GovernanceAction;
anchor: Anchor;
rewardAccount: RewardAddress;
deposit: string;
};

export type BasicProposal = {
type: "BasicProposal";
proposalType: ProposalType;
};

export type ScriptProposal = {
type: "ScriptProposal";
proposalType: ProposalType;
redeemer?: Redeemer;
scriptSource?: ScriptSource;
};

export type SimpleScriptProposal = {
type: "SimpleScriptProposal";
proposalType: ProposalType;
simpleScriptSource?: SimpleScriptSourceInfo;
};

export type Proposal = BasicProposal | ScriptProposal | SimpleScriptProposal;

116 changes: 116 additions & 0 deletions packages/mesh-core-cst/src/serializer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
Output,
PlutusDataType,
PlutusScript,
Proposal,
Protocol,
PubKeyTxIn,
RefTxIn,
Expand Down Expand Up @@ -129,6 +130,7 @@ import {
toCardanoVoter,
toCardanoVotingProcedure,
} from "../utils/vote";
import { toCardanoProposalProcedure } from "../utils/proposal";

const VKEY_PUBKEY_SIZE_BYTES = 32;
const VKEY_SIGNATURE_SIZE_BYTES = 64;
Expand Down Expand Up @@ -614,6 +616,7 @@ class CardanoSDKSerializerCore {
certificates,
withdrawals,
votes,
proposals,
totalCollateral,
collateralReturnAddress,
changeAddress,
Expand Down Expand Up @@ -663,6 +666,11 @@ class CardanoSDKSerializerCore {
} catch (e) {
throwErrorWithOrigin("Error serializing votes", e);
}
try {
this.addAllProposals(proposals);
} catch (e) {
throwErrorWithOrigin("Error serializing proposals", e);
}
try {
this.addAllCollateralInputs(collaterals);
} catch (e) {
Expand Down Expand Up @@ -1774,6 +1782,114 @@ class CardanoSDKSerializerCore {
this.addBasicVote({ type: "BasicVote", vote: vote.vote });
};

private addAllProposals = (proposals: Proposal[]) => {
for (let i = 0; i < proposals.length; i++) {
const proposal = proposals[i];
if (!proposal) continue;
switch (proposal.type) {
case "BasicProposal": {
this.addBasicProposal(proposal);
break;
}
case "ScriptProposal": {
this.addScriptProposal(proposal, i);
break;
}
case "SimpleScriptProposal": {
this.addSimpleScriptProposal(proposal);
break;
}
}
}
};

private addBasicProposal = (proposal: Proposal) => {
const currentProcedures = this.txBody.proposalProcedures();
const proposalProcedures = currentProcedures
? [...currentProcedures.values()]
: [];

const proposalProcedure = toCardanoProposalProcedure(
proposal.proposalType.governanceAction,
proposal.proposalType.anchor,
proposal.proposalType.rewardAccount,
BigInt(proposal.proposalType.deposit),
);

proposalProcedures.push(proposalProcedure);
this.txBody.setProposalProcedures(
CborSet.fromCore(
proposalProcedures.map(p => p.toCore()),
Serialization.ProposalProcedure.fromCore,
)
);
};

private addScriptProposal = (proposal: Proposal, index: number) => {
if (proposal.type !== "ScriptProposal") {
throw new Error("Expected ScriptProposal");
}
if (!proposal.scriptSource)
throw new Error("Script source not provided for plutus script proposal");
const plutusScriptSource = proposal.scriptSource as ScriptSource;
if (!plutusScriptSource) {
throw new Error(
"A script source for a plutus proposal was not plutus script somehow",
);
}
if (!proposal.redeemer) {
throw new Error("A redeemer was not provided for a plutus proposal");
}

// Add proposal redeemer to witness set
let redeemers = this.txWitnessSet.redeemers() ?? Redeemers.fromCore([]);
let redeemersList = [...redeemers.values()];
redeemersList.push(
new Redeemer(
RedeemerTag.Proposing,
BigInt(index),
fromBuilderToPlutusData(proposal.redeemer.data),
new ExUnits(
BigInt(proposal.redeemer.exUnits.mem),
BigInt(proposal.redeemer.exUnits.steps),
),
),
);
redeemers.setValues(redeemersList);
this.txWitnessSet.setRedeemers(redeemers);

if (plutusScriptSource.type === "Provided") {
this.addProvidedPlutusScript(plutusScriptSource.script);
} else if (plutusScriptSource.type === "Inline") {
this.addScriptRef(plutusScriptSource);
}
this.addBasicProposal(proposal);
};

private addSimpleScriptProposal = (proposal: Proposal) => {
if (proposal.type !== "SimpleScriptProposal") {
throw new Error("Expected SimpleScriptProposal");
}
if (!proposal.simpleScriptSource)
throw new Error("Script source not provided for native script proposal");
const nativeScriptSource: SimpleScriptSourceInfo =
proposal.simpleScriptSource as SimpleScriptSourceInfo;
if (!nativeScriptSource)
throw new Error(
"A script source for a native script was not a native script somehow",
);
if (nativeScriptSource.type === "Provided") {
this.scriptsProvided.add(
Script.newNativeScript(
NativeScript.fromCbor(HexBlob(nativeScriptSource.scriptCode)),
).toCbor(),
);
} else if (nativeScriptSource.type === "Inline") {
this.addSimpleScriptRef(nativeScriptSource);
}
this.addBasicProposal(proposal);
};

private mockVkeyWitnesses = (
numberOfRequiredWitnesses: number,
): VkeyWitness[] => {
Expand Down
Loading