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

feat(staking): stake on behalf of another address #419

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 19 additions & 19 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion contracts/tokenomics/staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-staking"
version = "2.0.0"
version = "2.1.0"
authors = ["Astroport"]
edition = "2021"
description = "Astroport Staking Contract"
Expand Down
28 changes: 19 additions & 9 deletions contracts/tokenomics/staking/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
attr, coin, ensure, entry_point, to_json_binary, BankMsg, Binary, CosmosMsg, Deps, DepsMut,
Env, MessageInfo, Reply, Response, StdError, StdResult, SubMsg, Uint128, WasmMsg,
attr, coin, ensure, to_json_binary, BankMsg, Binary, CosmosMsg, Deps, DepsMut, Env,
MessageInfo, Reply, Response, StdError, StdResult, SubMsg, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw_utils::{must_pay, parse_reply_instantiate_data, MsgInstantiateContractResponse};
Expand All @@ -18,9 +20,9 @@ use crate::error::ContractError;
use crate::state::{CONFIG, TRACKER_DATA};

/// Contract name that is used for migration.
const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
/// Contract version that is used for migration.
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

/// xASTRO information
const TOKEN_NAME: &str = "Staked Astroport Token";
Expand Down Expand Up @@ -113,7 +115,7 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Enter {} => execute_enter(deps, env, info),
ExecuteMsg::Enter { receiver } => execute_enter(deps, env, info, receiver),
ExecuteMsg::Leave {} => execute_leave(deps, env, info),
}
}
Expand Down Expand Up @@ -201,8 +203,14 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr
}
}

/// Enter stakes TokenFactory ASTRO for xASTRO. xASTRO is minted to the sender
fn execute_enter(deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
/// Enter stakes TokenFactory ASTRO for xASTRO.
/// xASTRO is minted to the receiver if provided or to the sender.
fn execute_enter(
deps: DepsMut,
env: Env,
info: MessageInfo,
receiver: Option<String>,
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;

// Ensure that the correct denom is sent. Sending zero tokens is prohibited on chain level
Expand Down Expand Up @@ -257,11 +265,13 @@ fn execute_enter(deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response,
.into(),
);

let recipient = receiver.unwrap_or_else(|| info.sender.to_string());

// TokenFactory minting only allows minting to the sender for now, thus we
// need to send the minted tokens to the recipient
messages.push(
BankMsg::Send {
to_address: info.sender.to_string(),
to_address: recipient.clone(),
amount: vec![minted_coins],
}
.into(),
Expand All @@ -279,7 +289,7 @@ fn execute_enter(deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response,
.set_data(staking_response)
.add_attributes([
attr("action", "enter"),
attr("recipient", info.sender),
attr("recipient", recipient),
attr("astro_amount", amount),
attr("xastro_amount", mint_amount),
]))
Expand Down
3 changes: 3 additions & 0 deletions contracts/tokenomics/staking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ pub enum ContractError {

#[error("Failed to parse or process reply message")]
FailedToParseReply {},

#[error("Contract can't be migrated!")]
MigrationError {},
}
1 change: 1 addition & 0 deletions contracts/tokenomics/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod contract;
pub mod state;

pub mod error;
pub mod migrate;
30 changes: 30 additions & 0 deletions contracts/tokenomics/staking/src/migrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{DepsMut, Env, Response};
use cw2::{get_contract_version, set_contract_version};

use crate::contract::{CONTRACT_NAME, CONTRACT_VERSION};
use astroport::pair::MigrateMsg;

use crate::error::ContractError;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
let contract_version = get_contract_version(deps.storage)?;

match contract_version.contract.as_ref() {
"astroport-staking" => match contract_version.version.as_ref() {
"2.0.0" => {}
_ => return Err(ContractError::MigrationError {}),
},
_ => return Err(ContractError::MigrationError {}),
}

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Ok(Response::new()
.add_attribute("previous_contract_name", &contract_version.contract)
.add_attribute("previous_contract_version", &contract_version.version)
.add_attribute("new_contract_name", CONTRACT_NAME)
.add_attribute("new_contract_version", CONTRACT_VERSION))
}
2 changes: 1 addition & 1 deletion contracts/tokenomics/staking/tests/common/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Helper {
self.app.execute_contract(
sender.clone(),
self.staking.clone(),
&ExecuteMsg::Enter {},
&ExecuteMsg::Enter { receiver: None },
&coins(amount, ASTRO_DENOM),
)
}
Expand Down
26 changes: 24 additions & 2 deletions contracts/tokenomics/staking/tests/staking_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn test_invalid_denom() {
.execute_contract(
owner.clone(),
helper.staking.clone(),
&ExecuteMsg::Enter {},
&ExecuteMsg::Enter { receiver: None },
&coins(1000u128, bad_denom),
)
.unwrap_err();
Expand All @@ -117,7 +117,7 @@ fn test_invalid_denom() {
.execute_contract(
owner.clone(),
helper.staking.clone(),
&ExecuteMsg::Enter {},
&ExecuteMsg::Enter { receiver: None },
&[coin(1000u128, bad_denom), coin(1000u128, ASTRO_DENOM)],
)
.unwrap_err();
Expand Down Expand Up @@ -221,6 +221,28 @@ fn test_enter_and_leave() {
// Check if the staking contract's xASTRO balance is 1000 (locked forever)
let amount = helper.query_balance(&staking, &xastro_denom).unwrap();
assert_eq!(amount.u128(), 1000);

// Check staking for specific recipient
let user = Addr::unchecked("user");
let recipient = Addr::unchecked("recipient");
helper.give_astro(10000, &user);
helper
.app
.execute_contract(
user.clone(),
helper.staking.clone(),
&ExecuteMsg::Enter {
receiver: Some(recipient.to_string()),
},
&coins(10000, ASTRO_DENOM),
)
.unwrap();

let amount = helper.query_balance(&user, &xastro_denom).unwrap();
assert_eq!(amount.u128(), 0);

let amount = helper.query_balance(&recipient, &xastro_denom).unwrap();
assert_eq!(amount.u128(), 10000);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion packages/astroport/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport"
version = "4.0.1"
version = "4.0.2"
authors = ["Astroport"]
edition = "2021"
description = "Common Astroport types, queriers and other utils"
Expand Down
3 changes: 2 additions & 1 deletion packages/astroport/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub struct InstantiateMsg {
#[cw_serde]
pub enum ExecuteMsg {
/// Deposits ASTRO in exchange for xASTRO
Enter {},
/// The receiver is optional. If not set, the sender will receive the xASTRO.
Enter { receiver: Option<String> },
/// Burns xASTRO in exchange for ASTRO
Leave {},
}
Expand Down
12 changes: 10 additions & 2 deletions schemas/astroport-staking/astroport-staking.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "astroport-staking",
"contract_version": "2.0.0",
"contract_version": "2.1.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down Expand Up @@ -40,14 +40,22 @@
"description": "This structure describes the execute messages available in the contract.",
"oneOf": [
{
"description": "Deposits ASTRO in exchange for xASTRO",
"description": "Deposits ASTRO in exchange for xASTRO The receiver is optional. If not set, the sender will receive the xASTRO.",
"type": "object",
"required": [
"enter"
],
"properties": {
"enter": {
"type": "object",
"properties": {
"receiver": {
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
},
Expand Down
Loading
Loading