Skip to content
Open
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
45 changes: 36 additions & 9 deletions contracts/ethereal-delta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ mod delta {
withdraw => restrict_to: [alpha];
prove_delta => restrict_to: [alpha];
set_dao_addr => restrict_to: [zero];
// from treasury to aa treasury
t_to_aat_some => restrict_to: [alpha]; // trans some or trans all
t_to_aat_all => restrict_to: [alpha]; // trans all
}
}

Expand All @@ -35,11 +38,9 @@ mod delta {
impl Delta {
pub fn from_nothing(dao_addr: ComponentAddress, power_zero: ResourceAddress,
power_alpha: ResourceAddress, power_delta: Bucket,
real:Bucket, euxlp: ResourceAddress, bang: ComponentAddress
real: Bucket, euxlp: ResourceAddress, bang: ComponentAddress
) -> ComponentAddress {
// needs to whitelist
// real, tlp, euxlp, exrd, xrd, eusd


let aa_treasury = (Vault::with_bucket(real), Vault::new(euxlp));

let treasury = KeyValueStore::new();
Expand Down Expand Up @@ -88,7 +89,7 @@ mod delta {
v.deref_mut().put(input);
return
};
self.treasury.insert(input.resource_address(), Vault::with_bucket(input))
self.treasury.insert(input.resource_address(), Vault::with_bucket(input));
}

pub fn withdraw(&mut self, resource: ResourceAddress, amount: Decimal) -> Bucket {
Expand All @@ -109,7 +110,7 @@ mod delta {
pub fn aa_tap(&mut self) -> (Option<Bucket>, Option<Bucket>) {
info!("aa_top IN");

// honestly it pulling all at once is a hack to add miaximum possible size
// honestly it pulling all at once is a hack to add maximum possible size
// without doing any calculation
(
if self.aa_treasury.0.is_empty() { None } else { Some(self.aa_treasury.0.take_all()) },
Expand All @@ -130,7 +131,7 @@ mod delta {
}
}

// pupeteer delta by alpha
// puppeteer delta by alpha
pub fn prove_delta(&self) -> FungibleProof {
self.power_delta.as_fungible().create_proof_of_amount(dec!(1))
}
Expand All @@ -139,6 +140,32 @@ mod delta {
self.dao_addr = new;
}

// todo move from treasury to aa treasury
// from treasury to aa treasury, take some or take all
pub fn t_to_aat_some(&mut self, a_real: Option<Decimal>, a_euxlp: Option<Decimal>) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes this is a nice addition thanks for filling it out

if let Some(mut v) = self.treasury.get_mut(&self.aa_treasury.0.resource_address()) {
if let Some(a) = a_real {
self.aa_treasury.0.put(v.deref_mut().take(a_real));
} else {
self.aa_treasury.0.put(v.deref_mut().take_all());
}
}
if let Some(mut v) = self.treasury.get_mut(&self.aa_treasury.1.resource_address()) {
if let Some(a) = a_euxlp {
self.aa_treasury.1.put(v.deref_mut().take(a_real));
} else {
self.aa_treasury.1.put(v.deref_mut().take_all());
}
}
}

// from treasury to aa treasury, take all
pub fn t_to_aat_all(&mut self) {
if let Some(mut v) = self.treasury.get_mut(&self.aa_treasury.0.resource_address()) {
self.aa_treasury.0.put(v.deref_mut().take_all());
}
if let Some(mut v) = self.treasury.get_mut(&self.aa_treasury.1.resource_address()) {
self.aa_treasury.1.put(v.deref_mut().take_all());
}
}
}
}
}
54 changes: 37 additions & 17 deletions contracts/ethereal-dex-eux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@ mod eux {
let in2 = b2.amount();
let pool1 = self.pool.0.amount();
let pool2 = self.pool.1.amount();
let pool_ratio = pool1 / pool2;

if (pool1 / pool2) < (in1 / in2) {
let in1new = in2 * pool1 / pool2;
let minted = self.pool_lp.1 * in1new / pool1;
if pool_ratio < (in1 / in2) {
let in1new = in2 * pool_ratio;
let minted = (self.pool_lp.1 / pool1) * in1new;

self.pool_lp.1 += minted;

Expand All @@ -182,9 +183,9 @@ mod eux {
Some(b1)
)

} else if (pool1 / pool2) > (in1 / in2) {
let in2new = in1 * pool2 / pool1;
let minted = self.pool_lp.1 * in2new / pool2;
} else if pool_ratio > (in1 / in2) {
let in2new = in1 * 1 / pool_ratio;
let minted = (self.pool_lp.1 / pool2) * in2new;

self.pool_lp.1 += minted;

Expand All @@ -198,7 +199,7 @@ mod eux {
)

} else {
let minted = self.pool_lp.1 * in1 / pool1;
let minted = (self.pool_lp.1 / pool1) * in1;
self.pool_lp.1 += minted;

self.pool.0.put(b1);
Expand All @@ -211,7 +212,7 @@ mod eux {
)
}
}

pub fn remove_liquidity(&mut self, input: Bucket) -> (Bucket, Bucket) {
assert!( !self.stopped && !self.power_eux.is_empty(),
"DEX stopped or empty");
Expand All @@ -236,14 +237,14 @@ mod eux {
let ra_in = input.resource_address();

if ra_in == self.pool.0.resource_address() {
let size_out = (size_in * self.pool.1.amount())
/ (size_in + self.pool.0.amount());
let size_out = size_in * (self.pool.1.amount()
/ (size_in + self.pool.0.amount()));

self.pool.0.put(input);
self.pool.1.take(size_out)
} else { // no need to check, will err on wrong ra
let size_out = (size_in * self.pool.0.amount())
/ (size_in + self.pool.1.amount());
let size_out = size_in * (self.pool.0.amount()
/ (size_in + self.pool.1.amount()));

self.pool.1.put(input);
self.pool.0.take(size_out)
Expand Down Expand Up @@ -418,9 +419,19 @@ mod eux {

// AUXILIARY (for interop)

// how many to input to get a set number on output?
pub fn in_given_out(&self, _output: Decimal, _resource_in: ResourceAddress) { // -> Decimal {
// how many to input to get a set number on output?
pub fn in_given_out(&self, output: Decimal, resource_in: ResourceAddress) -> Decimal {
if resource_in == self.pool.0.resource_address(){
// assert!(output < self.pool.1.amount(),'output amount error'); //to avoid negative/NaNs outcome?

(output * (self.pool.0.amount()
/ (self.pool.1.amount() - output))) / self.swap_fee
} else { // no need to check, will err on wrong ra
// assert!(output < self.pool.0.amount(),'output amount error'); //to avoid negative/NaNs outcome?

(output * (self.pool.1.amount()
/ (self.pool.0.amount() - output))) / self.swap_fee
}
}

// how many to input to push it to target price?
Expand Down Expand Up @@ -454,7 +465,6 @@ mod eux {
(self.pool.0.amount(), self.pool.1.amount())
}


// lookup spot price between the assets
// EUSD / EXRD
pub fn spot_price(&self) -> Decimal {
Expand All @@ -463,8 +473,18 @@ mod eux {
}

// simulated swap, returns the amount that will be returned with a regular swap
pub fn sim_swap(&self, _input: Decimal, _resource_in: ResourceAddress) { // -> Decimal {
pub fn sim_swap(&self, input: Decimal, resource_in: ResourceAddress) -> Decimal {
let size_in = input * self.swap_fee;

if resource_in == self.pool.0.resource_address() {

size_in * (self.pool.1.amount()
/ (size_in + self.pool.0.amount()))
} else { // no need to check, will err on wrong ra

size_in * (self.pool.0.amount()
/ (size_in + self.pool.1.amount()))
}
}
}
}
}
104 changes: 81 additions & 23 deletions contracts/ethereal-dex-tri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ mod tri {
let size_in = input.amount() * self.swap_fee;
let ra_in = input.resource_address();

// block transferred to 'swap_size' method to share swap size calc with 'sim_swap' method
let (ra_out, size_out) = self.swap_size(ra_in, size_in);

self.power_tri.as_fungible().authorize_with_amount(dec!(1), || {
pool.protected_deposit(input);
pool.protected_withdraw(ra_out, size_out,
WithdrawStrategy::Rounded(RoundingMode::ToZero))
})
}

// AUXILIARY (for interop)

// how many to input to get a set number on output?
pub fn in_given_out(&self, size_out: Decimal, ra_in: ResourceAddress) -> Decimal {
let reserves = self.vault_reserves();

let (ra_out, w_out) = if ra_in == self.resources.0.0 {
Expand All @@ -225,30 +239,49 @@ mod tri {
let reserves_out = reserves.get(&ra_out).expect("coherence error");
let reserves_in = reserves.get(&ra_in).expect("coherence error");

let size_out =
*reserves_out * (dec!("1") -
(*reserves_in / (*reserves_in + size_in))
.pow((dec!("1") - w_out) / w_out).expect("power incoherence")
);

self.power_tri.as_fungible().authorize_with_amount(dec!(1), || {
pool.protected_deposit(input);
pool.protected_withdraw(ra_out, size_out,
WithdrawStrategy::Rounded(RoundingMode::ToZero))
})
}

// AUXILIARY (for interop)

// how many to input to get a set number on output?
pub fn in_given_out(&self, _output: Decimal, _resource_in: ResourceAddress) { // -> Decimal {

(*reserves_in * (
(*reserves_out / (*reserves_out - size_out))
.pow(w_out / (dec!("1") - w_out)).expect("power incoherence")
- dec!("1"))
) / self.swap_fee
}

// how many to input to push it to target price?
// returns None, if target < spot
pub fn in_given_price(&self, _target: Decimal, _resource_in: ResourceAddress) { // -> Option<Decimal> {
pub fn in_given_price(&self, target: Decimal, ra_in: ResourceAddress) -> Option<Decimal> {
let reserves = self.vault_reserves();

let (spot_price,w_out) = if ra_in == self.resources.0.0 {
(
((*reserves.get(&self.resources.0.0).expect("incoherence") / self.resources.0.1)
/
(*reserves.get(&self.resources.1.0).expect("incoherence") / self.resources.1.1)),
self.resources.1.1
)
} else if ra_in == self.resources.1.0 {
(
((*reserves.get(&self.resources.1.0).expect("incoherence") / self.resources.1.1)
/
(*reserves.get(&self.resources.0.0).expect("incoherence") / self.resources.0.1)),
self.resources.0.1
)
} else {
panic!("wrong resource input")
};

let reserves_in = reserves.get(&ra_in).expect("coherence error");

if target > spot_price {
Some(
(*reserves_in * (
(target / spot_price)
.pow(w_out).expect("power incoherence")
- dec!("1")))
/ self.swap_fee
)
} else {
None
}
}

// dumps current # of in each bucket
Expand All @@ -257,8 +290,7 @@ mod tri {

pool.get_vault_amounts()
}



// lookup spot price between the assets
// todo check if it's REAL/EUXLP or the other way round
pub fn spot_price(&self) -> Decimal {
Expand All @@ -272,8 +304,34 @@ mod tri {
}

// simulated swap, returns the amount that will be returned with a regular swap
pub fn sim_swap(&self, _input: Decimal, _resource_in: ResourceAddress) { // -> Decimal {
pub fn sim_swap(&mut self, input: Decimal, resource_in: ResourceAddress) -> Decimal {
let (_ra_out, size_out) = self.swap_size(resource_in, input * self.swap_fee);

size_out
}

// 'out_given_in' and 'sim_swap' methods swap size calculation
fn swap_size(&mut self, ra_in: ResourceAddress, size_in: Decimal) -> (ResourceAddress,Decimal) {
let reserves = self.vault_reserves();

let (ra_out, w_out) = if ra_in == self.resources.0.0 {
self.resources.1
} else if ra_in == self.resources.1.0 {
self.resources.0
} else {
panic!("wrong resource input")
};

let reserves_out = reserves.get(&ra_out).expect("coherence error");
let reserves_in = reserves.get(&ra_in).expect("coherence error");

(
ra_out,
*reserves_out * (dec!("1") -
(*reserves_in / (*reserves_in + size_in))
.pow((dec!("1") - w_out) / w_out).expect("power incoherence")
)
)
}
}
}
}