Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernized syntax for v1.0 #14

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
33 changes: 0 additions & 33 deletions esy.lock/index.json

This file was deleted.

SuzanneSoy marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions ligo.esy.lock/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"checksum": "e92ce686850cf2560180a590d07494d3",
"root": "@ligo/dao-jsligo@link-dev:./ligo.json",
"node": {
"@ligo/[email protected]@d41d8cd9": {
"id": "@ligo/[email protected]@d41d8cd9",
"name": "@ligo/fa",
"version": "1.0.6",
"source": {
"type": "install",
"source": [
"archive:https://packages.ligolang.org/-/api/@ligo/fa/-/@ligo/fa-1.0.6.tgz#sha1:94927de94317e20b350e51ecfa90a647c322e665"
]
},
"overrides": [],
"dependencies": [],
"devDependencies": []
},
"@ligo/dao-jsligo@link-dev:./ligo.json": {
"id": "@ligo/dao-jsligo@link-dev:./ligo.json",
"name": "@ligo/dao-jsligo",
"version": "link-dev:./ligo.json",
"source": { "type": "link-dev", "path": ".", "manifest": "ligo.json" },
"overrides": [],
"dependencies": [ "@ligo/[email protected]@d41d8cd9" ],
"devDependencies": []
}
}
}
2 changes: 1 addition & 1 deletion package.json → ligo.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "ligoLANG <https://ligolang.org/>",
"main": "src/main.jsligo",
"dependencies": {
"@ligo/fa": "1.0.1"
"@ligo/fa": "1.0.6"
},
"bugs": {
"url": "https://github.com/ligolang/dao-jslgo/issues"
Expand Down
2 changes: 1 addition & 1 deletion src/constants.jsligo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const noOperation : list<operation> = list([])
export const noOperation : list<operation> = list([])
42 changes: 21 additions & 21 deletions src/errors.jsligo
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const proposalAlreadyExists = "PROPOSAL_ALREADY_EXISTS"
const votingPeriod = "VOTING_PERIOD"
const notVotingPeriod = "NOT_VOTING_PERIOD"
const noProposal = "NO_PROPOSAL"
const notCreator = "NOT_CREATOR"
const canceled = "CANCELED"
const timelockNotFound = "TIMELOCK_NOT_FOUND"
const timelockLocked = "TIMELOCK_LOCKED"
const timelockUnlocked = "TIMELOCK_UNLOCKED"
const notZeroAmount = "NOT_ZERO_AMOUNT"
const noLockedTokens = "NO_LOCKED_TOKENS"
const notEnoughBalance = "NOT_ENOUGH_BALANCE"
const receiverNotFound = "RECEIVER_NOT_FOUND"
const fa2TotalSupplyNotFound = "FA2_TOTAL_SUPPLY_NOT_FOUND"
const outcomeNotFound = "OUTCOME_NOT_FOUND"
const alreadyExecuted = "ALREADY_EXECUTED"
const notExecutable = "NOT_EXECUTABLE"
const hashNotFound = "HASH_NOT_FOUND"
const hashNotMatch = "HASH_NOT_MATCH"
const unpackMismatch = "UNPACK_MISMATCH"
const nothingToCancel = "NOTHING_TO_CANCEL"
export const proposalAlreadyExists = "PROPOSAL_ALREADY_EXISTS"
export const votingPeriod = "VOTING_PERIOD"
export const notVotingPeriod = "NOT_VOTING_PERIOD"
export const noProposal = "NO_PROPOSAL"
export const notCreator = "NOT_CREATOR"
export const canceled = "CANCELED"
export const timelockNotFound = "TIMELOCK_NOT_FOUND"
export const timelockLocked = "TIMELOCK_LOCKED"
export const timelockUnlocked = "TIMELOCK_UNLOCKED"
export const notZeroAmount = "NOT_ZERO_AMOUNT"
export const noLockedTokens = "NO_LOCKED_TOKENS"
export const notEnoughBalance = "NOT_ENOUGH_BALANCE"
export const receiverNotFound = "RECEIVER_NOT_FOUND"
export const fa2TotalSupplyNotFound = "FA2_TOTAL_SUPPLY_NOT_FOUND"
export const outcomeNotFound = "OUTCOME_NOT_FOUND"
export const alreadyExecuted = "ALREADY_EXECUTED"
export const notExecutable = "NOT_EXECUTABLE"
export const hashNotFound = "HASH_NOT_FOUND"
export const hashNotMatch = "HASH_NOT_MATCH"
export const unpackMismatch = "UNPACK_MISMATCH"
export const nothingToCancel = "NOTHING_TO_CANCEL"
18 changes: 9 additions & 9 deletions src/lambda.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ export type t =
| ["ParameterChange", parameterChange]
| ["OperationList", operationList];

const unpack = (hashOpt: option<bytes>, packed: bytes): t => {
return match(hashOpt, {
None: () => failwith(Errors.hashNotFound),
Some: (hash_: bytes) => {
export const unpack = (hashOpt: option<bytes>, packed: bytes): t => {
return match(hashOpt) {
when(None): failwith(Errors.hashNotFound);
when(Some(hash_)): do {
assert_with_error(
hash_ == Crypto.sha256(packed),
Errors.hashNotMatch
);

return match((Bytes.unpack(packed) as option<t>), {
None: () => failwith(Errors.unpackMismatch),
Some: (lambda_: t) => lambda_
});
return match(Bytes.unpack(packed) as option<t>) {
when(None): failwith(Errors.unpackMismatch);
when(Some(lambda_)): lambda_
};
}
});
};
};
158 changes: 84 additions & 74 deletions src/main.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,34 @@ export type storage = Storage.t;
type result = [list<operation>, storage];

const execute = (outcomeKey: nat, packed: bytes, s: storage) : result => {
const proposal = match(Big_map.find_opt(outcomeKey, s.outcomes), {
None: () => failwith(Errors.outcomeNotFound),
Some: (o: Outcome.t) => Outcome.getExecutableProposal(o)
});
const proposal = match(Big_map.find_opt(outcomeKey, s.outcomes)) {
when(None): failwith(Errors.outcomeNotFound);
when(Some(outcome)): Outcome.getExecutableProposal(outcome)
};

Timelock._checkUnlocked(proposal.timelock);
const lambda_ : Lambda.t = Lambda.unpack(proposal.hash, packed);

return match(lambda_, {
OperationList: (f: Lambda.operationList) => [
return match(lambda_) {
when(OperationList(f)): [
f(),
Storage.updateOutcome(outcomeKey, [proposal, Executed()], s)
],
ParameterChange: (f: Lambda.parameterChange) => [
];
when(ParameterChange(f)): [
Constants.noOperation,
Storage.updateOutcome(
outcomeKey,
[proposal, Executed()],
Storage.updateConfig(f,s)
)]
});
)
]
};
};

const propose = (p: Proposal.makeParams, s: storage) : result =>
match(s.proposal, {
Some: (_: Proposal.t) => failwith(Errors.proposalAlreadyExists),
None: () => [
match(s.proposal) {
when(Some(_proposal)): failwith(Errors.proposalAlreadyExists);
when(None): [
list([Token.transfer(
[s.governanceToken,
Tezos.get_sender(),
Expand All @@ -60,43 +61,49 @@ const propose = (p: Proposal.makeParams, s: storage) : result =>
Proposal.make(p, s.config.startDelay, s.config.votingPeriod),
s)
]
});
};

const cancel = (outcomeKeyOpt: option<nat>, s: storage) : result =>
[list([Token.transfer(
[s.governanceToken,
Tezos.get_self_address(),
s.config.burnAddress,
s.config.depositAmount])
]), match(outcomeKeyOpt, {
None: () => match(s.proposal, {
None: () => failwith(Errors.nothingToCancel),
Some: (p: Proposal.t) => {
Proposal._checkNotVotingPeriod(p);
assert_with_error(
p.creator == Tezos.get_sender(),
Errors.notCreator
);

return Storage.addOutcome([p, Canceled()], s);
}}),
Some: (outcomeKey: nat) => match(Big_map.find_opt(outcomeKey, s.outcomes), {
None: () => failwith(Errors.outcomeNotFound),
Some: (o: Outcome.t) => {
const [p, state] = o;
assert_with_error(
p.creator == Tezos.get_sender(),
Errors.notCreator
);
assert_with_error(
state != (Executed() as Outcome.state),
Errors.alreadyExecuted
);
Timelock._checkLocked(p.timelock);

return Storage.updateOutcome(outcomeKey, [p, Canceled()], s)
}})
})
[
list([Token.transfer(
[s.governanceToken,
Tezos.get_self_address(),
s.config.burnAddress,
s.config.depositAmount])
]),
match(outcomeKeyOpt) {
when(None):
match(s.proposal) {
when(None): failwith(Errors.nothingToCancel);
when(Some(proposal)): do {
Proposal._checkNotVotingPeriod(proposal);
assert_with_error(
proposal.creator == Tezos.get_sender(),
Errors.notCreator
);

return Storage.addOutcome([proposal, Canceled()], s);
}
};
when(Some(outcomeKey)):
match(Big_map.find_opt(outcomeKey, s.outcomes)) {
when(None): failwith(Errors.outcomeNotFound);
when(Some(outcome)): do {
const [p, state] = outcome;
assert_with_error(
p.creator == Tezos.get_sender(),
Errors.notCreator
);
assert_with_error(
state != (Executed() as Outcome.state),
Errors.alreadyExecuted
);
Timelock._checkLocked(p.timelock);

return Storage.updateOutcome(outcomeKey, [p, Canceled()], s)
}
}
}
];

const lock = (amount_: nat, s: storage) : result => {
Expand Down Expand Up @@ -133,33 +140,34 @@ const release = (amount_: nat, s: storage) : result => {
};

const vote = (choice: bool, s: storage) : storage =>
match(s.proposal, {
None: () => failwith(Errors.noProposal),
Some: (p: Proposal.t) => {
Proposal._checkIsVotingPeriod(p);
match(s.proposal) {
when(None): failwith(Errors.noProposal);
when(Some(proposal)): do {
Proposal._checkIsVotingPeriod(proposal);
const amount_ = Vault.getForUserExn([s.vault, Tezos.get_sender()]);
return Storage.updateVotes(p, [choice, amount_], s);
}});
return Storage.updateVotes(proposal, [choice, amount_], s);
}
};

const endVote = (s: storage) : result =>
match(s.proposal,{
None: () => failwith(Errors.noProposal),
Some: (p: Proposal.t) => {
Proposal._checkVotingPeriodEnded(p);
const totalSupply = match(Token.getTotalSupply(s.governanceToken),{
None: () => failwith(Errors.fa2TotalSupplyNotFound),
Some: (n: nat) => n
});
match(s.proposal) {
when(None): failwith(Errors.noProposal);
when(Some(proposal)): do {
Proposal._checkVotingPeriodEnded(proposal);
const totalSupply = match(Token.getTotalSupply(s.governanceToken)) {
when(None): failwith(Errors.fa2TotalSupplyNotFound);
when(Some(n)): n
};
const outcome = Outcome.make(
p,
proposal,
totalSupply,
s.config.refundThreshold,
s.config.quorumThreshold,
s.config.superMajority
);
const [_, state] = outcome;

let transferToAddr = p.creator;
let transferToAddr = proposal.creator;
if (Rejected_(WithoutRefund()) == state) {
transferToAddr = s.config.burnAddress;
}
Expand All @@ -171,17 +179,19 @@ const endVote = (s: storage) : result =>
s.config.depositAmount])])
, Storage.addOutcome(outcome, s)
];
}});
}
};

@entry
SuzanneSoy marked this conversation as resolved.
Show resolved Hide resolved
const main = (action: parameter, store: storage) : result => {
assert_with_error(Tezos.get_amount() == (0 as tez), Errors.notZeroAmount);
return match(action, {
Propose: (p: Proposal.makeParams) => propose(p, store),
Cancel: (nOpt: option<nat>) => cancel(nOpt, store),
Lock: (n: nat) => lock(n, store),
Release: (n: nat) => release(n, store),
Execute: (p: Outcome.executeParams) => execute(p.outcomeKey, p.packed, store),
Vote: (v: Vote.choice) => [Constants.noOperation, vote(v, store)],
EndVote: () => endVote(store)
});
return match(action) {
when(Propose(proposalMakeParams)): propose(proposalMakeParams, store);
when(Cancel(nOpt)): cancel(nOpt, store);
when(Lock(n)): lock(n, store);
when(Release(n)): release(n, store);
when(Execute(outcomeExecuteParam)): execute(outcomeExecuteParam.outcomeKey, outcomeExecuteParam.packed, store);
when(Vote(voteChoice)): [Constants.noOperation, vote(voteChoice, store)];
when(EndVote): endVote(store);
};
};
4 changes: 2 additions & 2 deletions src/outcome.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const makeState = (
}
}

const make = (p: Proposal.t,
export const make = (p: Proposal.t,
totalSupply: nat,
refundThreshold: nat,
quorumThreshold: nat,
Expand All @@ -57,7 +57,7 @@ const make = (p: Proposal.t,
)
];

const getExecutableProposal = (outcome: t) : Proposal.t => {
export const getExecutableProposal = (outcome: t) : Proposal.t => {
const [p, state] = outcome;

if ((Accepted() as state) == state) {
Expand Down
Loading