$NEAR, Fungible and Non-Fungible Tokens), and link them to specific private keys. Whoever has the private key can claim the drop into an existing account, or ask the contract to create a new one for them.
Particularly, it shows:
- How to create a token drops (NEAR, FT and NFT)
- How to leverage Function Call keys for enabling amazing UX
Contract Overview
The contract exposes 3 methods to create drops of NEAR tokens, FT, and NFT. To claim the tokens, the contract exposes two methods, one to claim in an existing account, and another that will create a new account and claim the tokens into it. This contract leverages NEAR unique feature of FunctionCall keys, which allows the contract to create new accounts and claim tokens on behalf of the user. Imagine Alice want to drop some NEAR to Bob:- Alice will call
create_near_droppassing some NEAR amount, and a Public Access Key - The Contract will check if Alice attached enough tokens and create the drop
- The Contract will add the
PublicKeyas aFunctionCall Keyto itself, that only allow to call the claim methods - Alice will give the
Private Keyto Bob - Bob will use the Key to sign a transaction calling the
claim_formethod - The Contract will check if the key is linked to a drop, and if it is, it will send the drop
Contract's interface
Contract's interface
create_near_drop(public_keys, amount_per_drop)
Creates #public_keys drops, each with amount_per_drop NEAR tokens on themcreate_ft_drop(public_keys, ft_contract, amount_per_drop)
Creates #public_keys drops, each with amount_per_drop FT tokens, corresponding to the ft_contractcreate_nft_drop(public_key, nft_contract)
Creates a drop with an NFT token, which will come from the nft_contractclaim_for(account_id)
Claims a drop, which will be sent to the existing account_idcreate_account_and_claim(account_id)
Creates the account_id, and then drops the tokens into itContract’s State
We can see in the contract’s state that the contract keeps track of differentPublicKeys, and links them to a specific DropId, which is simply an identifier for a Drop (see below).
top_level_account: The account that will be used to create new accounts, generally it will betestnetormainnetnext_drop_id: A simple counter used to assign unique identifiers to each dropdrop_id_by_key: AMapbetweenPublicKeyandDropId, which allows the contract to know what drops are claimable by a given keydrop_by_id: A simpleMapthat links eachDropIdwith the actualDropdata.
Drop Types
There are 3 types of drops, which differ in what the user will receive when they claims the corresponding drop - NEAR, fungible tokens (FTs) or non-fungible tokens (NFTs).Notice that in this example implementation users cannot mix drops. This is, you can either drop NEAR tokens, or FT, or NFTs, but not a mixture of them (i.e. you cannot drop 1 NEAR token and 1 FT token in the same drop)
Create a drop
Allcreate start by checking that the user deposited enough funds to create the drop, and then proceed to add the access keys to the contract’s account as FunctionCall Keys.
- NEAR Drop
- FT Drop
- NFT Drop
Storage Costs
While we will not go into the details of how the storage costs are calculated, it is important to know what is being taken into account:- The cost of storing each Drop, which will include storing all bytes associated with the
Dropstruct - The cost of storing each
PublicKey -> DropIdrelation in the maps - Cost of storing each
PublicKeyin the account
PublicKey on the state, but the cost of adding the key to the contract’s account as a FunctionCall key.
Claim a drop
In order to claim drop, a user needs to sign a transaction using thePrivate Key, which is the counterpart of the Public Key that was added to the contract.
All Drops have a counter which decreases by 1 each time a drop is claimed. This way, when all drops are claimed (counter == 0), we can remove all information from the Drop.
There are two ways to claim a drop: claim for an existing account and claim for a new account. The main difference between them is that the first one will send the tokens to an existing account, while the second one will create a new account and send the tokens to it.
- Existing Account
- New Account
Testing the Contract
The contract readily includes a sandbox testing to validate its functionality. To execute the tests, run the following command:- 🦀 Rust
Deploying the Contract to the NEAR network
In order to deploy the contract you will need to create a NEAR account.- Short
- Full
CLI: Interacting with the Contract
To interact with the contract through the console, you can use the following commands:- Short
- Full
Versioning for this articleAt the time of this writing, this example works with the following versions:
- near-cli:
0.17.0 - rustc:
1.82.0