Skip to content

Commit 41f7146

Browse files
committed
fix: code review improvements
1 parent 8f95c7d commit 41f7146

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

contrib/core-contract-tests/tests/clarigen-types.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4586,14 +4586,14 @@ export const contracts = {
45864586
[newRecipient: TypedAbiArg<string, "newRecipient">],
45874587
Response<boolean, bigint>
45884588
>,
4589-
calcVestedAmount: {
4590-
name: "calc-vested-amount",
4589+
calcClaimableAmount: {
4590+
name: "calc-claimable-amount",
45914591
access: "read_only",
45924592
args: [{ name: "burn-height", type: "uint128" }],
4593-
outputs: { type: { response: { ok: "uint128", error: "none" } } },
4593+
outputs: { type: "uint128" },
45944594
} as TypedAbiFunction<
45954595
[burnHeight: TypedAbiArg<number | bigint, "burnHeight">],
4596-
Response<bigint, null>
4596+
bigint
45974597
>,
45984598
getDeployBlockHeight: {
45994599
name: "get-deploy-block-height",
@@ -4651,6 +4651,11 @@ export const contracts = {
46514651
type: "uint128",
46524652
access: "constant",
46534653
} as TypedAbiVariable<bigint>,
4654+
STX_PER_ITERATION: {
4655+
name: "STX_PER_ITERATION",
4656+
type: "uint128",
4657+
access: "constant",
4658+
} as TypedAbiVariable<bigint>,
46544659
deployBlockHeight: {
46554660
name: "deploy-block-height",
46564661
type: "uint128",
@@ -4675,6 +4680,7 @@ export const contracts = {
46754680
INITIAL_MINT_VESTING_AMOUNT: 100_000_000_000_000n,
46764681
INITIAL_MINT_VESTING_ITERATIONS: 24n,
46774682
INITIAL_MINT_VESTING_ITERATION_BLOCKS: 4_383n,
4683+
STX_PER_ITERATION: 4_166_666_666_666n,
46784684
deployBlockHeight: 3n,
46794685
recipient: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM",
46804686
vestedClaimedAmount: 0n,

contrib/core-contract-tests/tests/sip-031/sip-031.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { project, accounts } from '../clarigen-types'; // where your [types.output] was specified
22
import { CoreNodeEventType, projectFactory } from '@clarigen/core';
3-
import { filterEvents, rov, rovOk, txErr, txOk } from '@clarigen/test';
3+
import { filterEvents, rov, txErr, txOk } from '@clarigen/test';
44
import { test, expect } from 'vitest';
55

66
const contracts = projectFactory(project, 'simnet');
@@ -106,19 +106,19 @@ test('calculating vested amounts at a block height', () => {
106106
return immediateAmount + vestingAmount;
107107
}
108108

109-
expect(rovOk(contract.calcVestedAmount(deployBlockHeight))).toBe(immediateAmount);
109+
expect(rov(contract.calcClaimableAmount(deployBlockHeight))).toBe(immediateAmount);
110110

111111
function expectAmount(month: bigint) {
112112
const burnHeight = deployBlockHeight + month * 4383n;
113-
expect(rovOk(contract.calcVestedAmount(burnHeight))).toBe(expectedAmount(burnHeight));
113+
expect(rov(contract.calcClaimableAmount(burnHeight))).toBe(expectedAmount(burnHeight));
114114
}
115115

116116
for (let i = 1n; i < 24n; i++) {
117117
expectAmount(i);
118118
}
119119
// At 24+ months, the entire vesting bucket should be unlocked
120-
expect(rovOk(contract.calcVestedAmount(deployBlockHeight + 24n * 4383n))).toBe(initialMintAmount);
121-
expect(rovOk(contract.calcVestedAmount(deployBlockHeight + 25n * 4383n))).toBe(initialMintAmount);
120+
expect(rov(contract.calcClaimableAmount(deployBlockHeight + 24n * 4383n))).toBe(initialMintAmount);
121+
expect(rov(contract.calcClaimableAmount(deployBlockHeight + 25n * 4383n))).toBe(initialMintAmount);
122122
});
123123

124124
// -----------------------------------------------------------------------------

stackslib/src/chainstate/stacks/boot/sip-031.clar

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
;; The amount of STX that is vested over the 24 months
1717
(define-constant INITIAL_MINT_VESTING_AMOUNT (- INITIAL_MINT_AMOUNT INITIAL_MINT_IMMEDIATE_AMOUNT))
1818

19+
;; The amount of STX that is vested per iteration
20+
(define-constant STX_PER_ITERATION (/ INITIAL_MINT_VESTING_AMOUNT INITIAL_MINT_VESTING_ITERATIONS))
21+
1922
(define-data-var recipient principal tx-sender)
2023

2124
(define-data-var deploy-block-height uint burn-block-height)
@@ -44,29 +47,24 @@
4447
(define-public (claim)
4548
(let
4649
(
47-
(balance (stx-get-balance (as-contract tx-sender)))
48-
(burn-height burn-block-height)
49-
(total-vested (calc-total-vested burn-height))
50+
(balance (stx-get-balance (as-contract tx-sender)))
51+
(total-vested (calc-total-vested burn-block-height))
5052
(vested-claimed (var-get vested-claimed-amount))
51-
;; Vested that has not yet been claimed
52-
(available-vested (- total-vested vested-claimed))
5353
;; Portion of the initial mint that is *still* locked (not yet vested)
5454
(reserved (- INITIAL_MINT_AMOUNT total-vested))
5555
;; Free balance = everything the caller may withdraw right now
5656
(claimable
5757
(if (> balance reserved)
5858
(- balance reserved)
5959
u0))
60-
(vested-to-claim (if (> available-vested claimable) claimable available-vested))
61-
(extra-to-claim (- claimable vested-to-claim))
6260
)
6361
(try! (validate-caller))
6462
(asserts! (> claimable u0) (err ERR_NOTHING_TO_CLAIM))
65-
(var-set vested-claimed-amount (+ vested-claimed vested-to-claim))
63+
(var-set vested-claimed-amount (+ vested-claimed total-vested))
6664

6765
(try! (as-contract (stx-transfer? claimable tx-sender (var-get recipient))))
6866
(ok claimable)
69-
)
67+
)
7068
)
7169

7270
(define-private (validate-caller)
@@ -79,28 +77,28 @@
7977
(let
8078
(
8179
(diff (- burn-height (var-get deploy-block-height)))
80+
;; Note: this rounds down
8281
(iterations (/ diff INITIAL_MINT_VESTING_ITERATION_BLOCKS))
83-
(stx-per-iteration (/ INITIAL_MINT_VESTING_AMOUNT INITIAL_MINT_VESTING_ITERATIONS))
84-
(vesting-multiple (* stx-per-iteration iterations))
82+
(vested-multiple (* STX_PER_ITERATION iterations))
8583

8684
;; If we have completed (or exceeded) the scheduled number of iterations,
8785
;; consider the *entire* vesting bucket unlocked. This avoids leaving a
8886
;; tiny remainder caused by integer-division truncation.
89-
(vesting-amount (if (>= iterations INITIAL_MINT_VESTING_ITERATIONS)
87+
(vested-amount (if (>= iterations INITIAL_MINT_VESTING_ITERATIONS)
9088
INITIAL_MINT_VESTING_AMOUNT
91-
vesting-multiple))
92-
(total-amount (+ INITIAL_MINT_IMMEDIATE_AMOUNT vesting-amount))
89+
vested-multiple))
90+
(total-amount (+ INITIAL_MINT_IMMEDIATE_AMOUNT vested-amount))
9391
)
9492
total-amount
9593
)
9694
)
9795

98-
;; Returns the amount of STX that is vested at `burn-height`
99-
(define-read-only (calc-vested-amount (burn-height uint))
96+
;; Returns the amount of STX that is claimable from the vested balance at `burn-height`
97+
(define-read-only (calc-claimable-amount (burn-height uint))
10098
(let
101-
(
102-
(total-vested (calc-total-vested burn-height))
103-
)
104-
(ok (- total-vested (var-get vested-claimed-amount)))
99+
(
100+
(total-vested (calc-total-vested burn-height))
101+
)
102+
(- total-vested (var-get vested-claimed-amount))
105103
)
106104
)

0 commit comments

Comments
 (0)