-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44231d6
commit 1dd9131
Showing
7 changed files
with
168 additions
and
154 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,145 @@ | ||
use self::pool::AllocateOrDeallocate; | ||
use super::*; | ||
|
||
pub trait AllocateType<E> | ||
pub trait AllocateType<P, E> | ||
where | ||
P: PoolType, | ||
E: Send + 'static, | ||
{ | ||
// TODO: This should probably be how we do it, but this generic `P` gets | ||
// annoying fn change_allocation_amount(&mut self, event: E) -> | ||
// Option<P::AllocationData>; | ||
fn change_allocation_amount(&mut self, event: E) -> Option<Vec<eI256>>; | ||
fn change_allocation_amount( | ||
&mut self, | ||
event: E, | ||
) -> Option<(AllocateOrDeallocate, P::AllocationData)>; | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
struct Allocate<A, E, S> | ||
struct Allocate<S, A, P, E> | ||
where | ||
A: AllocateType<E>, | ||
E: Send + 'static, | ||
S: State, | ||
A: AllocateType<P, E>, | ||
P: PoolType, | ||
E: Send + 'static, | ||
{ | ||
pub data: S::Data, | ||
pub allocate_type: A, | ||
_phantom_a: PhantomData<A>, | ||
_phantom_e: PhantomData<E>, | ||
_phantom_p: PhantomData<P>, | ||
} | ||
|
||
// TODO: This is actually the exact same as the `swap::Config` so... maybe they | ||
// can be combined. | ||
#[derive(Debug, Serialize, Deserialize, State)] | ||
pub struct Config<P: PoolType> { | ||
pub allocation_data: P::AllocationData, | ||
pub token_admin: String, | ||
pub _phantom: PhantomData<P>, | ||
} | ||
|
||
#[derive(State)] | ||
pub struct Processing<P, E> | ||
where | ||
P: PoolType, | ||
E: Send + 'static, | ||
{ | ||
pub pool: Pool<P>, | ||
pub client: Arc<ArbiterMiddleware>, | ||
pub messager: Messager, | ||
_phantom: PhantomData<E>, | ||
} | ||
|
||
#[allow(unused_variables)] | ||
// TODO: This start up is also very much the same as the `Swap` start up. More | ||
// than likely, `Update`, `Swap`, and `Allocate` can all be combined into one | ||
// type of behavior like a `Interact` behavior that just specializes to do | ||
// different things. | ||
#[async_trait::async_trait] | ||
impl<A, P, E> Behavior<E> for Allocate<A, E, Config<P>> | ||
impl<A, P, E> Behavior<E> for Allocate<Config<P>, A, P, E> | ||
where | ||
A: AllocateType<E> + Send, | ||
P: PoolType + Send, | ||
A: AllocateType<P, E> + Send, | ||
P: PoolType + Send + Sync, | ||
E: Send + 'static, | ||
{ | ||
type Processor = Allocate<A, E, Processing<P, E>>; | ||
type Processor = Allocate<PoolProcessing<P>, A, P, E>; | ||
async fn startup( | ||
mut self, | ||
client: Arc<ArbiterMiddleware>, | ||
messager: Messager, | ||
mut messager: Messager, | ||
) -> Result<Self::Processor> { | ||
todo!(); | ||
// TODO: Here we probably need to filter on the `PoolCreation` so that we get | ||
// the correct pool. | ||
let completed_todo = GetPoolTodo::<P>::complete(&mut messager).await; | ||
let (deployment_data, pool_creation) = ( | ||
completed_todo.deployment_data.unwrap(), | ||
completed_todo.pool_creation.unwrap(), | ||
); | ||
|
||
let (strategy_contract, solver_contract) = | ||
P::get_contracts(&deployment_data, client.clone()); | ||
let dfmm = DFMM::new(deployment_data.dfmm, client.clone()); | ||
|
||
// TODO: This sort of approval and token loop is also repeated in other places | ||
// like `Swap` and `Allocate`. | ||
// Get the intended tokens for the pool and do approvals. | ||
let mut tokens: Vec<ArbiterToken<ArbiterMiddleware>> = Vec::new(); | ||
for token_address in pool_creation.tokens.into_iter() { | ||
let token = ArbiterToken::new(token_address, client.clone()); | ||
let name = token.name().call().await?; | ||
messager | ||
.send( | ||
To::Agent(self.data.token_admin.clone()), | ||
TokenAdminQuery::MintRequest(MintRequest { | ||
token: name, | ||
mint_to: client.address(), | ||
mint_amount: parse_ether(100)?, | ||
}), | ||
) | ||
.await | ||
.unwrap(); | ||
assert_eq!( | ||
messager.get_next::<Response>().await.unwrap().data, | ||
Response::Success | ||
); | ||
token | ||
.approve(dfmm.address(), MAX) | ||
.send() | ||
.await | ||
.unwrap() | ||
.await | ||
.unwrap(); | ||
|
||
tokens.push(token); | ||
} | ||
|
||
// build pool for processor and stream | ||
let pool = Pool::<P> { | ||
id: pool_creation.id, | ||
dfmm, | ||
instance: P::create_instance(strategy_contract, solver_contract, pool_creation.params), | ||
tokens, | ||
liquidity_token: ERC20::new(pool_creation.liquidity_token, client.clone()), | ||
}; | ||
|
||
let processor = Self::Processor { | ||
data: PoolProcessing { | ||
messager, | ||
client, | ||
pool, | ||
}, | ||
allocate_type: self.allocate_type, | ||
_phantom_e: PhantomData, | ||
_phantom_p: PhantomData, | ||
}; | ||
|
||
Ok(processor) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<A, P, E> Processor<E> for Allocate<A, E, Processing<P, E>> | ||
impl<A, P, E> Processor<E> for Allocate<PoolProcessing<P>, A, P, E> | ||
where | ||
A: AllocateType<E> + Send, | ||
P: PoolType + Send, | ||
A: AllocateType<P, E> + Send, | ||
P: PoolType + Send + Sync, | ||
E: Send + 'static, | ||
{ | ||
async fn get_stream(&mut self) -> Result<Option<EventStream<E>>> { | ||
todo!("We have not implemented the 'get_stream' method yet for the 'Allocate' behavior."); | ||
default async fn get_stream(&mut self) -> Result<Option<EventStream<E>>> { | ||
Ok(None) | ||
} | ||
async fn process(&mut self, _event: E) -> Result<ControlFlow> { | ||
Ok(ControlFlow::Halt) | ||
default async fn process(&mut self, event: E) -> Result<ControlFlow> { | ||
if let Some((allocate_or_deallocate, allocation_data)) = | ||
self.allocate_type.change_allocation_amount(event) | ||
{ | ||
self.data | ||
.pool | ||
.change_allocation(allocate_or_deallocate, allocation_data) | ||
.await?; | ||
} | ||
|
||
Ok(ControlFlow::Continue) | ||
} | ||
} |
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.