The smart contract exposes multiple methods to handle donating money to a beneficiary set on initialization.
Install cargo-near
and run:
cargo near build
cargo test
In this example we will be using NEAR CLI to intract with the NEAR blockchain and the smart contract
If you want full control over of your interactions we recommend using the near-cli-rs.
The contract will be automatically initialized with a default beneficiary. To initialize the contract yourself do:
near call <deployed-to-account> init '{"beneficiary":"<account>"}' --accountId <deployed-to-account>
get_beneficiary
is a read-only method (view method) that returns the
beneficiary of the donations.
View methods can be called for free by anyone, even people without a NEAR account!
near view <deployed-to-account> get_beneficiary
change_beneficiary
is a read-only method (view method) that returns the
beneficiary of the donations.
View methods can be called for free by anyone, even people without a NEAR account!
near call <deployed-to-account> change_beneficiary {"new_beneficiary": "<new-baccount>"} --accountId <deployed-to-account>
donate
forwards any attached NEAR tokens to the beneficiary
while keeping
track of it.
donate
is a payable method for which can only be invoked using a NEAR account.
The account needs to attach NEAR Tokens and pay GAS for the transaction.
near call <deployed-to-account> donate --amount 1 --accountId <account>
#[payable]
pub fn donate(&mut self) -> String {
// Get who is calling the method and how much NEAR they attached
let donor: AccountId = env::predecessor_account_id();
let donation_amount = env::attached_deposit();
require!(
donation_amount > STORAGE_COST,
format!(
"Attach at least {} yoctoNEAR to cover for the storage cost",
STORAGE_COST
)
);
let mut donated_so_far: NearToken = self
.donations
.get(&donor)
.unwrap_or(NearToken::from_near(0));
let to_transfer = if donated_so_far.is_zero() {
// This is the user's first donation, lets register it, which increases storage
// Subtract the storage cost to the amount to transfer
donation_amount.saturating_sub(STORAGE_COST).to_owned()
} else {
donation_amount
};
// Persist in storage the amount donated so far
donated_so_far = donated_so_far.saturating_add(donation_amount);
self.donations.insert(&donor, &donated_so_far);
log!(
"Thank you {} for donating {}! You donated a total of {}",
donor.clone(),
donation_amount,
donated_so_far
);
// Send the NEAR to the beneficiary
Promise::new(self.beneficiary.clone()).transfer(to_transfer);
// Return the total amount donated so far
donated_so_far.to_string()
}
near view <deployed-to-account> number_of_donors
near view <deployed-to-account> get_donation_for_account '{"account_id":"<account>"}'
near view <deployed-to-account> get_donations
- cargo-near - NEAR smart contract development toolkit for Rust
- near CLI-RS - Iteract with NEAR blockchain from command line
- NEAR Rust SDK Documentation
- NEAR Documentation
- NEAR StackOverflow
- NEAR Discord
- NEAR Telegram Developers Community Group
- NEAR DevHub: Telegram, Twitter