Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Launchpad Architecture Refactoring #451

Open
wants to merge 43 commits into
base: main
Choose a base branch
from

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Dec 26, 2024

Description

This PR introduces a comprehensive refactoring of the Launchpad contract architecture, focusing on optimizing reward calculations and improving data structure efficiency. The main goals are to reduce computational overhead, enhance gas efficiency, and improve the overall maintainability of the codebase.

Key Changes

Architectural Improvements

  • Separated business logic into pure functions for better testability
  • Introduced domain-specific types for better data organization (e.g., ProjectInput, ProjectTierInfo, DepositState)
  • Improved error handling by returning errors in internal functions while maintaining panic in public interfaces
  • Implemented proper pointer handling for uint256 operations using Clone()

Code Structure

  • Split large functions into smaller, focused functions with single responsibilities
  • Maintained public function signatures for backward compatibility
  • Reduced code duplication through reusable helper functions

Major Refactored Components

Project Management (launchpad_init.gno -> launchpad.gno)

  • Added validation layer with validateProjectInput
  • Separated tier creation logic into createTier
  • Improved project creation workflow with clear steps

Reward Handling (launchpad_reward.gno -> reward.gno)

  • Separated reward validation and processing logic
  • Improved uint256 handling for calculations

Deposit Management (launchpad_deposit.gno)

  • Added DepositState for better state management
  • Separated deposit validation (validateDepositCollection)
  • Improved deposit processing with clear error handling
  • Added proper indexing management

Simplified and More Organized Types

  • Introduced new type groupings for better data organization and reduced memory footprint:
    • TimeInfo: Consolidated height and timestamp pairs
    • ProjectStats: Grouped project statistics fields
    • RefundInfo: Consolidated refund-related fields

Project Structure Improvements

  • Refactored Project struct for better maintainability:
    • Replaced individual tier fields with a map-based structure (map[uint64]Tier)
    • Introduced tiersRatios map for cleaner ratio management
    • Grouped time-related fields using TimeInfo
    • Consolidated project statistics into ProjectStats
    • Grouped refund information into RefundInfo

Benefits of New Structure

  • Reduced code duplication in time-related fields
  • Better encapsulation of related data
  • More flexible tier management through map structure
  • Improved readability and maintainability
  • Reduced potential for errors in related field updates
  • More intuitive grouping of related fields

Memory Optimization

  • Reduced struct size through better field organization
  • Improved memory locality for related fields
  • More efficient handling of optional fields

Performance Considerations

  • Maintained O(1) access for critical operations
  • Reduced memory allocations where possible
  • Optimized struct sizes and memory layout

Future Improvements

  • Additional caching strategies

@notJoon notJoon changed the title refactor: launchpad refactor: Launchpad Architecture Refactoring Jan 2, 2025
@notJoon notJoon self-assigned this Jan 2, 2025
@notJoon notJoon marked this pull request as ready for review January 4, 2025 04:11
launchpad/deposit.gno Show resolved Hide resolved
return totalAmount, nil
}

