> ## Documentation Index
> Fetch the complete documentation index at: https://docs.near.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Near Drop

> Learn how NEAR Drop enables token drops (NEAR, FT, NFT) claimable via private keys.

export const Github = ({url, start, end, fname, withSourceLink = true}) => {
  const [code, setCode] = useState(null);
  function toRaw(ref) {
    const fullUrl = ref.slice(ref.indexOf('https'));
    const [url] = fullUrl.split('#');
    const [org, repo, , branch, ...pathSeg] = new URL(url).pathname.split('/').slice(1);
    return `https://raw.githubusercontent.com/${org}/${repo}/${branch}/${pathSeg.join('/')}`;
  }
  async function fetchCode(url, fromLine, toLine) {
    let res;
    if (typeof window !== 'undefined') {
      const validUntil = localStorage.getItem(`${url}-until`);
      if (validUntil && Number(validUntil) > Date.now()) {
        res = localStorage.getItem(url);
      }
    }
    if (!res) {
      try {
        res = await (await fetch(url)).text();
        if (typeof window !== 'undefined') {
          localStorage.setItem(url, res);
          localStorage.setItem(`${url}-until`, String(Date.now() + 60000));
        }
      } catch {
        return 'Error fetching code, please try reloading';
      }
    }
    let body = res.split('\n');
    const from = fromLine ? Number(fromLine) - 1 : 0;
    const to = toLine ? Number(toLine) : body.length;
    body = body.slice(from, to);
    const precedingSpace = body.reduce((prev, line) => {
      if (line.length === 0) return prev;
      const spaces = line.match(/^\s+/);
      if (spaces) return Math.min(prev, spaces[0].length);
      return 0;
    }, Infinity);
    return body.map(line => line.slice(precedingSpace === Infinity ? 0 : precedingSpace)).join('\n');
  }
  function buildSourceUrl(url, start, end) {
    const base = url.split('#')[0];
    if (start && end) return `${base}#L${start}-L${end}`;
    if (start) return `${base}#L${start}`;
    return base;
  }
  useEffect(() => {
    const rawUrl = toRaw(url);
    fetchCode(rawUrl, start, end).then(res => setCode(res));
  }, [url, start, end]);
  const sourceUrl = buildSourceUrl(url, start, end);
  const startLine = start ? Number(start) : 1;
  const fileName = fname ?? sourceUrl.split('/').pop();
  return <div className="rounded-[0.625rem] border border-[#d0d7de] dark:border-[#30363d] overflow-hidden my-5 text-[0.8125rem] font-mono shadow-sm dark:shadow-[0_4px_24px_rgba(0,0,0,0.18)]">

      {}
      <div className="flex items-center justify-between py-2 px-[0.875rem] bg-[#f6f8fa] dark:bg-[#161b22] border-b border-[#d0d7de] dark:border-[#30363d]">
        <div className="flex items-center gap-2 text-[#656d76] dark:text-[#8b949e]">
          <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor">
            <path d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.905 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57A12.02 12.02 0 0 0 24 12c0-6.63-5.37-12-12-12z" />
          </svg>
          <span className="text-xs font-medium text-[#1f2328] dark:text-[#e6edf3]">
            {fileName}
          </span>
        </div>
        {start && end && <span className="text-[0.6875rem] text-[#656d76] dark:text-[#8b949e] bg-[#eaeef2] dark:bg-[#21262d] border border-[#d0d7de] dark:border-[#30363d] rounded-full py-0.5 px-2">
            Lines {start}–{end}
          </span>}
      </div>

      {}
      <div className="overflow-auto max-h-[480px] bg-white dark:bg-[#0d1117] [&_tr]:border-b-0 [&_td]:border-b-0">
        {code === null ? <div className="py-5 px-4 text-xs text-[#656d76] dark:text-[#6e7681]">
            Loading...
          </div> : <table className="w-full border-collapse leading-[1.6]">
            <tbody>
              {code.split('\n').map((line, i) => <tr key={i} className="align-top border-0">
                  <td style={{
    minWidth: '60px'
  }} className="select-none pl-2 pr-3 text-right text-[0.7rem] text-[#8c959f] dark:text-[#3d444d] w-[1%] whitespace-nowrap border-r border-0 border-r-[#d0d7de] dark:border-r-[#21262d]">
                    {startLine + i}
                  </td>
                  <td className="pl-4 pr-6 text-[0.8125rem] text-[#1f2328] dark:text-[#e6edf3] whitespace-pre">
                    {line || ' '}
                  </td>
                </tr>)}
            </tbody>
          </table>}
      </div>

      {}
      {withSourceLink && <div className="flex justify-end py-1.5 px-[0.875rem] bg-[#f6f8fa] dark:bg-[#161b22] border-t border-[#d0d7de] dark:border-[#21262d]">
          <a href={sourceUrl} target="_blank" rel="noreferrer noopener" className="text-[0.6875rem] font-medium text-[#656d76] dark:text-[#8b949e] no-underline flex items-center gap-[0.3rem] hover:text-[#1f2328] dark:hover:text-[#e6edf3] transition-colors">
            View on GitHub
            <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
              <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" />
              <polyline points="15 3 21 3 21 9" />
              <line x1="10" y1="14" x2="21" y2="3" />
            </svg>
          </a>
        </div>}
    </div>;
};

