feat(network): implement GetFilteredDelegationsAndRewardAccounts query (#552)
Signed-off-by: Santiago Carmuega <santiago@carmuega.me> Co-authored-by: Santiago Carmuega <santiago@carmuega.me>
This commit is contained in:
parent
5c3a7ccf19
commit
226a454ec6
5 changed files with 249 additions and 99 deletions
|
|
@ -202,6 +202,7 @@ impl GenericClient {
|
|||
{
|
||||
let request = AnyCbor::from_encode(request);
|
||||
let response = self.query_any(request).await?;
|
||||
|
||||
response.into_decode().map_err(ClientError::InvalidCbor)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ impl<'b> Decode<'b, ()> for BlockQuery {
|
|||
// 7 => Ok(Self::GetUTxOWhole),
|
||||
// 8 => Ok(Self::DebugEpochState),
|
||||
9 => Ok(Self::GetCBOR(d.decode()?)),
|
||||
// 10 => Ok(Self::GetFilteredDelegationsAndRewardAccounts(())),
|
||||
10 => Ok(Self::GetFilteredDelegationsAndRewardAccounts(d.decode()?)),
|
||||
11 => Ok(Self::GetGenesisConfig),
|
||||
// 12 => Ok(Self::DebugNewEpochState),
|
||||
13 => Ok(Self::DebugChainDepState),
|
||||
|
|
@ -369,3 +369,29 @@ impl<C> minicbor::encode::Encode<C> for TransactionOutput {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, C> minicbor::decode::Decode<'b, C> for FilteredDelegsRewards {
|
||||
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
|
||||
d.array()?;
|
||||
d.array()?;
|
||||
Ok(FilteredDelegsRewards {
|
||||
delegs: d.decode_with(ctx)?,
|
||||
rewards: d.decode_with(ctx)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> minicbor::encode::Encode<C> for FilteredDelegsRewards {
|
||||
fn encode<W: minicbor::encode::Write>(
|
||||
&self,
|
||||
e: &mut minicbor::Encoder<W>,
|
||||
ctx: &mut C,
|
||||
) -> Result<(), minicbor::encode::Error<W::Error>> {
|
||||
e.array(1)?;
|
||||
e.array(2)?;
|
||||
e.encode_with(self.delegs.clone(), ctx)?;
|
||||
e.encode_with(self.rewards.clone(), ctx)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ pub enum BlockQuery {
|
|||
GetUTxOWhole,
|
||||
DebugEpochState,
|
||||
GetCBOR(Box<BlockQuery>),
|
||||
GetFilteredDelegationsAndRewardAccounts(AnyCbor),
|
||||
GetFilteredDelegationsAndRewardAccounts(StakeAddrs),
|
||||
GetGenesisConfig,
|
||||
DebugNewEpochState,
|
||||
DebugChainDepState,
|
||||
|
|
@ -220,6 +220,30 @@ pub type Addr = Bytes;
|
|||
|
||||
pub type Addrs = Vec<Addr>;
|
||||
|
||||
#[derive(Debug, Encode, Decode, PartialEq, Eq, Clone, PartialOrd, Ord)]
|
||||
pub struct StakeAddr {
|
||||
#[n(0)]
|
||||
addr_type: u8,
|
||||
#[n(1)]
|
||||
payload: Addr,
|
||||
}
|
||||
|
||||
impl From<(u8, Bytes)> for StakeAddr {
|
||||
fn from((addr_type, payload): (u8, Bytes)) -> Self {
|
||||
Self { addr_type, payload }
|
||||
}
|
||||
}
|
||||
|
||||
pub type StakeAddrs = BTreeSet<StakeAddr>;
|
||||
pub type Delegations = KeyValuePairs<StakeAddr, Bytes>;
|
||||
pub type RewardAccounts = KeyValuePairs<StakeAddr, u64>;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct FilteredDelegsRewards {
|
||||
pub delegs: Delegations,
|
||||
pub rewards: RewardAccounts,
|
||||
}
|
||||
|
||||
pub type Pools = BTreeSet<Bytes>;
|
||||
|
||||
pub type Coin = AnyUInt;
|
||||
|
|
@ -494,6 +518,20 @@ pub async fn get_genesis_config(
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
/// Get the delegations and rewards for the given stake addresses.
|
||||
pub async fn get_filtered_delegations_rewards(
|
||||
client: &mut Client,
|
||||
era: u16,
|
||||
addrs: StakeAddrs,
|
||||
) -> Result<FilteredDelegsRewards, ClientError> {
|
||||
let query = BlockQuery::GetFilteredDelegationsAndRewardAccounts(addrs);
|
||||
let query = LedgerQuery::BlockQuery(era, query);
|
||||
let query = Request::LedgerQuery(query);
|
||||
let result = client.query(query).await?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Get a subset of the UTxO, filtered by transaction input.
|
||||
pub async fn get_utxo_by_txin(
|
||||
client: &mut Client,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue