You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.2 KiB
109 lines
3.2 KiB
6 years ago
|
Creating and Deploying a Contract
|
||
|
================================
|
||
6 years ago
|
|
||
|
There are 3 type of environments Remix can be plugged to:
|
||
|
`Javascript VM`, `Injected provider`, or `Web3 provider`. (for details see [Running transactions](http://remix.readthedocs.io/en/latest/run_tab.html))
|
||
|
|
||
|
Both `Web3 provider` and `Injected provider` require the use of an
|
||
|
external tool.
|
||
|
|
||
6 years ago
|
The external tool for `Web3 provider` is an Ethereum node and for
|
||
|
`Injected provider` Metamask.
|
||
6 years ago
|
|
||
|
The `JavaScript VM` mode is convenient because each execution runs in
|
||
6 years ago
|
your browser and you don't need any other software or Ethereum node to run it.
|
||
|
|
||
|
So, it is the easiest test environment - **no setup required!**
|
||
|
|
||
|
But keep in mind that reloading the browser when you are in the Javascript VM will restart Remix in an empty state.
|
||
6 years ago
|
|
||
6 years ago
|
For performance purposes ( which is to say - for testing in an environment that is closest to the mainnet), it might also be better to use an external node.
|
||
6 years ago
|
|
||
|
Selecting the VM mode
|
||
|
---------------------
|
||
|
|
||
|
Make sure the VM mode is selected. All accounts displayed in `Accounts`
|
||
|
should have 100 ether.
|
||
|
|
||
|
Sample contract
|
||
|
---------------
|
||
|
|
||
6 years ago
|
```
|
||
|
{.sourceCode .none}
|
||
|
pragma solidity ^0.5.1;
|
||
6 years ago
|
|
||
|
contract testContract {
|
||
|
|
||
|
uint value;
|
||
6 years ago
|
|
||
|
constructor (uint _p) public {
|
||
6 years ago
|
value = _p;
|
||
|
}
|
||
|
|
||
6 years ago
|
function setP(uint _n) payable public {
|
||
6 years ago
|
value = _n;
|
||
|
}
|
||
|
|
||
6 years ago
|
function setNP(uint _n) public {
|
||
6 years ago
|
value = _n;
|
||
|
}
|
||
|
|
||
6 years ago
|
function get () view public returns (uint) {
|
||
6 years ago
|
return value;
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
```
|
||
|
|
||
|
This contract is very basic. The goal is to quickly start to create and
|
||
|
to interact with a sample contract.
|
||
|
|
||
|
Deploying an instance
|
||
|
---------------------
|
||
|
|
||
|
The `Compile tab` displays information related to the current contract
|
||
|
(note that there can be more than one) (see ../compile\_tab).
|
||
|
|
||
|
Moving on, in the `Run tab` select, `JavaScript VM` to specify that you
|
||
|
are going to deploy an instance of the contract in the `JavaScript VM`
|
||
|
state.
|
||
|
|
||
6 years ago
|
![](images/a-jvm.png)
|
||
6 years ago
|
|
||
6 years ago
|
The constructor of `Ballot.sol` needs a parameter (of type `uint8`).
|
||
|
Give any value and click on `Deploy`.
|
||
6 years ago
|
|
||
6 years ago
|
The transaction which deploys the instance of `Ballot` is created.
|
||
6 years ago
|
|
||
|
In a "normal" blockchain, it can take several seconds to execute. This
|
||
|
is the time for the transaction to be mined. However, because we are
|
||
|
using the `JavaScript VM`, our execution is immediate.
|
||
|
|
||
|
The terminal will inform you about the transaction. You can see details
|
||
|
there and start debugging.
|
||
|
|
||
|
The newly created instance is displayed in the `run tab`.
|
||
|
|
||
6 years ago
|
![](images/a-jvm-instance.png)
|
||
6 years ago
|
|
||
|
Interacting with an instance
|
||
|
----------------------------
|
||
|
|
||
|
This new instance contains 3 actions which corresponds to the 3
|
||
|
functions (`setP`, `setPN`, `get`). Clicking on `SetP` or `SetPN` will
|
||
|
create a new transaction.
|
||
|
|
||
6 years ago
|
Note that `SetP` is `payable` (red button) : it is possible to send
|
||
6 years ago
|
value (Ether) to the contract.
|
||
|
|
||
6 years ago
|
`SetPN` is not payable (orange button - depending on the theme) : it is not possible to send
|
||
6 years ago
|
value (Ether) to the contract.
|
||
|
|
||
6 years ago
|
Clicking on `get` will not execute a transaction (usually its a blue button - depending on the theme). It doesn't execute a transaction because a `get` does not modify the state (variable
|
||
6 years ago
|
`value`) of this instance.
|
||
|
|
||
6 years ago
|
As `get` is `view` you can see the return value just below the
|
||
6 years ago
|
action.
|
||
|
|
||
6 years ago
|
![](images/a-jvm-calling-instance.png)
|