Skip to content

Commit

Permalink
Add update campaign path
Browse files Browse the repository at this point in the history
  • Loading branch information
af-afk committed Aug 16, 2024
1 parent 41848fe commit bc6a27e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
File renamed without changes.
73 changes: 72 additions & 1 deletion pkg/leo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type CampaignId = FixedBytes<8>;
#[solidity_storage]
#[entrypoint]
pub struct Leo {
version: StorageU8,

enabled: StorageBool,

emergency_council: StorageAddress,
Expand Down Expand Up @@ -112,6 +114,14 @@ pub struct StoragePosition {

#[external]
impl Leo {
pub fn ctor(&mut self, emergency: Address) -> Result<(), Vec<u8>> {
assert!(self.version.get().is_zero());
self.emergency_council.set(emergency);
self.version.set(U8::from(1));
self.enabled.set(true);
Ok(())
}

// Take a user's LP NFT using the NFT Manager, also recording the
// pool they LP'd, including the timestamp when they deposited it
// here. This also serves as the time it was last updated.
Expand Down Expand Up @@ -154,6 +164,8 @@ impl Leo {
starting: u64,
ending: u64,
) -> Result<(), Vec<u8>> {
assert!(self.enabled.get());

// Take the ERC20 from the user for the maximum run of the campaign.
let mut pool_campaigns = self.campaigns.setter(pool);
let mut pool_campaigns_ongoing = pool_campaigns.ongoing.setter(identifier);
Expand Down Expand Up @@ -185,7 +197,7 @@ impl Leo {
campaign_bal.maximum.set(new_maximum);

// Take the token's amounts for the campaign.
erc20::take(pool, new_maximum)?;
erc20::take(pool, extra_max)?;

evm::log(events::CampaignBalanceUpdated {
identifier: identifier.as_slice().try_into().unwrap(),
Expand All @@ -208,6 +220,62 @@ impl Leo {
Ok(())
}

/// Update a campaign by taking the last campaign versions item, and
/// setting the ending timestamp to the current timestamp, then inserting
/// a new record to the campaign versions array with the settings we
/// requested.
pub fn update_campaign(
&mut self,
identifier: CampaignId,
pool: Address,
tick_lower: i32,
tick_upper: i32,
per_second: U256,
extra_max: U256,
starting: u64,
ending: u64,
) -> Result<(), Vec<u8>> {
assert!(self.enabled.get());
assert_eq!(
self.campaign_balances.getter(identifier).owner.get(),
msg::sender()
);
// Push to the campaign versions the new content, setting the previous
// campaign's ending timestamp to the current timestamp.
let ongoing_campaigns = &mut self.campaigns.setter(pool).ongoing;
let mut campaign_versions = ongoing_campaigns.setter(identifier);
let campaign_versions_len = campaign_versions.len();
assert!(campaign_versions_len > 0);
campaign_versions
.setter(campaign_versions_len - 1)
.unwrap()
.ending
.set(U64::from(block::timestamp()));
let mut campaign = campaign_versions.grow();
campaign.tick_lower.set(I32::try_from(tick_lower).unwrap());
campaign.tick_upper.set(I32::try_from(tick_upper).unwrap());
campaign.per_second.set(per_second);
campaign.starting.set(U64::try_from(starting).unwrap());
campaign.ending.set(U64::try_from(ending).unwrap());

if !extra_max.is_zero() {
let mut campaign_bal = self.campaign_balances.setter(identifier);
let existing_maximum = campaign_bal.maximum.get();
let new_maximum = existing_maximum + extra_max;
campaign_bal.maximum.set(new_maximum);

// Take the token's amounts for the campaign.
erc20::take(pool, extra_max)?;

evm::log(events::CampaignBalanceUpdated {
identifier: identifier.as_slice().try_into().unwrap(),
newMaximum: new_maximum,
});
}

Ok(())
}

/// Return campaign details, of the form the lower tick, the upper tick,
/// the amount sent per second in the campaign, the token that's being
/// distributed, and the amount distributed so far, as well as the maximum
Expand Down Expand Up @@ -238,6 +306,7 @@ impl Leo {
// Collect the token rewards paid by Seawater for LP'ing in this
// position, then send to the user.
pub fn collect_pool_rewards(&self, pool: Address, id: U256) -> Result<(u128, u128), Vec<u8>> {
assert!(self.enabled.get());
assert!(self.positions.get(id).owner.get() == msg::sender());
let (amount_0, amount_1) = seawater::collect_yield_single_to(id, pool, msg::sender());
Ok((amount_0, amount_1))
Expand All @@ -257,6 +326,7 @@ impl Leo {
position_id: U256,
campaign_ids: Vec<CampaignId>,
) -> Result<Vec<(Address, U256)>, Vec<u8>> {
assert!(self.enabled.get());
assert!(self.positions.getter(position_id).owner.get() == msg::sender());

// Iterate through every copy of the campaign details until we pass the ending.
Expand Down Expand Up @@ -354,6 +424,7 @@ impl Leo {
// Divest LP positions from this contract, sending them back to the
// original owner.
pub fn divest_positions(&mut self, position_ids: Vec<(Address, U256)>) -> Result<(), Vec<u8>> {
assert!(self.enabled.get());
for (pool, id) in position_ids {
// Check if the user owns this position.
assert!(self.positions.getter(id).owner.get() == msg::sender());
Expand Down
9 changes: 6 additions & 3 deletions pkg/leo/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[cfg(all(test, not(target_arch = "wasm32")))]
mod testing {
use libleo;
use proptest::prelude::*;

use stylus_sdk::{
alloy_primitives::{address, Address, FixedBytes, U256},
Expand All @@ -20,6 +19,8 @@ mod testing {
let expected_starting = block::timestamp();
let expected_ending = expected_starting + 1000;

leo.ctor(Address::ZERO).unwrap();

leo.create_campaign(
CAMPAIGN_ID, // Identifier
POOL, // Pool
Expand Down Expand Up @@ -89,14 +90,16 @@ mod proptesting {

libleo::host::advance_time(secs_in);

leo.ctor(Address::ZERO).unwrap();

leo.create_campaign(
CAMPAIGN_ID, // Identifier
POOL, // Pool
-20, // Tick lower
100, // Tick upper
U256::from(2), // Per second distribution
per_second, // Per second distribution
POOL, // Token to send
U256::from(100), // Starting pool of liquidity
starting_pool, // Starting pool of liquidity
expected_starting, // Starting timestamp
expected_ending, // Ending timestamp
).unwrap();
Expand Down
5 changes: 5 additions & 0 deletions pkg/seawater/src/host_erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ pub fn transfer_to_sender(_token: Address, _amount: U256) -> Result<(), Error> {
)
}

/// Does the same thing as [transfer_to_sender] for testing reasons.
pub fn transfer_to_addr(_addr: Address, _token: Address, _amount: U256) -> Result<(), Error> {
transfer_to_sender(_token, _amount);
}

/// Pretends to give a specific address tokens.
pub fn take_from_to(_token: Address, _recipient: Address, _amount: U256) -> Result<(), Error> {
// TODO
Expand Down
3 changes: 2 additions & 1 deletion pkg/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export \

cargo test --features testing

#node --test --loader tsx ethers-tests/test.ts
#node --test --loader tsx ethers-tests/seawater.ts
#node --test --loader tsx ethers-tests/leo.ts

0 comments on commit bc6a27e

Please sign in to comment.