-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add vesting to the main contract * Add tests * Polish scarb fmt * Update Scarb in CI too to fix CI * Implement PR feedback * Fix vesting tests with snforge v0.23
- Loading branch information
Showing
7 changed files
with
255 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use starknet::{ContractAddress, ClassHash}; | ||
|
||
#[starknet::interface] | ||
trait IveCARM<TContractState> { | ||
fn name(self: @TContractState) -> felt252; | ||
fn symbol(self: @TContractState) -> felt252; | ||
fn decimals(self: @TContractState) -> u8; | ||
fn mint(ref self: TContractState, recipient: ContractAddress, amount: u256); | ||
fn burn(ref self: TContractState, account: ContractAddress, amount: u256); | ||
fn upgrade(ref self: TContractState, new_class_hash: ClassHash); | ||
} | ||
|
||
#[starknet::contract] | ||
mod MyToken { | ||
use openzeppelin::access::ownable::ownable::OwnableComponent::InternalTrait; | ||
use starknet::ContractAddress; | ||
use starknet::ClassHash; | ||
use openzeppelin::token::erc20::ERC20Component; | ||
use openzeppelin::access::ownable::ownable::OwnableComponent; | ||
|
||
component!(path: ERC20Component, storage: erc20, event: ERC20Event); | ||
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); | ||
|
||
// ERC20 Component | ||
#[abi(embed_v0)] | ||
impl ERC20Impl = ERC20Component::ERC20Impl<ContractState>; | ||
|
||
#[abi(embed_v0)] | ||
impl ERC20CamelOnlyImpl = ERC20Component::ERC20CamelOnlyImpl<ContractState>; | ||
|
||
impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>; | ||
|
||
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>; | ||
|
||
// Ownable Component | ||
#[abi(embed_v0)] | ||
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>; | ||
|
||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
erc20: ERC20Component::Storage, | ||
#[substorage(v0)] | ||
ownable: OwnableComponent::Storage, | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
Upgraded: Upgraded, | ||
// #[flat] | ||
ERC20Event: ERC20Component::Event, | ||
OwnableEvent: OwnableComponent::Event | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct Upgraded { | ||
class_hash: ClassHash | ||
} | ||
|
||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, owner: ContractAddress) { | ||
self.ownable.initializer(owner); | ||
} | ||
|
||
#[abi(embed_v0)] | ||
impl VeCARMImpl of super::IveCARM<ContractState> { | ||
// Did not import Erc20MetaData, so we can change decimals | ||
// so we need to define name, symbol and decimals ourselves | ||
fn name(self: @ContractState) -> felt252 { | ||
'vote escrowed Carmine Token' | ||
} | ||
|
||
fn symbol(self: @ContractState) -> felt252 { | ||
'veCARM' | ||
} | ||
|
||
fn decimals(self: @ContractState) -> u8 { | ||
18 | ||
} | ||
|
||
fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) { | ||
self.ownable.assert_only_owner(); | ||
self.erc20._mint(recipient, amount); | ||
} | ||
|
||
fn burn(ref self: ContractState, account: ContractAddress, amount: u256) { | ||
self.ownable.assert_only_owner(); | ||
self.erc20._burn(account, amount); | ||
} | ||
|
||
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { | ||
self.ownable.assert_only_owner(); | ||
assert(!new_class_hash.is_zero(), 'Class hash cannot be zero'); | ||
starknet::replace_class_syscall(new_class_hash).unwrap(); | ||
self.emit(Upgraded { class_hash: new_class_hash }); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod vesting; | ||
mod basic; | ||
mod test_treasury; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use core::option::OptionTrait; | ||
use core::result::ResultTrait; | ||
use array::ArrayTrait; | ||
use core::traits::TryInto; | ||
use debug::PrintTrait; | ||
use starknet::ContractAddress; | ||
use snforge_std::{ | ||
BlockId, declare, ContractClassTrait, ContractClass, start_prank, start_warp, CheatTarget | ||
}; | ||
|
||
use konoha::vesting::{IVestingDispatcher, IVestingDispatcherTrait, IVesting}; | ||
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait}; | ||
|
||
// returns gov addr, token addr | ||
fn test_setup() -> (ContractAddress, ContractAddress) { | ||
let new_gov_contract: ContractClass = declare("Governance") | ||
.expect('unable to declare Governance'); | ||
let new_token_contract: ContractClass = declare("MyToken").expect('unable to declare MyToken'); | ||
let new_gov_addr: ContractAddress = | ||
0x001405ab78ab6ec90fba09e6116f373cda53b0ba557789a4578d8c1ec374ba0f | ||
.try_into() | ||
.unwrap(); | ||
let mut token_constructor = ArrayTrait::new(); | ||
token_constructor.append(new_gov_addr.into()); // Owner | ||
let (token_address, _) = new_token_contract | ||
.deploy(@token_constructor) | ||
.expect('unable to deploy token'); | ||
let mut gov_constructor: Array<felt252> = ArrayTrait::new(); | ||
gov_constructor.append(token_address.into()); | ||
let (gov_address, _) = new_gov_contract | ||
.deploy_at(@gov_constructor, new_gov_addr) | ||
.expect('unable to deploy gov'); | ||
|
||
(gov_address, token_address) | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ('not self-call',))] | ||
fn test_unauthorized_add_vesting_schedule() { | ||
let (gov_address, _) = test_setup(); | ||
|
||
let gov_vesting = IVestingDispatcher { contract_address: gov_address }; | ||
|
||
start_warp(CheatTarget::All, 1); | ||
|
||
gov_vesting.add_linear_vesting_schedule(10, 10, 10, 1000000, 0x1.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ('not yet eligible',))] | ||
fn test_unauthorized_vest_early() { | ||
let (gov_address, _) = test_setup(); | ||
|
||
let gov_vesting = IVestingDispatcher { contract_address: gov_address }; | ||
|
||
start_warp(CheatTarget::All, 1); | ||
start_prank(CheatTarget::One(gov_address), gov_address); | ||
|
||
let grantee: ContractAddress = 0x1.try_into().unwrap(); | ||
|
||
gov_vesting.add_linear_vesting_schedule(10, 10, 10, 1000000, grantee); | ||
|
||
gov_vesting.vest(grantee, 10); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ('nothing to vest',))] | ||
fn test_vest_twice() { | ||
let (gov_address, _) = test_setup(); | ||
|
||
let gov_vesting = IVestingDispatcher { contract_address: gov_address }; | ||
|
||
start_warp(CheatTarget::All, 1); | ||
start_prank(CheatTarget::One(gov_address), gov_address); | ||
|
||
let grantee: ContractAddress = 0x1.try_into().unwrap(); | ||
|
||
gov_vesting.add_linear_vesting_schedule(10, 10, 10, 1000000, grantee); | ||
|
||
start_warp(CheatTarget::All, 11); | ||
|
||
gov_vesting.vest(grantee, 10); | ||
gov_vesting.vest(grantee, 10); | ||
} | ||
|
||
#[test] | ||
fn test_add_simple_vesting_schedule() { | ||
let (gov_address, token_address) = test_setup(); | ||
|
||
let gov_vesting = IVestingDispatcher { contract_address: gov_address }; | ||
let tok = IERC20Dispatcher { contract_address: token_address }; | ||
|
||
start_warp(CheatTarget::All, 1); | ||
start_prank(CheatTarget::One(gov_address), gov_address); | ||
|
||
let grantee: ContractAddress = 0x1.try_into().unwrap(); | ||
gov_vesting.add_linear_vesting_schedule(10, 10, 10, 1000001, grantee); | ||
|
||
start_warp(CheatTarget::All, 11); // past first vest | ||
// anyone can claim for the grantee | ||
gov_vesting.vest(grantee, 10); | ||
assert(tok.balance_of(grantee) == 100000, 'vesting unsuccessful'); | ||
|
||
// grantee themselves can claim too | ||
start_prank(CheatTarget::One(gov_address), grantee); | ||
start_warp(CheatTarget::All, 21); // past second vest | ||
gov_vesting.vest(grantee, 20); | ||
assert(tok.balance_of(grantee) == 200000, 'vesting unsuccessful'); | ||
|
||
start_warp(CheatTarget::All, 101); // past last vest. no requirement to vest in order | ||
gov_vesting.vest(grantee, 100); | ||
// leftover tokens are included in last vest. (remainder after division) | ||
assert(tok.balance_of(grantee) == 300001, 'vesting unsuccessful'); | ||
} |