Skip to content

Commit

Permalink
πŸ› fix revoke
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Jul 11, 2022
1 parent de5dad1 commit f345d3c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 45 deletions.
10 changes: 5 additions & 5 deletions src/interfaces/starkvest.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ from starkware.cairo.common.uint256 import Uint256
@contract_interface
namespace IStarkVest:
###
# Compute and return releaseable amount of tokens for a vesting.
# Compute and return releasable amount of tokens for a vesting.
# @param vesting_id the vesting identifier
# @return the amount of releaseable tokens
# @return the amount of releasable tokens
###
func releaseable_amount(vesting_id : felt) -> (releaseable_amount : Uint256):
func releasable_amount(vesting_id : felt) -> (releasable_amount : Uint256):
end
###
Expand All @@ -26,13 +26,13 @@ namespace IStarkVest:
# Compute and return the amount of tokens withdrawable by the owner.
# @return the amount of withdrawable tokens
###
func withdrawable_amount() -> (releaseable_amount : Uint256):
func withdrawable_amount() -> (releasable_amount : Uint256):
end
###
# Revokes the vesting identified by vesting_id.
# @param vesting_id the vesting identifier
# @return the amount of releaseable tokens
# @return the amount of releasable tokens
###
func revoke(vesting_id : felt):
end
Expand Down
65 changes: 44 additions & 21 deletions src/starkvest/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Starkware dependencies
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.uint256 import Uint256, uint256_le
from starkware.cairo.common.uint256 import Uint256, uint256_le, uint256_lt
from starkware.cairo.common.hash import hash2
from starkware.cairo.common.math import (
assert_not_zero,
Expand Down Expand Up @@ -125,19 +125,19 @@ namespace StarkVest:
end

###
# Compute and return releaseable amount of tokens for a vesting.
# Compute and return releasable amount of tokens for a vesting.
# @param vesting_id the vesting identifier
# @return the amount of releaseable tokens
# @return the amount of releasable tokens
###
func releaseable_amount{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
func releasable_amount{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
vesting_id : felt
) -> (releaseable_amount : Uint256):
) -> (releasable_amount : Uint256):
let (vesting) = vestings_.read(vesting_id)
with_attr error_message("StarkVest: invalid beneficiary"):
assert_not_zero(vesting.beneficiary)
end
let (releaseable_amount) = _releaseable_amount(vesting)
return (releaseable_amount)
let (releasable_amount) = _releasable_amount(vesting)
return (releasable_amount)
end
# ------
Expand Down Expand Up @@ -225,7 +225,7 @@ namespace StarkVest:
###
# Revokes the vesting identified by vesting_id.
# @param vesting_id the vesting identifier
# @return the amount of releaseable tokens
# @return the amount of releasable tokens
###
func revoke{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
vesting_id : felt
Expand All @@ -240,8 +240,20 @@ namespace StarkVest:
# Check that vesting is revocable
internal.assert_revocable(vesting)

# TODO: release releasable tokens
# TODO: update vestings_total_amount_
# Release releasable tokens
let (releasable_amount) = _releasable_amount(vesting)
release_if_tokens_releasable(vesting_id, releasable_amount)

# Get the vesting from storage
let (vesting) = vestings_.read(vesting_id)

# Update vestings total_amount
let (unreleased_tokens) = SafeUint256.sub_le(vesting.amount_total, vesting.released)
let (vestings_total_amount) = vestings_total_amount_.read()
let (new_vestings_total_amount) = SafeUint256.sub_le(
vestings_total_amount, unreleased_tokens
)
vestings_total_amount_.write(new_vestings_total_amount)

# Update vesting
let vesting = Vesting(
Expand All @@ -262,6 +274,17 @@ namespace StarkVest:
return ()
end

func release_if_tokens_releasable{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr
}(vesting_id : felt, amount : Uint256):
let (has_tokens_to_release) = uint256_lt(Uint256(0, 0), amount)
if has_tokens_to_release == TRUE:
release(vesting_id, amount)
return ()
end
return ()
end

###
# Release vested amount of tokens.
# @param vesting_id the vesting identifier
Expand All @@ -288,11 +311,11 @@ namespace StarkVest:
assert_not_zero(owner_or_beneficiary)
end
# Check that account has enough releaseable tokens
let (releaseable_amount) = _releaseable_amount(vesting)
# Check that account has enough releasable tokens
let (releasable_amount) = _releasable_amount(vesting)
with_attr error_message(
"StarkVest: cannot release tokens, not enough vested releasable tokens"):
uint256_le(amount, releaseable_amount)
uint256_le(amount, releasable_amount)
end
# Update vesting
Expand Down Expand Up @@ -376,11 +399,11 @@ namespace StarkVest:
# ------
###
# Get releaseable amount of tokens for a vesting
# Get releasable amount of tokens for a vesting
###
func _releaseable_amount{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
func _releasable_amount{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
vesting : Vesting
) -> (releaseable_amount : Uint256):
) -> (releasable_amount : Uint256):
alloc_locals
# Get current timestamp
let (current_time) = get_block_timestamp()
Expand All @@ -391,7 +414,7 @@ namespace StarkVest:
let sum = before_cliff + is_revoked
let (before_cliff_or_is_revoked) = is_not_zero(sum)
# Either we are before the cliff or the vesting is revoked
# In both cases the amount of releaseable tokens is 0
# In both cases the amount of releasable tokens is 0
if before_cliff_or_is_revoked == TRUE:
let vested_amount = Uint256(0, 0)
return (vested_amount)
Expand All @@ -404,8 +427,8 @@ namespace StarkVest:
# Vesting is over
# Return total minus what has already been released
if after_vesting_end == TRUE:
let (releaseable_amount) = SafeUint256.sub_lt(vesting.amount_total, vesting.released)
return (releaseable_amount)
let (releasable_amount) = SafeUint256.sub_lt(vesting.amount_total, vesting.released)
return (releasable_amount)
end

# Compute how much time we are past the start of vesting
Expand All @@ -423,8 +446,8 @@ namespace StarkVest:
let (tmp_amount) = SafeUint256.mul(vesting.amount_total, final_vested_seconds)
let (vested_amount, _) = SafeUint256.div_rem(tmp_amount, vesting_duration_uint256)
# Substract released tokens
let (releaseable_amount) = SafeUint256.sub_lt(vested_amount, vesting.released)
return (releaseable_amount)
let (releasable_amount) = SafeUint256.sub_lt(vested_amount, vesting.released)
return (releasable_amount)
end

###
Expand Down
12 changes: 6 additions & 6 deletions src/starkvest/starkvest.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ func get_contract_balance{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, rang
end

###
# Compute and return releaseable amount of tokens for a vesting.
# Compute and return releasable amount of tokens for a vesting.
# @param vesting_id the vesting identifier
# @return the amount of releaseable / vested tokens
# @return the amount of releasable / vested tokens
###
@view
func releaseable_amount{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
func releasable_amount{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
vesting_id : felt
) -> (releaseable_amount : Uint256):
return StarkVest.releaseable_amount(vesting_id)
) -> (releasable_amount : Uint256):
return StarkVest.releasable_amount(vesting_id)
end

# -----
Expand Down Expand Up @@ -120,7 +120,7 @@ end
###
# Revokes the vesting identified by vesting_id.
# @param vesting_id the vesting identifier
# @return the amount of releaseable tokens
# @return the amount of releasable tokens
###
@external
func revoke{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(vesting_id : felt):
Expand Down
14 changes: 7 additions & 7 deletions tests/integrations/starkvest/test_nominal_case.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func test_e2e{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}

# Set block time to 999 (1 second before vesting starts)
%{ stop_warp = warp(999, ids.starkvest) %}
let (releasable_amount) = starkvest_instance.releaseable_amount(vesting_id)
let (releasable_amount) = starkvest_instance.releasable_amount(vesting_id)
%{ stop_warp() %}
assert releasable_amount = Uint256(0, 0)

# Set block time to 2800 (1800 second after vesting starts)
# Should have vested 50% of tokens
%{ stop_warp = warp(2800, ids.starkvest) %}
let (releasable_amount) = starkvest_instance.releaseable_amount(vesting_id)
let (releasable_amount) = starkvest_instance.releasable_amount(vesting_id)
assert releasable_amount = Uint256(500, 0)

# Check balance of vesting contract before release
Expand All @@ -102,7 +102,7 @@ func test_e2e{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}

# Release 100 tokens
starkvest_instance.release(vesting_id, Uint256(100, 0))
let (releasable_amount) = starkvest_instance.releaseable_amount(vesting_id)
let (releasable_amount) = starkvest_instance.releasable_amount(vesting_id)
%{ stop_warp() %}
assert releasable_amount = Uint256(400, 0)

Expand Down Expand Up @@ -162,11 +162,11 @@ namespace starkvest_instance:
return (vesting_id)
end

func releaseable_amount{
func releasable_amount{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr, starkvest : felt
}(vesting_id : felt) -> (releaseable_amount : Uint256):
let (releaseable_amount) = IStarkVest.releaseable_amount(starkvest, vesting_id)
return (releaseable_amount)
}(vesting_id : felt) -> (releasable_amount : Uint256):
let (releasable_amount) = IStarkVest.releasable_amount(starkvest, vesting_id)
return (releasable_amount)
end

func revoke{
Expand Down
12 changes: 6 additions & 6 deletions tests/units/starkvest/test_starkvest.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,25 @@ func test_vest_some_tokens{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, ran
%{ stop() %}

%{ stop_warp = warp(2800) %}
let (vested_amount) = StarkVest.releaseable_amount(vesting_id)
let (vested_amount) = StarkVest.releasable_amount(vesting_id)
%{ stop_warp() %}

assert vested_amount = Uint256(500, 0)

%{ stop_warp = warp(3700) %}
let (vested_amount) = StarkVest.releaseable_amount(vesting_id)
let (vested_amount) = StarkVest.releasable_amount(vesting_id)
%{ stop_warp() %}

assert vested_amount = Uint256(750, 0)

%{ stop_warp = warp(3988) %}
let (vested_amount) = StarkVest.releaseable_amount(vesting_id)
let (vested_amount) = StarkVest.releasable_amount(vesting_id)
%{ stop_warp() %}

assert vested_amount = Uint256(830, 0)

%{ stop_warp = warp(4600) %}
let (vested_amount) = StarkVest.releaseable_amount(vesting_id)
let (vested_amount) = StarkVest.releasable_amount(vesting_id)
%{ stop_warp() %}

assert vested_amount = Uint256(1000, 0)
Expand Down Expand Up @@ -391,7 +391,7 @@ func test_release_nominal_case{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
# Got to timestamp 100
###
%{ stop_warp = warp(100) %}
let (releasable_amount) = StarkVest.releaseable_amount(vesting_id)
let (releasable_amount) = StarkVest.releasable_amount(vesting_id)
assert releasable_amount = Uint256(100, 0)

# ##
Expand All @@ -412,7 +412,7 @@ func test_release_nominal_case{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
###
# Assert that releasable amount is now 0
###
let (releasable_amount) = StarkVest.releaseable_amount(vesting_id)
let (releasable_amount) = StarkVest.releasable_amount(vesting_id)
assert releasable_amount = Uint256(0, 0)

return ()
Expand Down

0 comments on commit f345d3c

Please sign in to comment.