NEAR Drop is a smart contract that allows users to create token drops (`$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:

1. How to create a token drops (NEAR, FT and NFT)
2. How to leverage Function Call keys for enabling amazing UX

<Tip>
  This example showcases a simplified version of the contract that both [Keypom](https://github.com/keypom/keypom) and the [Token Drop Utility](https://docs.near.org/toolbox?tab=linkdrops) use to distribute tokens to users
</Tip>

***

## 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](/protocol/accounts-contracts/access-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:

1. Alice will call `create_near_drop` passing some NEAR amount, and a **Public** Access Key
2. The Contract will check if Alice attached enough tokens and create the drop
3. The Contract will add the `PublicKey` as a `FunctionCall Key` to itself, that **only allow to call the claim methods**
4. Alice will give the `Private Key` to Bob
5. Bob will use the Key to sign a transaction calling the `claim_for` method
6. The Contract will check if the key is linked to a drop, and if it is, it will send the drop

It is important to notice that, in step (5), Bob will be using the Contract's account to sign the transaction, and not his own account. Remember that in step (3) the contract added the key to itself, meaning that anyone with the key can call the claim methods in the name of the contract.

<Accordion title="Contract's interface">
  #### `create_near_drop(public_keys, amount_per_drop)`

  Creates `#public_keys` drops, each with `amount_per_drop` NEAR tokens on them

  #### `create_ft_drop(public_keys, ft_contract, amount_per_drop)`

  Creates `#public_keys` drops, each with `amount_per_drop` FT tokens, corresponding to the `ft_contract`

  #### `create_nft_drop(public_key, nft_contract)`

  Creates a drop with an NFT token, which will come from the `nft_contract`

  #### `claim_for(account_id)`

  Claims a drop, which will be sent to the existing `account_id`

  #### `create_account_and_claim(account_id)`

  Creates the `account_id`, and then drops the tokens into it
</Accordion>

***

## Contract's State

We can see in the contract's state that the contract keeps track of different `PublicKeys`, 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 be `testnet` or `mainnet`
* `next_drop_id`: A simple counter used to assign unique identifiers to each drop
* `drop_id_by_key`: A `Map` between `PublicKey` and `DropId`, which allows the contract to know what drops are claimable by a given key
* `drop_by_id`: A simple `Map` that links each `DropId` with the actual `Drop` data.

<Github fname="lib.rs" language="rust" url="https://github.com/near-examples/near-drop/blob/main/src/lib.rs" start="22" end="29" />

***

## 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).

<Github fname="drop_types.rs" url="https://github.com/near-examples/near-drop/blob/main/src/drop_types.rs" start="8" end="16" />

<Github fname="near_drop.rs" url="https://github.com/near-examples/near-drop/blob/main/src/near_drop.rs" start="9" end="16" />

<Github fname="ft_drop.rs" url="https://github.com/near-examples/near-drop/blob/main/src/ft_drop.rs" start="16" end="24" />

<Github fname="nft_drop.rs" url="https://github.com/near-examples/near-drop/blob/main/src/nft_drop.rs" start="15" end="22" />

<Info>
  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)
