区块链基础、turffle基础、使用web3.js

区块链基础学习

BTC

  • 挖矿
  • 钱包
  • 交易所(coinbase、火币、okex、币安)
  • OTC

ETH

  • ICO

  • Web3.0

  • Filecoin

  • Ann自动交易模式(uniswap)

Polygon应用

  • 借贷

    优点:稳定

    缺点:repay、币类种数少

  • 交易所

    缺点:深度不够

  • 机枪池

初始化创建truffle

C:\Users\28694\Desktop\truffle>truffle init

Starting init...
================

> Copying project files to C:\Users\28694\Desktop\truffle

Init successful, sweet!


C:\Users\28694\Desktop\truffle>truffle compile

Compiling your contracts...
===========================
> Compiling .\contracts\MathContract.sol
> Compiling .\contracts\Migrations.sol
> Artifacts written to C:\Users\28694\Desktop\truffle\build\contracts
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang


C:\Users\28694\Desktop\truffle>truffle develop
Truffle Develop started at http://127.0.0.1:9545/

Accounts:
(0) 0x5e3677ca9d139cfd3bf00aa1d9b5515704a99f5d
(1) 0x58376f818f27e673313025b09877bd9a96e5ddea
(2) 0x07cf330a59d16d71294c1f8049912ac3910e490f
(3) 0x79e2aa4278046ebeadf5cd43aaf76d0beefc2197
(4) 0x225e940daafda4428f775c532b1f7fd7391b9996
(5) 0x9b2500457a365ee561555b5d0979049dc3f1cd63
(6) 0xf28d264f52cc359bb2d981115b9e29accc6f3046
(7) 0x7ef049916bba2b2c60466b115ab8682cc25a425b
(8) 0x12542fbfe1b734262598bdd8a9e1ab00cc679c43
(9) 0xb7cb6de963ea163ce6b14121719071c31ac44d60

Private Keys:
(0) b52e0a2ede91498470633e2d11fcd49e03788384522439707b0690d9d0b5fd31
(1) 7003a39cf2c9c077c367c9b26940f3cb045d38529910f25affa2fbcad0d9ed6b
(2) 739f775cd40f3d4f1e3c8f6c457cf2d00886c139420d66756ac40745df136587
(3) 43f20e44bed3449e96175df532ac679be2ff11ac27d25291c5a0204d99023533
(4) 3ce46c10165e3f350c302d48ded2dc61b01b4ad77b447391a2403298e350c380
(5) 415b7b21a3fcb84eff1615dd9d834558deebfa370b190de9f786fc81c0af4b17
(6) 86fbb69032abd35f88899d799d3c19f3e8a43261e158443fc8318d490cec5bcf
(7) a697556fdedfe7de6a99917155d6ab18100646a546b50086f1e15ae5d7ff0fc6
(8) 62c27fb8fd5c02ad2f034a1f21df30afb99fd89779d88bd784ec95929ed80d83
(9) e3d3fdfdd35bd619103b225d56141d04f9d8aae46098b8accb5ea0891faa1f82

Mnemonic: sort music crime frown alley episode damage proof spray annual cost enact

⚠️  Important ⚠️  : This mnemonic was created for you by Truffle. It is not secure.
Ensure you do not use it on production blockchains, or else you risk losing funds.

truffle(develop)> truffle deploy

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'develop'
> Network id:      5777
> Block gas limit: 6721975 (0x6691b7)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x2747307163f2c5fd97bf86c45da6c61f4ed97d7340b8cdbe17d042d383aa8b09
   > Blocks: 0            Seconds: 0
   > contract address:    0x88ea5317642419fE7Bc161285EF05c4D08007Cff
   > block number:        1
   > block timestamp:     1621933958
   > account:             0x5e3677Ca9D139cfD3BF00aA1D9b5515704a99f5D
   > balance:             99.99616114
   > gas used:            191943 (0x2edc7)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00383886 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00383886 ETH


2_initial_migration.js
======================

   Deploying 'MathContract2'
   -------------------------
   > transaction hash:    0x2f5603d7dc4cb0dbf4bcc0fa78f949bbdd1c4f83bacbe4c12782355f631ba06e
   > Blocks: 0            Seconds: 0
   > contract address:    0x87Df81bcf6B4CA8BC47F09875fD40A0Dd8B2e7C2
   > block number:        3
   > block timestamp:     1621933958
   > account:             0x5e3677Ca9D139cfD3BF00aA1D9b5515704a99f5D
   > balance:             99.99345072
   > gas used:            93183 (0x16bff)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00186366 ETH

部署合约

  1. 方法一,创建一个新的合约实例
truffle(develop)> var myContract
undefined
//部署完成后myContract里面存的就是实例
truffle(develop)> MathContract2.deployed().then(function(instance){myContract = instance})
undefined
truffle(develop)> myContract.mulAtoB(3,4)
BN { negative: 0, words: [ 12, <1 empty item> ], length: 1, red: null }
  1. 方法二,创建一个新的合约实例
