Skip to content

Commit

Permalink
#41 #42 Base test scenario for Electorate is implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
anilhelvaci committed Nov 21, 2022
1 parent 3bdedab commit 52e9176
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 165 deletions.
106 changes: 105 additions & 1 deletion contract/src/governance/lendingPoolElectionManager.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,114 @@
import { E, Far } from '@endo/far';
import { makeStore } from '@agoric/store';
import { CONTRACT_ELECTORATE } from '@agoric/governance';

/**
*
* @param {ZCF} zcf
* @param {Object} privateArgs
*/
const start = (zcf, privateArgs) => {
const start = async (zcf, privateArgs) => {
/** @type ZoeService */
const zoe = zcf.getZoeService();
const {
timer,
governedContractInstallation,
governed: {
issuerKeywordRecord: governedIssuerKeywordRecord,
terms: contractTerms,
},
} = zcf.getTerms();

assert(
contractTerms.governedParams[CONTRACT_ELECTORATE],
X`Contract must declare ${CONTRACT_ELECTORATE} as a governed parameter`,
);

const augmentedTerms = harden({
...contractTerms,
electionManager: zcf.getInstance(),
});

const {
creatorFacet: governedCF,
instance: governedInstance,
publicFacet: governedPF,
adminFacet,
} = await E(zoe).startInstance(
governedContractInstallation,
governedIssuerKeywordRecord,
// @ts-expect-error XXX governance types
augmentedTerms,
privateArgs.governed,
);

const questionSeats = makeStore('QuestionSeats');

const limitedCreatorFacet = E(governedCF).getLimitedCreatorFacet();
const governedParamMgrRetriever = E(governedCF).getParamMgrRetriever();

/** @type {() => Promise<Instance>} */
const getElectorateInstance = async () => {
const invitationAmount = await E(governedPF).getInvitationAmount(
CONTRACT_ELECTORATE,
);
return invitationAmount.value[0].instance;
};

/** @type {() => Promise<PoserFacet>} */
const getUpdatedPoserFacet = async () => {
const newInvitation = await E(
E(governedParamMgrRetriever).get({ key: 'governedParams' }),
).getInternalParamValue(CONTRACT_ELECTORATE);

return E(E(zoe).offer(newInvitation)).getOfferResult();
};
const poserFacet = await getUpdatedPoserFacet();
assert(poserFacet, 'question poser facet must be initialized');

const makePoseQuestionsInvitation = () => {
/** @type OfferHandler */
const poseQuestion = (poserSeat) => {

};

return zcf.makeInvitation(poseQuestion, 'PoseQuestionsInvittion');
};

const makeVoteOnQuestionInvitation = () => {
/** @type OfferHandler */
const voteOnQuestion = (voterSeat) => {

};

return zcf.makeInvitation(voteOnQuestion, 'VoteOnQuestionInvitation');
};

const makeRedeemAssetInvitation = () => {
/** @type OfferHandler */
const redeem = (voterSeat) => {

};

return zcf.makeInvitation(redeem, 'VoteOnQuestionInvitation');
};

const publicFacet = Far('PublicFacet', {
makePoseQuestionsInvitation,
makeVoteOnQuestionInvitation,
makeRedeemAssetInvitation,
getGovernedContract: () => governedInstance
});

const creatorFacet = Far('CreatorFacet', {
getElectorateInstance,
getCreatorFacet: () => limitedCreatorFacet,
getAdminFacet: () => adminFacet,
getInstance: () => governedInstance,
getPublicFacet: () => governedPF,
});

return { creatorFacet, publicFacet };
};

harden(start);
Expand Down
66 changes: 24 additions & 42 deletions contract/src/governance/lendingPoolElectorate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
startCounter,
} from './tools.js';
import { E, Far } from '@endo/far';
import { makeHandle } from '@agoric/zoe/src/makeHandle.js';

