# Deployer

## Deployer

### Deployer functionality

The main functionality the deployer exposes is (obviously) the ability to deploy compiled contract.

This is achieved through the `deploy(contract, [libraries], [params])` function.

#### deploy(contract, \[libraries], \[params])

Parameters:

* `contract` - descriptor object for contract to be deployed. More

  details below
* `libraries` - key-value object containing all libraries which will

  be linked to the contract.
* `params` - the constructor params you'd need to pass on deploy (if

  there are any)

The contract is descriptor object that needs to have atleast the following three fields:

* `contractName` - the name of the contract
* `abi` - the abi interface of the contract
* `bytecode` - the compiled bytecode

The easiest way to get such descriptor is to compile your solidity files via etherlime compile

The libraries object should be in the following format:

```javascript
    {
        libraryName0: '0xAddressOfLibrary0',
        libraryName1: '0xAddressOfLibrary1'
    }
```

If the contract to be deployed doesn't contains any libraries, `{}`, `undefined`, `null`, `false` or `0` can be passed. For convenience we have made the deploy function to work even without this parameter passed.

Example

Linking libraries

```javascript
    const contractUsingQueueAndLinkedList = require('...');

    const libraries = {
        Queue: '0x655341AabD39a5ee0939796dF610aD685a984C53,
        LinkedList: '0x619acBB5Dafc5aC340B6de4821835aF50adb29c1'
    }

    await deployer.deploy(contractUsingQueueAndLinkedList, libraries);
```

Skipping linking on contract without arguments

```javascript
    const contractWithoutLibraries = require('...');

    await deployer.deploy(contractWithoutLibraries);
```

Skipping linking on contract with arguments

```javascript
    const contractWithoutLibraries = require('...');

    await deployer.deploy(contractWithoutLibraries, false, param1, param2);
```

#### deployAndVerify(platform, contract, \[libraries], \[params])

The main functionality the deployAndVerify exposes is (obviously) the ability to deploy and then verify compiled contract on Etherscan or Blockscout platforms. This method exposes the same features as `deploy` method, but in addition automatically verifies the deployed smart contract using Etherscan/Blockscout APIs.

In order to use the `deployAndVerify` method of the deployer for Etherscan platform, an API Key is required. You can create your Etherscan API Key [here](https://etherscan.io/login?cmd=last).

Parameters:

* `platform` - point the platform you want to verify your contract. Choices: `etherscan` , `blockscout`&#x20;
* `contract` - descriptor object for contract to be deployed. More

  details below
* `libraries` - key-value object containing all libraries which will

  be linked to the contract.
* `params` - the constructor params you'd need to pass on deploy (if

  there are any)

The deployment method reads the API Key (needed only for using Etherscan) form the deployer `defaultOverrides` object.

Passing API Key to the deployer:

* Passing the API Key to the `defaultOverrides` object:

```javascript
    deployer.defaultOverrides = { gasLimit: 4700000, gasPrice: 3000000000, etherscanApiKey: '3DQYBPZZS77YDR15NKJHURVTV9WI2KH6UY' };
```

* Setting the API Key through the deployer `setVerifierApiKey` setter:

```javascript
    deployer.setVerifierApiKey('3DQYBPZZS77YDR15NKJHURVTV9WI2KH6UY')
```

* Passing the API Key from `etherlime deploy` command with optional parameter

  ```
  etherscanApiKey:  `etherlime deploy --secret="Your private key" --network="rinkeby" --etherscanApiKey="3DQYBPZZS77YDR15NKJHURVTV9WI2KH6UY"`
  ```

```javascript
    const deploy = async (network, secret, etherscanApiKey) => {
    const deployer = new etherlime.InfuraPrivateKeyDeployer(secret, network, "INFURA_API_KEY");
    deployer.defaultOverrides = { gasLimit: 4700000, gasPrice: 3000000000, etherscanApiKey };
    };
```

Network is automatically detected based on the network that the deployer is set to deploy. The supported networks are:

* `mainnet`
* `ropsten`
* `rinkeby`
* `kovan`
* `goerli`

#### estimateGas(contract, \[libraries], \[params])

Estimates the gas that this transaction is going to cost you.

Parameters:

* `contract` - descriptor object for contract to be deployed
* `libraries` - key-value object containing all libraries which will

  be linked to the contract.
