|
35 | 35 |
|
36 | 36 | ;; The block height at which vesting starts. On Mainnet, this is
|
37 | 37 | ;; burn height 907740, which is what is specified in SIP-031.
|
38 |
| -(define-constant DEPLOY_BLOCK_HEIGHT (if is-in-mainnet u907740 burn-block-height)) |
| 38 | +(define-constant DEPLOY_BLOCK_HEIGHT (if is-in-mainnet |
| 39 | + u907740 |
| 40 | + burn-block-height |
| 41 | +)) |
39 | 42 |
|
40 | 43 | ;; The authorized recipient of the funds.
|
41 | 44 | ;; Note than in production environments, `tx-sender` is
|
42 | 45 | ;; replaced with a hard-coded address.
|
43 | 46 | (define-data-var recipient principal tx-sender)
|
44 | 47 |
|
45 |
| -(define-read-only (get-recipient) (var-get recipient)) |
| 48 | +(define-read-only (get-recipient) |
| 49 | + (var-get recipient) |
| 50 | +) |
46 | 51 |
|
47 |
| -(define-read-only (get-deploy-block-height) DEPLOY_BLOCK_HEIGHT) |
| 52 | +(define-read-only (get-deploy-block-height) |
| 53 | + DEPLOY_BLOCK_HEIGHT |
| 54 | +) |
48 | 55 |
|
49 | 56 | ;; Update the recipient of the funds.
|
50 | 57 | ;; May only be called by the current `recipient`.
|
51 | 58 | ;; Returns `true` if the recipient was updated.
|
52 |
| -(define-public (update-recipient (new-recipient principal)) (begin |
| 59 | +(define-public (update-recipient (new-recipient principal)) |
| 60 | + (begin |
53 | 61 | (begin
|
54 |
| - (try! (validate-caller)) |
55 |
| - (print { |
56 |
| - topic: "update-recipient", |
57 |
| - old-recipient: (var-get recipient), |
58 |
| - new-recipient: new-recipient, |
59 |
| - }) |
60 |
| - (var-set recipient new-recipient) |
61 |
| - (ok true)) |
62 |
| -)) |
| 62 | + (try! (validate-caller)) |
| 63 | + (print { |
| 64 | + topic: "update-recipient", |
| 65 | + old-recipient: (var-get recipient), |
| 66 | + new-recipient: new-recipient, |
| 67 | + }) |
| 68 | + (var-set recipient new-recipient) |
| 69 | + (ok true) |
| 70 | + ) |
| 71 | + ) |
| 72 | +) |
63 | 73 |
|
64 | 74 | ;; Transfer all currently withdrawable STX (vested + extra) to `recipient`.
|
65 | 75 | ;; Errors with `ERR_NOTHING_TO_CLAIM` if there is nothing to withdraw.
|
66 | 76 | (define-public (claim)
|
67 |
| - (let |
68 |
| - ( |
69 |
| - (claimable (calc-claimable-amount burn-block-height)) |
70 |
| - ) |
71 |
| - (try! (validate-caller)) |
72 |
| - (asserts! (> claimable u0) (err ERR_NOTHING_TO_CLAIM)) |
73 |
| - |
74 |
| - (try! (as-contract (stx-transfer? claimable tx-sender (var-get recipient)))) |
75 |
| - (print { |
76 |
| - topic: "claim", |
77 |
| - claimable: claimable, |
78 |
| - recipient: (var-get recipient), |
79 |
| - }) |
80 |
| - (ok claimable) |
81 |
| - ) |
| 77 | + (let ((claimable (calc-claimable-amount burn-block-height))) |
| 78 | + (try! (validate-caller)) |
| 79 | + (asserts! (> claimable u0) (err ERR_NOTHING_TO_CLAIM)) |
| 80 | + (try! (as-contract (stx-transfer? claimable tx-sender (var-get recipient)))) |
| 81 | + (print { |
| 82 | + topic: "claim", |
| 83 | + claimable: claimable, |
| 84 | + recipient: (var-get recipient), |
| 85 | + }) |
| 86 | + (ok claimable) |
| 87 | + ) |
82 | 88 | )
|
83 | 89 |
|
84 | 90 | ;; Authorization check. Verify that the caller is the current `recipient`.
|
85 | 91 | ;; This also prevents `recipient` calling into this contract
|
86 | 92 | ;; via an indirect contract-call.
|
87 | 93 | (define-private (validate-caller)
|
88 |
| - (if (is-eq (var-get recipient) contract-caller tx-sender) |
89 |
| - (ok true) |
90 |
| - (err ERR_NOT_ALLOWED)) |
| 94 | + (if (is-eq (var-get recipient) contract-caller tx-sender) |
| 95 | + (ok true) |
| 96 | + (err ERR_NOT_ALLOWED) |
| 97 | + ) |
91 | 98 | )
|
92 | 99 |
|
93 | 100 | ;; Returns the *total* vested amount at `burn-height`, i.e.
|
94 | 101 | ;; immediate bucket + linear vesting so far (DOES NOT subtract any claims).
|
95 | 102 | (define-private (calc-total-vested (burn-height uint))
|
96 |
| - (let |
97 |
| - ( |
98 |
| - (diff (- burn-height DEPLOY_BLOCK_HEIGHT)) |
99 |
| - ;; Note: this rounds down |
100 |
| - (iterations (/ diff INITIAL_MINT_VESTING_ITERATION_BLOCKS)) |
101 |
| - (vested-multiple (/ (* STX_PER_ITERATION diff) INITIAL_MINT_VESTING_ITERATION_BLOCKS)) |
102 |
| - |
103 |
| - ;; If we have completed (or exceeded) the scheduled number of iterations, |
104 |
| - ;; consider the *entire* vesting bucket unlocked. This avoids leaving a |
105 |
| - ;; tiny remainder caused by integer-division truncation. |
106 |
| - (vested-amount (if (>= iterations INITIAL_MINT_VESTING_ITERATIONS) |
107 |
| - INITIAL_MINT_VESTING_AMOUNT |
108 |
| - vested-multiple)) |
109 |
| - (total-amount (+ INITIAL_MINT_IMMEDIATE_AMOUNT vested-amount)) |
110 |
| - ) |
111 |
| - total-amount |
| 103 | + (let ( |
| 104 | + (diff (- burn-height DEPLOY_BLOCK_HEIGHT)) |
| 105 | + ;; Note: this rounds down |
| 106 | + (iterations (/ diff INITIAL_MINT_VESTING_ITERATION_BLOCKS)) |
| 107 | + (vested-multiple (/ (* STX_PER_ITERATION diff) INITIAL_MINT_VESTING_ITERATION_BLOCKS)) |
| 108 | + ;; If we have completed (or exceeded) the scheduled number of iterations, |
| 109 | + ;; consider the *entire* vesting bucket unlocked. This avoids leaving a |
| 110 | + ;; tiny remainder caused by integer-division truncation. |
| 111 | + (vested-amount (if (>= iterations INITIAL_MINT_VESTING_ITERATIONS) |
| 112 | + INITIAL_MINT_VESTING_AMOUNT |
| 113 | + vested-multiple |
| 114 | + )) |
| 115 | + (total-amount (+ INITIAL_MINT_IMMEDIATE_AMOUNT vested-amount)) |
112 | 116 | )
|
| 117 | + total-amount |
| 118 | + ) |
113 | 119 | )
|
114 | 120 |
|
115 | 121 | ;; Returns the amount of STX that is claimable from the vested balance at `burn-height`
|
116 | 122 | (define-read-only (calc-claimable-amount (burn-height uint))
|
117 |
| - (if (< burn-height DEPLOY_BLOCK_HEIGHT) |
118 |
| - u0 |
119 |
| - (let |
120 |
| - ( |
121 |
| - (reserved (- INITIAL_MINT_AMOUNT (calc-total-vested burn-height))) |
122 |
| - (balance (stx-get-balance (as-contract tx-sender))) |
123 |
| - (claimable |
124 |
| - (if (> balance reserved) |
125 |
| - (- balance reserved) |
126 |
| - u0)) |
127 |
| - ) |
128 |
| - claimable |
129 |
| - ) |
| 123 | + (if (< burn-height DEPLOY_BLOCK_HEIGHT) |
| 124 | + u0 |
| 125 | + (let ( |
| 126 | + (reserved (- INITIAL_MINT_AMOUNT (calc-total-vested burn-height))) |
| 127 | + (balance (stx-get-balance (as-contract tx-sender))) |
| 128 | + (claimable (if (> balance reserved) |
| 129 | + (- balance reserved) |
| 130 | + u0 |
| 131 | + )) |
| 132 | + ) |
| 133 | + claimable |
130 | 134 | )
|
| 135 | + ) |
131 | 136 | )
|
0 commit comments