From 1ecb27491f7ccf6df72165846a779ca81729d765 Mon Sep 17 00:00:00 2001 From: AADI Date: Thu, 25 Sep 2025 16:29:13 +0530 Subject: [PATCH] new commit --- contracts/escrow_system.tolk | 44 ++++++------- contracts/simple_dao.tolk | 118 ++++++++++++++++++++++++++--------- 2 files changed, 110 insertions(+), 52 deletions(-) diff --git a/contracts/escrow_system.tolk b/contracts/escrow_system.tolk index e4100e6..6de65a0 100644 --- a/contracts/escrow_system.tolk +++ b/contracts/escrow_system.tolk @@ -1,19 +1,17 @@ -{/* - Below you will find an EscrowSystem contract that allows user to initialize an escrow, request funds, release funds, and cancel escrow. - The EscrowSystem struct is the main storage of the contract. - It contains: - - owner: address - - recipient: address - - amount: uint64 - - isReleased: bool - - isRequested: bool +// Below you will find an EscrowSystem contract that allows user to initialize an escrow, request funds, release funds, and cancel escrow. +// The EscrowSystem struct is the main storage of the contract. +// It contains: +// - owner: address +// - recipient: address +// - amount: uint64 +// - isReleased: bool +// - isRequested: bool - Your task it to fix the errors in the contract and make it work as expected. +// Your task it to fix the errors in the contract and make it work as expected. - Helpful Blueprint sdk commands: - - npx blueprint build - To build the contract - - npx blueprint test tests/EscrowSystem.spec.ts - To test the contract -*/} +// Helpful Blueprint sdk commands: +// - npx blueprint build - To build the contract +// - npx blueprint test tests/EscrowSystem.spec.ts - To test the contract tolk 1.1 @@ -29,7 +27,7 @@ fun EscrowSystem.load() { return EscrowSystem.fromCell(contract.getData()); } fun EscrowSystem.store(self) { - contract.setData(self); + contract.setData(self.toCell()); } struct (0xE3D2C1B4) InitializeEscrow { @@ -47,7 +45,7 @@ struct (0xB1C2D3E4) CancelEscrow { queryId: uint32 } -type MessageBody = RequestFunds & ReleaseFunds & CancelEscrow & InitializeEscrow +type MessageBody = RequestFunds | ReleaseFunds | CancelEscrow | InitializeEscrow fun onInternalMessage(in: InMessage) { val msg = lazy MessageBody.fromSlice(in.body); @@ -55,10 +53,12 @@ fun onInternalMessage(in: InMessage) { match (msg) { InitializeEscrow => { var escrow = lazy EscrowSystem.load(); - escrow.recipient = recipient; - escrow.amount = amount; + escrow.owner = in.senderAddress; + escrow.recipient = msg.recipient; + escrow.amount = msg.amount; escrow.isReleased = false; escrow.isRequested = false; + escrow.store(); } RequestFunds => { var escrow = lazy EscrowSystem.load(); @@ -68,8 +68,9 @@ fun onInternalMessage(in: InMessage) { escrow.store(); } ReleaseFunds => { - val escrow = lazy EscrowSystem.load(); - assert (!escrow.isRequested) throw 108; + var escrow = lazy EscrowSystem.load(); + assert (in.senderAddress == escrow.owner) throw 103; + assert (escrow.isRequested) throw 108; assert (!escrow.isReleased) throw 109; escrow.isReleased = true; escrow.store(); @@ -81,7 +82,8 @@ fun onInternalMessage(in: InMessage) { res.send(SEND_MODE_CARRY_ALL_BALANCE); } CancelEscrow=>{ - val escrow = lazy EscrowSystem.load(); + var escrow = lazy EscrowSystem.load(); + assert (in.senderAddress == escrow.owner) throw 104; assert (!escrow.isReleased) throw 107; escrow.isReleased = true; escrow.store(); diff --git a/contracts/simple_dao.tolk b/contracts/simple_dao.tolk index 59c2367..bbda49f 100644 --- a/contracts/simple_dao.tolk +++ b/contracts/simple_dao.tolk @@ -1,36 +1,92 @@ -{/* - Create a Simple DAO contract that allows user to vote yes or no - The SimpleDao struct is the main storage of the contract. - It contains: - - queryId: uint32 - - yesVotes: uint32 - - noVotes: uint32 - - totalVotes: uint32 - - The contract has 2 functionalities : - - Record a vote - - Reset the votes - The contract has a yesVotes that is used to identify the yes votes. - The contract has a noVotes that is used to identify the no votes. - The contract has a totalVotes that is used to identify the total votes. - - There will be 2 structs : - - RecordVote : This struct is used to record a vote. - - ResetVotes : This struct is used to reset the votes. - - The struct definitions are : - - RecordVote(queryId: uint32, vote: bool) : 0xF4A2B1C9 - - ResetVotes(queryId: uint32) : 0xD4E7B328 - - There will be 1 getter function : - - getVotes : This function is used to get the votes. (yesVotes, noVotes, totalVotes) - - Helpful Blueprint sdk commands: - - npx blueprint build - To build the contract - - npx blueprint test tests/SimpleDao.spec.ts - To test the contract -*/} +// Create a Simple DAO contract that allows user to vote yes or no +// The SimpleDao struct is the main storage of the contract. +// It contains: +// - queryId: uint32 +// - yesVotes: uint32 +// - noVotes: uint32 +// - totalVotes: uint32 + +// The contract has 2 functionalities : +// - Record a vote +// - Reset the votes +// The contract has a yesVotes that is used to identify the yes votes. +// The contract has a noVotes that is used to identify the no votes. +// The contract has a totalVotes that is used to identify the total votes. + +// There will be 2 structs : +// - RecordVote : This struct is used to record a vote. +// - ResetVotes : This struct is used to reset the votes. + +// The struct definitions are : +// - RecordVote(queryId: uint32, vote: bool) : 0xF4A2B1C9 +// - ResetVotes(queryId: uint32) : 0xD4E7B328 + +// There will be 1 getter function : +// - getVotes : This function is used to get the votes. (yesVotes, noVotes, totalVotes) + +// Helpful Blueprint sdk commands: +// - npx blueprint build - To build the contract +// - npx blueprint test tests/SimpleDao.spec.ts - To test the contract tolk 1.1 +struct SimpleDao { + queryId: uint32; + yesVotes: uint32; + noVotes: uint32; + totalVotes: uint32; +} + +fun SimpleDao.load() { + return SimpleDao.fromCell(contract.getData()); +} + +fun SimpleDao.store(self) { + contract.setData(self.toCell()); +} + +struct (0xF4A2B1C9) RecordVote { + queryId: uint32; + vote: bool; +} + +struct (0xD4E7B328) ResetVotes { + queryId: uint32; +} + +type MessageBody = RecordVote | ResetVotes; + fun onInternalMessage(in: InMessage) { + val msg = lazy MessageBody.fromSlice(in.body); + + match (msg) { + RecordVote => { + var dao = lazy SimpleDao.load(); + dao.queryId = msg.queryId; + + if (msg.vote) { + dao.yesVotes += 1; + } else { + dao.noVotes += 1; + } + dao.totalVotes += 1; + dao.store(); + } + ResetVotes => { + var dao = lazy SimpleDao.load(); + dao.queryId = msg.queryId; + dao.yesVotes = 0; + dao.noVotes = 0; + dao.totalVotes = 0; + dao.store(); + } + else => { + assert (in.body.isEmpty()) throw 100; + } + } +} + +get fun getVotes(): (uint32, uint32, uint32) { + var dao = lazy SimpleDao.load(); + return (dao.yesVotes, dao.noVotes, dao.totalVotes); }