A ScratchX extension for connecting to the Ethereum blockchain and ERC20 token contracts.
Scratch does not support loading custom extensions so the ScratchX Editor is used instead.
The JavaScript code for the Ethereum extension is in ethBlock.js. This can be loaded into SctatchX in two was
- From a local file
Clone this repository to your local machine using
git clone https://github.com/naddison36/eth-scratch.git
cd eth-scratch
Open the ScratchX Editor (you will have to allow the Flash plug-in), select the Scripts
tab and then select More Blocks
. Then hold the control
button on the keyboard and click the Load Experimental Extension
button.
- From the web
In theory, you can load from an external url but I haven't gotten that to work.
Once loaded, the extension has a number of report and command blocks to get data from the Ethereum blockchain or send signed transaction via a browser wallet like MetaMask.
Before any of the ERC20 methods can be called the Set token address
command block must be used. This will instantiate an ERC20 contract that that can then be used in the transfer
, transferFrom
, approve
and mint
command blocks. It's also needed for the token balance
report block.
The command blocks should pop up a browser wallet to sign the transaction and send them onto the Ethereum network. Here's and example for signing a token transfer using MetaMask.
There is no error handling with ScratchX extensions so the best that can be done is logging errors in the browser's JavaScript console.
- ScratchX Documentation
- ScratchX Editor
- GitHub Pages site for these extensions
In order to get around the browser's cross-site scripting rules, ScratchX blocks can be loaded from GitHub Pages with a crossdomain.xml file in the GitHub user's repository, not this project repository. eg naddison36/naddison36.github.io. See Setting up crossdomain.xml for more details.
A simple ERC20 contract to test the integration between the Scratch extension and a token contract is /contracts/GameToken.sol.
This contract is dependent on the Open Zeppelin smart contract library. Run the following to install the Open Zeppelin
npm install openzeppelin-solidity
The easiest way to compile and deploy the test token contract to a test network like Ropsten is using Remix. In order to be able to load all the dependent Open Zeppelin contracts in remix, remixd can be used to connect remix running in the browser to the local filesystem where the GameToken and dependent Open Zeppelin files are found. To install and run remixd
npm install -g remixd
remixd -s <your full path>/eth-scratch --remix-ide https://remix.ethereum.org
See Access your local filesystem by using RemixD for more details.
In order to connect Remix to MetaMask, the Enable Personal Mode
setting in Remix needs to be disabled.
Your Ropsten account will need some Ether in order to deploy the contract and send transactions. The MetaMask Ether Faucet can be used to get Ropsten Ether.