Hello guys,
I’m quite new to æternity blockchain and functional programming. I have some experience with imperative languages like C, JavaScript etc. Still, I decided to try Sophia and I’m stuck.
My goal is to write the contact that similar to this one written in Solidity:
pragma solidity ^0.4.25;
contract CryptoZombie {
event NewZombie(uint hamsterId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
function _createZombie(string _name, uint _dna) private {
uint id = hamsters.push(Hamster(_name, _dna)) - 1;
emit NewHamster(id, _name, _dna);
}
function _generateRandomDna(string _str) private returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % dnaModulus;
}
function createRandomZombie(string _name) public {
uint randDna = _generateRandomDna(_name);
_createHamster(_name, randDna);
}
}
I have only a very broad understanding of how to do it. Here’s what I’ve done so far:
contract CryptoZombie =
let dnaDigits = 16
let dnaModulus = 10 % dnaDigits
type Zombie {
name: string,
dna: uint
}
private function create_zombie(new_name:string, new_dna:int) =
private function generate_random_dna(str:string) =
public function create_random_zombie(name:string) =
So, my questions are:
- Do Sophia supports modulus operator? Your contract editor shows me an error: “While calling compileContract (body), POST to //sdk-testnet.aepps.com/v2/debug/contracts/code/compile failed with 403: {case_clause,{error,{scan_error,{3,25},”% 16"}}}"
- For creating a struct in Sophia, should I use type or ‘record state’? I don’t have a good understanding which one to choose.
- How can I create the array of structs like in Solidity? I know that Sophia doesn’t support arrays, so what should I choose: lists or tuples, or maybe even maps?
- How to push a new zombie to the list/array/map? I mean, how to do it in the most correct way?
- How can I implement pseudo-random generation, e.g.
uint rand = uint(keccak256(abi.encodePacked(_str)));
in Sophia? Does it support any hash-function? - I know that events aren’t implemented yet, but still, how to write them correctly and how to fire them in functions?