Skip to content

Commit

Permalink
update sender to grant on daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
Buckram123 committed Jan 17, 2024
1 parent ad5af98 commit 7b4412b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace.dependencies]

cw-orch = "0.19.1"

[patch.crates-io]
abstract-adapter = { git = "https://github.com/abstractsdk/abstract", branch = "cw-orch-authz/patch" }
Expand Down
5 changes: 2 additions & 3 deletions savings-bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cw-orch = { version = "0.19.1", features = [
"daemon",
], git = "https://github.com/AbstractSDK/cw-orchestrator", branch = "feature/add-authz-wrapping" }
cw-orch = { workspace = true, features = ["daemon"] }
abstract-app = { git = "https://github.com/abstractsdk/abstract", branch = "cw-orch-authz/patch" }
osmosis-std = { version = "0.21.0" }
cosmos-sdk-proto = { version = "0.20.0" }
dotenv = "0.15.0"
Expand Down
54 changes: 42 additions & 12 deletions savings-bot/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use abstract_app::abstract_core::app::{AppConfigResponse, BaseQueryMsg};
use cosmos_sdk_proto::cosmwasm::wasm::v1::{
query_client::QueryClient, QueryContractsByCodeRequest,
};
use cw_orch::daemon::sender::Sender;
use cw_orch::daemon::sender::{Sender, SenderOptions};
use cw_orch::daemon::Wallet;
use cw_orch::prelude::*;
use cw_orch::state::ChainState;
use cw_orch::{
anyhow,
daemon::{networks::OSMO_5, Daemon},
Expand All @@ -13,6 +16,8 @@ use dotenv::dotenv;
use log::{log, Level};
use tonic::transport::Channel;

use app::msg::{AppExecuteMsg, AppQueryMsg, AvailableRewardsResponse, ExecuteMsg, QueryMsg};

async fn fetch_contracts(channel: Channel, code_id: u64) -> anyhow::Result<Vec<String>> {
let mut cw_querier = QueryClient::new(channel);
let contract_addrs = cw_querier
Expand All @@ -28,21 +33,39 @@ async fn fetch_contracts(channel: Channel, code_id: u64) -> anyhow::Result<Vec<S
anyhow::Ok(contract_addrs)
}

fn autocompound(daemon: &Daemon, contract_addrs: Vec<String>) -> anyhow::Result<()> {
fn update_sender_to_grant(daemon: &mut Daemon, contract_addr: &Addr) -> anyhow::Result<()> {
// Get config of an app to get proxy address(granter)
let app_config: AppConfigResponse =
daemon.query(&QueryMsg::Base(BaseQueryMsg::BaseConfig {}), &contract_addr)?;

Check failure on line 39 in savings-bot/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> savings-bot/src/main.rs:39:68 | 39 | daemon.query(&QueryMsg::Base(BaseQueryMsg::BaseConfig {}), &contract_addr)?; | ^^^^^^^^^^^^^^ help: change this to: `contract_addr` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_borrow)]`

// TODO: check if grant is indeed given
let authz_granter = app_config.proxy_address;
daemon.set_sender(Wallet::new(Sender::new_with_options(
&daemon.state(),
sender_options_constructor(authz_granter.to_string()),
)?));
Ok(())
}

fn autocompound(daemon: &mut Daemon, contract_addrs: Vec<String>) -> anyhow::Result<()> {
for contract in contract_addrs {
let addr = Addr::unchecked(contract);
use app::msg::*;
// TODO: Should look into different query to see the cooldown
let available_rewards: AvailableRewardsResponse =
daemon.query(&QueryMsg::from(AppQueryMsg::AvailableRewards {}), &addr)?;
daemon.query(&QueryMsg::from(AppQueryMsg::AvailableRewards {}), &addr)?;
// If not empty - autocompound
if !available_rewards.available_rewards.is_empty() {
// TODO: Daemon set authZ
daemon.execute(
&ExecuteMsg::from(AppExecuteMsg::Autocompound {}),
&[],
&addr,
)?;
// Update sender on daemon to use grant of contract
let sender_update_result = update_sender_to_grant(daemon, &addr);

// Execute autocompound, if we have grant(s)
if sender_update_result.is_ok() {
daemon.execute(
&ExecuteMsg::from(AppExecuteMsg::Autocompound {}),
&[],
&addr,
)?;
}
}
}

Expand All @@ -56,7 +79,7 @@ fn main() -> anyhow::Result<()> {
env_logger::init();

let rt = Runtime::new()?;
let daemon = Daemon::builder()
let mut daemon = Daemon::builder()
.handle(rt.handle())
.chain(OSMO_5)
.build()?;
Expand All @@ -68,6 +91,13 @@ fn main() -> anyhow::Result<()> {
.block_on(fetch_contracts(daemon.channel(), code_id))?;

// Autocompound
autocompound(&daemon, contract_addrs)?;
autocompound(&mut daemon, contract_addrs)?;
Ok(())
}

// TODO: remove when constructor to SenderOptions added
fn sender_options_constructor(granter: String) -> SenderOptions {
let mut sender_options = SenderOptions::default();
sender_options.authz_granter = Some(granter);
sender_options
}

0 comments on commit 7b4412b

Please sign in to comment.