Skip to content

Commit

Permalink
add deposit for replenishment calls
Browse files Browse the repository at this point in the history
  • Loading branch information
piakushin committed Jan 31, 2023
1 parent c04ba90 commit 617506b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Binary file modified dist/nft_benefits_vault.wasm
Binary file not shown.
17 changes: 12 additions & 5 deletions src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,16 @@ impl Contract {
if replenish {
for replenisher in vault.replenishers().iter() {
log!(
"calling replenisher: {}.{}({})",
"calling replenisher: {}.{}({}) with deposit {}",
replenisher.contract_id(),
replenisher.callback(),
replenisher.args()
replenisher.args(),
replenisher.deposit(),
);
let replenish = Promise::new(replenisher.contract_id().clone()).function_call(
replenisher.callback().to_owned(),
replenisher.args().as_bytes().to_vec(),
0,
replenisher.deposit(),
Gas::ONE_TERA * 100,
);
promise = Some(if let Some(p) = promise {
Expand All @@ -251,6 +252,7 @@ impl Contract {
nft_id: TokenId,
callback: String,
args: String,
deposit: U128,
) {
assert_one_yocto();

Expand All @@ -261,8 +263,13 @@ impl Contract {

let contract_id = env::predecessor_account_id();

log!("add replenisher: [{}].{}(args)", contract_id, callback);
vault.add_replenisher(contract_id, callback, args);
log!(
"add replenisher: [{}].{}(args) with deposit: {}",
contract_id,
callback,
deposit.0
);
vault.add_replenisher(contract_id, callback, args, deposit);

self.vaults.insert(&nft_id, &vault);
}
Expand Down
7 changes: 6 additions & 1 deletion src/interface/ft_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ impl FungibleTokenReceiver for Contract {
// so predecessor account IS the FT contract.
let fungible_token = env::predecessor_account_id();

log!("received {} tokens from {}", amount, sender_id);
log!(
"received {} tokens from {}, msg: {}",
amount,
sender_id,
msg
);

let request = match Request::from_json(&msg) {
Ok(req) => req,
Expand Down
18 changes: 16 additions & 2 deletions src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
collections::{UnorderedMap, UnorderedSet},
env::sha256,
json_types::U128,
require,
serde::{Deserialize, Serialize},
AccountId,
Expand All @@ -17,12 +18,13 @@ pub struct Vault {
replenishers: UnorderedSet<Replenisher>,
}

#[derive(Debug, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[derive(Debug, BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct Replenisher {
contract_id: AccountId,
callback: String,
args: String,
deposit: U128,
}

impl Vault {
Expand Down Expand Up @@ -68,11 +70,18 @@ impl Vault {
/// # Panics
///
/// Panics if replenisher is already registered.
pub fn add_replenisher(&mut self, contract_id: AccountId, callback: String, args: String) {
pub fn add_replenisher(
&mut self,
contract_id: AccountId,
callback: String,
args: String,
deposit: U128,
) {
let replenisher = Replenisher {
contract_id,
callback,
args,
deposit,
};
require!(
!self.replenishers.contains(&replenisher),
Expand Down Expand Up @@ -117,4 +126,9 @@ impl Replenisher {
pub fn args(&self) -> &str {
self.args.as_ref()
}

/// Returns required deposit for the call.
pub fn deposit(&self) -> u128 {
self.deposit.0
}
}

0 comments on commit 617506b

Please sign in to comment.