# @chainlink/contracts

Chainlink's solidity contracts and contract abstractions

# Migration Notice

This package stores all of chainlink's smart contracts, previously known as the `evm/` directory. The previous package was called `chainlink`, now it is `@chainlink/contracts`.

# Layout

This package makes use of `@chainlink/belt` to output artifacts along with ethers.js and truffle contract abstractions from the smart contracts stored within `src`. The resulting layout of the package when published looks like:

```sh
contracts
├── abi # abi output from src/
│   ├── v0.4
│   ├── v0.5
│   └── v0.6
├── ethers # ethers contract abstractions codegenned from abis
│   ├── v0.4
│   ├── v0.5
│   └── v0.6
├── src # the contracts themselves, in .sol form
│   ├── v0.4
│   ├── v0.5
│   └── v0.6
└── truffle  # truffle contract abstractions codegenned from abis
    ├── v0.4
    ├── v0.5
    └── v0.6
```

# Usage

Install this package with the following command:

```sh
yarn add @chainlink/contracts
```

These smart contracts can be imported as a dependency in various ways described below.

## Solidity

The solidity smart contracts themselves can simply be imported via the `src` directory of `@chainlink/contracts`. If you wanted to consume the v0.4.x version of our Oracle smart contract, you could do the following:

```solidity
import "@chainlink/contracts/src/v0.4/Chainlinked.sol";
```

## Artifacts

JSON artifacts generated by [sol-compiler](https://sol-compiler.com/) are available under the `abi` directory. If you wanted to consume the v0.4.x version of our Oracle JSON artifact with Javascript, you could do the following:

```js
const OracleV4Json = require('@chainlink/contracts/abi/v0.4/Oracle.json')
```

## Ethers

This library ships with [ethers.js](https://github.com/ethers-io/ethers.js/) contract abstractions generated by [typechain](https://github.com/ethereum-ts/TypeChain). To use these, make sure you have ethers.js installed as a dependency:

```sh
yarn add ethers@^4.0.44
```

If you wanted to consume the v0.4.x version of our Oracle with Javascript, you could do the following:

```ts
import { OracleFactory } from '@chainlink/contracts/ethers/v0.4/OracleFactory'
```

This gives a fully typed (if using typescript) version of a ethers.js contract factory. See the ethers [documentation](https://docs.ethers.io/ethers.js/html/api-contract.html) on usage.

## Truffle

This library ships with [@truffle/contract](https://github.com/trufflesuite/truffle/tree/master/packages/contract#readme) abstractions of each of our smart contracts. To use these, make sure you have @truffle/contract as a dependency.

```sh
yarn add @truffle/contract@^4.1.8
```

If you wanted to consume the v0.4.x version of our Oracle with Javascript, you could do the following:

```js
const { Oracle } = require('@chainlink/contracts/truffle/v0.4/Oracle')
```

For usage, see the @truffle/contract documentation.

For ease of use with testing, if the environment variable `NODE_ENV` is set, the imported truffle contract abstraction will automatically set its provider to `web3.currentProvider`, avoiding the need to perform a `setProvider` call before using it during your truffle tests.

```sh
NODE_ENV=true yarn truffle test
```

## Development


### Setup

After cloning the [chainlink repo](https://github.com/smartcontractkit/chainlink/), run `yarn install` and then `yarn setup:contracts`.

### Testing

After walking through the above setup commands, you can run `yarn test` from within `evm-contracts/` or run `yarn wsrun @chainlink/contracts test` from the root.