* `params` - the constructor params you'd need to pass on deploy (if

  there are any)

The contract is descriptor object is the same as above.

Example :

```javascript
    const estimate = await deployer.estimateGas(TestContract, randomParam1, randomParam2);
    // returns something like "2470692"
```

### Deployers

## InfuraPrivateKeyDeployer

```
InfuraPrivateKeyDeployer(privateKey, network, apiKey, [defaultOverrides])
```

Parameters:

* `privateKey` - The private key to the deployment wallet/signer

  instance
* `network` - network as found in `ethers.providers.networks`
* `apiKey` - your Infura API key
* `defaultOverrides` - \[Optional] object overriding the deployment

  settings for `gasPrice` , `gasLimit` and `chainId`.

```javascript
    const etherlime = require('etherlime-lib');

    const TestContract = require('./TestContract.json');

    const defaultConfigs = {
        gasPrice: 20000000000,
        gasLimit: 4700000,
        chainId: 0 // Suitable for deploying on private networks like Quorum
    }

    const deploy = async (network, secret) => {

        const deployer = new etherlime.InfuraPrivateKeyDeployer('Your Private Key Goes Here',
           'ropsten', 'Your Infura API Key', defaultConfigs);

        const result = await deployer.deploy(TestContract, '0xda8a06f1c910cab18ad187be1faa2b8606c2ec86', 1539426974);
    }
```

### Setters

> * provider . setPrivateKey (privateKey)
>   * `privateKey` - The private key to the deployment wallet/signer
>
>     instance
> * provider . setNetwork (network)
>   * `network` - network as found in `ethers.providers.networks`
> * provider . setApiKey (apiKey)
>   * `apiKey` - your Infura API key
> * provider . setDefaultOverrides (defaultOverrides)
>   * `defaultOverrides` - object overriding the deployment settings
>
>     for `gasPrice` , `gasLimit` and `chainId`.
> * provider . setSigner (signer)
>   * `signer` - ethers.Wallet instance
> * provider . setProvider (provider)
>   * `provider` - ethers.provider instance

Example :

```javascript
    const deployer = new etherlime.InfuraPrivateKeyDeployer(privateKey,
       network, apiKey, defaultConfigs);
    const newNetwork = 'ropsten';
    deployer.setNetwork(newNetwork);
```

## JSONRPCPrivateKeyDeployer

```
JSONRPCPrivateKeyDeployer(privateKey, nodeUrl, [defaultOverrides])
```

Parameters:

* `privateKey` - The private key to the deployment wallet/signer

  instance
* `nodeUrl` - the url to the node you are trying to connect (local or

  remote). The `nodeUrl` may also be specified as an object with properties:

  * url — the JSON-RPC URL (required)
  * user — a username to use for Basic Authentication \[optional]
  * password — a password to use for Basic Authentication \[optional]
  * allowInsecure — allow Basic Authentication over an insecure HTTP network (default: false)
* `defaultOverrides` - \[Optional] object overriding the deployment

  settings for `gasPrice` , `gasLimit` and `chainId`.

```javascript
    const etherlime = require('etherlime-lib');

    const TestContract = require('./TestContract.json');

    const defaultConfigs = {
        gasPrice: 20000000000,
        gasLimit: 4700000,
        chainId: 0 // Suitable for deploying on private networks like Quorum
    }

    const deploy = async (network, secret) => {

        const deployer = new etherlime.JSONRPCPrivateKeyDeployer('Your Private Key Goes Here',
           'http://localhost:8545/', defaultConfigs);

        const result = await deployer.deploy(TestContract);
    }
```

### Setters

> * provider . setPrivateKey (privateKey)
>   * `privateKey` - The private key to the deployment wallet/signer
>
>     instance
> * provider . setNodeUrl (nodeUrl)
>   * `nodeUrl` - the url to the node you are trying to connect
>
>     (local or remote)
> * provider . setDefaultOverrides (defaultOverrides)
>   * `defaultOverrides` - object overriding the deployment settings
>
>     for `gasPrice` , `gasLimit` and `chainId`.
> * provider . setSigner (signer)
>   * `signer` - ethers.Wallet instance
> * provider . setProvider (provider)
>   * `provider` - ethers.provider instance

Example :

