Skip to content

LLL Examples for PoC 4

Gav Wood edited this page Apr 1, 2014 · 19 revisions

Bank

A very simple bank. You can send ether in a transaction to load your account with funds. Withdraw your ether by specifying a 32-byte data argument of the amount to withdraw. Withdraw to a different account by specifying a second data argument with the account.

    (if (>= (sload (caller)) (calldataload 0))
      ;; Withdrawal:
      (seq
        ;; Subtract the value from the balance of the account
        (sstore (caller) (- (sload (caller)) (calldataload 0)))
        ;; Transfer the funds either to...
        (if (<= (calldatasize) 32)
          (call (caller) (calldataload 0) 0 0 0 0 0)  ; ...the sender...
          (call (calldataload 32) (calldataload 0) 0 0 0 0 0)  ; ...or the supplied account.
        )
      )
      ;; Deposit; just increase the account balance by that amount.
      (sstore (caller) (+ (sload (caller)) (callvalue)))
    )

Splitter

Simple cash splitter; splits the value sent amongst each of the addresses given as data items.

(seq
  ;; Set @0 to the number of receivers.
  (mstore 0 (/ (calldatasize) 20))

  ;; Set @32 as the value to be paid out to each address.
  (mstore 32 (/ (callvalue) (mload 0)))

  ;; Cycle through each address with @64 being the index.
  (mstore 64 0)                    ; Initialise @64.
  (for (< (mload 64) (mload 0))   ; Exit once @64 hits the argument count.
    (seq
      ;; Send to 'i'th argument (assuming it's an address).
      (call (calldataload (* (mload 64) 20)) (mload 32) 0 0 0 0 0)

      (mstore 64 (+ (mload 64) 1))      ; Increment @64.
    )
  )
)

Clone this wiki locally