Contract - SP
Address:
ID:

SUMMARY
FULL NAMEsimple poll
STATUSInactive
TRANSACTIONS0
LAST ACTIVATION BLOCK72,669
BALANCES
ASSETAMOUNT
(* NAME_START:test:NAME_END *)

// Nonce: 4X"8
(* This contract implements a simple poll.
   One can send the contract any amount of ZP from an address,
   in order to signal with all of that address's assets.
   The contract returns all ZP spent tp it.

   To signal for a particular item:
     1. Use the item to be signalled as the command to the contract
     2. Spend any amount of ZP to the contract (it will be immediately returned)

   To take a poll:
     1. Fix particular block numbers as the start and end of polling.
     2. Create a unique identifier to identify signalling addresses.
     3. Participants prefix their command with the unique identifier.
     3. After polling ends, tally the asset amounts signalling for each outcome. *)

open Zen.Cost
open Zen.Data
open Zen.Types

module Arr = Zen.Array
module Asset = Zen.Asset
module CR = Zen.ContractResult
module Hash = Zen.Hash
module RT = Zen.ResultT
module Tx = Zen.TxSkeleton

// compressed public key
type cpk = byte ** hash

// compress a public key
val compress: publicKey -> cpk `cost` 305
let compress pk = let open FStar.UInt8 in // 13
    let parity = (Arr.item 32 pk %^ 2uy) +^ 2uy in
    let aux (i:nat{i < 32}): byte `cost` 5 = ret (Arr.item (31-i) pk) in
    let! x = Arr.init_pure 32 aux in // 292
    ret (parity, x)

// hash a compressed public key
val hashCPK: cpk -> hash `cost` 225
let hashCPK (parity, x) = // 7
    Hash.updateByte parity Hash.empty // 6
    >>= Hash.updateHash x // 192
    >>= Hash.finalize // 20

// return all zp to sender
let main (tx:txSkeleton) _ _ _ (sender:sender) _ _ _ : CR.t `cost` 681 = // 20
    match sender with
    | PK pk ->
        // amount of ZP in the tx
        let! amount = Tx.getAvailableTokens Asset.zenAsset tx in // 64
        //sender's pk hash
        let! pkHash = compress pk >>= hashCPK in // 530
        // lock all ZP to sender's pk
        Tx.lockToPubKey Asset.zenAsset amount pkHash tx // 64
        >>= CR.ofTxSkel // 3

    | Contract _ | Anonymous ->
        RT.autoFailw "Sender must sign with public key"

let cf _ _ _ _ _ _ _ : nat `cost` 1 = ret 681