diff --git a/contracts/near/context-proxy/src/mutate.rs b/contracts/near/context-proxy/src/mutate.rs index 06322bcf1..4679be1f9 100644 --- a/contracts/near/context-proxy/src/mutate.rs +++ b/contracts/near/context-proxy/src/mutate.rs @@ -130,6 +130,21 @@ impl ProxyContract { return PromiseOrValue::Value(true); } + // Calculate gas allocation using Gas type + let used_gas = env::used_gas(); + let prepaid_gas = env::prepaid_gas(); + let remaining_gas = prepaid_gas.saturating_sub(used_gas); + + // Reserve 30 TGas for completion callback + let gas_for_completion = Gas::from_tgas(30); + let available_gas = remaining_gas.saturating_sub(gas_for_completion); + + // Use at least 5 TGas per call but no more than the available gas + let gas_per_call = std::cmp::max( + Gas::from_tgas(5), + Gas::from_gas(available_gas.as_gas() / promise_actions.len() as u64), + ); + let mut chained_promise: Option = None; for action in promise_actions { @@ -139,15 +154,15 @@ impl ProxyContract { method_name, args, deposit, - gas, } => { let account_id: AccountId = AccountId::from_str(receiver_id.as_str()).expect("Invalid account ID"); + Promise::new(account_id).function_call( method_name, args.into(), - NearToken::from_near(deposit), - Gas::from_gas(gas), + NearToken::from_yoctonear(deposit), + gas_per_call, ) } ProposalAction::Transfer { diff --git a/contracts/near/context-proxy/tests/common/config_helper.rs b/contracts/near/context-proxy/tests/common/config_helper.rs index bb73f52ed..06b11d875 100644 --- a/contracts/near/context-proxy/tests/common/config_helper.rs +++ b/contracts/near/context-proxy/tests/common/config_helper.rs @@ -165,13 +165,16 @@ impl ConfigContractHelper { .view(self.config_contract.id(), "fetch_nonce") .args_json(json!({ "context_id": context_id, - "member_id": member_id + "member_id": member_id, })) .await? .json()?; + if res.is_none() { + // User doesn't have a nonce yet return Ok(0); } + Ok(res.unwrap()) } } diff --git a/contracts/near/context-proxy/tests/sandbox.rs b/contracts/near/context-proxy/tests/sandbox.rs index d4d30af1d..bdacf6692 100644 --- a/contracts/near/context-proxy/tests/sandbox.rs +++ b/contracts/near/context-proxy/tests/sandbox.rs @@ -381,7 +381,6 @@ async fn test_execute_proposal() -> Result<()> { method_name: "increment".to_string(), args: serde_json::to_string(&Vec::::new())?, deposit: 0, - gas: 1_000_000_000_000, }]; create_and_approve_proposal(&proxy_helper, &relayer_account, &actions, members).await?; @@ -531,7 +530,6 @@ async fn test_combined_proposals() -> Result<()> { method_name: "increment".to_string(), args: serde_json::to_string(&Vec::::new())?, deposit: 0, - gas: 1_000_000_000_000, }, ProposalAction::SetActiveProposalsLimit { active_proposals_limit: 5, @@ -578,7 +576,6 @@ async fn test_combined_proposal_actions_with_promise_failure() -> Result<()> { method_name: "non_existent_method".to_string(), // This method does not exist args: serde_json::to_string(&Vec::::new())?, deposit: 0, - gas: 1_000_000_000_000, }, ProposalAction::SetActiveProposalsLimit { active_proposals_limit: 5, diff --git a/crates/context/config/src/client/env/proxy/types/starknet.rs b/crates/context/config/src/client/env/proxy/types/starknet.rs index bed54ba1f..a53f0ab42 100644 --- a/crates/context/config/src/client/env/proxy/types/starknet.rs +++ b/crates/context/config/src/client/env/proxy/types/starknet.rs @@ -299,7 +299,6 @@ impl From for ProposalAction { .collect::>() .join(","), deposit: 0, - gas: 0, } } StarknetProposalActionWithArgs::Transfer(receiver, amount) => { diff --git a/crates/context/config/src/icp.rs b/crates/context/config/src/icp.rs index 582fa01f1..0fc859c98 100644 --- a/crates/context/config/src/icp.rs +++ b/crates/context/config/src/icp.rs @@ -47,7 +47,6 @@ impl TryFrom for ICProposalAction { method_name, args, deposit, - gas: _, } => ICProposalAction::ExternalFunctionCall { receiver_id: receiver_id .parse::() @@ -99,7 +98,6 @@ impl From for ProposalAction { method_name, args, deposit, - gas: 0, }, ICProposalAction::Transfer { receiver_id, diff --git a/crates/context/config/src/lib.rs b/crates/context/config/src/lib.rs index dbd2b6a75..de773081c 100644 --- a/crates/context/config/src/lib.rs +++ b/crates/context/config/src/lib.rs @@ -145,7 +145,6 @@ pub enum ProposalAction { method_name: String, args: String, deposit: NativeToken, - gas: Gas, }, Transfer { receiver_id: String,