oracle生成助记符,如何在POA Network开发和部署智能合约

$ npm install truffle-hdwallet-provider dotenv --save-dev6、从Metamask钱包中复制助记符:设置/显示种子词7、在Truffle项目中创建一个.env并复制这个助记符MNEMONIC="COPY HERE YOUR 12 MNEMONIC WORDS YOU DO NOT WANT TO SHARE"8、打开truffle.js文件并添加以下配置require('dotenv').config();const HDWalletProvider = require('truffle-hdwallet-provider');module.exports = {// See // for more about customizing your Truffle configuration!networks: {poa: {provider: function() {return new HDWalletProvider(process.env.MNEMONIC,"https://sokol.poa.network")},network_id: 77,gas: 500000,gasPrice: 1000000000},development: {host: "127.0.0.1",port: 8545,network_id: "*" // Match any network id}}};9、将部署运行到POA Sokol网络$ truffle migrate --network poaUsing network 'poa'.Running migration: 1_initial_migration.jsDeploying Migrations...... 0x3a2e4be0c784bf5df3ca4251d27dc724ae5863d5de0e1eae4babb0c636b8c571Migrations: 0xb497ad71c0168d040027cfdcf9a44c7f8f270d0dSaving successful migration to network...... 0x8ebbf70d5a162ba46e2fa4266aafe360d9f32e32c30ed17a083045d2afeeaf46Saving artifacts...Running migration: 2_deploy_contracts.jsDeploying Counter...... 0xdf3009e60daec1217237661b4b10298b5f4504efef8a6f93cdc8198486260766Counter: 0xfbc93e43a6a95c1cee79aa8ec2382a4edd5ad2bcSaving artifacts...步骤3:通过Web应用程序与智能合约交互在下一步中,我们将使用react、web3和truffle开发一个DAPP,与先前部署在POA sokol网络上的智能合约进行交互。1、初始化React项目$ npx create-react-app frontend$ cd frontend2、安装必要的依赖项· truffle-contract:是一个以太坊智能合约抽象库· web3:这是与以太坊兼容的JavaScript API,它实现了通用JSON RPC规范$ npm install truffle-contract web3 --save3、编辑package.json并在脚本部分下添加以下行以从webapp访问Truffle合约工作"link-contracts": "run-script-os","link-contracts:linux:darwin": "cd src && ln -s ../../build/contracts contracts","link-contracts:win32": "cd src && mklink \\D contracts ..\\..\\build\\contracts"此文件的完整代码可在此处获得:package.jsonhttps://github.com/gjeanmart/kauri-content/blob/master/poa_tutorial_sokol_part1/frontend/package.json4、打开编辑src/app.js· 使用Web3连接到节点import Web3 from 'web3'class App extends Component {constructor(props) {super(props)if (typeof web3 != 'undefined') {this.web3Provider = web3.currentProvider} else {this.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545')}this.web3 = new Web3(this.web3Provider)}(...)}· 使用truffle-contract加载Truffle artefacts 并与智能合约交互import TruffleContract from 'truffle-contract'import Counter from './contracts/Counter.json'class App extends React.Component {constructor(props) {super(props)this.state = {account: '0x0',value: '',loading: true}(... web3 ...)}componentDidMount() {const counter = TruffleContract(Counter)counter.setProvider(this.web3Provider)this.web3.ETH.getAccounts((error, accounts) => {const account = accounts[0]this.setState({ account})counter.deployed().then((instance) => {this.counter = instancereturn this.counter.geTCOunter.call()}).then((value) => {return this.setState({ value: value.toNumber(), loading: false })})})}setValue(value) {this.setState({ loading: true })this.counter.increment({ from: this.state.account, gas: 50000 }).then((r) => {this.setState({ value: value.toNumber(), loading: false })})}(...)}5、创建一个指向Truffle JSON artefacts 的链接$ npm run link-contracts:linux:darwin6、启动Web服务器$ npm start7、结果展示如果Metamask已解锁并连接到Solok网络,则web3提供程序将自动连接到节点并检索计数器值。当用户点击“increment”时,会弹出Metamask以签署交易并将其发送到POA Solok网络。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值