Help me to correct this contract

Hello,

I am ultra beginner in Sophia and Blockchain in general. I would like to create my own smart contract based by a gamble game named satoshi dice. I start writing the contract and get some errors. but first I would like to know if this contract that I create is logic or not. I mean by logic if the syntax errors that I met are few. and Thanks

contract SatoshiDice =
record Bet = {
    user : address,
    block : int,
    cap : int,
    amount : int}
FEE_NUMERATOR = 1;
FEE_DENOMINATOR = 100;
MAXIMUM_CAP = 100000;
MAXIMUM_BET_SIZE : 100000000;

owner : address;
counter = 0;      *// I want to make this a int and be public*
bets : map(int, Bet) *// also those bets must be public*
datatype event =
  BetPlaced(id int, user address, cap int, amount int); 
| Roll(id int, rolled int);            *//Here I mean the two events ,this syntax is true?*


entrypoint SatoshiDice() =
  owner = Call.caller;

payable stateful entrypoint wager(cap : int)=
  require(cap <= MAXIMUM_CAP);
  require(Call.value <= MAXIMUM_BET_SIZE);
  counter++;
  bets[counter] = Bet(Call.caller, block.number + 3, cap, Call.value);
  BetPleaced(counter, Call.caller, cap, Call.value);

stateful entrypoint roll(id : int)=
  Bet storage bet = bets[id];   
  require(Call.caller == bet.user);
  require(block.number >= bet.block);
  
  bytes(32) random = String.sha3(block.blockhsh(bet.block), id); 
  int rolled = Bytes.to_int(random) % MAXIMUM_CAP;
  if(rolled < bet.cap)
  	payout = bet.amount * MAXIMUM_CAP / bet.cap;
  	fee = payout * FEE_NUMERATOR / FEE_DENOMINATOR;
  	payout -= fee;
  	transfer_item(payout);

  Roll(id,rolled);
  delete bets[id];

payable stateful entrypoint()

and here is a picture night mode :

Thank you all.
Best Regards

3 Likes

Hello, did you checkout the documentation of sophia language already? https://github.com/aeternity/protocol/blob/master/contracts/sophia.md

1 Like

Yeah sure. and I try to write as the same syntax.

1 Like

Okay, let me give you some help.

First of all, indentation is very important in sophia smart contracts, inner blocks have to be indented more.

will not work as its on the same level of indentation. https://github.com/aeternity/protocol/blob/master/contracts/sophia.md#layout-blocks

There is no such thing as global constants in sophia. You can either store it in state and initialize the value or make a hardcoded function

e.g. function FEE_NUMERATOR() = 1. Also there is no trialing semicolon ; in sophia.

this should probably be part of a record state = { ... } where you define your state. If you define any state you need a init function anyways. Also comments as *// will not work, needs to be without the star //. https://github.com/aeternity/protocol/blob/master/contracts/sophia.md#mutable-state
https://github.com/aeternity/protocol/blob/master/contracts/sophia.md#comments

I guess here you want to mutate state, so you need to make the entrypoint stateful and use the put keyword to modify state. e.g.

stateful entrypoint set_owner() =
  put(state{ owner = Call.caller})

keep in mind that anyone can call this function, so you would want to initialize your owner in the init function. https://github.com/aeternity/protocol/blob/master/contracts/sophia.md#stateful-functions

Emitting events is done using Chain.event(...). https://github.com/aeternity/protocol/blob/master/contracts/sophia.md#events

Variables are defined with the let keyword, e.g. let random : bytes(32) = ....

Update and Delete from map syntax works differently, checkout the documentation here. https://github.com/aeternity/protocol/blob/master/contracts/sophia.md#maps-and-records

I hoped this helped. If there are any further questions, please let me know

3 Likes

Thank you very much for your support. it really helpfull.
sorry for my late reply, I’ll try and let you know.
Thanks

2 Likes

Hey @charingane please confirm whether it’s all working now.

Great line by line review :+1: :100:

2 Likes