Skip to main content

Donation

Our donation example enables to forward NEAR Tokens to an account while keeping track of it. It is one of the simplest examples on making a contract handle tranfers.

img Frontend of the Donation App


Obtaining the Donation Exampleโ€‹

You have two options to start the Donation Example.

  1. You can use the app through Github Codespaces, which will open a web-based interactive environment.
  2. Clone the repository locally and use it from your computer.
CodespacesClone locally
Open in GitHub Codespaces๐ŸŒ https://github.com/near-examples/donation-examples.git

Structure of the Exampleโ€‹

The example is divided in two main components:

  1. The smart contract, available in two flavors: rust and javascript
  2. The frontend, that interacts with an already deployed contract.
โ”Œโ”€โ”€ sandbox-ts # sandbox testing
โ”‚ โ”œโ”€โ”€ src
โ”‚ โ”‚ โ””โ”€โ”€ main.ava.ts
โ”‚ โ”œโ”€โ”€ ava.config.cjs
โ”‚ โ””โ”€โ”€ package.json
โ”œโ”€โ”€ src # contract's code
โ”‚ โ”œโ”€โ”€ contract.ts
โ”‚ โ”œโ”€โ”€ model.ts
โ”‚ โ””โ”€โ”€ utils.ts
โ”œโ”€โ”€ package.json # package manager
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ tsconfig.json # test script

Frontendโ€‹

The donation example includes a frontend that interacts with an already deployed smart contract, allowing user to donate NEAR tokens to a faucet service.


Running the Frontendโ€‹

To start the frontend you will need to install the dependencies and start the server.

cd frontend
yarn
yarn dev

Go ahead and login with your NEAR account. If you don't have one, you will be able to create one in the moment. Once logged in, input the amount of NEAR you want to donate and press the donate button. You will be redirected to the NEAR Wallet to confirm the transaction. After confirming it, the donation will be listed in the "Latest Donations".


Understanding the Frontendโ€‹

The frontend is a Next.JS project generated by create-near-app. Check DonationsTable.jsx and DonationsForm.jsx to understand how components are displayed and interacting with the contract.

An interesting aspect of the donation example is that it showcases how to retrieve a result after being redirected to the NEAR wallet to accept a transaction.


Smart Contractโ€‹

The contract exposes methods to donate tokens (donate), and methods to retrieve the recorded donations (e.g. get_donation_for_account).


Testing the Contractโ€‹

The contract readily includes a set of unit and sandbox testing to validate its functionality. To execute the tests, run the following commands:

cd contract-ts
yarn
yarn test
tip

The integration tests use a sandbox to create NEAR users and simulate interactions with the contract.


Deploying the Contract to the NEAR networkโ€‹

In order to deploy the contract you will need to create a NEAR account.

# Create a new account pre-funded by a faucet
near create-account <accountId> --useFaucet

Go into the directory containing the smart contract (cd contract-ts or cd contract-rs), build and deploy it:

cargo near build

cargo near deploy <accountId>
tip

To interact with your contract from the frontend, simply replace the variable CONTRACT_NAME in the index.js file.


CLI: Interacting with the Contractโ€‹

To interact with the contract through the console, you can use the following commands

Get donationsโ€‹

near view donation.near-examples.testnet get_donations '{"from_index": "0","limit": "10"}'

Get beneficiaryโ€‹

near view donation.near-examples.testnet get_beneficiary

Get number of donorsโ€‹

near view donation.near-examples.testnet number_of_donors

Get donation for an accountโ€‹

# Require accountId
near view donation.near-examples.testnet get_donation_for_account '{"account_id":<accountId>}'

# Replace <accountId> with your account ID
# Require deposit
near call donation.near-examples.testnet donate --accountId <accountId> --deposit 0.1

tip

If you're using your own account, replace donation.near-examples.testnet with your accountId.


Moving Forwardโ€‹

A nice way to learn is by trying to expand a contract. Modify the donation example so it accumulates the tokens in the contract instead of sending it immediately. Then, make a method that only the beneficiary can call to retrieve the tokens.

Versioning for this article

At the time of this writing, this example works with the following versions:

  • near-cli: 4.0.13
  • node: 18.19.1
  • rustc: 1.77.0
Was this page helpful?