You are viewing a single comment's thread from:

RE: Truffle实战指南

in #starnote5 years ago

TruffleContract版,truffle-contract 是对web3.js的封装,promise的用法
通过⽣成的合约信息创建TruffleContract对象,然后deployed() 拿到合约对象。

cnpm install truffle-contract --save
import TruffleContract from 'truffle-contract'
Vue.prototype.TruffleContract = TruffleContract

合约

pragma solidity >=0.4.22 <0.7.0;
contract SimpleString {
    string storedData;
    mapping(uint => string) public people;
    uint public id = 0;
    event SimpleEvent(uint i, string t);

    function set(string memory message) public {       
        id += 1;
        people[id] = message;
        emit SimpleEvent(id, message);
    }
    function  reset(uint uid, string memory message) public {
        people[uid] = message;
    }
    function get(uint uid) public view returns (string memory) {
        return people[uid];
    }
}
//get方法
async  function main(){
  let res = await that.axios.get('SimpleString.json')
  // console.log(667, res.data)
  let contract = that.TruffleContract(res.data)
  // console.log(77, contract)
  contract.setProvider(web3Provider)
  let instance = await contract.deployed()
  // console.log(31111, instance)
  //将得到的数据存入vuex中
  that.$store.commit('saveInstance', instance)

  let s = await instance.id()
  // console.log(933, s.words[0])
  let posts = []
  for(let i=1; i <= s.words[0] ; i++){
    let s1 = await instance.get(i)
    posts.push(JSON.parse(s1))
  }
  console.log(899, posts)
  that.posts = posts
  that.isLoading = false
  //将得到的数据存入vuex中
  that.$store.commit('saveHot', posts)
}
main()

//set方法
async function main(){
  let post2 = {
    "title": title,
    "nickname": nickname,
    "addr": addr,
    "category": 'hive-180932',
    "permlink": permlink,
    "body": body,
    "created": new Date().toISOString().split('.')[0],
    "json_metadata": jsonMetadata
  }
  let ss = await instance.set(JSON.stringify(post2), {from: addr})
  that.$router.push({path:'/'})
}
main()

Coin Marketplace

STEEM 0.13
TRX 0.35
JST 0.036
BTC 115828.84
ETH 4470.31
SBD 0.86