```javascript
    const deployer = new etherlime.JSONRPCPrivateKeyDeployer(privateKey,
       nodeUrl, defaultOverrides);
    const newNodeUrl = http://localhost:9545;
    deployer.setNodeUrl(newNodeUrl);
```

## EtherlimeGanacheDeployer

```
EtherlimeGanacheDeployer([privateKey], [port], [defaultOverrides])
```

Parameters:

* `privateKey` - \[Optional] The private key to the deployment

  wallet/signer instance. Defauts to the first one in the

  etherlime ganache
* `port` - \[Optional] the port you've ran the etherlime ganache on.

  Defaults to 8545.
* `defaultOverrides` - \[Optional] object overriding the deployment

  settings for `gasPrice` , `gasLimit` and `chainId`.

**This deployer only works with etherlime ganache**

```javascript
    const etherlime = require('etherlime-lib');

    const TestContract = require('./TestContract.json');

    const defaultConfigs = {
        gasPrice: 20000000000,
        gasLimit: 4700000,
        chainId: 0 // Suitable for deploying on private networks like Quorum
    }

    const deploy = async (network, secret) => {

        const deployer = new etherlime.EtherlimeGanacheDeployer();

        const result = await deployer.deploy(TestContract);
    }
```

### Setters

> * provider . setPrivateKey (privateKey)
>   * `privateKey` - The private key to the deployment wallet/signer
>
>     instance
> * provider . setPort (port)
>   * `port` - the port you've ran the etherlime ganache on.
> * provider . setDefaultOverrides (defaultOverrides)
>   * `defaultOverrides` - object overriding the deployment settings
>
>     for `gasPrice` , `gasLimit` and `chainId`.
> * provider . setNodeUrl (nodeUrl)
>   * `nodeUrl` - the url to the node you are trying to connect
>
>     (local or remote)
> * provider . setSigner (signer)
>   * `signer` - ethers.Wallet instance
> * provider . setProvider (provider)
>   * `provider` - ethers.provider instance

Example :

```javascript
    const deployer = new etherlime.EtherlimeGanacheDeployer();
    const port = 9545;
    deployer.setPort(port);
```

## ZosJSONRPCPrivateKeyDeployer

ZosJSONRPCPrivateKeyDeployer is a powerful object giving you the ability to deploy and operate with a smart contract on ZeppelinOS platform.

To use it, first you need to add `etherlime-zos-deployer` as a dependency in your project with `npm install`.

```
    ZosJSONRPCPrivateKeyDeployer(privateKey, nodeUrl, [defaultOverrides])
```

Parameters:

* `privateKey` - The private key to the deployment wallet/signer instance
* `nodeUrl` - the url to the node you are trying to connect (local or remote)
* `defaultOverrides` - \[Optional] object overriding the deployment settings for `gasPrice` , `gasLimit` and `chainId`.

```javascript
    const etherlime = require('etherlime-zos-deployer');

    const TestContract = require('./TestContract.json');

    const defaultConfigs = {
        gasPrice: 20000000000,
        gasLimit: 4700000,
        chainId: 0 // Suitable for deploying on private networks like Quorum
    }

    const deploy = async (network, secret) => {

        const deployer = new etherlime.ZosJSONRPCPrivateKeyDeployer('Your Private Key Goes Here', 'http://localhost:8545/', defaultConfigs);

        const result = await deployer.deploy(TestContract);
    }
```

### Setters

> * provider.setPrivateKey(privateKey)
>   * `privateKey` - The private key to the deployment wallet/signer instance
> * provider.setNodeUrl(nodeUrl)
>   * `nodeUrl` - the url to the node you are trying to connect (local or remote)
> * provider.setDefaultOverrides(defaultOverrides)
>   * `defaultOverrides` - object overriding the deployment settings for `gasPrice` , `gasLimit` and `chainId`.
> * provider.setSigner(signer)
>   * `signer` - ethers.Wallet instance
> * provider.setProvider(provider)
>   * `provider` - ethers.provider instance

### Example

```javascript
    const deployer = new etherlime.ZosJSONRPCPrivateKeyDeployer(privateKey, nodeUrl, defaultOverrides);
     const newNodeUrl = http://localhost:9545;
    deployer.setNodeUrl(newNodeUrl);
```
