Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ pub mod TicketMaster {
/// @param issuance_reduction_bips The issuance reduction expressed in basis points (out of
/// 10,000)
/// @param treasury_address The address of the treasury
/// @param recipients The addresses of the recipients of the tokens
/// @param amounts The amounts of tokens to mint to the recipients
/// @param distribution_end_time The end time of the token distribution
/// @param buyback_order_config The configuration for the buyback orders
#[constructor]
Expand All @@ -131,8 +129,6 @@ pub mod TicketMaster {
issuance_reduction_price_duration: u64,
issuance_reduction_bips: u128,
treasury_address: ContractAddress,
recipients: Array<ContractAddress>,
amounts: Array<u256>,
distribution_end_time: u64,
buyback_order_config: BuybackOrderConfig,
) {
Expand All @@ -150,11 +146,6 @@ pub mod TicketMaster {
assert(treasury_address != zero_address, 'Invalid treasury address');
assert(total_supply > 0, 'Invalid total supply');

// Validate recipients and amounts arrays
let recipients_len = recipients.len();
let amounts_len = amounts.len();
assert(recipients_len == amounts_len, 'Arrays length mismatch');

let current_time = starknet::get_block_timestamp();
assert!(distribution_end_time > current_time, "End time must be greater than now");

Expand Down Expand Up @@ -191,22 +182,34 @@ pub mod TicketMaster {
self.distribution_end_time.write(distribution_end_time);
self.buyback_order_config.write(buyback_order_config);

// Distribute tokens to initial recipients
let total_distributed = _distribute_initial_tokens(
ref self, recipients, amounts, total_supply, zero_address,
);

let remaining_supply = total_supply.into() - total_distributed;

// register token with Ekubo registry
_register_token(ref self);

// Store tokens remaining for distribution
self.tokens_for_distribution.write(remaining_supply - ERC20_UNIT.into());
self.tokens_for_distribution.write(total_supply.into());
}

#[abi(embed_v0)]
impl TicketMasterImpl of ITicketMaster<ContractState> {
fn premint_tokens(
ref self: ContractState, recipients: Array<ContractAddress>, amounts: Array<u256>,
) {
self.ownable.assert_only_owner();
assert(self.deployment_state.read() == 0, 'Deployment state is not 0');
assert(recipients.len() == amounts.len(), 'Arrays length mismatch');

let token_available = self.tokens_for_distribution.read();

// Distribute tokens to initial recipients
let total_distributed = _distribute_initial_tokens(
ref self, recipients, amounts, token_available.try_into().unwrap(),
);
Comment on lines +204 to +206

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using .unwrap() on a try_into() conversion can lead to a contract panic if the conversion fails. While tokens_for_distribution is initialized from a u128 and is not expected to exceed u128::max_value in the current implementation, it's a best practice to avoid unwrap() in production code for robustness. Using expect() with a descriptive message would be safer and provide better debugging information in case of an unexpected state.

            let token_available_u128 = token_available.try_into().expect('Tokens for distribution > u128');
            let total_distributed = _distribute_initial_tokens(
                ref self, recipients, amounts, token_available_u128,
            );


let remaining_supply = token_available - total_distributed;

self.tokens_for_distribution.write(remaining_supply);
}

/// @notice Initializes the TWAMM pools for the distribution and buyback tokens
/// @dev This function should be called as step 1 after deployment
/// @param distribution_initial_tick The initial tick for the distribution pool
Expand Down Expand Up @@ -1226,25 +1229,24 @@ pub mod TicketMaster {
/// @param recipients Array of recipient addresses
/// @param amounts Array of token amounts to distribute
/// @param total_supply Total supply of tokens
/// @param zero_address The zero address for validation
/// @return Total amount of tokens distributed
fn _distribute_initial_tokens(
ref self: ContractState,
recipients: Array<ContractAddress>,
amounts: Array<u256>,
total_supply: u128,
zero_address: ContractAddress,
) -> u256 {
let recipients_len = recipients.len();
let mut total_distributed: u256 = 0;

if recipients_len > 0 {
let zero_address: ContractAddress = 0.try_into().unwrap();
let mut i = 0;
while i < recipients_len {
let recipient = *recipients.at(i);
let amount = *amounts.at(i);

// Ensure recipient is not zero address
// Validate recipient is not zero address
assert(recipient != zero_address, 'Invalid recipient address');

// Mint tokens directly to recipient
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use starknet::ContractAddress;
#[starknet::interface]
pub trait ITicketMaster<TContractState> {
fn init_distribution_pool(ref self: TContractState, distribution_initial_tick: i129) -> u256;
fn premint_tokens(
ref self: TContractState, recipients: Array<ContractAddress>, amounts: Array<u256>,
);
fn provide_initial_liquidity(
ref self: TContractState,
payment_token_amount: u128,
Expand Down
8 changes: 0 additions & 8 deletions tests/helper.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ pub fn ticket_master_calldata_custom(
issuance_reduction_price_duration: u64,
issuance_reduction_bips: u128,
treasury_address: ContractAddress,
recipients: Array<ContractAddress>,
amounts: Array<u256>,
distribution_end_time: u64,
buyback_order_config: BuybackOrderConfig,
) -> Array<felt252> {
Expand All @@ -155,8 +153,6 @@ pub fn ticket_master_calldata_custom(
issuance_reduction_price_duration.serialize(ref calldata);
issuance_reduction_bips.serialize(ref calldata);
treasury_address.serialize(ref calldata);
recipients.serialize(ref calldata);
amounts.serialize(ref calldata);
distribution_end_time.serialize(ref calldata);
buyback_order_config.serialize(ref calldata);

Expand All @@ -183,8 +179,6 @@ pub fn ticket_master_calldata(
let symbol: ByteArray = "BDT";
let total_supply: u128 = DUNGEON_TICKET_SUPPLY;
let distribution_pool_fee: u128 = DISTRIBUTION_POOL_FEE_BPS;
let recipients: Array<ContractAddress> = array![];
let amounts: Array<u256> = array![];
let distribution_end_time: u64 = DISTRIBUTION_END_TIME;
let buyback_order_config = BUYBACK_ORDER_CONFIG;

Expand All @@ -207,8 +201,6 @@ pub fn ticket_master_calldata(
issuance_reduction_price_duration,
issuance_reduction_bips,
treasury_address,
recipients,
amounts,
distribution_end_time,
buyback_order_config,
)
Expand Down
Loading