astro_assembly/
utils.rs

1use astroport::tokenfactory_tracker;
2use cosmwasm_std::{Deps, QuerierWrapper, StdResult, Uint128};
3
4use astroport_governance::assembly::Config;
5use astroport_governance::assembly::Proposal;
6use astroport_governance::builder_unlock::{
7    AllocationResponse, QueryMsg as BuilderUnlockQueryMsg, State,
8};
9use astroport_governance::voting_escrow;
10use astroport_governance::voting_escrow::LockInfoResponse;
11
12use crate::state::CONFIG;
13
14/// Calculates an address' voting power at the specified block.
15///
16/// * **sender** address whose voting power we calculate.
17///
18/// * **proposal** proposal for which we want to compute the `sender` (voter) voting power.
19pub fn calc_voting_power(deps: Deps, sender: String, proposal: &Proposal) -> StdResult<Uint128> {
20    let config = CONFIG.load(deps.storage)?;
21
22    let xastro_vp: Uint128 = deps.querier.query_wasm_smart(
23        &config.xastro_denom_tracking,
24        &tokenfactory_tracker::QueryMsg::BalanceAt {
25            address: sender.clone(),
26            // Get voting power at the block before the proposal starts
27            timestamp: Some(proposal.start_time - 1),
28        },
29    )?;
30
31    let locked_amount: AllocationResponse = deps.querier.query_wasm_smart(
32        config.builder_unlock_addr,
33        &BuilderUnlockQueryMsg::Allocation {
34            account: sender.clone(),
35            timestamp: Some(proposal.start_time - 1),
36        },
37    )?;
38
39    let vxastro_vp = if let Some(vxastro_contract) = &config.vxastro_contract {
40        deps.querier
41            .query_wasm_smart(
42                vxastro_contract,
43                &voting_escrow::QueryMsg::LockInfo {
44                    user: sender,
45                    timestamp: Some(proposal.start_time - 1),
46                },
47            )
48            .map(|resp: LockInfoResponse| resp.amount)?
49    } else {
50        Uint128::zero()
51    };
52
53    Ok(xastro_vp
54        + (locked_amount.status.amount - locked_amount.status.astro_withdrawn)
55        + vxastro_vp)
56}
57
58/// Calculates the combined total voting power at a specified timestamp (that is relevant for a specific proposal).
59/// Combined voting power includes:
60/// * xASTRO total supply
61/// * ASTRO tokens which are still locked in the builder's unlock contract
62///
63/// ## Parameters
64/// * **config** contract settings.
65/// * **timestamp** timestamp for which we calculate the total voting power.
66pub fn calc_total_voting_power_at(
67    querier: QuerierWrapper,
68    config: &Config,
69    timestamp: u64,
70) -> StdResult<Uint128> {
71    let total: Uint128 = querier.query_wasm_smart(
72        &config.xastro_denom_tracking,
73        &tokenfactory_tracker::QueryMsg::TotalSupplyAt {
74            timestamp: Some(timestamp),
75        },
76    )?;
77
78    // Total amount of ASTRO locked in the initial builder's unlock schedule
79    let builder_state: State = querier.query_wasm_smart(
80        &config.builder_unlock_addr,
81        &BuilderUnlockQueryMsg::State {
82            timestamp: Some(timestamp),
83        },
84    )?;
85
86    Ok(total + builder_state.remaining_astro_tokens)
87}