Twitter Ethereum DApp
Status
Working release for Ethereum local testnet
Overview
This project is a very basic Ethereum DApp, which listen for Tweets which contains a specific hashtag (#giveMeTSTToken) - "give me twitter streaming token". If such a Tweet appears, the application sends TST token to the specified ERC20 Smart Contract address. This code was written during the Swisscom Blockchain Academy Seminar "Ethereum for Developer", May 2019.
The code runs actually only on a local testnet
Requirements
Following tools are required:
- Twitter Developer Account (You need a access token for the Twitter API)
- Truffle
- Homebrew (Mac packaging manager)
- NPM
- Python with various libraries
Project structure
The project structure is a Truffle and NPM project. With NPM we can add additional libraries if required and also manage all tools. With Truffle we have the testing framework.
twitter-DApp
├── build
│ └── contracts # JSON builds from Ethereum smart contracts
├── contracts # Contains all the Ethereum Smart Contracts
├── migrartions # Contains scripts for Smart Contracts deployment for Truffle
├── node_modules # npm packages
├── contractJSONABI.json # application binary interface (ABI), needed for for encoding/decoding data into/out of the machine code [Reference](https://ethereum.stackexchange.com/questions/234/what-is-an-abi-and-why-is-it-needed-to-interact-with-contracts)
├── package-lock.json # Describe a single npm representation of a dependency packages
├── package.json # npm installtion packages is called by `npm install`
├── requirements.txt # packages list for Python (installed via pip)
├── truffle-config.js # Configuration file which setting up a single development network [Doc](https://www.trufflesuite.com/docs/truffle/reference/configuration)
├── twitter_credentials_TODO.py # Twitter API credentials file (have to be modified)
├── twitter_streamer.py # file which listening for Tweets
Step-by-Step installation guide for Mac
One of the easiest way is to use Homebrew for installation of Python3, NPM etc. Run this in your Terminal if you need to install Homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- change
twitter_credentials_TODO.py
totwitter_credentials.py
and fill in your own keys from the twitter developer account - Install npm (for mac:
brew install node
). Check if installation was successfull with:npm -v
. If you already have npm Update NPMnpm update
ornpm update --save
- Download and install Ganache or
npm install -g ganache-cli
- Install Truffle:
npm install -g truffle
- [OPTIONAL] Install xcode developer tools separatly. If you have
homebrew
installed on your mac,xcode command line tools
are already installed.xcode-select --install
maybe you need additional configs according to Doc and Doc - Install virtualvenv for isolated Python environments:
pip3 install virtualenv
- Create isolated Python environment: first
cd
into project, secondvirtualenv venv
- Activate virtual environment:
source venv/bin/activate
- Install dependencies:
pip3 install -r requirements.txt
If you have made additional installations with pip3
run pip3 freeze > requirements.txt
to rebuild requirements file for installtion dependencies.
To stop the isolated (venv)
environment use command: deactivate
Run the code
- Open command line tool (e.g Terminal for Mac)
cd
into project folder- Start local testnet:
ganache -cli
(If you use Metamask, initialize Metamask with the Mnemonic which ganache-cli provides you) - Open a new Terminal tab
cd
into project folder and deploy Smart Contract on the local testnet:truffle deploy
[OPTIONAL] Retrieve the addresses from deployed contracts if needed with:truffle networks
- change the
contract_address
in methodon_data(self, raw_data)
fromtwitter_streamer.py
with address provided aftertruffle deploy
of the Smart Contract - change
receiver_address
in methodon_data(self, raw_data)
fromtwitter_streamer.py
with your own (Metamask) address - Open a new Terminal tab
cd
into project folder and activate virtual environment:source venv/bin/activate
- Choose the
localhost
network in Metamask - Add the TST Token to Metamask:
add custom token
-> paste contract address which was provided duringtruffle deploy
-> Tokensymbol:TST
- Start
twitter_streamer.py
in a new Terminal with:python3 twitter_streamer.py
- Post a Tweet on Twitter with hashtag:
#giveMeTSTToken
Troubleshooting
- Make sure you have started
ganache-cli
(maybe with with the right Mnemonic) - Make sure you initialized MetaMask with the exactly the same Mnemonic like from
ganahce-cli
Future ideas (to do)
- add functionality to set the recipient address in the tweet itself
- make code running on Rinkeby test net. For that you need to specifie the private key manually in the code (not implemented yet)