Skip to content

Commit 4068628

Browse files
authored
Merge pull request #97 from MisoShiru69/main
Indexer
2 parents 1443dd6 + 1f62deb commit 4068628

File tree

16 files changed

+1257
-0
lines changed

16 files changed

+1257
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "indexer"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
9+
10+
[dev-dependencies]
11+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
12+
13+
[profile.release]
14+
opt-level = 's' # Optimize for size.
15+
lto = true # Enable Link Time Optimization.
16+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
17+
panic = 'abort' # Abort on panic.
18+
strip = "debuginfo" # Strip debug info.
19+
20+
[lib]
21+
crate-type = ["cdylib", "lib"]

4-portfolio-management-yield-farming/indexer/README.md

Lines changed: 282 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use scrypto::prelude::*;
2+
3+
blueprint! {
4+
struct IndexPool {
5+
6+
//index pool vault that holds tokens
7+
index_pool:Vault
8+
}
9+
10+
impl IndexPool {
11+
12+
pub fn new(
13+
resource_address:ResourceAddress
14+
) -> ComponentAddress {
15+
let component = Self {
16+
index_pool:Vault::new(resource_address)
17+
}
18+
.instantiate()
19+
.globalize();
20+
return component;
21+
}
22+
23+
//Deposit tokens into index pool vault
24+
pub fn deposit(&mut self, tokens:Bucket){
25+
self.index_pool.put(tokens);
26+
}
27+
28+
//Get blance of index pool vault
29+
pub fn balance(&self) -> Decimal {
30+
return self.index_pool.amount();
31+
}
32+
33+
//Withdraws tokens from index pool vault
34+
pub fn withdraw(&mut self, amount:Decimal) -> Bucket {
35+
return self.index_pool.take(amount);
36+
}
37+
38+
pub fn take_all(&mut self) -> Bucket {
39+
return self.index_pool.take_all();
40+
}
41+
42+
}
43+
}

4-portfolio-management-yield-farming/indexer/src/indexer.rs

