Skip to content
Open
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
44 changes: 23 additions & 21 deletions contracts/escrow_system.tolk
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 {
Expand All @@ -47,18 +45,20 @@ 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);

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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
118 changes: 87 additions & 31 deletions contracts/simple_dao.tolk
Original file line number Diff line number Diff line change
@@ -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);
}