diff --git a/auction/auction.fi b/auction/auction.fi new file mode 100644 index 0000000..7b88fd5 --- /dev/null +++ b/auction/auction.fi @@ -0,0 +1,158 @@ +struct Claim( + bool resolved, + string buyer, + string seller +); + +struct Asset( + string name, + string desc, + mutez bid, + address manager, + address winner, + Claim claim, + timestamp end, + timestamp resPeriod +); + +storage map[int=>Asset] auction; +storage int total; + +entry addAsset(Asset asset){ + if (input.asset.end < NOW || input.asset.end > input.asset.resPeriod ) { + throw(string "Invalid end or resolution dates."); + } + let nat len = length(storage.auction); + if (len == nat 0) { + storage.auction.push(int 0, input.asset); + storage.total = int 1; + } else { + storage.auction.push(storage.total, input.asset); + storage.total.add(int 1); + } +} + +entry addClaimBuyer(int id, string claim){ + if (in(storage.auction, input.id) == bool false) { + throw(string "No item exists by that Id for this auction."); + } + let Asset asset = storage.auction.get(input.id); + if (SENDER != asset.winner) { + throw(string "You are not authorized to add to claim."); + } + if (asset.end > NOW) { + throw(string "You can't make a claim on an auction still live."); + } + if (asset.resPeriod > NOW) { + asset.claim.resolved = bool false; + asset.claim.buyer = input.claim; + storage.auction.push(input.id, asset); + } +} + +entry addClaimSeller(int id, string claim){ + if (in(storage.auction, input.id) == bool false) { + throw(string "No item exists by that Id for this auction."); + } + let Asset asset = storage.auction.get(input.id); + if (SENDER != asset.manager) { + throw(string "You are not authorized to add to claim."); + } + if (asset.end > NOW) { + throw(string "You can't make a claim on an auction still live."); + } + asset.claim.seller = input.claim; + storage.auction.push(input.id, asset); +} + +entry resolveClaim(int id){ + if (in(storage.auction, input.id) == bool false) { + throw(string "No item exists by that Id for this auction."); + } + let Asset asset = storage.auction.get(input.id); + if (SENDER != asset.winner) { + throw(string "You are not authorized to resolve this claim."); + } + if (asset.end > NOW) { + throw(string "You can't resolve a claim on an auction still live."); + } + if (asset.claim.resolved == bool true) { + throw(string "There is nothing to resolve."); + } + asset.claim.resolved = bool true; + storage.auction.push(input.id, asset); +} + +entry withdraw(int id){ + if (in(storage.auction, input.id) == bool false) { + throw(string "No item exists by that Id for this auction."); + } + let Asset asset = storage.auction.get(input.id); + if (SENDER != asset.manager) { + throw(string "You are not authorized to withdraw from this auction."); + } + if (asset.claim.resolved == bool false) { + throw(string "You must resolve the claim with the buyer to receive funds.") + } + + if (asset.end > NOW || NOW < asset.resPeriod) { + throw(string "The auction has not ended or the resolution period has not past.") + } + if (asset.bid != mutez 0) { + transfer(SENDER, asset.bid); + } else { + throw(string "You cannot withdraw 0.") + } +} + +entry refund(int id){ + if (in(storage.auction, input.id) == bool false) { + throw(string "No item exists by that Id for this auction."); + } + let Asset asset = storage.auction.get(input.id); + if (SENDER != asset.manager) { + throw(string "You are not authorized to send a refund for this auction."); + } + + if (asset.end > NOW) { + throw(string "The auction has not ended."); + } + + if (asset.bid != mutez 0) { + transfer(asset.winner, asset.bid); + } else { + throw(string "You cannot refund 0.") + } + asset.claim.resolved = bool true; + storage.auction.push(input.id, asset); +} + +entry bid(int id){ + if (in(storage.auction, input.id) == bool false) { + throw(string "No item exists by that Id for this auction."); + } + let Asset asset = storage.auction.get(input.id); + let address oldWinner = asset.winner; + + if (SENDER == asset.manager) { + throw(string "You cant bid against your own item."); + } + + if (asset.end < NOW) { + throw(string "The auction for the item requested has already ended."); + } + if (AMOUNT == mutez 0) { + throw(string "You cannot bid 0."); + } + if (asset.bid >= AMOUNT){ + transfer(SENDER, AMOUNT); + throw(string "Sending funds back - bid too low."); + } else { + if (asset.bid != mutez 0) { + transfer(oldWinner, AMOUNT); + } + asset.winner = SENDER; + asset.bid = AMOUNT; + storage.auction.push(input.id, asset) + } +} diff --git a/auction/auction.fi.abi b/auction/auction.fi.abi new file mode 100644 index 0000000..81d92e6 --- /dev/null +++ b/auction/auction.fi.abi @@ -0,0 +1 @@ +{"struct":[{"name":"Claim","type":[{"name":"resolved","type":["bool"]},{"name":"buyer","type":["string"]},{"name":"seller","type":["string"]}]},{"name":"Asset","type":[{"name":"name","type":["string"]},{"name":"desc","type":["string"]},{"name":"bid","type":["mutez"]},{"name":"manager","type":["address"]},{"name":"winner","type":["address"]},{"name":"claim","type":["Claim"]},{"name":"end","type":["timestamp"]},{"name":"resPeriod","type":["timestamp"]}]}],"storage":[{"name":"auction","type":["map",["int"],["Asset"]]},{"name":"total","type":["int"]}],"entry":[{"name":"addAsset","id":"0xb03afe75","input":[{"name":"asset","type":["Asset"]}],"temp":[{"name":"len","type":["nat"]}],"code":[["if",["or",["lt",["input.asset.end"],["NOW"]],["gt",["input.asset.end"],["input.asset.resPeriod"]]],["throw",["string","Invalid end or resolution dates."]]],["set","len",["length",["storage.auction"]]],["if",["eq",["len"],["nat","0"]],[["ext","push","storage.auction",["int","0"],["input.asset"]],["set","storage.total",["int","1"]]],[["ext","push","storage.auction",["storage.total"],["input.asset"]],["ext","add","storage.total",["int","1"]]]]]},{"name":"addClaimBuyer","id":"0xaf8ead49","input":[{"name":"id","type":["int"]},{"name":"claim","type":["string"]}],"temp":[{"name":"asset","type":["Asset"]}],"code":[["if",["eq",["in",["storage.auction"],["input.id"]],["bool","false"]],["throw",["string","No item exists by that Id for this auction."]]],["set","asset",["ext","get","storage.auction",["input.id"]]],["if",["neq",["SENDER"],["asset.winner"]],["throw",["string","You are not authorized to add to claim."]]],["if",["gt",["asset.end"],["NOW"]],["throw",["string","You can't make a claim on an auction still live."]]],["if",["gt",["asset.resPeriod"],["NOW"]],[["set","asset.claim.resolved",["bool","false"]],["set","asset.claim.buyer",["input.claim"]],["ext","push","storage.auction",["input.id"],["asset"]]]]]},{"name":"addClaimSeller","id":"0x16e502df","input":[{"name":"id","type":["int"]},{"name":"claim","type":["string"]}],"temp":[{"name":"asset","type":["Asset"]}],"code":[["if",["eq",["in",["storage.auction"],["input.id"]],["bool","false"]],["throw",["string","No item exists by that Id for this auction."]]],["set","asset",["ext","get","storage.auction",["input.id"]]],["if",["neq",["SENDER"],["asset.manager"]],["throw",["string","You are not authorized to add to claim."]]],["if",["gt",["asset.end"],["NOW"]],["throw",["string","You can't make a claim on an auction still live."]]],["set","asset.claim.seller",["input.claim"]],["ext","push","storage.auction",["input.id"],["asset"]]]},{"name":"resolveClaim","id":"0x4121a61e","input":[{"name":"id","type":["int"]}],"temp":[{"name":"asset","type":["Asset"]}],"code":[["if",["eq",["in",["storage.auction"],["input.id"]],["bool","false"]],["throw",["string","No item exists by that Id for this auction."]]],["set","asset",["ext","get","storage.auction",["input.id"]]],["if",["neq",["SENDER"],["asset.winner"]],["throw",["string","You are not authorized to resolve this claim."]]],["if",["gt",["asset.end"],["NOW"]],["throw",["string","You can't resolve a claim on an auction still live."]]],["if",["eq",["asset.claim.resolved"],["bool","true"]],["throw",["string","There is nothing to resolve."]]],["set","asset.claim.resolved",["bool","true"]],["ext","push","storage.auction",["input.id"],["asset"]]]},{"name":"withdraw","id":"0x200e7681","input":[{"name":"id","type":["int"]}],"temp":[{"name":"asset","type":["Asset"]}],"code":[["if",["eq",["in",["storage.auction"],["input.id"]],["bool","false"]],["throw",["string","No item exists by that Id for this auction."]]],["set","asset",["ext","get","storage.auction",["input.id"]]],["if",["neq",["SENDER"],["asset.manager"]],["throw",["string","You are not authorized to withdraw from this auction."]]],["if",["eq",["asset.claim.resolved"],["bool","false"]],["throw",["string","You must resolve the claim with the buyer to receive funds."]]],["if",["or",["gt",["asset.end"],["NOW"]],["lt",["NOW"],["asset.resPeriod"]]],["throw",["string","The auction has not ended or the resolution period has not past."]]],["if",["neq",["asset.bid"],["mutez","0"]],["transfer",["SENDER"],["asset.bid"]],["throw",["string","You cannot withdraw 0."]]]]},{"name":"refund","id":"0x75ed3e40","input":[{"name":"id","type":["int"]}],"temp":[{"name":"asset","type":["Asset"]}],"code":[["if",["eq",["in",["storage.auction"],["input.id"]],["bool","false"]],["throw",["string","No item exists by that Id for this auction."]]],["set","asset",["ext","get","storage.auction",["input.id"]]],["if",["neq",["SENDER"],["asset.manager"]],["throw",["string","You are not authorized to send a refund for this auction."]]],["if",["gt",["asset.end"],["NOW"]],["throw",["string","The auction has not ended."]]],["if",["neq",["asset.bid"],["mutez","0"]],["transfer",["asset.winner"],["asset.bid"]],["throw",["string","You cannot refund 0."]]],["set","asset.claim.resolved",["bool","true"]],["ext","push","storage.auction",["input.id"],["asset"]]]},{"name":"bid","id":"0x65e3abe5","input":[{"name":"id","type":["int"]}],"temp":[{"name":"asset","type":["Asset"]},{"name":"oldWinner","type":["address"]}],"code":[["if",["eq",["in",["storage.auction"],["input.id"]],["bool","false"]],["throw",["string","No item exists by that Id for this auction."]]],["set","asset",["ext","get","storage.auction",["input.id"]]],["set","oldWinner",["asset.winner"]],["if",["eq",["SENDER"],["asset.manager"]],["throw",["string","You cant bid against your own item."]]],["if",["lt",["asset.end"],["NOW"]],["throw",["string","The auction for the item requested has already ended."]]],["if",["eq",["AMOUNT"],["mutez","0"]],["throw",["string","You cannot bid 0."]]],["if",["ge",["asset.bid"],["AMOUNT"]],[["transfer",["SENDER"],["AMOUNT"]],["throw",["string","Sending funds back - bid too low."]]],[["if",["neq",["asset.bid"],["mutez","0"]],["transfer",["oldWinner"],["AMOUNT"]]],["set","asset.winner",["SENDER"]],["set","asset.bid",["AMOUNT"]],["ext","push","storage.auction",["input.id"],["asset"]]]]]}]} \ No newline at end of file diff --git a/auction/auction.fi.ml b/auction/auction.fi.ml new file mode 100644 index 0000000..b1d5433 --- /dev/null +++ b/auction/auction.fi.ml @@ -0,0 +1 @@ +parameter bytes;storage (pair (map int (pair string (pair string (pair mutez (pair address (pair address (pair (pair bool (pair string string)) (pair timestamp timestamp)))))))) int);code{DUP;CDR;NIL operation;PAIR;SWAP;CAR;DUP;PUSH nat 4;PUSH nat 0;SLICE;IF_NONE{PUSH nat 100;FAILWITH}{};DUP;PUSH bytes 0xb03afe75;COMPARE;EQ;IF{DROP;DUP;SIZE;PUSH nat 4;SWAP;SUB;DUP;GT;IF{}{PUSH nat 102;FAILWITH};ABS;PUSH nat 4;SLICE;IF_NONE{PUSH nat 101;FAILWITH}{};UNPACK (pair string (pair string (pair mutez (pair address (pair address (pair (pair bool (pair string string)) (pair timestamp timestamp)))))));IF_NONE{PUSH nat 103;FAILWITH}{};PAIR;NONE nat;PAIR;DUP;CDADDDDDDAR;DIP{NOW;};COMPARE;LT;DIP{DUP;CDADDDDDDAR;DIP{DUP;CDADDDDDDDR;};COMPARE;GT;};OR;IF{PUSH string "Invalid end or resolution dates.";FAILWITH;}{};DUP;CDDDAR;SIZE;SWAP;SET_CAR;DUP;CAR;DIP{PUSH nat 0;};COMPARE;EQ;IF{PUSH int 0;DIP{DUP;CDAR;SOME;};DIIP{DUP;CDDDAR;};UPDATE;SWAP;SET_CDDDAR;PUSH int 1;SWAP;SET_CDDDDR;}{DUP;CDDDDR;DIP{DUP;CDAR;SOME;};DIIP{DUP;CDDDAR;};UPDATE;SWAP;SET_CDDDAR;DUP;CDDDDR;DIP{PUSH int 1;};ADD;SWAP;SET_CDDDDR;};CDDR;}{DUP;PUSH bytes 0xaf8ead49;COMPARE;EQ;IF{DROP;DUP;SIZE;PUSH nat 4;SWAP;SUB;DUP;GT;IF{}{PUSH nat 102;FAILWITH};ABS;PUSH nat 4;SLICE;IF_NONE{PUSH nat 101;FAILWITH}{};UNPACK (pair int string);IF_NONE{PUSH nat 103;FAILWITH}{};PAIR;NONE (pair string (pair string (pair mutez (pair address (pair address (pair (pair bool (pair string string)) (pair timestamp timestamp)))))));PAIR;DUP;CDAAR;DIP{DUP;CDDDAR;};MEM;DIP{PUSH bool False;};COMPARE;EQ;IF{PUSH string "No item exists by that Id for this auction.";FAILWITH;}{};DUP;CDAAR;DIP{DUP;CDDDAR;};GET;IF_NONE{PUSH string "Key not found in map";FAILWITH}{};SWAP;SET_CAR;SENDER;DIP{DUP;CADDDDAR;};COMPARE;NEQ;IF{PUSH string "You are not authorized to add to claim.";FAILWITH;}{};DUP;CADDDDDDAR;DIP{NOW;};COMPARE;GT;IF{PUSH string "You can't make a claim on an auction still live.";FAILWITH;}{};DUP;CADDDDDDDR;DIP{NOW;};COMPARE;GT;IF{PUSH bool False;SWAP;SET_CADDDDDAAR;DUP;CDADR;SWAP;SET_CADDDDDADAR;DUP;CDAAR;DIP{DUP;CAR;SOME;};DIIP{DUP;CDDDAR;};UPDATE;SWAP;SET_CDDDAR;}{};CDDR;}{DUP;PUSH bytes 0x16e502df;COMPARE;EQ;IF{DROP;DUP;SIZE;PUSH nat 4;SWAP;SUB;DUP;GT;IF{}{PUSH nat 102;FAILWITH};ABS;PUSH nat 4;SLICE;IF_NONE{PUSH nat 101;FAILWITH}{};UNPACK (pair int string);IF_NONE{PUSH nat 103;FAILWITH}{};PAIR;NONE (pair string (pair string (pair mutez (pair address (pair address (pair (pair bool (pair string string)) (pair timestamp timestamp)))))));PAIR;DUP;CDAAR;DIP{DUP;CDDDAR;};MEM;DIP{PUSH bool False;};COMPARE;EQ;IF{PUSH string "No item exists by that Id for this auction.";FAILWITH;}{};DUP;CDAAR;DIP{DUP;CDDDAR;};GET;IF_NONE{PUSH string "Key not found in map";FAILWITH}{};SWAP;SET_CAR;SENDER;DIP{DUP;CADDDAR;};COMPARE;NEQ;IF{PUSH string "You are not authorized to add to claim.";FAILWITH;}{};DUP;CADDDDDDAR;DIP{NOW;};COMPARE;GT;IF{PUSH string "You can't make a claim on an auction still live.";FAILWITH;}{};DUP;CDADR;SWAP;SET_CADDDDDADDR;DUP;CDAAR;DIP{DUP;CAR;SOME;};DIIP{DUP;CDDDAR;};UPDATE;SWAP;SET_CDDDAR;CDDR;}{DUP;PUSH bytes 0x4121a61e;COMPARE;EQ;IF{DROP;DUP;SIZE;PUSH nat 4;SWAP;SUB;DUP;GT;IF{}{PUSH nat 102;FAILWITH};ABS;PUSH nat 4;SLICE;IF_NONE{PUSH nat 101;FAILWITH}{};UNPACK int;IF_NONE{PUSH nat 103;FAILWITH}{};PAIR;NONE (pair string (pair string (pair mutez (pair address (pair address (pair (pair bool (pair string string)) (pair timestamp timestamp)))))));PAIR;DUP;CDAR;DIP{DUP;CDDDAR;};MEM;DIP{PUSH bool False;};COMPARE;EQ;IF{PUSH string "No item exists by that Id for this auction.";FAILWITH;}{};DUP;CDAR;DIP{DUP;CDDDAR;};GET;IF_NONE{PUSH string "Key not found in map";FAILWITH}{};SWAP;SET_CAR;SENDER;DIP{DUP;CADDDDAR;};COMPARE;NEQ;IF{PUSH string "You are not authorized to resolve this claim.";FAILWITH;}{};DUP;CADDDDDDAR;DIP{NOW;};COMPARE;GT;IF{PUSH string "You can't resolve a claim on an auction still live.";FAILWITH;}{};DUP;CADDDDDAAR;DIP{PUSH bool True;};COMPARE;EQ;IF{PUSH string "There is nothing to resolve.";FAILWITH;}{};PUSH bool True;SWAP;SET_CADDDDDAAR;DUP;CDAR;DIP{DUP;CAR;SOME;};DIIP{DUP;CDDDAR;};UPDATE;SWAP;SET_CDDDAR;CDDR;}{DUP;PUSH bytes 0x200e7681;COMPARE;EQ;IF{DROP;DUP;SIZE;PUSH nat 4;SWAP;SUB;DUP;GT;IF{}{PUSH nat 102;FAILWITH};ABS;PUSH nat 4;SLICE;IF_NONE{PUSH nat 101;FAILWITH}{};UNPACK int;IF_NONE{PUSH nat 103;FAILWITH}{};PAIR;NONE (pair string (pair string (pair mutez (pair address (pair address (pair (pair bool (pair string string)) (pair timestamp timestamp)))))));PAIR;DUP;CDAR;DIP{DUP;CDDDAR;};MEM;DIP{PUSH bool False;};COMPARE;EQ;IF{PUSH string "No item exists by that Id for this auction.";FAILWITH;}{};DUP;CDAR;DIP{DUP;CDDDAR;};GET;IF_NONE{PUSH string "Key not found in map";FAILWITH}{};SWAP;SET_CAR;SENDER;DIP{DUP;CADDDAR;};COMPARE;NEQ;IF{PUSH string "You are not authorized to withdraw from this auction.";FAILWITH;}{};DUP;CADDDDDAAR;DIP{PUSH bool False;};COMPARE;EQ;IF{PUSH string "You must resolve the claim with the buyer to receive funds.";FAILWITH;}{};DUP;CADDDDDDAR;DIP{NOW;};COMPARE;GT;DIP{NOW;DIP{DUP;CADDDDDDDR;};COMPARE;LT;};OR;IF{PUSH string "The auction has not ended or the resolution period has not past.";FAILWITH;}{};DUP;CADDAR;DIP{PUSH mutez 0;};COMPARE;NEQ;IF{UNIT;DIP{DUP;CADDAR;};DIIP{SENDER;CONTRACT unit;IF_NONE{PUSH string "Invalid contract";FAILWITH}{};};TRANSFER_TOKENS;DIP{DUP;CDDAR;};CONS;SWAP;SET_CDDAR;}{PUSH string "You cannot withdraw 0.";FAILWITH;};CDDR;}{DUP;PUSH bytes 0x75ed3e40;COMPARE;EQ;IF{DROP;DUP;SIZE;PUSH nat 4;SWAP;SUB;DUP;GT;IF{}{PUSH nat 102;FAILWITH};ABS;PUSH nat 4;SLICE;IF_NONE{PUSH nat 101;FAILWITH}{};UNPACK int;IF_NONE{PUSH nat 103;FAILWITH}{};PAIR;NONE (pair string (pair string (pair mutez (pair address (pair address (pair (pair bool (pair string string)) (pair timestamp timestamp)))))));PAIR;DUP;CDAR;DIP{DUP;CDDDAR;};MEM;DIP{PUSH bool False;};COMPARE;EQ;IF{PUSH string "No item exists by that Id for this auction.";FAILWITH;}{};DUP;CDAR;DIP{DUP;CDDDAR;};GET;IF_NONE{PUSH string "Key not found in map";FAILWITH}{};SWAP;SET_CAR;SENDER;DIP{DUP;CADDDAR;};COMPARE;NEQ;IF{PUSH string "You are not authorized to send a refund for this auction.";FAILWITH;}{};DUP;CADDDDDDAR;DIP{NOW;};COMPARE;GT;IF{PUSH string "The auction has not ended.";FAILWITH;}{};DUP;CADDAR;DIP{PUSH mutez 0;};COMPARE;NEQ;IF{UNIT;DIP{DUP;CADDAR;};DIIP{DUP;CADDDDAR;CONTRACT unit;IF_NONE{PUSH string "Invalid contract";FAILWITH}{};};TRANSFER_TOKENS;DIP{DUP;CDDAR;};CONS;SWAP;SET_CDDAR;}{PUSH string "You cannot refund 0.";FAILWITH;};PUSH bool True;SWAP;SET_CADDDDDAAR;DUP;CDAR;DIP{DUP;CAR;SOME;};DIIP{DUP;CDDDAR;};UPDATE;SWAP;SET_CDDDAR;CDDR;}{DUP;PUSH bytes 0x65e3abe5;COMPARE;EQ;IF{DROP;DUP;SIZE;PUSH nat 4;SWAP;SUB;DUP;GT;IF{}{PUSH nat 102;FAILWITH};ABS;PUSH nat 4;SLICE;IF_NONE{PUSH nat 101;FAILWITH}{};UNPACK int;IF_NONE{PUSH nat 103;FAILWITH}{};PAIR;NONE address;NONE (pair string (pair string (pair mutez (pair address (pair address (pair (pair bool (pair string string)) (pair timestamp timestamp)))))));PAIR;PAIR;DUP;CDAR;DIP{DUP;CDDDAR;};MEM;DIP{PUSH bool False;};COMPARE;EQ;IF{PUSH string "No item exists by that Id for this auction.";FAILWITH;}{};DUP;CDAR;DIP{DUP;CDDDAR;};GET;IF_NONE{PUSH string "Key not found in map";FAILWITH}{};SWAP;SET_CAAR;DUP;CAADDDDAR;SWAP;SET_CADR;SENDER;DIP{DUP;CAADDDAR;};COMPARE;EQ;IF{PUSH string "You cant bid against your own item.";FAILWITH;}{};DUP;CAADDDDDDAR;DIP{NOW;};COMPARE;LT;IF{PUSH string "The auction for the item requested has already ended.";FAILWITH;}{};AMOUNT;DIP{PUSH mutez 0;};COMPARE;EQ;IF{PUSH string "You cannot bid 0.";FAILWITH;}{};DUP;CAADDAR;DIP{AMOUNT;};COMPARE;GE;IF{UNIT;DIP{AMOUNT;};DIIP{SENDER;CONTRACT unit;IF_NONE{PUSH string "Invalid contract";FAILWITH}{};};TRANSFER_TOKENS;DIP{DUP;CDDAR;};CONS;SWAP;SET_CDDAR;PUSH string "Sending funds back - bid too low.";FAILWITH;}{DUP;CAADDAR;DIP{PUSH mutez 0;};COMPARE;NEQ;IF{UNIT;DIP{AMOUNT;};DIIP{DUP;CADR;CONTRACT unit;IF_NONE{PUSH string "Invalid contract";FAILWITH}{};};TRANSFER_TOKENS;DIP{DUP;CDDAR;};CONS;SWAP;SET_CDDAR;}{};SENDER;SWAP;SET_CAADDDDAR;AMOUNT;SWAP;SET_CAADDAR;DUP;CDAR;DIP{DUP;CAAR;SOME;};DIIP{DUP;CDDDAR;};UPDATE;SWAP;SET_CDDDAR;};CDDR;}{DROP;PUSH nat 400;FAILWITH;}}}}}}}}; \ No newline at end of file diff --git a/delegation_voting/delegationVoting.js b/delegation_voting/delegationVoting.js new file mode 100644 index 0000000..288b144 --- /dev/null +++ b/delegation_voting/delegationVoting.js @@ -0,0 +1,265 @@ +var fi = require("fi-compiler"); +var eztz = require("/home/brice/eztz/").eztz +var yaml = require('js-yaml'); +var fs = require('fs'); +var stdio = require('stdio'); +var argv = require('minimist')(process.argv.slice(2)); + +var conf; +try { + const config = yaml.safeLoad(fs.readFileSync('conf.yaml', 'utf8')); + conf = JSON.stringify(config, null, 4); +} catch (e) { + console.log(e); +} +conf = JSON.parse(conf); + +var ficode = ` +struct Proposal ( + string description, + int votes +); + +storage map[address=>bool] voter; +storage map[int=>Proposal] ballot; +storage address manager; +storage string info; + + +entry addAuthorizedVoter(address addr){ + if (SENDER == storage.manager) { + storage.voter.push(input.addr, bool false); + } else { + throw(string "Not authorized to add a voter."); + } +} + +entry addProposal(int id, Proposal proposal){ + if (SENDER == storage.manager) { + storage.ballot.push(input.id, input.proposal); + } else { + throw(string "Not authorized to add a ballet."); + } +} + +entry addInfo(string desc){ + if (SENDER == storage.manager) { + storage.info = input.desc; + } else { + throw(string "Not authorized to set description."); + } +} + +entry placeVote(int vote){ + +if (in(storage.voter, SENDER) == bool false) { + throw(string "You are not authorized to vote."); + } + + if (in(storage.ballot, input.vote) == bool false) { + throw(string "No proposal exists for your vote."); + } + + + let bool myVote = storage.voter.get(SENDER); + let Proposal proposal = storage.ballot.get(input.vote); + + if (myVote == bool false) { + myVote = bool true; + + proposal.votes.add(int 1); + storage.ballot.push(input.vote, proposal); + storage.voter.push(SENDER, myVote); + } else { + throw(string "You have already voted. Voters may only vote once."); + } +} +`; + +var compiled = fi.compile(ficode); +fi.abi.load(compiled.abi); + + +var wallet = eztz.crypto.extractKeys(conf.wallet.sk) + +// if (argv.password) { +// (conf.wallet.sk, argv.password).then(function (res) { +// wallet = res; +// }).catch(function (e) { +// console.log(e) +// }); +// } else { +// stdio.question('What is the password for your wallet?', function (err, passwd) { +// eztz.crypto.extractEncryptedKeys(conf.wallet.sk, passwd).then(function (res) { +// wallet = res; +// }).catch(function (e) { +// console.log(e) +// }); +// }); +// } + +var gas_limit; +var fee; +if (argv.gaslimit) { + gas_limit = argv.gaslimit; +} else { + console.log("Please specify a gas_limit, denoted --gaslimit="); + process.exit() +} + +if (argv.fee) { + fee = argv.fee; +} else { + console.log("Please specify a network fee, denoted --fee="); + process.exit(); +} + +if (argv.alphanet) { + eztz.node.setProvider("https://alphanet-node.tzscan.io/") +} else if (argv.network) { + eztz.node.setProvider(argv.network) +} else { + eztz.node.setProvider("https://mainnet-node.tzscan.io/") +} + +if (argv.addvoters) { + console.log("adding voters") + addAuthorizedVoters(argv.addvoters, wallet) +} + +if (argv.addvoter) { + console.log("adding voter") + input = { + addr: argv.addvoter + } + console.log(input) + addAuthorizedVoter(input, wallet) +} +if (argv.addinfo) { + console.log("adding info") + input = { + desc: argv.addinfo + } + addInfo(input, wallet) +} +if (argv.addproposal) { + console.log("adding proposal") + if (argv.proposalid) { + input = { + id: argv.proposalid, + proposal: argv.addproposal + } + addProposal(argv.addproposal, wallet) + } else { + console.log("Must also specify a proposalid (int).") + } +} + +if (argv.placevote) { + input = { + vote: argv.placevote + } + placeVote(input, wallet) +} + + + +function placeVote(input, keys) { + vbytes = fi.abi.entry("placeVote", input) + vbytes = vbytes.substr(2); + var operation = { + "kind": "transaction", + "amount": "0", + "destination": conf.contract, + "fee": fee, + "gas_limit": gas_limit, + "parameters": { + "bytes": vbytes + } + }; + + eztz.rpc.sendOperation(keys.pkh, operation, keys).then(function (res) { + console.log(res); + }).catch(function (e) { + console.log(e) + }); +} + +function addInfo(input, keys) { + ibytes = fi.abi.entry("addInfo", input) + ibytes = ibytes.substr(2); + var operation = { + "kind": "transaction", + "amount": "0", + "destination": conf.contract, + "fee": fee, + "gas_limit": gas_limit, + "parameters": { + "bytes": ibytes + } + }; + + eztz.rpc.sendOperation(keys.pkh, operation, keys).then(function (res) { + console.log(res); + }).catch(function (e) { + console.log(e) + }); +} + +function addProposal(input, keys) { + pbytes = fi.abi.entry("addProposal", input) + pbytes = pbytes.substr(2); + var operation = { + "kind": "transaction", + "amount": "0", + "destination": conf.contract, + "fee": fee, + "gas_limit": gas_limit, + "parameters": { + "bytes": pbytes + } + }; + + eztz.rpc.sendOperation(keys.pkh, operation, keys).then(function (res) { + console.log(res); + }).catch(function (e) { + console.log(e) + }); +} + +function addAuthorizedVoter(input, keys) { + vbytes = fi.abi.entry("addAuthorizedVoter", input) + console.log(vbytes) + vbytes = vbytes.substr(2); + var operation = { + "kind": "transaction", + "amount": "0", // This is in mutez, i.e. 1000000 = 1.00 tez + "destination": conf.contract, + "fee": fee, + "gas_limit": gas_limit, + "parameters": { + "bytes": vbytes + } + }; + + eztz.rpc.sendOperation(keys.pkh, operation, keys).then(function (res) { + console.log(res); + }).catch(function (e) { + console.log(e) + }); +} + +function addAuthorizedVoters(addr, keys) { + var query = "/chains/main/blocks/head/context/delegates/" + addr + "/delegated_contracts"; + eztz.node.query(query).then(function (res) { //tz1YCABRTa6H8PLKx2EtDWeCGPaKxUhNgv47 + res.forEach(function (contract) { + var input = { + addr: contract + }; + // console.log(input) + addAuthorizedVoter(input, keys) + }); + }).catch(function (e) { + console.log(e) + }); +} \ No newline at end of file