Lines changed: 432 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod oracle1;
2+
mod oracle2;
3+
mod indexer;
4+
mod index_pool;
5+
mod ociswap;
6+
mod oci_pools;
7+
mod radex;
8+
mod radex_pools;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use scrypto::prelude::*;
2+
3+
blueprint! {
4+
5+
struct OciPool {
6+
7+
//Vault to hold liquidity for trading
8+
oci_pool:Vault
9+
}
10+
11+
impl OciPool {
12+
13+
pub fn new(
14+
resource_address:ResourceAddress
15+
) -> ComponentAddress {
16+
let component = Self {
17+
oci_pool:Vault::new(resource_address)
18+
}
19+
.instantiate()
20+
.globalize();
21+
return component;
22+
23+
}
24+
25+
//Method to deposit funds into Ociswap pools
26+
pub fn deposit(&mut self, tokens:Bucket){
27+
self.oci_pool.put(tokens);
28+
}
29+
30+
//Method to withdraw funds from Ociswap pools
31+
pub fn withdraw(&mut self, token_amount:Decimal)->Bucket{
32+
return self.oci_pool.take(token_amount);
33+
}
34+
35+
//Method to get balance
36+
pub fn balance(&self) -> Decimal {
37+
return self.oci_pool.amount();
38+
}
39+
}
40+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use scrypto::prelude::*;
2+
use crate::oracle1::*;
3+
use crate::oci_pools::*;
4+
5+
blueprint! {
6+
struct Ociswap {
7+
8+
//Hashmap to store resource address to token vaults
9+
oci_pool:HashMap<ResourceAddress, OciPool>,
10+
11+
//This stores the ocracle component address
12+
oracle1_address:Option<ComponentAddress>,
13+
}
14+
15+
impl Ociswap {
16+
17+
pub fn new() -> ComponentAddress{
18+
let component = Self {
19+
oci_pool:HashMap::new(),
20+
oracle1_address:None,
21+
}
22+
.instantiate()
23+
.globalize();
24+
return component;
25+
}
26+
27+
//Create a pool of tokens which will be used to trade with on Ociswap
28+
pub fn create_pool(&mut self, token_address:ResourceAddress) {
29+
//Uses the oci pool blueprint to create an ociswap pool component
30+
let new_oci_pool:ComponentAddress = OciPool::new(token_address);
31+
32+
//Update hashmap with resource address and oci pool component
33+
self.oci_pool.insert(token_address, new_oci_pool.into());
34+
}
35+
36+
//Add tokens to the created pools
37+
pub fn add_liquidity(&mut self, token:Bucket) {
38+
39+
//Get the tokens resource address
40+
let token_address = token.resource_address();
41+
42+
//Get the component from the oci pool hashmap using the tokens resource address
43+
let token_address:&OciPool = self.oci_pool.get(&token_address).unwrap();
44+
45+
//Deposit tokens into component's vault
46+
token_address.deposit(token);
47+
}
48+
49+
//Remove tokens from liquidity pools
50+
pub fn remove_liquidity(&mut self, token_amount:Decimal, token_address:ResourceAddress)->Bucket {
51+
52+
//Get the component from the oci pool hashmap using the tokens resource address
53+
let token_component:&OciPool = self.oci_pool.get(&token_address).unwrap();
54+
55+
//Withdraw tokens from component's vault
56+
let return_bucket:Bucket = token_component.withdraw(token_amount);
57+
58+
return return_bucket;
59+
}
60+
61+
62+
63+
//Swap tokens by providing a bucket of tokens and the resource address wanted token
64+
pub fn swap(&mut self, token1:Bucket, token2_address:ResourceAddress ) -> Bucket{
65+
66+
//Determine how many tokens are in the bucket
67+
let token1_amount:Decimal = token1.amount();
68+
69+
//Determing the resource address
70+
let token1_address:ResourceAddress = token1.resource_address();
71+
72+
//Get the componet from the oci pool hashmap using the resource address of the deposited token
73+
let oci_pool_address1:&OciPool = self.oci_pool.get(&token1_address).unwrap();
74+
75+
//Deposit the token into the oci pool component
76+
oci_pool_address1.deposit(token1);
77+
78+
//Get the component from the oci pool hashmap using the resource address of the wanted token
79+
let oci_pool_address2:&OciPool = self.oci_pool.get(&token2_address).unwrap();
80+
81+
//Get the Oracle component
82+
let oracle1_component_address:ComponentAddress = self.oracle1_address.unwrap();
83+
let oracle1_compoent:Oracle1 = oracle1_component_address.into();
84+
85+
//Get the price of deposited token
86+
let oracle1_price1:Decimal = oracle1_compoent.get_price(token1_address).into();
87+
info!("[NOTE] The price of {:?} is {} USD",borrow_resource_manager!(token1_address).metadata(), oracle1_price1);
88+
89+
//Get the price of the wanted token
90+
let oracle_price2:Decimal = oracle1_compoent.get_price(token2_address.into());
91+
info!("[NOTE] The price of {:?} is {} USD",borrow_resource_manager!(token2_address).metadata(), oracle_price2);
92+
93+
//Calculate the amount of the wanted token based on the price of the tokens
94+
let return_amount:Decimal = (token1_amount * oracle1_price1) / oracle_price2;
95+
info!("[CALC] {} tokens X ${} per token = ${}",token1_amount, oracle1_price1, (token1_amount * oracle1_price1));
96+
info!("[CALC] ${} / ${} per token = {} tokens",(token1_amount * oracle1_price1), oracle_price2, return_amount);
97+
info!("[FINAL] {} {:?} has been deposited",return_amount, borrow_resource_manager!(token2_address).metadata());
98+
info!("------------------------------------------------------------------------------------");
99+
100+
//Withdraw wanted token from the Ociswap pool component
101+
let return_bucket:Bucket = oci_pool_address2.withdraw(return_amount);
102+
103+
//Returned swapped token to user
104+
return return_bucket;
105+
}
106+
107+
//Assign Oracle component address
108+
pub fn oracle1_address(&mut self, oracle1_address:ComponentAddress){
109+
self.oracle1_address = Some(oracle1_address);
110+
info!("{}", self.oracle1_address.unwrap());
111+
}
112+
113+
//This method shows the balance of the liquidity pools
114+
pub fn show_liquidity_pool(&self){
115+
info!("LIQUIDITY POOL BALANCES");
116+
for (resource_address, component_address) in &self.oci_pool {
117+
let oci_pool_component:&OciPool = component_address;
118+
let balance = oci_pool_component.balance();
119+
info!("TOKEN:[{:?}] *** TOKEN BALANCE: {} ",borrow_resource_manager!(*resource_address).metadata(), balance);
120+
}
121+
}
122+
}
123+
}
124+
125+
126+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use scrypto::prelude::*;
2+
3+
blueprint! {
4+
struct Oracle1 {
5+
6+
//Hashmap used to store token prices
7+
token_price: HashMap<ResourceAddress, Decimal>,
8+
9+
}
10+
11+
impl Oracle1 {
12+
13+
pub fn new() -> ComponentAddress{
14+
15+
let component = Self {
16+
token_price: HashMap::new(),
17+
}
18+
.instantiate()
19+
.globalize();
20+
return component;
21+
}
22+
23+
//Method used to set token prices
24+
pub fn set_price(&mut self, token_address:ResourceAddress, price:Decimal) {
25+
self.token_price.insert(token_address, price);
26+
}
27+
28+
//Method used to get token prices
29+
pub fn get_price(&self, token_address: ResourceAddress) -> Decimal{
30+
let token_price:Decimal = *self.token_price.get(&token_address).unwrap();
31+
return token_price;
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use scrypto::prelude::*;
2+
3+
blueprint! {
4+
struct Oracle2 {
5+
6+
//Hashmap used to store token prices
7+
token_price: HashMap<ResourceAddress, Decimal>,
8+
9+
}
10+
11+
impl Oracle2 {
12+
13+
pub fn new() -> ComponentAddress{
14+
15+
let component = Self {
16+
token_price: HashMap::new(),
17+
}
18+
.instantiate()
19+
.globalize();
20+
return component;
21+
}
22+
23+
//Method used to set token prices
24+
pub fn set_price(&mut self, token_address:ResourceAddress, price:Decimal) {
25+
self.token_price.insert(token_address, price);
26+
}
27+
28+
//Method used to get token prices
29+
pub fn get_price(&self, token_address: ResourceAddress) -> Decimal{
30+
let token_price:Decimal = *self.token_price.get(&token_address).unwrap();
31+
return token_price;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)