# 5. Using Conflux-Truffle

{% hint style="info" %}
This tutorial is suitable for Conflux Core.
{% endhint %}

## 1. Open Sandbox

Note: Sandbox functionality is only available after logging in to ChainIDE via GitHub.

<figure><img src="https://3745657014-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MYy-lqJKjq1m0yBAX4r%2Fuploads%2FsJQgwCxT9QAAm5TrHmP1%2Fimage.png?alt=media&#x26;token=766b14c5-2276-4438-993b-3fb69d471935" alt=""><figcaption></figcaption></figure>

Open Sandbox

<figure><img src="https://3745657014-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MYy-lqJKjq1m0yBAX4r%2Fuploads%2F5OSb4GRhuQED8aWNrz4S%2Fimage.png?alt=media&#x26;token=a325bb25-916b-442d-9ad7-0c8747719c69" alt=""><figcaption></figcaption></figure>

## 2. Create a project

**create project**

create a empty project

```
$ cfxtruffle init 
```

create project from templates -- [box](https://www.trufflesuite.com/boxes)

```
$ mkdir MetaCoin
$ cd MetaCoin
$ cfxtruffle unbox metacoin
```

A cfxtruffle project will include these folders:

* `build` contract compiled stuff（json）
* `contracts` solidity code
* `migrations` migration scripts
* `test` testing file
* `truffle-config.js` config file

**create new contracts, tests, migrations**

`cfxtruffle create` Helper to create new contracts, migrations and tests

```
$ cfxtruffle create contract MetaCoin
$ cfxtruffle create migration MetaCoin
$ cfxtruffle create test MetaCoin
```

The create command will create files in `contracts`, `migrations`, `test` folder. Notice the automated created migration file's name will include timestamp, you need manual change to the correct sequal number. For detail reason check [here](https://www.trufflesuite.com/docs/truffle/getting-started/running-migrations#migration-files)

## **3. Compile contract**

```
$ cfxtruffle compile
```

All compiled stuff saved at `build` folder.

### 4. Deploy to remote node

cfxtruffle now support deploy contract to a remote node, the only work to do is set the `privateKeys` in truffle-config.

```json
development: {
    host: "test.confluxrpc.com",
    port: 80,
    network_id: "*",
    type: "conflux",
    // the magic field
    privateKeys: ["keys1xxxxxx", "keys1xxxxxx"],   // you can also directly set one key here: privateKeys: "one key"
},
```

## **5. deploy contract**

```
$ cfxtruffle deploy # or cfxtruffle migrate
```

`deploy` is a alias to `migrate`, truffle use `migration` command run contract deploy and migrate.

1. Every script in migration folder define a migrate task, if you add a new contract you should add a new migration scripts.
2. Every truffle project will have a contract called `Migration` which used to save the project's last migration number, this contract have two method: `last_completed_migration()`, `setCompleted(num)`
3. When cfxtruffle deploy run it will get the last deployed number from chain, will only run new added migration tasks.

`cfxtruffle deploy` also provide three paramter (--reset, --from, --to) which can control the migration running rule. For detail explanation check [here](https://www.trufflesuite.com/docs/truffle/getting-started/running-migrations)

## **6. interact with contract**

cfxtruffle also provide a command that enable us interact with a contract convenient.

```bash
$ cfxtruffle console  # run console in your project root, it will open a interactive console
# initiate a contract instanse
cfxtruffle(develop)> let instance = await MetaCoin.deployed()
cfxtruffle(develop)> instance
# invoke contract state query method
cfxtruffle(develop)> let balance = await instance.getBalance(accounts[0])
cfxtruffle(develop)> balance.toNumber()
# invoke contract state change method
cfxtruffle(develop)> let result = await instance.sendCoin(accounts[1], 10, {from: accounts[0]})
cfxtruffle(develop)> result
# most truffle commands also available here
cfxtruffle(develop)> compile
cfxtruffle(develop)> networks
# You can also access to `js-conflux-sdk`'s cfx, cfxutil
cfxtruffle(develop)> let balance = await cfx.getBalance("0x-one-address")
cfxtruffle(develop)> cfxutil.unit.fromCFXToDrip(123)
```

For detail documentation check truffle [console](https://www.trufflesuite.com/docs/truffle/getting-started/using-truffle-develop-and-the-console) and [interact with contract](https://www.trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts). And for the documentation of `js-conflux-sdk` find it [here](https://github.com/conflux-chain/js-conflux-sdk)

## **7. Contract testing**

Truffle comes standard with an automated testing framework (built onto mocha and chai)

Unit test code examples

```javascript
const MetaCoin = artifacts.require("MetaCoin");

contract('MetaCoin', (accounts) => {
  it('should put 10000 MetaCoin in the first account', async () => {
    const metaCoinInstance = await MetaCoin.deployed();
    const balance = await metaCoinInstance.getBalance.call(accounts[0]);

    assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
  });
}
```

run tests

```
$ cfxtruffle test
```

## 8. Truffle commands not supported now

* develop
* build

## 9. Conclude

This quickstart showed you the basics of the Truffle project lifecycle, but there is much more to learn. Please continue on with the rest of [conflux-truffle github repo](https://github.com/Conflux-Chain/conflux-truffle).
