embark/docs/using-contracts.rst

180 lines
4.0 KiB
ReStructuredText
Raw Normal View History

2017-03-04 23:36:00 +00:00
Configuring & Using Contracts
=============================
2017-01-14 23:11:43 +00:00
Embark will automatically take care of deployment for you and set all
needed JS bindings. For example, the contract below:
.. code:: javascript
# app/contracts/simple_storage.sol
contract SimpleStorage {
uint public storedData;
function SimpleStorage(uint initialValue) {
storedData = initialValue;
}
function set(uint x) {
storedData = x;
}
function get() constant returns (uint retVal) {
return storedData;
}
}
Will automatically be available in Javascript as:
.. code:: javascript
# app/js/index.js
2018-03-15 20:11:42 +00:00
import SimpleStorage from 'Embark/contracts/SimpleStorage';
SimpleStorage.methods.set(100).send();
SimpleStorage.methods.get().call().then(function(value) { console.log(value) });
SimpleStorage.methods.storedData().call().then(function(value) { console.log(value) });
2017-01-14 23:11:43 +00:00
You can specify for each contract and environment its gas costs and
arguments:
.. code:: json
# config/contracts.json
{
"development": {
"gas": "auto",
"contracts": {
"SimpleStorage": {
"args": [
100
2018-03-15 20:11:42 +00:00
],
"gas": 800000
2017-01-14 23:11:43 +00:00
}
}
}
}
If you are using multiple contracts, you can pass a reference to another
contract as ``$ContractName``, Embark will automatically replace this
with the correct address for the contract.
2018-03-15 20:11:42 +00:00
You can also specify interfaces and choose to not deploy contracts (for e.g in case they are interfaces)
2017-01-14 23:11:43 +00:00
.. code:: json
# config/contracts.json
{
...
"development": {
"contracts": {
"SimpleStorage": {
"args": [
100,
"$MyStorage"
2017-01-14 23:11:43 +00:00
]
},
"MyStorage": {
"args": [
"initial string"
]
},
"MyMainContract": {
"args": [
"$SimpleStorage"
2017-01-14 23:11:43 +00:00
]
2018-03-15 20:11:42 +00:00
},
"MyContractInteface": {
"deploy": false
2017-01-14 23:11:43 +00:00
}
}
}
...
}
You can now deploy many instances of the same contract. e.g
.. code:: json
# config/contracts.json
{
"development": {
"contracts": {
"Currency": {
"deploy": false,
"args": [
100
]
},
"Usd": {
"instanceOf": "Currency",
"args": [
200
]
},
"MyCoin": {
"instanceOf": "Currency",
"args": [
200
]
}
}
}
}
...
Account from which you want to deploy a contract can be specified using "from" or "fromIndex" parameters.
| "from" - should be account address string.
| "fromIndex" - should be index in accounts array as retrieved by web3.eth.getAccounts() .
If both "from" and "fromIndex" are specified, the "from" will be used.
Example:
.. code:: json
# config/contracts.json
{
"development": {
"contracts": {
"Currency": {
"deploy": true,
"from": '0xfeedaa0e295b09cd84d6ea2cce390eb443bcfdfc',
"args": [
100
]
},
"MyStorage": {
"fromIndex": 0,
"args": [
"initial string"
]
},
}
}
}
...
2017-01-14 23:11:43 +00:00
Contracts addresses can be defined, If an address is defined the
contract wouldn't be deployed but its defined address will be used
instead.
.. code:: json
# config/contracts.json
{
...
"development": {
"contracts": {
"UserStorage": {
"address": "0x123456"
},
"UserManagement": {
"args": [
"$UserStorage"
]
}
}
}
...
}
2018-03-15 20:11:42 +00:00