5. Using Conflux-Truffle
Use Conflux-Truffle to quickly build a dApp on Conflux Core
Last updated
Was this helpful?
Use Conflux-Truffle to quickly build a dApp on Conflux Core
Last updated
Was this helpful?
Note: Sandbox functionality is only available after logging in to ChainIDE via GitHub.
Open Sandbox
create project
create a empty project
$ cfxtruffle init
create project from templates -- box
$ 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
$ cfxtruffle compile
All compiled stuff saved at build
folder.
cfxtruffle now support deploy contract to a remote node, the only work to do is set the privateKeys
in truffle-config.
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"
},
$ cfxtruffle deploy # or cfxtruffle migrate
deploy
is a alias to migrate
, truffle use migration
command run contract deploy and migrate.
Every script in migration folder define a migrate task, if you add a new contract you should add a new migration scripts.
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)
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
cfxtruffle also provide a command that enable us interact with a contract convenient.
$ 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 and interact with contract. And for the documentation of js-conflux-sdk
find it here
Truffle comes standard with an automated testing framework (built onto mocha and chai)
Unit test code examples
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
develop
build
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.