Skip to content

Commit

Permalink
Dynamic calculation of gas for batch recording (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ authored Jan 18, 2024
1 parent 1c3e9b4 commit 8fe03ae
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 16 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ tokio = "1.28.0"
anyhow = "1.0.75"
async-trait = "0.1.74"

near-units = "0.2.0"
near-workspaces = "0.9.0"

near-sdk = { git = "https://github.com/sweatco/near-sdk-rs", rev = "8c48b26cc48d969c1e5f3162141fe9c824fccecd" }
near-contract-standards = { git = "https://github.com/sweatco/near-sdk-rs", rev = "8c48b26cc48d969c1e5f3162141fe9c824fccecd" }
integration-trait = { git = "https://github.com/sweatco/integration-utils.git", rev = "b3e5b747b930ebc04601146deda13f12615d5e01" }
integration-utils = { git = "https://github.com/sweatco/integration-utils.git", rev = "b3e5b747b930ebc04601146deda13f12615d5e01" }
integration-trait = { git = "https://github.com/sweatco/integration-utils.git", rev = "2c3512ffba1540271a6b09ed266961173fce15c8" }
integration-utils = { git = "https://github.com/sweatco/integration-utils.git", rev = "2c3512ffba1540271a6b09ed266961173fce15c8" }

sweat-model = { path = "model" }
sweat-integration = { path = "sweat-integration" }
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ near-contract-standards = { workspace = true }
anyhow = { workspace = true }
tokio = { workspace = true }

near-units = "0.2.0"
near-units = { workspace = true }
borsh = "0.10.3"
maplit = "1.0"
tracing = "0.1.37"
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod common;
mod defer;
mod formula;
mod interface;
mod measure;
mod mint;
mod prepare;
mod transfer;
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/measure/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod record_batch;
36 changes: 36 additions & 0 deletions integration-tests/src/measure/record_batch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![cfg(test)]

use anyhow::Result;
use integration_utils::measure::outcome_storage::OutcomeStorage;
use near_workspaces::types::Gas;
use sweat_model::SweatApiIntegration;

use crate::{prepare::IntegrationContext, prepare_contract};

#[ignore]
#[tokio::test]
async fn single_record_batch() -> anyhow::Result<()> {
let gas = measure_record_batch().await?;

dbg!(&gas);

Ok(())
}

async fn measure_record_batch() -> Result<Gas> {
let mut context = prepare_contract().await?;

let oracle = context.oracle().await?;

let (gas, _) = OutcomeStorage::measure_total(
&oracle,
context
.ft_contract()
.record_batch(Default::default())
.with_user(&oracle)
.call(),
)
.await?;

Ok(gas)
}
Binary file modified res/sweat.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.74"
channel = "1.75"
4 changes: 2 additions & 2 deletions sweat-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ integration-utils = { workspace = true }

sweat-model = { workspace = true, features = ["integration-test"] }

near-units = "0.2.0"
near-units = { workspace = true }
near-sdk = { workspace = true }
near-contract-standards = { workspace = true }
near-workspaces = "0.9.0"
near-workspaces = { workspace = true }
11 changes: 9 additions & 2 deletions sweat/src/defer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use sweat_model::SweatDefer;

use crate::*;

const ONE_GGAS: u64 = Gas::ONE_TERA.0 / 1000;

#[near_bindgen]
impl SweatDefer for Contract {
fn defer_batch(&mut self, steps_batch: Vec<(AccountId, u32)>, holding_account_id: AccountId) -> PromiseOrValue<()> {
Expand All @@ -11,6 +13,8 @@ impl SweatDefer for Contract {
"Unauthorized access! Only oracle can call that!"
);

let batch_len = steps_batch.len() as u64;

let mut accounts_tokens: Vec<(AccountId, U128)> = Vec::new();
let mut total_effective: U128 = U128(0);
let mut total_fee: U128 = U128(0);
Expand All @@ -28,16 +32,19 @@ impl SweatDefer for Contract {
"amounts": accounts_tokens,
});

// These values calculated in `measure_record_batch_for_hold_test` in claim contract.
let record_batch_for_hold_gas = Gas::ONE_TERA * 8 + Gas(batch_len * ONE_GGAS * 320);

Promise::new(holding_account_id.clone())
.function_call(
"record_batch_for_hold".to_string(),
hold_arguments.to_string().into_bytes(),
0,
Gas(20 * 1_000_000_000_000),
record_batch_for_hold_gas,
)
.then(
ext_ft_transfer_callback::ext(env::current_account_id())
.with_static_gas(Gas(5 * 1_000_000_000_000))
.with_static_gas(Gas::ONE_TERA * 5)
.on_record(
holding_account_id,
total_effective,
Expand Down

0 comments on commit 8fe03ae

Please sign in to comment.