//new web3.eth.Contract(abi的json格式[合约的contract address][, options])
truffle(develop)> myContract = new web3.eth.Contract([{"constant":true,"inputs":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"uint256","name":"b","type":"uint256"}],"name":"mulAtoB","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"}],'0x87Df81bcf6B4CA8BC47F09875fD40A0Dd8B2e7C2')
truffle(develop)>myContract.methods.mulAtoB(3,4).call()

配置文件truffle-config.js

//修改地址要把development改成develop才生效
	develop: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
    },
        
//可以修改版本信息
        compilers: {
    solc: {
      version: "0.6.0",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }
    }
  }

使用web3.js

  • 查看web3.js版本
truffle(develop)> web3.version
'1.3.6'ving migration to chain.
  • 当前在用的通信服务提供器
truffle(develop)> web3.currentProvider
HttpProvider {--------------------------
  withCredentials: false, 0.00186366 ETH
  timeout: 0,
  headers: undefined,
  agent: undefined,
  connected: true,
  host: 'http://127.0.0.1:9545/',
  httpAgent: Agent {   0.00570252 ETH
    _events: [Object: null prototype] {
      free: [Function (anonymous)],
      newListener: [Function: maybeEnableKeylog]
    },ng migration to chain.
    _eventsCount: 2,   Seconds: 0
    _maxListeners: undefined,
    defaultPort: 80,
    protocol: 'http:',.version
    options: { keepAlive: false, path: null },
    requests: {},
    sockets: {},
    freeSockets: {},
    keepAliveMsecs: 1000,
    keepAlive: false,
    maxSockets: Infinity,
    maxFreeSockets: 256,
    scheduling: 'fifo',
    maxTotalSockets: Infinity,
    totalSocketCount: 0,
    [Symbol(kCapture)]: false
  },
  send: [Function (anonymous)],
  _alreadyWrapped: true
}
  • 查看当前账户
truffle(develop)> web3.eth.getAccounts()
[
  '0x5e3677Ca9D139cfD3BF00aA1D9b5515704a99f5D',
  '0x58376f818f27e673313025B09877BD9A96E5ddEA',
  '0x07cF330a59D16D71294c1F8049912ac3910E490F',
  '0x79E2Aa4278046EBeADf5CD43aAf76d0Beefc2197',
  '0x225E940DAaFDA4428F775c532b1F7fd7391B9996',
  '0x9b2500457A365ee561555B5D0979049dC3f1cD63',
  '0xF28d264F52cc359BB2D981115B9E29aCcC6f3046',
  '0x7ef049916bba2B2c60466B115aB8682Cc25A425B',
  '0x12542fbfe1B734262598bdd8a9e1aB00cC679c43',
  '0xb7cB6de963Ea163Ce6b14121719071c31AC44D60'
]
  • 修改默认账户
truffle(develop)> web3.eth.defaultAccount='0x5e3677Ca9D139cfD3BF00aA1D9b5515704a99f5D'
  • 获取账户金额
truffle(develop)> web3.eth.getBalance('0x5e3677Ca9D139cfD3BF00aA1D9b5515704a99f5D')
'99992903960000000000'
  • wei转成以太坊
truffle(develop)> web3.utils.fromWei('99992903960000000000','ether')
'99.99290396'
  • 不同账户转账
truffle(develop)> web3.eth.sendTransaction({from:'0x58376f818f27e673313025B09877BD9A96E5ddEA',to:'0x5e3677Ca9D139cfD3BF00aA1D9b5515704a99f5D',value:web3.utils.toWei('50','ether')})
  • 查询不同的块
truffle(develop)> web3.eth.getTransaction('0xed2af19c2767b7275f5e13e39bfbd3bee9db8e122f29a6ac6b77507be27d0514')
{
  hash: '0xed2af19c2767b7275f5e13e39bfbd3bee9db8e122f29a6ac6b77507be27d0514',
  nonce: 0,
  blockHash: '0x4ca20dc9dd0ef97e721c70da90a440ad98ec0b84e03e093ea45c5dd688c701be',
  blockNumber: 5,
  transactionIndex: 0,
  from: '0x58376f818f27e673313025B09877BD9A96E5ddEA',
  to: '0x5e3677Ca9D139cfD3BF00aA1D9b5515704a99f5D',
  value: '50000000000000000000',
  gas: '0x15f90',
  gasPrice: '2000000000',
  input: '0x',
  v: '0xa95',
  r: '0x8ac12a4c86b5881a27b2038453cf0bbd34d56e9714609e8fb979ce30629f66f4',
  s: '0x15cd532725fe5393139d805d5b7995142201fbf2b9e9f0e9532141741d322c40'
}
posted @ 2021-05-26 01:05  zcmuer  阅读(705)  评论(0)    收藏  举报