Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/rewards distribution consumer #104

Merged
merged 46 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
de2da96
Custom / local rewards distribution option / routing
Jan 14, 2025
386c3ec
Rewards distribution routing / plumbing
Jan 15, 2025
75547d9
Rename to RewardInfo for clarity
Jan 16, 2025
c521f28
Rewards distribution first step (fps) acct
Jan 16, 2025
25ec779
Rename to btc delegations to avoid name clashing
Jan 17, 2025
1ec0ccf
Add per delegation distribution map
Jan 17, 2025
5054eb5
Distribution info at new delegation
Jan 17, 2025
f7053e3
Distribution info at delegation unbonding
Jan 17, 2025
6a7fcc7
Add staker's (canonical) addr to delegation info
Jan 19, 2025
19f41ac
Add (canonical) staker index to delegations
Jan 19, 2025
58f0725
Use raw bytes for canonical addr index
Jan 19, 2025
d25d9b2
Withdraw rewards first impl
Jan 20, 2025
b3d6171
Add withdraw rewards message
Jan 20, 2025
647d540
Update schemas
Jan 20, 2025
686e21f
Remove unused dep
Jan 20, 2025
c66f884
Fix / improvement: stakers by fp delegations key
Jan 20, 2025
96be33b
Update lock file
Jan 20, 2025
3c21b70
Improve recipient vs staker addr processing
Jan 20, 2025
d36b94a
Improve doc comment
Jan 20, 2025
fb9ab37
Delegations distribution slashing
Jan 20, 2025
50fb1d7
Fix: Subtract delegation from stake when slashed
Jan 20, 2025
c0e3774
Add pending rewards query
Jan 22, 2025
970e504
Add all pending rewards query
Jan 22, 2025
6d38a81
Fix: All pending rewards continuation context / pagination
Jan 22, 2025
ec743f0
Improve comments
Jan 23, 2025
23fc23d
Replace rewards denom with bonded denom for consistency
Jan 23, 2025
c91580b
Add rewards distribution test
Jan 24, 2025
0d4785d
Workaround the lack of custom messages routing / mocking in mt
Jan 24, 2025
892ec61
Fix: Missing delegations by staker indexing
Jan 24, 2025
54fcf3f
Improve rewards clearing / zeroing logic
Jan 24, 2025
25840d9
Fix: MintRewards multi-test impl
Jan 24, 2025
7b47da0
Fix: Depend on the new MintRewards mt impl
Jan 24, 2025
c0bd7d9
Two delegations over multiple FPs distribution test
Jan 24, 2025
df56166
Remove printouts
Jan 24, 2025
073f457
Add note / FIXME
Jan 24, 2025
e97a815
Fix: Simple straightforward claim rewards impl first
Jan 26, 2025
26c3979
Remove useless / redundant points alignment
Jan 26, 2025
bc49bee
Open up the claim rewards handler for simplicity and ergonomics
Jan 27, 2025
e2fe49f
Refactor distribution info creation
Jan 27, 2025
df8f783
Refactor distribution info update
Jan 27, 2025
d58cbf7
Use update instead of save / load
Jan 27, 2025
0711c85
Rename to DelegationDistribution for clarity
Jan 27, 2025
2497493
Add rewards routing comments
maurolacy Jan 29, 2025
749400c
Add update finality handler / msg to staking contract
Jan 29, 2025
b7e7f7b
Add distribute rewards guard / auth
Jan 29, 2025
2e104f5
Add extract_prefix helper for clarity
Jan 29, 2025
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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions contracts/babylon/schema/babylon-contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
"description": "`fp_distribution` is the list of finality providers and their rewards",
"type": "array",
"items": {
"$ref": "#/definitions/RewardsDistribution"
"$ref": "#/definitions/RewardInfo"
}
}
},
Expand Down Expand Up @@ -369,7 +369,7 @@
},
"additionalProperties": false
},
"RewardsDistribution": {
"RewardInfo": {
"type": "object",
"required": [
"fp_pubkey_hex",
Expand Down
4 changes: 2 additions & 2 deletions contracts/babylon/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"description": "`fp_distribution` is the list of finality providers and their rewards",
"type": "array",
"items": {
"$ref": "#/definitions/RewardsDistribution"
"$ref": "#/definitions/RewardInfo"
}
}
},
Expand Down Expand Up @@ -198,7 +198,7 @@
},
"additionalProperties": false
},
"RewardsDistribution": {
"RewardInfo": {
"type": "object",
"required": [
"fp_pubkey_hex",
Expand Down
32 changes: 21 additions & 11 deletions contracts/babylon/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,33 @@ fn reply_init_finality_callback(
cfg.btc_finality = Some(finality_addr.clone());
Ok::<_, ContractError>(cfg)
})?;
// Set the BTC staking contract address to the BTC finality contract
// Set the BTC finality contract address to the BTC staking contract
let cfg = CONFIG.load(deps.storage)?;
let msg = finality_api::ExecuteMsg::UpdateStaking {
staking: cfg
.btc_staking
.ok_or(ContractError::BtcStakingNotSet {})?
let msg = btc_staking_api::ExecuteMsg::UpdateFinality {
finality: cfg
.btc_finality
.ok_or(ContractError::BtcFinalityNotSet {})?
.to_string(),
};
let wasm_msg = WasmMsg::Execute {
let staking_addr = cfg.btc_staking.ok_or(ContractError::BtcStakingNotSet {})?;
let wasm_msg_1 = WasmMsg::Execute {
contract_addr: staking_addr.to_string(),
msg: to_json_binary(&msg)?,
funds: vec![],
};

// Set the BTC staking contract address to the BTC finality contract
let msg = finality_api::ExecuteMsg::UpdateStaking {
staking: staking_addr.to_string(),
};
let wasm_msg_2 = WasmMsg::Execute {
contract_addr: finality_addr.to_string(),
msg: to_json_binary(&msg)?,
funds: vec![],
};
Ok(Response::new().add_message(wasm_msg))
Ok(Response::new()
.add_message(wasm_msg_1)
.add_message(wasm_msg_2))
}

pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<QueryResponse, ContractError> {
Expand Down Expand Up @@ -330,10 +343,7 @@ pub fn execute(
Ok(Response::new())
}
}
None => {
// TODO: Send payload over the custom IBC channel for distribution
Ok(Response::new())
}
None => Err(ContractError::IbcTransferInfoNotSet {}),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/babylon/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub enum ContractError {
BtcFinalityNotSet {},
#[error("Invalid configuration: {msg}")]
InvalidConfig { msg: String },
#[error("IBC transfer info not set")]
IbcTransferInfoNotSet {},
}

#[derive(Error, Debug, PartialEq)]
Expand Down
10 changes: 2 additions & 8 deletions contracts/babylon/src/msg/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Uint128;
use cosmwasm_std::{Binary, StdError, StdResult};

use babylon_apis::finality_api::Evidence;

use crate::msg::btc_header::BtcHeader;
use babylon_apis::btc_staking_api::RewardInfo;
#[cfg(not(target_arch = "wasm32"))]
use {
crate::msg::btc_header::{BtcHeaderResponse, BtcHeadersResponse},
Expand Down Expand Up @@ -119,16 +119,10 @@ pub enum ExecuteMsg {
/// `SendRewards` is a message sent by the finality contract, to send rewards to Babylon
SendRewards {
/// `fp_distribution` is the list of finality providers and their rewards
fp_distribution: Vec<RewardsDistribution>,
fp_distribution: Vec<RewardInfo>,
},
}

#[cw_serde]
pub struct RewardsDistribution {
pub fp_pubkey_hex: String,
pub reward: Uint128,
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
Expand Down
82 changes: 82 additions & 0 deletions contracts/btc-finality/schema/btc-finality.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"covenant_pks",
"covenant_quorum",
"min_slashing_tx_fee_sat",
"rewards_denom",
"slashing_pk_script",
"slashing_rate"
],
Expand Down Expand Up @@ -74,6 +75,9 @@
"format": "uint64",
"minimum": 0.0
},
"rewards_denom": {
"type": "string"
},
"slashing_pk_script": {
"description": "`slashing_pk_script` is the pk script that the slashed BTC goes to. The pk script is in string format on Bitcoin.",
"type": "string"
Expand Down Expand Up @@ -181,6 +185,60 @@
}
},
"additionalProperties": false
},
{
"description": "`DistributeRewards` is a message sent by the finality contract, to distribute rewards to delegators",
"type": "object",
"required": [
"distribute_rewards"
],
"properties": {
"distribute_rewards": {
"type": "object",
"required": [
"fp_distribution"
],
"properties": {
"fp_distribution": {
"description": "`fp_distribution` is the list of finality providers and their rewards",
"type": "array",
"items": {
"$ref": "#/definitions/RewardInfo"
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "`WithdrawRewards` is a message sent by the staker, or the Babylon contract on behalf of the staker, to withdraw rewards from BTC staking via the given FP.\n\nThe optional `recipient` is the address on the Consumer side to receive the rewards. If not provided, the rewards will be sent to the sender of this message. Only the Babylon contract can set the recipient to a different address than the sender's.",
"type": "object",
"required": [
"withdraw_rewards"
],
"properties": {
"withdraw_rewards": {
"type": "object",
"required": [
"fp_pubkey_hex"
],
"properties": {
"fp_pubkey_hex": {
"type": "string"
},
"recipient": {
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
],
"definitions": {
Expand Down Expand Up @@ -517,6 +575,22 @@
},
"additionalProperties": false
},
"RewardInfo": {
"type": "object",
"required": [
"fp_pubkey_hex",
"reward"
],
"properties": {
"fp_pubkey_hex": {
"type": "string"
},
"reward": {
"$ref": "#/definitions/Uint128"
}
},
"additionalProperties": false
},
"SignatureInfo": {
"description": "SignatureInfo is a BIP-340 signature together with its signer's BIP-340 PK",
"type": "object",
Expand Down Expand Up @@ -553,6 +627,10 @@
},
"additionalProperties": false
},
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
},
"UnbondedBtcDelegation": {
"description": "UnbondedBTCDelegation is sent from Babylon to the Consumer chain upon an early unbonded BTC delegation",
"type": "object",
Expand Down Expand Up @@ -1813,6 +1891,7 @@
"covenant_pks",
"covenant_quorum",
"min_slashing_tx_fee_sat",
"rewards_denom",
"slashing_pk_script",
"slashing_rate"
],
Expand Down Expand Up @@ -1844,6 +1923,9 @@
"format": "uint64",
"minimum": 0.0
},
"rewards_denom": {
"type": "string"
},
"slashing_pk_script": {
"description": "`slashing_pk_script` is the pk script that the slashed BTC goes to. The pk script is in string format on Bitcoin.",
"type": "string"
Expand Down
74 changes: 74 additions & 0 deletions contracts/btc-finality/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,60 @@
}
},
"additionalProperties": false
},
{
"description": "`DistributeRewards` is a message sent by the finality contract, to distribute rewards to delegators",
"type": "object",
"required": [
"distribute_rewards"
],
"properties": {
"distribute_rewards": {
"type": "object",
"required": [
"fp_distribution"
],
"properties": {
"fp_distribution": {
"description": "`fp_distribution` is the list of finality providers and their rewards",
"type": "array",
"items": {
"$ref": "#/definitions/RewardInfo"
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "`WithdrawRewards` is a message sent by the staker, or the Babylon contract on behalf of the staker, to withdraw rewards from BTC staking via the given FP.\n\nThe optional `recipient` is the address on the Consumer side to receive the rewards. If not provided, the rewards will be sent to the sender of this message. Only the Babylon contract can set the recipient to a different address than the sender's.",
"type": "object",
"required": [
"withdraw_rewards"
],
"properties": {
"withdraw_rewards": {
"type": "object",
"required": [
"fp_pubkey_hex"
],
"properties": {
"fp_pubkey_hex": {
"type": "string"
},
"recipient": {
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
],
"definitions": {
Expand Down Expand Up @@ -428,6 +482,22 @@
},
"additionalProperties": false
},
"RewardInfo": {
"type": "object",
"required": [
"fp_pubkey_hex",
"reward"
],
"properties": {
"fp_pubkey_hex": {
"type": "string"
},
"reward": {
"$ref": "#/definitions/Uint128"
}
},
"additionalProperties": false
},
"SignatureInfo": {
"description": "SignatureInfo is a BIP-340 signature together with its signer's BIP-340 PK",
"type": "object",
Expand Down Expand Up @@ -464,6 +534,10 @@
},
"additionalProperties": false
},
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
},
"UnbondedBtcDelegation": {
"description": "UnbondedBTCDelegation is sent from Babylon to the Consumer chain upon an early unbonded BTC delegation",
"type": "object",
Expand Down
4 changes: 4 additions & 0 deletions contracts/btc-finality/schema/raw/instantiate.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"covenant_pks",
"covenant_quorum",
"min_slashing_tx_fee_sat",
"rewards_denom",
"slashing_pk_script",
"slashing_rate"
],
Expand Down Expand Up @@ -70,6 +71,9 @@
"format": "uint64",
"minimum": 0.0
},
"rewards_denom": {
"type": "string"
},
"slashing_pk_script": {
"description": "`slashing_pk_script` is the pk script that the slashed BTC goes to. The pk script is in string format on Bitcoin.",
"type": "string"
Expand Down
4 changes: 4 additions & 0 deletions contracts/btc-finality/schema/raw/response_to_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"covenant_pks",
"covenant_quorum",
"min_slashing_tx_fee_sat",
"rewards_denom",
"slashing_pk_script",
"slashing_rate"
],
Expand Down Expand Up @@ -39,6 +40,9 @@
"format": "uint64",
"minimum": 0.0
},
"rewards_denom": {
"type": "string"
},
"slashing_pk_script": {
"description": "`slashing_pk_script` is the pk script that the slashed BTC goes to. The pk script is in string format on Bitcoin.",
"type": "string"
Expand Down
Loading
Loading