You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1;; Create a string variable that can store maximum 100 characters2 (defvargreet (public (string100)))
34 (defn__init__ [] :external5 (set self/greet "Hello World"))
1 (defstructPerson2name (string100)
3age:uint256)
45 (defvars6nums (public (array:uint25610)) ;; fixed size list, must be bounded7myMap (public (hash-map:address:uint256))
8person (public Person))
910 (defn__init__ [] :external11 (doto self/nums
12 (set-at0123) ;; this updates self.nums[0]13 (set-at9456)) ;; this updates self.nums[9]1415;; copies self.nums to array in memory16 (defvararr (array:uint25610) self/nums)
17 (set-at arr 0123) ;; does not modify self/nums1819;; this updates self/myMap20 (doto self/myMap
21 (set-at msg/sender 1) ;; self.myMap[msg.sender] = 122 (set-at msg/sender 11)) ;; self.myMap[msg.sender] = 112324;; this updates self/person25 (doto self/person
26 (set-in age 11)
27 (set-in name "Dasy"))
2829;; you could put defvar inside a doto like the arr example30;; above, but I don't think that is very readable31;; doing it this way is clearer, leaving the defvar out of doto32;; Person struct is copied into memory33 (defvarpPersonself/person)
34 (set-in p name "Solidity"))
Dynamic Arrays
1;; dynamic array of type uint256, max 3 elements2 (defvarnums (public (dyn-array:uint2563)))
34 (defn__init__ [] :external5 (doto self/nums
6 (.append11)
7 (.append22)
8 (.append33)
9;; this will revert, appending to array with max 3 elements10;; (.append self/nums 44)11 )
12;; delete all elements13 (set self/nums [])
14;; set values15 (set self/nums [123]))
1617 (defnexamples [(dyn-array:uint2565) xs] (dyn-array:uint2568) [:external:pure]
18 (defvarys (dyn-array:uint2568) [123])
19 (for [x xs]
20 (.append ys x))
21 (return ys))
2223 (defnfilter [(dyn-array:address5) addrs] (dyn-array:address5) [:external:pure]
24 (defvarnonzeros (dyn-array:address5) [])
25 (for [addr addrs]
26 (if (!= addr (empty:address))
27 (do (.append nonzeros addr))))
28 (return nonzeros))
Functions
1 (defnmultiply [:uint256 x y] :uint256 [:external:pure]
2 (* x y))
34 (defndivide [:uint256 x y] :uint256 [:external:pure]
5 (/ x y))
67 (defnmultiOut [] '(:uint256:bool) [:external:pure]
8 '(1 True))
910 (defnaddAndSub [:uint256 x y] '(:uint256:uint256) [:external:pure]
11 '((+ x y) (- x y)))
Internal and External Functions
1;; internal functions can only be called inside this contract2 (defn_add [:uint256 x y] :uint256 [:internal:pure]
3 (+ x y))
45;; external functions can only be called from outside this contract6 (defnextFunc [] :bool [:external:view]
7True)
89;; external functions can only be called from outside this contract10 (defnavg [:uint256 x y] :uint256 [:external:view]
11;; cannot call other external function12;; (.extFunc self)1314;; can call internal functions15 (defvarz:uint256 (self/_add x y))
16 (/ (+ x y)
172))
1819 (defn_sqr [:uint256 x] :uint256 [:internal:pure]
20 (* x x))
2122 (defnsumOfSquares [:uint256 x y] :uint256 [:external:view]
23 (+ (self/_sqr x)
24 (self/_sqr y)))
View and Pure Functions
1 (defvarnum (public:uint256))
23;; Pure functions do not read any state or global variables4 (defnpureFunc [:uint256 x] :uint256 [:external:pure]
5x)
67;; View functions might read state or global state, or call an internal function8 (defnviewFunc [:uint256 x] :bool [:external:view]
9 (> x self/num))
1011 (defnsum [:uint256 x y z] :uint256 [:external:pure]
12 (+ x y z))
1314 (defnaddNum [:uint256 x] :uint256 [:external:view]
15 (+ x self/num))
Constructor
1 (defvarsowner (public:address)
2createdAt (public:uint256)
3expiresAt (public:uint256)
4name (public (string10)))
56 (defn__init__ [(string10) name :uint256 duration] :external7;; set owner to caller8 (set self/owner msg/sender)
9;; set name from input10 (set self/name name)
11 (set self/createdAt block/timestamp)
12 (set self/expiresAt (+ block/timestamp
13 duration)))
1;; receive ether into the contract2 (defn__default__ [] [:external:payable]
3 (pass))
45 (defnsendEther [:address to :uint256 amount] :external6;; calls the default fn in the receiving contract7 (send to amount))
89 (defnsendAll [:address to] :external10 (send to self/balance))
Raw Call
1 (defntestRawCall [:address to :uint256 x y] :uint256:external2 (defvarres (bytes32)
3 (raw_call to
4 (concat (method_id"multiply(uint256,uint256)")
5 (convert x :bytes32)
6 (convert y :bytes32))
7:max_outsize328:gas1000009:value010 ))
11 (defvarz:uint256 (convert res :uint256))
12z)
1314 (defnsendEth [:address to] [:external:payable]
15 (raw_call to b"":value msg/value))