func CollectDepositGns() uint64 {
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to check if the launchpad program is finished or not?

Deposited GNS is only claimable when the longest tier in a launchpad program is finished. (project token rewards are always claimable regardless of this)

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems that condition checks has been removed in somewhere in the commits. But I can't find where it was.

Comment on lines 539 to 541
// Deprecated: use satisfyDepositConditions instead
func checkDepositConditions(project Project) {
if project.conditions == nil {
return
}

caller := std.PrevRealm().Addr()
for _, condition := range project.conditions {
if condition.minAmount == 0 {
continue
}

// check balance
var balance uint64
if condition.tokenPath == consts.GOV_XGNS_PATH {
balance = xgns.BalanceOf(a2u(caller))
} else {
balance = common.BalanceOf(condition.tokenPath, caller)
}
if balance < condition.minAmount {
panic(addDetailToError(
errNotEnoughBalance,
ufmt.Sprintf("insufficient balance(%d) for token(%s)", balance, condition.tokenPath),
))
}
}
}

// Deprecated: use isProjectActive instead
func checkProjectActive(project Project) bool {
if project.started.height > uint64(std.GetHeight()) {
// not started yet
return false
}

if project.ended.height < uint64(std.GetHeight()) {
// already ended
return false
}

return true
}

// Deprecated: use isTierActive instead
func checkTierActive(project Project, tier Tier) bool {
if tier.ended.height < uint64(std.GetHeight()) {
return false
}

return true
}
Copy link
Member

Choose a reason for hiding this comment

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

remove?

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines +18 to +28
// depositId -> deposit
deposits = make(map[string]Deposit)

// proejct -> tier -> []depositId
depositsByProject = make(map[string]map[string][]string)

// user -> []depositId
depositsByUser = make(map[std.Address][]string)

// user -> project -> []depositId
depositsByUserByProject = make(map[std.Address]map[string][]string)
Copy link
Member

Choose a reason for hiding this comment

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

avl

Copy link
Member Author

@notJoon notJoon Jan 8, 2025

Choose a reason for hiding this comment

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

The launchpad's map type wasn't modified to AVL since the values used inside the existing map type are primitive types. (This answer applies to all similar comments below)

Copy link
Member

Choose a reason for hiding this comment

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

@onlyhyde Are we keeping primitive maps?

Comment on lines +33 to +36
Deposits map[string]Deposit
DepositsByProject map[string]map[string][]string
DepositsByUser map[std.Address][]string
DepositsByUserProject map[std.Address]map[string][]string
Copy link
Member

Choose a reason for hiding this comment

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

avl

launchpad/deposit.gno Outdated Show resolved Hide resolved
launchpad/launchpad.gno Outdated Show resolved Hide resolved
launchpad/launchpad.gno Outdated Show resolved Hide resolved
launchpad/launchpad.gno Outdated Show resolved Hide resolved
launchpad/launchpad.gno Outdated Show resolved Hide resolved
launchpad/launchpad.gno Outdated Show resolved Hide resolved
@@ -30,7 +30,7 @@ func TestCreateProjectSingleRecipient(t *testing.T) {
testCreateProject(t)
testMockProtocolFee(t)
testDepositGnsToTier30(t)
testCollectProtocolFee(t)
// testCollectProtocolFee(t)
Copy link
Member

Choose a reason for hiding this comment

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

reason for commenting?

launchpad/reward.gno Outdated Show resolved Hide resolved
r3v4s
r3v4s previously approved these changes Jan 6, 2025
@r3v4s r3v4s self-requested a review January 6, 2025 06:31
Copy link
Member

@r3v4s r3v4s left a comment

Choose a reason for hiding this comment

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

Left few comments.

@onlyhyde onlyhyde force-pushed the refactor-launchpad branch from 189add5 to 5504b2e Compare January 8, 2025 08:17
Copy link

sonarqubecloud bot commented Jan 9, 2025

)

func currentBalance() uint64 {
func getCurrentBalance() uint64 {
// TODO: implement this after checking gns distribution is working
Copy link
Member

Choose a reason for hiding this comment

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

handle TODO

}

/// XXXX
Copy link
Member

Choose a reason for hiding this comment

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

what is this

if currentHeight > self.EndHeight {
currentHeight = self.EndHeight
}

// ????? height 차이에 의한 오차 고
Copy link
Member

Choose a reason for hiding this comment

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

handle this

return u256.Zero().Rsh(self.PriceAccumulation, 128).Uint64()
}


// amount MUST be less than or equal to the amount of xGNS staked
// This function does not check it
Copy link
Member

Choose a reason for hiding this comment

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

if this function doesn't check, then which function verify it??

@@ -32,11 +33,27 @@ func currentBalance() uint64 {
return currentGNSBalance
}

func getCurrentProtocolFeeBalance() map[string]uint64 {
gotAccuProtocolFee := pf.GetAccuTransferToGovStaker()
Copy link
Member

Choose a reason for hiding this comment

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

maybe missing pf.DistributeProtocolFee() ??
or remove this function and uncomment L#23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants