# Quickstart

## Setting up the client

Instantiate the `ToucanClient` and set a `signer` & `provider` to interact with our smart contracts.

{% hint style="info" %}
We recommend using to use [ethers.js ^5.6.4](https://docs.ethers.org/v5/api/signer/) for the signer and provider.
{% endhint %}

{% tabs %}
{% tab title="ethers.js" %}
Read the [ethers.js `Signer` docs ->](https://docs.ethers.org/v5/api/signer/)&#x20;

<table><thead><tr><th width="260">Network</th><th>JsonRpcProvider</th><th data-hidden>JsonRpcProvider</th><th data-hidden></th></tr></thead><tbody><tr><td>Celo Mainnet</td><td><code>https://forno.celo.org</code></td><td></td><td></td></tr><tr><td>Celo Alfajores Testnet</td><td><code>https://alfajores-forno.celo-testnet.org</code></td><td></td><td></td></tr><tr><td>Polygon Mainnet</td><td><code>https://polygon-rpc.com</code></td><td></td><td></td></tr></tbody></table>

<pre class="language-typescript"><code class="lang-typescript"><strong>import ToucanModule from "toucan-sdk";
</strong>import { ethers } from "ethers";

const ToucanClient = ToucanModule.default;

// ethers signer and provider
const provider = new ethers.providers.JsonRpcProvider(
  "https://rpc.ankr.com/polygon"
);

// make sure to set your private key in your .env file
const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

// set signer &#x26; provider
const toucan = new ToucanClient("polygon", provider, signer);
</code></pre>

{% endtab %}

{% tab title="wagmi" %}
{% hint style="info" %}
Note that when you are considering using [wagmi](https://0.3.x.wagmi.sh/docs/hooks/useSigner), only versions under 1.0 will work as this library has not yet been upgraded to [viem](https://viem.sh/).
{% endhint %}

Read the[ wagmi `useSigner` docs ->](https://0.3.x.wagmi.sh/docs/hooks/useSigner)

```typescript
import ToucanClient from "toucan-sdk";
import { useProvider, useSigner } from "wagmi";

// get signer & provider
const { data: signer } = useSigner();
const provider = useProvider();

// set signer & provider
const toucan = new ToucanClient("alfajores", provider, signer);
```

{% endtab %}
{% endtabs %}

You could also set the `signer` and / or `provider` later if you prefer that, they are optional — but you will need to set them if you want to interact with contracts. The `provider` is read-only, while the `signer` allows both writing to and reading from the blockchain.

```typescript
import ToucanClient from "toucan-sdk";

const toucan = new ToucanClient("polygon");
toucan.setProvider(provider);
toucan.setSigner(signer);
```

If you don't have a signer or a provider set, you can still interact with the subgraph.

## Retire Carbon Credits

To retire carbon credits on mainnet, you will need to have TCO2 tokens in your account. These can be acquired by buying carbon reference tokens from a DEX like [Uniswap](https://uniswap.org/), then [redeeming](https://docs.toucan.earth/toucan/carbon-pools#so-what-is-a-carbon-pool) the reference tokens for TCO2s. You can then retire these TCO2 tokens through the Toucan app UI, or programmatically with the `ToucanClient.retire()` method.

If you already own NCT tokens, you can follow this example. Get your test tokens at the [Toucan Faucet](https://faucet.toucan.earth). You can find more ways to retire, and mint `RetirementCertificates`, in [this](https://docs.toucan.earth/developers/sdk/contract-interactions) list of all SDK functions.

**Redeem your Pool Tokens and get an array of redeemed TCO2s**

```typescript
const tco2addresses = await toucan.redeemAuto("NCT", parseEther("1"));
```

**Retire the TCO2 tokens**

```typescript
await toucan.retire(parseEther("1"), tco2addresses[0].address);
```

## Offset Carbon Credits with the OffsetHelper

The functions in the OffsetHelper will bundle the following steps into a single transaction:

* Exchange ERC20 tokens e.g., cUSD or USDC for pool tokens (e.g., NCT) at a DEX like [Uniswap](https://uniswap.org/) or [SushiSwap](https://www.sushi.com), etc. (depending on the network)
* Interact with the pool contract to redeem the tokens for TCO2
* Interact with the TCO2 token contract to retire the TCO2 the amount purchased and redeemed

{% hint style="info" %}
Note that if you're using the swap functionality, the exact number of TCO2 tokens retired will depend on the stablecoin : reference token exchange rate, which can vary moment to moment.
{% endhint %}

Using these functions, you can easily offset TCO2 tokens using other cryptocurrencies and stablecoins. Bear in mind that using these functions will not let you choose a specific project to retire. To specify your the project you want to retire, you'd need to use the [`redeemMany`](https://docs.toucan.earth/developers/contract-interactions#redeemmany) function, which is not supported by the OffsetHelper at this point.

```typescript
const cUSD = "0x765DE816845861e75A25fCA122bb6898B8B1282a";
const tx = await toucan.autoOffsetExactInToken(cUSD, "NCT", parseEther("0.01"));
```
