本篇内容介绍了“Web3E开发框架怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Web3E,即Web3 for Embedded,是一个面向Arduino嵌入设备的全功能Web3开发框架,开发语言为C/C++。Web3E可以帮助嵌入设备开发者快速实现能够接入以太坊区块链的物联网/IoT设备,为物联网开发者打开了一扇新的大门。
Web3E主要在ESP32上进行测试,ESP8266也可以正常工作。Web3E还包含了一个快速开发DApp注入器,可以很方便地将你的嵌入设备转换为以太坊DApp。
Web3E的开发始于一个简单的需求:开发一个能够在ESP32上运行的门禁DApp。这期间经历了相当多的挫折,我们意识到需要一个方法来简化物联网嵌入设备的DApp的开发,这就是开发Web3E的最初动机。
Web3E的主要特性包括:
支持TokenScript接口
开箱即用的以太坊DApp注入器,可以立刻将物联网嵌入设备转化为支持ECDSA密码学技术 的以太坊DApp
经过优化精简的密码学算法实现
交易系统已经充分优化,以太坊ERC20和ERC875合约都有实际使用
建议使用Platformio安装Web3E,因为Web3E目前已经是Platformio开发库的一份子了,所以不需要克隆原始的Web3E代码库。
使用Web3E很简单,只需要在Platformio中创建一个新项目,然后参考如下内容修改platformio.ini:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
; Serial Monitor options
monitor_speed = 115200
lib_deps =
# Using a library name
Web3E
在Web3E中预置了4个物联网+以太坊的示例应用:
Simple DApp:展示如何创建可以运行在嵌入设备上的DApp。板载密码学引擎 可以与用户输入充分交互,在ESP32上的公钥恢复和签名验证可以毫秒级完成
查询钱包余额:展示在嵌入设备上如何查询ERC20代币余额以及非同质化通证(NFT)余额
交易广播:展示在嵌入设备上如何实现ERC20和ERC875代币的转账交易
以太币转账:展示如何在嵌入设备上实现以太币转账
例如,下面的代码展示了如何使用Web3E让物联网嵌入设备支持以太币转账:
// 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);
下面的代码使用Web3E在物联网嵌入设备上查询指定的以太坊地址的以太币余额:
//obtain balance in Wei
uint256_t balance = web3.EthGetBalance(&address);
//get string balance as Eth (18 decimals)
string balanceStr = Util::ConvertWeiToEthString(&balance, 18);
使用Web3E让嵌入设备支持ERC20代币的发送要复杂一点,但考虑到这是在用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);
“Web3E开发框架怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3843525/blog/3158843