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

# Deploying to Testnet

> Lets deploy our action contract to testnet.

export const Github = ({url, start, end, fname, language, 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 fileName = fname ?? sourceUrl.split('/').pop();
  return <div className="my-5">
      {code === null ? <div>Loading...</div> : <CodeBlock language={language} filename={fileName} lines>
          {code}
        </CodeBlock>}
      {withSourceLink && <div className="flex justify-end" style={{
    marginTop: "-1rem"
  }}>
          <a href={sourceUrl} target="_blank" rel="noreferrer noopener" className="text-[0.6875rem] font-medium text-[#656d76] no-underline hover:text-[#1f2328] dark:text-[#8b949e] dark:hover:text-[#e6edf3]">
            See code on GitHub
          </a>
        </div>}
    </div>;
};

In the previous sections, we saw how a simple auction smart contract is implemented, and checked its correctness using sandbox testing.

The time has come to release it on the actual blockchain and interact with it! In this section, we will show you how to create a simple testnet account, deploy the contract, and interact with it from the CLI.

<Info>
  **Networks**

  NEAR has two main networks for you to use: `testnet` and `mainnet`. The `testnet` network behaves exactly as the main network but uses test tokens with no real value
</Info>

***

## Testnet Account

To deploy a contract, you need a testnet account. If you don't have one, you can create one using the following command:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
# create-account using near-cli (contractId has end with .testnet)
near create <contractId> --useFaucet
```

Replace `<contractId>` with the name you want to give to your account, and make sure it ends with `.testnet`.

The account will be created with **10 NEAR** (these are test tokens).

<Info>
  **Testnet Faucet**

  Notice that we are using the `--useFaucet` flag to automatically request test tokens from the NEAR faucet.

  The faucet is only available on the testnet network - which is the default network for the CLI
</Info>

***

## Deploying the Contract

To deploy the contract, you need to compile the contract code into WebAssembly (WASM) and then deploy it to the network

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
# compile the contract using cargo-near
cargo near build

# deploy the contract
near deploy <contractId> ./target/near/auction-contract.wasm

# initialize the contract, it finishes in 2 minutes
TWO_MINUTES_FROM_NOW=$(date -v+2M +%s000000000)
near call <contractId> init '{"end_time": "'$TWO_MINUTES_FROM_NOW'", "auctioneer": "<auctioneerId>"}' --useAccount <contractId>
```

Replace `<auctioneerId>` with the name of another account, this should not be the same as the contract account as we intend on removing its keys.

***

## Locking the contract

As mentioned previously we should lock the account by removing the keys. This allows our users to interact with the contract without having to trust the account owner.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
near account delete-keys 
```

Next, specify the contract account and click the right arrow → to delete all the keys. Make sure you select testnet.

<Warning>
  Be extra careful to delete the keys from the correct account as you'll never be able to access the account again!
</Warning>

Now that the contract is deployed, initialized, and locked we can send transactions to it using the CLI.

<Tip>
  **Interactive CLI**
  NEAR's CLI is interactive meaning you can type `near` and click through all the possible options without having to remember certain commands
</Tip>

***

## Interacting with the Contract

We are now ready to start bidding by calling the `bid` function on the contract. We recommend that you create **two new accounts** to simulate different bidders.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
# call the contract to bid 
near call <contractId> bid --useAccount <bidderId> --deposit 1 

# get the highest bid
near view <contractId> get_highest_bid
```

Notice that we call the `bid` function without arguments, but attach 1 NEAR to the transaction. This is the amount we are bidding.

For the `get_highest_bid` function, we don't need to specify which user is calling it, as it is a view function and does not require gas to be executed.

***

## Conclusion

We have now seen how to deploy a contract to `testnet` and interact with it using the NEAR CLI.

A word of advice before moving forward. When people learn how to use the CLI, they get lazy and start testing new contract features directly on the testnet. While this is tempting, it is not recommended.

Do not use testnet as your **only way** to test contracts. Always test your contracts on the **sandbox environment first**, and only deploy to the testnet when you are confident that everything is working as expected.

<Tip>
  **Frontend**

  Generally, you will use the CLI only to deploy and initialize the contract. Afterward, all interactions will be made from a frontend. This is why in the [next section](./2.1-frontend) we'll move on to creating a frontend to interact with the contract.
</Tip>
