Truffle Box for WeatherData API generated using TruSource.
The project includes an example contract that implements the WeatherData OracleAPI, demonstrating its use along with a local listening server. You are encouraged to replace Example.sol
with you own contract that inherits OracleAPI.sol
, uses its operations and implements the trusource_callback
.
For more details, please see the docs.
- Node.js v8.9.4 (required) / v10.5.0+ (recommended)
Ensure you are in a new and empty directory.
-
Run the
unbox
command vianpx
. This will install all necessary dependencies.npx truffle unbox TruSource/Aerisweather-WeatherData
-
Alternatively, you can install Truffle globally and run the
unbox
command.npm i -g truffle truffle unbox TruSource/Aerisweather-WeatherData
-
You could also download this repository and install the dependencies yourself.
git clone https://github.com/TruSource/Aerisweather-WeatherData cd Aerisweather-WeatherData npm i
-
Start an Ethereum client. We will use
ganache-cli
.npx ganache-cli
Note: the client needs to support WebSockets (do not use
truffle develop
for this reason).For other options, see Truffle - Choosing an Ethereum client.
-
Migrate the smart contracts.
// in a 2nd terminal (i.e. not in the eth client terminal) truffle migrate
-
Start the TruSource local server. It will listen for events, fetch and return data to requesting contracts.
// in a 3rd terminal npm start
-
Using the provided Truffle script
./server/calls.js
, call functions inExample.sol
. This will execute a set of contracts calls, testing each operation with provided example arguments.// in another terminal (i.e. not in eth client or server terminals) truffle exec server/calls.js
Trusource currently supports the Ropsten test network (see list of supported networks here).
To deploy to a live network (e.g. Ropsten):
-
Uncomment line 4 to 12 in
truffle-config.js
. -
Set the
SEED_PHRASE
(mnemonic) andETHEREUM_RPC_URL
environment variables in.env
. -
Migrate the contracts.
truffle migrate --network ropsten
- Whitelist the contract that will call the WeatherData API at Trusource.io.
The structure is similar to a standard Truffle project. The difference is the additional server directory.
├── build # Compiled files
├── contracts # Solidity contracts
├── migrations # Deployment files
├── server # Local server test script
├── test # Test files for testing contracts
├── .env.template
├── .solhint.json
├── package.json
├── LICENSE
├── README.md
├── .truffle-box.json
└── truffle.config.js # Truffle configuration file
Example.sol
inherits the WeatherData OracleAPI and demonstrates usage of its operations. OracleAPI.sol
, Oracle.sol
, and Resolver.sol
make up the oracle architecture and must be unchanged.
See here for more details.
calls.js
runs a set of example calls on Example.sol
, see Local Development for instructions.
oas.json
is the OpenAPI specification of the WeatherData API used by the local server, it must remain unchanged. If it is lost or changed, it can be found here.
See here for more details.
Tests are written in JavaScript and ensure contract calls execute without failure, events are correctly emitted and the callback is implemented.
cp .env.template .env
The WeatherData API requires authentication, please sign up for the free tier and obtain credentials. Provide these credentials in and rename the file to .env
. These credentials with be used for authentication when running the local development tests.
Truffle can run tests written in Solidity or JavaScript against your smart contracts. Note that an Ethereum client must be running.
truffle test
We use Solhint to lint the code - you can run it using npm run lint
.
-
What is a blockchain oracle?
Blockchain oracles are third-party services that provide smart contracts with external information. They serve as bridges between blockchains and the outside world. -- Binance
-
What about truffle develop?
We do not recommend using
truffle develop
because the command spawns a development blockchain that lacks WebSockets support. You will be able to compile and migrate contracts just fine.Error: The current provider doesn't support subscriptions: HttpProvider
will appear when runningnpm run calls
ornpm test
. We subscribe toLogResult
events in theserver/calls.js
Truffle script.More details about the differences between
truffle console
andtruffle develop
on Truffle's website. -
Changing ports
ganache-cli and @trusource/server both defaults to port 8545. Both can be changed by running with -p option. You will then need to update the port value in the development object in networks in
truffle-config.json
.
MIT