Web3E, or Web3 for Embedded, is a full-featured Web3 development framework for Arduino embedded devices in C/C++.Web3E helps embedded device developers quickly implement Internet of Things/IoT devices that can access the chain of Taifang blocks, opening a new door for Internet of Things developers.
1. Introduction to Web3E
Web3E is primarily tested on ESP32, and ESP8266 works as well.Web3E also includes a fast-developing DApp injector that makes it easy to convert your embedded devices to Ethernet DApp.
The development of Web3E began with a simple need to develop an access DApp that can run on ESP32.There have been considerable setbacks during this period, and we realized that we needed a way to simplify the development of DApp for embedded devices in the Internet of Things, which was the initial motivation for the development of Web3E.
The main features of Web3E include:
- Support TokenScript interface
- Out-of-the-box ETF DApp injector can instantly convert the Internet of Things embedded device into ETF DApp supporting ECDSA cryptography technology
- Optimized and streamlined cryptographic algorithm implementation
- The trading system has been fully optimized for the actual use of the Taifang ERC20 and ERC875 contracts
2. Web3E Installation
It is recommended that you use Platformio to install Web3E, since Web3E is now part of the Platformio development library, you do not need to clone the original Web3E code library.
Using Web3E is simple, just create a new project in Platformio and modify platformio.ini with the following references:
[env:esp32dev] platform = espressif32 board = esp32dev framework = arduino ; Serial Monitor options monitor_speed = 115200 lib_deps = # Using a library name Web3E
3. Examples and usage of Web3E
Four Internet of Things + Ethernet sample applications are preset in Web3E:
- Simple DApp: Shows how to create a DApp that can run on an embedded device.The onboard cryptographic engine can fully interact with user input and public key recovery and signature verification on ESP32 can be done in milliseconds
- Query Wallet Balance: Shows how to query ERC20 token balances and non-homogeneous pass (NFT) balances on embedded devices
- Transaction Broadcast: Shows how to transfer ERC20 and ERC875 currency on embedded devices
- Etheric currency transfer: Shows how to implement Etheric currency transfer on embedded devices
For example, the code below shows how to use Web3E to enable the Internet of Things embedded devices to support transfer in ethernic currency:
// Setup Web3 and Contract with Private Key ... Contract contract(&web3, ""); contract.SetPrivateKey(PRIVATE_KEY); uint32_t nonceVal = (uint32_t)web3.EthGetTransactionCount(&address); //obtain the next nonce uint256_t weiValue = Util::ConvertToWei(0.25, 18); //send 0.25 eth unsigned long long gasPriceVal = 1000000000ULL; uint32_t gasLimitVal = 90000; string emptyString = ""; string toAddress = "0xC067A53c91258ba513059919E03B81CF93f57Ac7"; string result = contract.SendTransaction( nonceVal, gasPriceVal, gasLimitVal, &toAddress, &weiValue, &emptyString);
The following code uses Web3E to query the ETRIC balance for the specified ETRIC address on the Internet of Things embedded device:
//obtain balance in Wei uint256_t balance = web3.EthGetBalance(&address); //get string balance as Eth (18 decimals) string balanceStr = Util::ConvertWeiToEthString(&balance, 18);
Using Web3E to enable embedded devices to support sending ERC20 tokens is a little more complicated, but it is acceptable considering that this is using C/C++:
string contractAddr = "0x20fe562d797a42dcb3399062ae9546cd06f63280"; Contract contract(&web3, contractAddr.c_str()); contract.SetPrivateKey(<Your private key>); //Get contract name string param = contract.SetupContractData("name()", &addr); string result = contract.ViewCall(¶m); string interpreted = Util::InterpretStringResult(web3.getString(&result).c_str()); Serial.println(interpreted.c_str()); //Get Contract decimals param = contract.SetupContractData("decimals()", &addr); result = contract.ViewCall(¶m); int decimals = web3.getInt(&result); Serial.println(decimals); unsigned long long gasPriceVal = 22000000000ULL; uint32_t gasLimitVal = 4300000; //amount of erc20 token to send, note we use decimal value obtained earlier uint256_t weiValue = Util::ConvertToWei(0.1, decimals); //get nonce uint32_t nonceVal = (uint32_t)web3.EthGetTransactionCount(&addr); string toAddress = "0x007bee82bdd9e866b2bd114780a47f2261c684e3"; string valueStr = "0x00"; //Setup contract function call string p = contract.SetupContractData("transfer(address,uint256)", &toAddress, &weiValue); //push transaction to ethereum result = contract.SendTransaction(nonceVal, gasPriceVal, gasLimitVal, &contractAddr, &valueStr, &p); string transactionHash = web3.getString(&result);
Connecting embedded devices to ETF is an important part of the ability to make smart contracts work because machines are naturally easy to follow the rules.Web3E provides a relatively complete solution, and while currently only available for some Arduino devices, it is definitely a good tool for Internet of Things embedded device developers.If you want to learn systematically how to develop knowledge in Taifang, you can refer to the following tutorials: Introduction to Taifang DApp Development | Decentralized E-commerce DApp Reality