</Info>

***

## Create a drop

All `create` 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](/protocol/accounts-contracts/access-keys).

<Tabs>
  <Tab title="NEAR Drop">
    <Github fname="create_near_drop" url="https://github.com/near-examples/near-drop/blob/main/src/lib.rs" start="44" end="66" />

    <Github fname="near_drop" url="https://github.com/near-examples/near-drop/blob/main/src/near_drop.rs" start="63" end="95" />
  </Tab>

  <Tab title="FT Drop">
    <Github fname="create_ft_drop" url="https://github.com/near-examples/near-drop/blob/main/src/lib.rs" start="68" end="89" />

    <Github fname="ft_drop" url="https://github.com/near-examples/near-drop/blob/main/src/ft_drop.rs" start="108" end="142" />
  </Tab>

  <Tab title="NFT Drop">
    <Github fname="create_nft_drop" url="https://github.com/near-examples/near-drop/blob/main/src/lib.rs" start="91" end="103" />

    <Github fname="nft_drop" url="https://github.com/near-examples/near-drop/blob/main/src/nft_drop.rs" start="80" end="106" />
  </Tab>
</Tabs>

### 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:

1. The cost of storing each Drop, which will include storing all bytes associated with the `Drop` struct
2. The cost of storing each `PublicKey -> DropId` relation in the maps
3. Cost of storing each `PublicKey` in the account

Notice that (3) is not the cost of storing the byte representation of the `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 the `Private 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.

<Tabs>
  <Tab title="Existing Account">
    <Github fname="claim_for" url="https://github.com/near-examples/near-drop/blob/main/src/claim.rs" start="11" end="14" />

    <Github fname="internal_claim" url="https://github.com/near-examples/near-drop/blob/main/src/claim.rs" start="58" end="85" />
  </Tab>

  <Tab title="New Account">
    <Github fname="create_account_and_claim" url="https://github.com/near-examples/near-drop/blob/main/src/claim.rs" start="16" end="41" />

    <Github fname="resolve_account_create" url="https://github.com/near-examples/near-drop/blob/main/src/claim.rs" start="43" end="56" />

    <Github fname="internal_claim" url="https://github.com/near-examples/near-drop/blob/main/src/claim.rs" start="58" end="85" />
  </Tab>
</Tabs>

***

### Testing the Contract

The contract readily includes a sandbox testing to validate its functionality. To execute the tests, run the following command:

<Tabs>
  <Tab title="🦀 Rust">
    ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
    cargo test
    ```
  </Tab>
</Tabs>

<Tip>
  The `integration tests` use a sandbox to create NEAR users and simulate interactions with the contract.
</Tip>

***

### Deploying the Contract to the NEAR network

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

<Tabs>
  <Tab title="Short">
    ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
    # Create a new account pre-funded by a faucet
    near create-account <accountId> --useFaucet
    ```
  </Tab>

  <Tab title="Full">
    ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
    # Create a new account pre-funded by a faucet
    near account create-account sponsor-by-faucet-service <my-new-dev-account>.testnet autogenerate-new-keypair save-to-keychain network-config testnet create
    ```
  </Tab>
</Tabs>

Then build and deploy the contract:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
cargo near build

cargo near deploy <accountId> with-init-call new json-args '{"top_level_account": "testnet"}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' network-config testnet sign-with-keychain send
```

***

### CLI: Interacting with the Contract

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

