From 0c2514f4b0fa52bf430caa20f1512234eb8fc36f Mon Sep 17 00:00:00 2001 From: Timofey <5527315+epanchee@users.noreply.github.com> Date: Mon, 6 May 2024 14:50:20 +0400 Subject: [PATCH 1/2] feat(staking): stake on behalf of another address --- Cargo.lock | 40 +++++++++---------- contracts/tokenomics/staking/Cargo.toml | 2 +- contracts/tokenomics/staking/src/contract.rs | 28 ++++++++----- contracts/tokenomics/staking/src/error.rs | 3 ++ contracts/tokenomics/staking/src/lib.rs | 1 + contracts/tokenomics/staking/src/migrate.rs | 30 ++++++++++++++ .../tokenomics/staking/tests/common/helper.rs | 2 +- .../staking/tests/staking_integration.rs | 26 +++++++++++- packages/astroport/Cargo.toml | 2 +- packages/astroport/src/staking.rs | 3 +- schemas/astroport-maker/astroport-maker.json | 2 +- .../astroport-staking/astroport-staking.json | 12 +++++- schemas/astroport-staking/raw/execute.json | 10 ++++- 13 files changed, 122 insertions(+), 39 deletions(-) create mode 100644 contracts/tokenomics/staking/src/migrate.rs diff --git a/Cargo.lock b/Cargo.lock index 86f766569..c59cdd38a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,7 +57,7 @@ dependencies = [ name = "astro-token-converter" version = "1.0.0" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus 1.2.0", @@ -72,7 +72,7 @@ name = "astro-token-converter-neutron" version = "1.0.0" dependencies = [ "astro-token-converter", - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-std", "cw-utils 1.0.3", "cw2 1.1.2", @@ -114,7 +114,7 @@ dependencies = [ [[package]] name = "astroport" -version = "4.0.1" +version = "4.0.2" dependencies = [ "astroport-circular-buffer 0.2.0", "cosmwasm-schema", @@ -173,7 +173,7 @@ name = "astroport-factory" version = "1.7.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-pair 1.5.1", "cosmwasm-schema", "cosmwasm-std", @@ -208,7 +208,7 @@ name = "astroport-governance" version = "1.2.0" source = "git+https://github.com/astroport-fi/astroport-governance#182dd5bc201dd634995b5e4dc9e2774495693703" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus 0.15.1", @@ -220,7 +220,7 @@ name = "astroport-governance" version = "3.0.0" source = "git+https://github.com/astroport-fi/hidden_astroport_governance#e1c4475708c5d92acece729ae939d8caac4295d6" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus 1.2.0", @@ -234,7 +234,7 @@ version = "1.1.0" dependencies = [ "anyhow", "astro-token-converter", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-native-coin-registry", "astroport-pair 1.5.1", @@ -259,7 +259,7 @@ name = "astroport-liquidity-manager" version = "1.1.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-incentives", "astroport-native-coin-registry", @@ -282,7 +282,7 @@ name = "astroport-maker" version = "1.5.0" dependencies = [ "astro-satellite-package", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-governance 3.0.0", "astroport-native-coin-registry", @@ -303,7 +303,7 @@ name = "astroport-mocks" version = "0.2.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-native-coin-registry", "astroport-pair 1.5.1", @@ -380,7 +380,7 @@ dependencies = [ name = "astroport-pair" version = "1.5.1" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-mocks", "cosmwasm-schema", @@ -419,7 +419,7 @@ name = "astroport-pair-concentrated" version = "3.0.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-circular-buffer 0.2.0", "astroport-factory 1.7.0", "astroport-mocks", @@ -445,7 +445,7 @@ version = "1.0.0" dependencies = [ "anyhow", "astro-token-converter", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-pair 1.3.3", "cosmwasm-schema", @@ -467,7 +467,7 @@ name = "astroport-pair-stable" version = "3.5.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-circular-buffer 0.2.0", "astroport-factory 1.7.0", "astroport-mocks", @@ -512,7 +512,7 @@ dependencies = [ name = "astroport-pair-xyk-sale-tax" version = "1.6.0" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-mocks", "astroport-pair 1.3.3", @@ -536,7 +536,7 @@ name = "astroport-pcl-common" version = "2.0.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "cosmwasm-schema", "cosmwasm-std", @@ -567,10 +567,10 @@ dependencies = [ [[package]] name = "astroport-staking" -version = "2.0.0" +version = "2.1.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-tokenfactory-tracker", "cosmwasm-schema", "cosmwasm-std", @@ -587,7 +587,7 @@ dependencies = [ name = "astroport-tokenfactory-tracker" version = "1.0.0" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-schema", "cosmwasm-std", "cw-multi-test 0.20.0 (git+https://github.com/astroport-fi/cw-multi-test?branch=feat/bank_with_send_hooks)", @@ -620,7 +620,7 @@ name = "astroport-vesting" version = "1.4.0" dependencies = [ "astro-token-converter", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-vesting 1.3.1", "cosmwasm-schema", "cosmwasm-std", diff --git a/contracts/tokenomics/staking/Cargo.toml b/contracts/tokenomics/staking/Cargo.toml index 942587921..8cdc4de2a 100644 --- a/contracts/tokenomics/staking/Cargo.toml +++ b/contracts/tokenomics/staking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "astroport-staking" -version = "2.0.0" +version = "2.1.0" authors = ["Astroport"] edition = "2021" description = "Astroport Staking Contract" diff --git a/contracts/tokenomics/staking/src/contract.rs b/contracts/tokenomics/staking/src/contract.rs index a33a0fd53..47f06c6dd 100644 --- a/contracts/tokenomics/staking/src/contract.rs +++ b/contracts/tokenomics/staking/src/contract.rs @@ -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}; @@ -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"; @@ -113,7 +115,7 @@ pub fn execute( msg: ExecuteMsg, ) -> Result { 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), } } @@ -201,8 +203,14 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result Result { +/// 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, +) -> Result { let config = CONFIG.load(deps.storage)?; // Ensure that the correct denom is sent. Sending zero tokens is prohibited on chain level @@ -257,11 +265,13 @@ fn execute_enter(deps: DepsMut, env: Env, info: MessageInfo) -> Result Result Result { + 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)) +} diff --git a/contracts/tokenomics/staking/tests/common/helper.rs b/contracts/tokenomics/staking/tests/common/helper.rs index f0f03de22..6d26afe36 100644 --- a/contracts/tokenomics/staking/tests/common/helper.rs +++ b/contracts/tokenomics/staking/tests/common/helper.rs @@ -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), ) } diff --git a/contracts/tokenomics/staking/tests/staking_integration.rs b/contracts/tokenomics/staking/tests/staking_integration.rs index e71e41fd1..49eea7f39 100644 --- a/contracts/tokenomics/staking/tests/staking_integration.rs +++ b/contracts/tokenomics/staking/tests/staking_integration.rs @@ -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(); @@ -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(); @@ -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] diff --git a/packages/astroport/Cargo.toml b/packages/astroport/Cargo.toml index c2f4115d8..5aef75cc8 100644 --- a/packages/astroport/Cargo.toml +++ b/packages/astroport/Cargo.toml @@ -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" diff --git a/packages/astroport/src/staking.rs b/packages/astroport/src/staking.rs index 1ed50298b..e5e25e046 100644 --- a/packages/astroport/src/staking.rs +++ b/packages/astroport/src/staking.rs @@ -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 }, /// Burns xASTRO in exchange for ASTRO Leave {}, } diff --git a/schemas/astroport-maker/astroport-maker.json b/schemas/astroport-maker/astroport-maker.json index 5c6a0743d..25938cdda 100644 --- a/schemas/astroport-maker/astroport-maker.json +++ b/schemas/astroport-maker/astroport-maker.json @@ -1,6 +1,6 @@ { "contract_name": "astroport-maker", - "contract_version": "1.4.0", + "contract_version": "1.5.0", "idl_version": "1.0.0", "instantiate": { "$schema": "http://json-schema.org/draft-07/schema#", diff --git a/schemas/astroport-staking/astroport-staking.json b/schemas/astroport-staking/astroport-staking.json index b5a065dda..f24b96216 100644 --- a/schemas/astroport-staking/astroport-staking.json +++ b/schemas/astroport-staking/astroport-staking.json @@ -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#", @@ -40,7 +40,7 @@ "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" @@ -48,6 +48,14 @@ "properties": { "enter": { "type": "object", + "properties": { + "receiver": { + "type": [ + "string", + "null" + ] + } + }, "additionalProperties": false } }, diff --git a/schemas/astroport-staking/raw/execute.json b/schemas/astroport-staking/raw/execute.json index 79b499e44..943083067 100644 --- a/schemas/astroport-staking/raw/execute.json +++ b/schemas/astroport-staking/raw/execute.json @@ -4,7 +4,7 @@ "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" @@ -12,6 +12,14 @@ "properties": { "enter": { "type": "object", + "properties": { + "receiver": { + "type": [ + "string", + "null" + ] + } + }, "additionalProperties": false } }, From 82370041178717ed920fde320d739fb95c8c8561 Mon Sep 17 00:00:00 2001 From: Timofey <5527315+epanchee@users.noreply.github.com> Date: Mon, 6 May 2024 16:28:32 +0400 Subject: [PATCH 2/2] update Cargo.lock --- Cargo.lock | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f75f7b96..be712f1bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,7 +57,7 @@ dependencies = [ name = "astro-token-converter" version = "1.0.0" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus 1.2.0", @@ -72,7 +72,7 @@ name = "astro-token-converter-neutron" version = "1.0.0" dependencies = [ "astro-token-converter", - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-std", "cw-utils 1.0.3", "cw2 1.1.2", @@ -114,7 +114,7 @@ dependencies = [ [[package]] name = "astroport" -version = "4.0.1" +version = "4.0.2" dependencies = [ "astroport-circular-buffer 0.2.0", "cosmwasm-schema", @@ -173,7 +173,7 @@ name = "astroport-factory" version = "1.7.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-pair 1.5.1", "cosmwasm-schema", "cosmwasm-std", @@ -208,7 +208,7 @@ name = "astroport-governance" version = "1.2.0" source = "git+https://github.com/astroport-fi/astroport-governance#182dd5bc201dd634995b5e4dc9e2774495693703" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus 0.15.1", @@ -233,7 +233,7 @@ version = "1.1.0" dependencies = [ "anyhow", "astro-token-converter", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-native-coin-registry", "astroport-pair 1.5.1", @@ -258,7 +258,7 @@ name = "astroport-liquidity-manager" version = "1.1.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-incentives", "astroport-native-coin-registry", @@ -281,7 +281,7 @@ name = "astroport-maker" version = "1.5.0" dependencies = [ "astro-satellite-package", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-governance 3.0.0", "astroport-native-coin-registry", @@ -302,7 +302,7 @@ name = "astroport-mocks" version = "0.2.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-native-coin-registry", "astroport-pair 1.5.1", @@ -379,7 +379,7 @@ dependencies = [ name = "astroport-pair" version = "1.5.1" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-mocks", "cosmwasm-schema", @@ -418,7 +418,7 @@ name = "astroport-pair-concentrated" version = "3.0.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-circular-buffer 0.2.0", "astroport-factory 1.7.0", "astroport-mocks", @@ -444,7 +444,7 @@ version = "1.0.0" dependencies = [ "anyhow", "astro-token-converter", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-pair 1.3.3", "cosmwasm-schema", @@ -466,7 +466,7 @@ name = "astroport-pair-stable" version = "3.5.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-circular-buffer 0.2.0", "astroport-factory 1.7.0", "astroport-mocks", @@ -511,7 +511,7 @@ dependencies = [ name = "astroport-pair-xyk-sale-tax" version = "1.6.0" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "astroport-mocks", "astroport-pair 1.3.3", @@ -535,7 +535,7 @@ name = "astroport-pcl-common" version = "2.0.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-factory 1.7.0", "cosmwasm-schema", "cosmwasm-std", @@ -566,10 +566,10 @@ dependencies = [ [[package]] name = "astroport-staking" -version = "2.0.0" +version = "2.1.0" dependencies = [ "anyhow", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-tokenfactory-tracker", "cosmwasm-schema", "cosmwasm-std", @@ -586,7 +586,7 @@ dependencies = [ name = "astroport-tokenfactory-tracker" version = "1.0.0" dependencies = [ - "astroport 4.0.1", + "astroport 4.0.2", "cosmwasm-schema", "cosmwasm-std", "cw-multi-test 0.20.0 (git+https://github.com/astroport-fi/cw-multi-test?branch=feat/bank_with_send_hooks)", @@ -619,7 +619,7 @@ name = "astroport-vesting" version = "1.4.0" dependencies = [ "astro-token-converter", - "astroport 4.0.1", + "astroport 4.0.2", "astroport-vesting 1.3.1", "cosmwasm-schema", "cosmwasm-std",