Skip to content

Commit

Permalink
Feat remove gas from NEAR contract call (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov authored Dec 19, 2024
1 parent 7cfddfa commit c140917
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
21 changes: 18 additions & 3 deletions contracts/near/context-proxy/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Promise> = None;

for action in promise_actions {
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion contracts/near/context-proxy/tests/common/config_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
3 changes: 0 additions & 3 deletions contracts/near/context-proxy/tests/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ async fn test_execute_proposal() -> Result<()> {
method_name: "increment".to_string(),
args: serde_json::to_string(&Vec::<u8>::new())?,
deposit: 0,
gas: 1_000_000_000_000,
}];

create_and_approve_proposal(&proxy_helper, &relayer_account, &actions, members).await?;
Expand Down Expand Up @@ -531,7 +530,6 @@ async fn test_combined_proposals() -> Result<()> {
method_name: "increment".to_string(),
args: serde_json::to_string(&Vec::<u8>::new())?,
deposit: 0,
gas: 1_000_000_000_000,
},
ProposalAction::SetActiveProposalsLimit {
active_proposals_limit: 5,
Expand Down Expand Up @@ -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::<u8>::new())?,
deposit: 0,
gas: 1_000_000_000_000,
},
ProposalAction::SetActiveProposalsLimit {
active_proposals_limit: 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ impl From<StarknetProposalActionWithArgs> for ProposalAction {
.collect::<Vec<_>>()
.join(","),
deposit: 0,
gas: 0,
}
}
StarknetProposalActionWithArgs::Transfer(receiver, amount) => {
Expand Down
2 changes: 0 additions & 2 deletions crates/context/config/src/icp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl TryFrom<ProposalAction> for ICProposalAction {
method_name,
args,
deposit,
gas: _,
} => ICProposalAction::ExternalFunctionCall {
receiver_id: receiver_id
.parse::<Principal>()
Expand Down Expand Up @@ -99,7 +98,6 @@ impl From<ICProposalAction> for ProposalAction {
method_name,
args,
deposit,
gas: 0,
},
ICProposalAction::Transfer {
receiver_id,
Expand Down
1 change: 0 additions & 1 deletion crates/context/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ pub enum ProposalAction {
method_name: String,
args: String,
deposit: NativeToken,
gas: Gas,
},
Transfer {
receiver_id: String,
Expand Down

0 comments on commit c140917

Please sign in to comment.