Skip to content

Commit

Permalink
Merge pull request #815 from near/serhii/fix-price-formatting
Browse files Browse the repository at this point in the history
u128 -> U128 in experimental_signature_deposit
  • Loading branch information
ailisp authored Aug 28, 2024
2 parents 546e457 + 49405ea commit aa41ddf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions chain-signatures/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use k256::elliptic_curve::sec1::ToEncodedPoint;
use k256::Scalar;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::LookupMap;
use near_sdk::json_types::U128;
use near_sdk::{
env, log, near_bindgen, AccountId, CryptoHash, Gas, GasWeight, NearToken, Promise,
PromiseError, PublicKey,
Expand Down Expand Up @@ -134,7 +135,7 @@ impl VersionedMpcContract {
}
// Check deposit
let deposit = env::attached_deposit();
let required_deposit = self.experimental_signature_deposit();
let required_deposit: u128 = self.experimental_signature_deposit().into();
if deposit.as_yoctonear() < required_deposit {
return Err(InvalidParameters::InsufficientDeposit.message(format!(
"Attached {}, Required {}",
Expand Down Expand Up @@ -217,16 +218,17 @@ impl VersionedMpcContract {
/// This experimental function calculates the fee for a signature request.
/// The fee is volatile and depends on the number of pending requests.
/// If used on a client side, it can give outdate results.
pub fn experimental_signature_deposit(&self) -> u128 {
pub fn experimental_signature_deposit(&self) -> U128 {
const CHEAP_REQUESTS: u32 = 3;
let pending_requests = match self {
Self::V0(mpc_contract) => mpc_contract.request_counter,
};
match pending_requests {
0..=CHEAP_REQUESTS => 1,
0..=CHEAP_REQUESTS => U128::from(1),
_ => {
(pending_requests - CHEAP_REQUESTS) as u128
* NearToken::from_millinear(50).as_yoctonear()
let expensive_requests = (pending_requests - CHEAP_REQUESTS) as u128;
let price = expensive_requests * NearToken::from_millinear(50).as_yoctonear();
U128::from(price)
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions chain-signatures/contract/tests/user_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ async fn test_experimental_signature_deposit() -> anyhow::Result<()> {
.view("experimental_signature_deposit")
.await
.unwrap()
.json()
.unwrap();
assert_eq!(deposit, 1u128);
.json::<String>()
.unwrap()
.parse()?;
assert_eq!(deposit, 1);

let alice = worker.dev_create_account().await?;
let path = "test";
Expand Down Expand Up @@ -92,8 +93,9 @@ async fn test_experimental_signature_deposit() -> anyhow::Result<()> {
.view("experimental_signature_deposit")
.await
.unwrap()
.json()
.unwrap();
.json::<String>()
.unwrap()
.parse()?;
assert_eq!(deposit, NearToken::from_millinear(50).as_yoctonear());
Ok(())
}

0 comments on commit aa41ddf

Please sign in to comment.