/**
*
Expand All @@ -26,54 +27,34 @@ const start = (zcf) => {
subscriber: questionsSubscriber,
} = makePublishKit();

const initGovernedContext = async (keyword, brand, issuer, treshold) => {
Object.assign(governedContext, {
keyword: keyword,
brand: brand,
issuer: issuer,
treshold: treshold
});
await zcf.saveIssuer(issuer, keyword);
harden(governedContext);
const updateTotalSupply = totalSupply => {
governedContext.totalSupply = totalSupply;
};

const addQuestion = async (counterInstallation, questionSpec) => {
assertGovernedContextInitialized(governedContext);

const makeAddQuestionInvitation = () => {
/** @type OfferHandler */
const addQuestion = async (poserSeat, { counterInstallation, questionSpec }) => {
assertGovernedContextInitialized(governedContext);
assertCanPoseQuestions(poserSeat, governedContext.keyword, governedContext.treshold);
// TODO: assertOfferArgs
const { publicFacet, instance } = await startCounter(
zcf,
questionSpec,
quorumThreshold(governedContext.totalSupply, questionSpec.quorumRule),
counterInstallation,
allQuestions,
questionsPublisher,
);

const { zcfSeat: questionSeat } = zcf.makeEmptySeatKit();
const { give: { [governedContext.keyword]: amountToLock } } = poserSeat.getProposal();

questionSeat.incrementBy(
poserSeat.decrementBy(harden({ [governedContext.keyword]: amountToLock })),
);

zcf.reallocate(questionSeat, poserSeat);

const { creatorFacet, publicFacet, deadline, questionHandle, instance } = await startCounter(
zcf,
questionSpec,
quorumThreshold(questionSpec.quorumRule),
counterInstallation,
allQuestions,
questionsPublisher,
);

const questionFacet = { voteCap: creatorFacet, publicFacet, deadline, questionSeat };
allQuestions.set(questionHandle, questionFacet);
return { publicFacet, instance };
};

return { publicFacet, instance };
};
return zcf.makeInvitation(addQuestion, 'AddQuestion');
const voteOnQuestion = (questionHandle, positions, shares) => {
const { voteCap } = allQuestions.get(questionHandle);
return E(voteCap).submitVote(makeHandle('Voter'), positions, shares);
};

const eleltorateFacet = {
makeAddQuestionInvitation,
initGovernedContext,
addQuestion,
voteOnQuestion,
updateTotalSupply,
};

const publicFacet = Far('PublicFacet', {
Expand All @@ -86,8 +67,9 @@ const start = (zcf) => {
const creatorFacet = Far('CreatorFacet', {
getElectorateFacetInvitation: () => getElectorateFacetInvitation(zcf, eleltorateFacet),
getQuestionSubscriber: () => questionsSubscriber,
makeAddQuestionInvitation,
initGovernedContext,
addQuestion,
voteOnQuestion,
updateTotalSupply,
});

return { creatorFacet, publicFacet };
Expand Down
15 changes: 6 additions & 9 deletions contract/src/governance/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import { getOpenQuestions, getQuestion, startCounter } from '@agoric/governance/

const { ceilDivide } = natSafeMath;

const COMMITEE_SIZE = 3;

export const quorumThreshold = quorumRule => {
export const quorumThreshold = (committeeSize, quorumRule) => {
switch (quorumRule) {
case QuorumRule.MAJORITY:
return ceilDivide(COMMITEE_SIZE, 2);
return ceilDivide(committeeSize, 2);
case QuorumRule.ALL:
return COMMITEE_SIZE;
return committeeSize;
case QuorumRule.NO_QUORUM:
return 0;
default:
Expand Down Expand Up @@ -45,10 +43,9 @@ export const getElectorateFacetInvitation = (zcf, electorateFacet) => {

export const assertGovernedContextInitialized = (governedContext) => {
console.log('governedContext', governedContext);
const { keyword, brand, issuer, treshold } = governedContext;
assert(keyword || brand || issuer || treshold,
X`Make sure you initialize the governedContext with the following +
properities: keyword, brand, issuer, treshold`);
const { totalSupply } = governedContext;
assert(totalSupply,
X`Make sure you initialize the governedContext with the following properities: totalSupply`);
};

export {
Expand Down
Loading

0 comments on commit 52e9176

Please sign in to comment.