<Tabs>
  <Tab title="Short">
    ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
    # create a NEAR drop
    near call <account-id> create_near_drop '{"public_keys": ["ed25519:AvBVZDQrg8pCpEDFUpgeLYLRGUW8s5h57NGhb1Tc4H5q", "ed25519:4FMNvbvU4epP3HL9mRRefsJ2tMECvNLfAYDa9h8eUEa4"], "amount_per_drop": "10000000000000000000000"}' --useAccount <account-id> --deposit 1 --gas 100000000000000

    # create a FT drop
    near call <account-id> create_ft_drop '{"public_keys": ["ed25519:HcwvxZXSCX341Pe4vo9FLTzoRab9N8MWGZ2isxZjk1b8", "ed25519:5oN7Yk7FKQMKpuP4aroWgNoFfVDLnY3zmRnqYk9fuEvR"], "amount_per_drop": "1", "ft_contract": "<ft-contract-account-id>"}' --useAccount <account-id> --gas 100000000000000

    # create a NFT drop
    near call <account-id> create_nft_drop '{"public_key": "ed25519:HcwvxZXSCX341Pe4vo9FLTzoRab9N8MWGZ2isxZjk1b8", "nft_contract": "<nft-contract-account-id>"}' --useAccount <account-id> --gas 100000000000000

    # claim to an existing account
    # see the full version

    # claim to a new account
    # see the full version
    ```
  </Tab>

  <Tab title="Full">
    ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
    # create a NEAR drop
    near contract call-function as-transaction <account-id> create_near_drop json-args '{"public_keys": ["ed25519:AvBVZDQrg8pCpEDFUpgeLYLRGUW8s5h57NGhb1Tc4H5q", "ed25519:4FMNvbvU4epP3HL9mRRefsJ2tMECvNLfAYDa9h8eUEa4"], "amount_per_drop": "10000000000000000000000"}' prepaid-gas '100.0 Tgas' attached-deposit '1 NEAR' sign-as <account-id> network-config testnet sign-with-keychain send

    # create a FT drop
    near contract call-function as-transaction <account-id> create_ft_drop json-args '{"public_keys": ["ed25519:HcwvxZXSCX341Pe4vo9FLTzoRab9N8MWGZ2isxZjk1b8", "ed25519:5oN7Yk7FKQMKpuP4aroWgNoFfVDLnY3zmRnqYk9fuEvR"], "amount_per_drop": "1", "ft_contract": "<ft-contract-account-id>"}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' sign-as <account-id> network-config testnet sign-with-keychain send

    # create a NFT drop
    near contract call-function as-transaction <account-id> create_nft_drop json-args '{"public_key": "ed25519:HcwvxZXSCX341Pe4vo9FLTzoRab9N8MWGZ2isxZjk1b8", "nft_contract": "<nft-contract-account-id>"}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' sign-as <account-id> network-config testnet sign-with-keychain send

    # claim to an existing account
    near contract call-function as-transaction <account-id> claim_for json-args '{"account_id": "<claimer-account-id>"}' prepaid-gas '30.0 Tgas' attached-deposit '0 NEAR' sign-as <account-id> network-config testnet sign-with-plaintext-private-key --signer-public-key ed25519:AvBVZDQrg8pCpEDFUpgeLYLRGUW8s5h57NGhb1Tc4H5q --signer-private-key ed25519:3yVFxYtyk7ZKEMshioC3BofK8zu2q6Y5hhMKHcV41p5QchFdQRzHYUugsoLtqV3Lj4zURGYnHqMqt7zhZZ2QhdgB send

    # claim to a new account
    near contract call-function as-transaction <account-id> create_account_and_claim json-args '{"account_id": "<claimer-account-id>"}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' sign-as <account-id> network-config testnet sign-with-plaintext-private-key --signer-public-key ed25519:4FMNvbvU4epP3HL9mRRefsJ2tMECvNLfAYDa9h8eUEa4 --signer-private-key ed25519:2xZcegrZvP52VrhehvApnx4McL85hcSBq1JETJrjuESC6v6TwTcr4VVdzxaCReyMCJvx9V4X1ppv8cFFeQZ6hJzU send
    ```
  </Tab>
</Tabs>

<Note>
  **Versioning for this article**

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

  * near-cli: `0.17.0`
  * rustc: `1.82.0`
</Note>
