-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to Cairo 2.3.1 & Components (#26)
* Update Scarb to 2.3.0 * Update to Scarb 2.3.1 * Add Scarb.lock * Refactor airdrop to be its own Component * WIP * Fix syntax, use new * Remove vote_investor as it's Carmine only * Migrate main contract file * Finish migration * Add CI * Fix scarb version in CI * Format
- Loading branch information
Showing
10 changed files
with
180 additions
and
331 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,6 @@ | ||
# Code generated by scarb DO NOT EDIT. | ||
version = 1 | ||
|
||
[[package]] | ||
name = "governance" | ||
version = "0.2.0" |
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,65 +1,85 @@ | ||
// https://github.com/CarmineOptions/carmine-api/blob/master/carmine-api-airdrop/src/merkle_tree.rs | ||
|
||
mod Airdrop { | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IAirdrop<TContractState> { | ||
fn claim( | ||
ref self: TContractState, claimee: ContractAddress, amount: u128, proof: Array::<felt252> | ||
); | ||
} | ||
|
||
#[starknet::component] | ||
mod airdrop { | ||
use governance::contract::IGovernance; | ||
use array::ArrayTrait; | ||
use hash::LegacyHash; | ||
use traits::Into; | ||
use starknet::ContractAddress; | ||
use starknet::ContractAddressIntoFelt252; | ||
use starknet::event::EventEmitter; | ||
use starknet::ContractAddress; | ||
//use starknet::event::EventEmitter; | ||
use traits::TryInto; | ||
use option::OptionTrait; | ||
|
||
use governance::merkle_tree::MerkleTree; | ||
use governance::merkle_tree::MerkleTreeTrait; | ||
|
||
use governance::contract::Governance; | ||
use governance::contract::Governance::ContractState; | ||
use governance::traits::IGovernanceTokenDispatcher; | ||
use governance::traits::IGovernanceTokenDispatcherTrait; | ||
use governance::contract::Governance::{governance_token_address, airdrop_claimed, merkle_root}; | ||
|
||
fn get_merkle_root() -> felt252 { | ||
let state = Governance::unsafe_new_contract_state(); | ||
let root = merkle_root::InternalContractStateTrait::read(@state.merkle_root); | ||
if (root == 0) { | ||
// part of migration, to be removed later | ||
0x6d5f4866e61240e8f14de3d5c994153b1bcbf58603f64fa1a0500074b8c8d38 // airdrop week5-week8, from round_2_composed.csv | ||
} else { | ||
root | ||
} | ||
#[storage] | ||
struct Storage { | ||
airdrop_claimed: LegacyMap::<ContractAddress, u128>, | ||
merkle_root: felt252 | ||
} | ||
|
||
// Lets claimee claim from merkle tree the amount - claimed_so_far | ||
fn claim(claimee: ContractAddress, amount: u128, proof: Array::<felt252>) { | ||
let mut state = Governance::unsafe_new_contract_state(); | ||
#[derive(starknet::Event, Drop)] | ||
#[event] | ||
enum Event { | ||
Claimed: Claimed | ||
} | ||
|
||
let mut merkle_tree = MerkleTreeTrait::new(); | ||
let amount_felt: felt252 = amount.into(); | ||
let leaf = LegacyHash::hash(claimee.into(), amount_felt); | ||
#[derive(starknet::Event, Drop)] | ||
struct Claimed { | ||
address: ContractAddress, | ||
received: u128 | ||
} | ||
#[embeddable_as(AirdropImpl)] | ||
impl Airdrop< | ||
TContractState, +HasComponent<TContractState> | ||
> of super::IAirdrop<ComponentState<TContractState>> { | ||
// Lets claimee claim from merkle tree the amount - claimed_so_far | ||
fn claim( | ||
ref self: ComponentState<TContractState>, | ||
claimee: ContractAddress, | ||
amount: u128, | ||
proof: Array::<felt252> | ||
) { | ||
let mut merkle_tree = MerkleTreeTrait::new(); | ||
let amount_felt: felt252 = amount.into(); | ||
let leaf = LegacyHash::hash(claimee.into(), amount_felt); | ||
|
||
let root = merkle_tree.compute_root(leaf, proof.span()); | ||
assert(root == get_merkle_root(), 'invalid proof'); | ||
let root = merkle_tree.compute_root(leaf, proof.span()); | ||
let state = Governance::unsafe_new_contract_state(); | ||
let stored_root = self.merkle_root.read(); | ||
assert(root == stored_root, 'invalid proof'); | ||
|
||
let claimed_so_far: u128 = airdrop_claimed::InternalContractStateTrait::read( | ||
@state.airdrop_claimed, claimee | ||
); | ||
assert(claimed_so_far < amount, 'claiming more than eligible for'); | ||
let to_mint = amount - claimed_so_far; | ||
let claimed_so_far: u128 = self.airdrop_claimed.read(claimee); | ||
assert(claimed_so_far < amount, 'claiming more than eligible for'); | ||
let to_mint = amount - claimed_so_far; | ||
|
||
// Mint | ||
let govtoken_addr = governance_token_address::InternalContractStateTrait::read( | ||
@state.governance_token_address | ||
); | ||
IGovernanceTokenDispatcher { | ||
contract_address: govtoken_addr | ||
}.mint(claimee, u256 { high: 0, low: to_mint }); | ||
// Mint | ||
let govtoken_addr = state.get_governance_token_address(); | ||
IGovernanceTokenDispatcher { contract_address: govtoken_addr } | ||
.mint(claimee, u256 { high: 0, low: to_mint }); | ||
|
||
// Emit event | ||
state.emit(Governance::Claimed { address: claimee, received: to_mint }); | ||
// Write new claimed amt | ||
self.airdrop_claimed.write(claimee, to_mint + claimed_so_far); | ||
|
||
// Write new claimed amt | ||
airdrop_claimed::InternalContractStateTrait::write( | ||
ref state.airdrop_claimed, claimee, to_mint + claimed_so_far | ||
); | ||
// Emit event | ||
self.emit(Claimed { address: claimee, received: to_mint }); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.