以太坊ETH-智能合约开发-truffle框架使用入门

0. 背景

基于以太坊的Solidity 智能合约,开发一个简单的抵押借贷系统。主要用ETH抵押进合约,贷出ERC20 Token

1. 环境准备

1.1. 安装Truffle

1.2 根据教程初始化Truffle工程框架

wujinquan@wujinquandeMacBook-Pro  ~/workspace/eth/truffle/defi  truffle init

Starting unbox...
=================

✔ Preparing to download box
✔ Downloading
✔ cleaning up temporary files
✔ Setting up box

Unbox successful, sweet!

Commands:

  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test

 wujinquan@wujinquandeMacBook-Pro  ~/workspace/eth/truffle/defi  ls
contracts         migrations        test              truffle-config.js

2. 编写智能合约

主要先添加DeFi.sol (包含 ERC20 token + 抵押合约 ) + 2_deploy_contracts.js(部署脚本)
源码见Github仓库,结果目录如下,各目录作用具体参考Truffle文档

 wujinquan@wujinquandeMacBook-Pro  ~/workspace/eth/truffle/defi  tree
.
├── contracts
│   ├── DeFi.sol
│   └── Migrations.sol
├── migrations
│   ├── 1_initial_migration.js
│   └── 2_deploy_contracts.js
├── test
└── truffle-config.js

4 directories, 5 files
 wujinquan@wujinquandeMacBook-Pro  ~/workspace/eth/truffle/defi 

3. 进入Truffle develop模式

在项目目录执行truffle develop进入console 目录,此时truffle提供了一个模拟的区块链环境
RPC端口9545

 wujinquan@wujinquandeMacBook-Pro  ~/workspace/eth/truffle/pledge  truffle develop
Truffle Develop started at http://127.0.0.1:9545/

Accounts:
(0) 0x843acfb41e5c0f1e0587c5b765d897ccdea8c4dd
(1) 0xa39ddba54789f9741673a7c4292fd7a4be59cf7b
(2) 0xdad88d893ef2d2b890b6c36d70fa1388a5538be8
(3) 0x07f426542f849f2e8f0529ae0297b53fbfab0f9f
(4) 0xf79680a4fdc887b50229058d37c107b68f3757d9
(5) 0xd35aaf9c6ec31e16ae09c8e612933b7bd28a3612
(6) 0xcea96ca483382f1f5584d83e37ac9c6c60c679df
(7) 0x88fcfd016e2985080aafe70fe0f9335f74d5fa88
(8) 0x19cf95ab9732bad2e2e4b3ea72d16d42bf7ca943
(9) 0x69930144c06a8a767a5cddcf18c5b5548a88d542

Private Keys:
(0) 2ab3e81a2c3455abca80098dadfac7bb92ed28705f10d1333efc90c79971309b
(1) d143735dbb6034e1134d2432c0ceb924c6624090d86d1b951746a28e7483917c
(2) 2753554ac688b59b02b22c60d1a6ac8d0bacde295fe0e0ed8d39782c007b79f7
(3) 9fa4a3e7e8a0bcd1ad8129eeb57302b13b72505a13059dd11d80c3c24a714893
(4) 7a0b7153891d807a4468af075e4e82832aa89337a3cb93ef3a9f702cb769ee58
(5) 94162906aa252be0f2f6e9b9ff6f9574595580356628e4cb5f90647b9467edec
(6) b9c3508e200dbab89b57bf2eee11615138c86908eb64f5778d22518b11d190df
(7) 5f56d048071df927af740525906dfb4c1d129738f64d940c18b1b8db275d5e06
(8) 2c5850424560bacdfc7f3ebb188f48d82c062b1bff0610f1ba3ffb7e42753e2b
(9) ef576c79b977ebfc450dd0fefebe9d6b657c97fcc7924d0ae78df34d587049ae

Mnemonic: grant boat distance require trap pair theme together spray blush glory simple

⚠️  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)>

4. 编译、部署合约

执行compile命令编译合约
如需重新编译所有合约,可执行compile --all

truffle(develop)> compile

Compiling your contracts...
===========================
> Compiling ./contracts/DeFi.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/wujinquan/workspace/eth/truffle/defi/build/contracts
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang

truffle(develop)>

执行migrate命令,Truffle会根据./migration/2_deploy_contracts.js 内容部署合约
如需重新部署所有合约,可执行migrate --reset

truffle(develop)> migrate

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



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


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

   Replacing 'Migrations'
   ----------------------
   > transaction hash:    0x9cd5c4465f3aa9a889b97f395728c9e9720f569b0838ba6d944b551f8362eeb9
   > Blocks: 0            Seconds: 0
   > contract address:    0xa24e2615D5EEE2e0303C37dC28Db44e44e59847B
   > block number:        1
   > block timestamp:     1584410533
   > account:             0x843ACfB41E5c0F1E0587C5B765d897cCDeA8c4DD
   > balance:             99.9967165
   > gas used:            164175
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.0032835 ETH


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


2_deploy_contracts.js
=====================

   Replacing 'DeFi'
   ----------------
   > transaction hash:    0xfc95470f7a1f79942ea6fed7a41d959adf38c00a8964c4c2a418ecbe4de23992
   > Blocks: 0            Seconds: 0
   > contract address:    0xf170c784D9E23c764467CA9fC97F42a4dAC147bE
   > block number:        3
   > block timestamp:     1584410533
   > account:             0x843ACfB41E5c0F1E0587C5B765d897cCDeA8c4DD
   > balance:             99.97388586
   > gas used:            1099191
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.02198382 ETH


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


Summary
=======
> Total deployments:   2
> Final cost:          0.02526732 ETH


truffle(develop)>

5. Truffle与合约交互

具体可参考Truffle官方与合约进行交互文档

5.1 定义抽象合约对象

truffle(develop)> let defi = await DeFi.deployed()
undefined
truffle(develop)>let accounts = await web3.eth.getAccounts()
undefined
truffle(develop)> accounts
[
  '0x843ACfB41E5c0F1E0587C5B765d897cCDeA8c4DD',
  '0xa39DdbA54789f9741673a7C4292fd7a4be59Cf7B',
  '0xdaD88D893Ef2d2B890b6c36D70fa1388a5538Be8',
  '0x07F426542F849F2e8F0529Ae0297b53FBFaB0f9f',
  '0xf79680a4fdC887B50229058d37C107b68F3757D9',
  '0xD35aaf9C6eC31E16aE09C8e612933B7bD28a3612',
  '0xceA96Ca483382f1F5584D83e37Ac9c6C60C679DF',
  '0x88Fcfd016E2985080AAfe70fe0F9335f74d5fa88',
  '0x19Cf95aB9732bAD2E2E4b3eA72d16d42bf7ca943',
  '0x69930144c06A8A767A5cDdcF18c5B5548A88D542'
]

5.2 查看抽象合约对象的所有内容

truffle(develop)> defi   //输入抽象合约对象名称即可
TruffleContract {
  constructor: [Function: TruffleContract] {
    _constructorMethods: {
      configureNetwork: [Function: configureNetwork],
      setProvider: [Function: setProvider],
      new: [Function: new],
      at: [AsyncFunction: at],
      deployed: [AsyncFunction: deployed],
      defaults: [Function: defaults],
      hasNetwork: [Function: hasNetwork],
      isDeployed: [Function: isDeployed],
      detectNetwork: [AsyncFunction: detectNetwork],
      setNetwork: [Function: setNetwork],
      setNetworkType: [Function: setNetworkType],
      setWallet: [Function: setWallet],
      resetAddress: [Function: resetAddress],
      link: [Function: link],
      clone: [Function: clone],
      addProp: [Function: addProp],
      toJSON: [Function: toJSON],
      decodeLogs: [Function: decodeLogs]
    },
    _properties: {
      contract_name: [Object],
      contractName: [Object],
      gasMultiplier: [Object],
      timeoutBlocks: [Object],
      autoGas: [Object],
      numberFormat: [Object],
      abi: [Object],
      metadata: [Function: metadata],
      network: [Function: network],
      networks: [Function: networks],
      address: [Object],
      transactionHash: [Object],
      links: [Function: links],
      events: [Function: events],
      binary: [Function: binary],
      deployedBinary: [Function: deployedBinary],
      unlinked_binary: [Object],
      bytecode: [Object],
      deployedBytecode: [Object],
      sourceMap: [Object],
      deployedSourceMap: [Object],
      source: [Object],
      sourcePath: [Object],
      legacyAST: [Object],
      ast: [Object],
      compiler: [Object],
      schema_version: [Function: schema_version],
      schemaVersion: [Function: schemaVersion],
      updated_at: [Function: updated_at],
      updatedAt: [Function: updatedAt],
      userdoc: [Function: userdoc],
      devdoc: [Function: devdoc],
      networkType: [Object]
    },
    _property_values: {},
    _json: {
      contractName: 'DeFi',
      abi: [Array],
      metadata: '{"compiler":{"version":"0.5.16+commit.9c3226ce"},"language":"Solidity","output":{"abi":[{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Pledge","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Redeem","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"pledge","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"value","type":"uint256"}],"name":"redeem","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}],"devdoc":{"methods":{}},"userdoc":{"methods":{}}},"settings":{"compilationTarget":{"/Users/wujinquan/workspace/eth/truffle/defi/contracts/DeFi.sol":"DeFi"},"evmVersion":"istanbul","libraries":{},"optimizer":{"enabled":false,"runs":200},"remappings":[]},"sources":{"/Users/wujinquan/workspace/eth/truffle/defi/contracts/DeFi.sol":{"keccak256":"0x7f2b209bbc3ef1bb20037720e61cc73ed51b7bd84cc50a7fa3464fbca469df30","urls":["bzz-raw://f33559e741ff60f1a63696c0ec1c7c1356e3a2db02bbc16216501078fa6f8f36","dweb:/ipfs/QmS5mSprtY26HhoJ4sAsv3t2C6X87YkY7FyFuyr4azG894"]}},"version":1}',
      bytecode: '0x60806040523480156200001157600080fd5b506040518060400160405280600581526020017f546f6b656e000000000000000000000000000000000000000000000000000000815250600190805190602001906200005f92919062000111565b506040518060400160405280600281526020017f544b00000000000000000000000000000000000000000000000000000000000081525060029080519060200190620000ad92919062000111565b506012600360006101000a81548160ff021916908360ff16021790555033600660006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550620001c0565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200015457805160ff191683800117855562000185565b8280016001018555821562000185579182015b828111156200018457825182559160200191906001019062000167565b5b50905062000194919062000198565b5090565b620001bd91905b80821115620001b95760008160009055506001016200019f565b5090565b90565b61114f80620001d06000396000f3fe6080604052600436106100a75760003560e01c806388ffe8671161006457806388ffe867146103035780638da5cb5b1461032557806395d89b411461037c578063a9059cbb1461040c578063db006a751461047f578063dd62ed3e146104d2576100a7565b806306fdde03146100ac578063095ea7b31461013c57806318160ddd146101af57806323b872dd146101da578063313ce5671461026d57806370a082311461029e575b600080fd5b3480156100b857600080fd5b506100c1610557565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101015780820151818401526020810190506100e6565b50505050905090810190601f16801561012e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561014857600080fd5b506101956004803603604081101561015f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506105f5565b604051808215151515815260200191505060405180910390f35b3480156101bb57600080fd5b506101c4610732565b6040518082815260200191505060405180910390f35b3480156101e657600080fd5b50610253600480360360608110156101fd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610738565b604051808215151515815260200191505060405180910390f35b34801561027957600080fd5b50610282610a2b565b604051808260ff1660ff16815260200191505060405180910390f35b3480156102aa57600080fd5b506102ed600480360360208110156102c157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610a3e565b6040518082815260200191505060405180910390f35b61030b610a86565b604051808215151515815260200191505060405180910390f35b34801561033157600080fd5b5061033a610c18565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561038857600080fd5b50610391610c3e565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103d15780820151818401526020810190506103b6565b50505050905090810190601f1680156103fe5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561041857600080fd5b506104656004803603604081101561042f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610cdc565b604051808215151515815260200191505060405180910390f35b34801561048b57600080fd5b506104b8600480360360208110156104a257600080fd5b8101908080359060200190929190505050610ec6565b604051808215151515815260200191505060405180910390f35b3480156104de57600080fd5b50610541600480360360408110156104f557600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050611093565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105ed5780601f106105c2576101008083540402835291602001916105ed565b820191906000526020600020905b8154815290600101906020018083116105d057829003601f168201915b505050505081565b6000816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561064257600080fd5b81600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60045481565b6000816000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561078557600080fd5b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054826000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561081057600080fd5b600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561089957600080fd5b816000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600360009054906101000a900460ff1681565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b6000803411610afd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f4e6f7420656e6f7567682045544820746f20706c656467652e0000000000000081525060200191505060405180910390fd5b346000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555034600760003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550346004600082825401925050819055507f5e91ea8ea1c46300eb761859be01d7b16d44389ef91e03a163a87413cbf55b953334604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a16001905090565b600660009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610cd45780601f10610ca957610100808354040283529160200191610cd4565b820191906000526020600020905b815481529060010190602001808311610cb757829003601f168201915b505050505081565b6000808211610cea57600080fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610d3557600080fd5b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054826000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540111610dbf57600080fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905092915050565b60008060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054821115610f7c576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f4e6f7420656e6f7567682045544820746f2072656465656d2e0000000000000081525060200191505060405180910390fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816004600082825403925050819055503373ffffffffffffffffffffffffffffffffffffffff166108fc839081150290604051600060405180830381858888f1935050505015801561101e573d6000803e3d6000fd5b507f222838db2794d11532d940e8dec38ae307ed0b63cd97c233322e221f998767a63383604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a160019050919050565b6000600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490509291505056fea265627a7a7231582051b19ea578887e595baa77b26773976236e608fa916805385509eaa0d4ac636864736f6c63430005100032',
      deployedBytecode: '0x6080604052600436106100a75760003560e01c806388ffe8671161006457806388ffe867146103035780638da5cb5b1461032557806395d89b411461037c578063a9059cbb1461040c578063db006a751461047f578063dd62ed3e146104d2576100a7565b806306fdde03146100ac578063095ea7b31461013c57806318160ddd146101af57806323b872dd146101da578063313ce5671461026d57806370a082311461029e575b600080fd5b3480156100b857600080fd5b506100c1610557565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101015780820151818401526020810190506100e6565b50505050905090810190601f16801561012e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561014857600080fd5b506101956004803603604081101561015f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506105f5565b604051808215151515815260200191505060405180910390f35b3480156101bb57600080fd5b506101c4610732565b6040518082815260200191505060405180910390f35b3480156101e657600080fd5b50610253600480360360608110156101fd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610738565b604051808215151515815260200191505060405180910390f35b34801561027957600080fd5b50610282610a2b565b604051808260ff1660ff16815260200191505060405180910390f35b3480156102aa57600080fd5b506102ed600480360360208110156102c157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610a3e565b6040518082815260200191505060405180910390f35b61030b610a86565b604051808215151515815260200191505060405180910390f35b34801561033157600080fd5b5061033a610c18565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561038857600080fd5b50610391610c3e565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103d15780820151818401526020810190506103b6565b50505050905090810190601f1680156103fe5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561041857600080fd5b506104656004803603604081101561042f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610cdc565b604051808215151515815260200191505060405180910390f35b34801561048b57600080fd5b506104b8600480360360208110156104a257600080fd5b8101908080359060200190929190505050610ec6565b604051808215151515815260200191505060405180910390f35b3480156104de57600080fd5b50610541600480360360408110156104f557600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050611093565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105ed5780601f106105c2576101008083540402835291602001916105ed565b820191906000526020600020905b8154815290600101906020018083116105d057829003601f168201915b505050505081565b6000816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561064257600080fd5b81600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60045481565b6000816000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561078557600080fd5b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054826000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561081057600080fd5b600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561089957600080fd5b816000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600360009054906101000a900460ff1681565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b6000803411610afd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f4e6f7420656e6f7567682045544820746f20706c656467652e0000000000000081525060200191505060405180910390fd5b346000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555034600760003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550346004600082825401925050819055507f5e91ea8ea1c46300eb761859be01d7b16d44389ef91e03a163a87413cbf55b953334604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a16001905090565b600660009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610cd45780601f10610ca957610100808354040283529160200191610cd4565b820191906000526020600020905b815481529060010190602001808311610cb757829003601f168201915b505050505081565b6000808211610cea57600080fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610d3557600080fd5b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054826000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540111610dbf57600080fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905092915050565b60008060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054821115610f7c576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f4e6f7420656e6f7567682045544820746f2072656465656d2e0000000000000081525060200191505060405180910390fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816004600082825403925050819055503373ffffffffffffffffffffffffffffffffffffffff166108fc839081150290604051600060405180830381858888f1935050505015801561101e573d6000803e3d6000fd5b507f222838db2794d11532d940e8dec38ae307ed0b63cd97c233322e221f998767a63383604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a160019050919050565b6000600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490509291505056fea265627a7a7231582051b19ea578887e595baa77b26773976236e608fa916805385509eaa0d4ac636864736f6c63430005100032',
      sourceMap: '4343:1509:0:-;;;4702:48;8:9:-1;5:2;;;30:1;27;20:12;5:2;4702:48:0;369:14;;;;;;;;;;;;;;;;;:4;:14;;;;;;;;;;;;:::i;:::-;;451:13;;;;;;;;;;;;;;;;;:6;:13;;;;;;;;;;;;:::i;:::-;;549:2;538:8;;:13;;;;;;;;;;;;;;;;;;4735:10;4727:5;;:18;;;;;;;;;;;;;;;;;;4343:1509;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;',
      deployedSourceMap: '4343:1509:0:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;94:19;;8:9:-1;5:2;;;30:1;27;20:12;5:2;94:19:0;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;23:1:-1;8:100;33:3;30:1;27:10;8:100;;;99:1;94:3;90:11;84:18;80:1;75:3;71:11;64:39;52:2;49:1;45:10;40:15;;8:100;;;12:14;94:19:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3667:240;;8:9:-1;5:2;;;30:1;27;20:12;5:2;3667:240:0;;;;;;13:2:-1;8:3;5:11;2:2;;;29:1;26;19:12;2:2;3667:240:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;175:26;;8:9:-1;5:2;;;30:1;27;20:12;5:2;175:26:0;;;:::i;:::-;;;;;;;;;;;;;;;;;;;2450:660;;8:9:-1;5:2;;;30:1;27;20:12;5:2;2450:660:0;;;;;;13:2:-1;8:3;5:11;2:2;;;29:1;26;19:12;2:2;2450:660:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;146:23;;8:9:-1;5:2;;;30:1;27;20:12;5:2;146:23:0;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;816:106;;8:9:-1;5:2;;;30:1;27;20:12;5:2;816:106:0;;;;;;13:2:-1;8:3;5:11;2:2;;;29:1;26;19:12;2:2;816:106:0;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;4769:491;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;4403:20;;8:9:-1;5:2;;;30:1;27;20:12;5:2;4403:20:0;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;119:21;;8:9:-1;5:2;;;30:1;27;20:12;5:2;119:21:0;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;23:1:-1;8:100;33:3;30:1;27:10;8:100;;;99:1;94:3;90:11;84:18;80:1;75:3;71:11;64:39;52:2;49:1;45:10;40:15;;8:100;;;12:14;119:21:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1231:736;;8:9:-1;5:2;;;30:1;27;20:12;5:2;1231:736:0;;;;;;13:2:-1;8:3;5:11;2:2;;;29:1;26;19:12;2:2;1231:736:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;5279:571;;8:9:-1;5:2;;;30:1;27;20:12;5:2;5279:571:0;;;;;;13:2:-1;8:3;5:11;2:2;;;29:1;26;19:12;2:2;5279:571:0;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;3993:139;;8:9:-1;5:2;;;30:1;27;20:12;5:2;3993:139:0;;;;;;13:2:-1;8:3;5:11;2:2;;;29:1;26;19:12;2:2;3993:139:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;94:19;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;3667:240::-;3734:12;3784:6;3760:8;:20;3769:10;3760:20;;;;;;;;;;;;;;;;:30;;3752:39;;;;;;3827:6;3795:7;:19;3803:10;3795:19;;;;;;;;;;;;;;;:29;3815:8;3795:29;;;;;;;;;;;;;;;:38;;;;3869:8;3848:38;;3857:10;3848:38;;;3879:6;3848:38;;;;;;;;;;;;;;;;;;3897:4;3890:11;;3667:240;;;;:::o;175:26::-;;;;:::o;2450:660::-;2532:12;2584:6;2565:8;:15;2574:5;2565:15;;;;;;;;;;;;;;;;:25;;2557:34;;;;;;2691:8;:13;2700:3;2691:13;;;;;;;;;;;;;;;;2681:6;2665:8;:13;2674:3;2665:13;;;;;;;;;;;;;;;;:22;:39;;2657:48;;;;;;2758:7;:14;2766:5;2758:14;;;;;;;;;;;;;;;:26;2773:10;2758:26;;;;;;;;;;;;;;;;2748:6;:36;;2740:45;;;;;;2838:6;2819:8;:15;2828:5;2819:15;;;;;;;;;;;;;;;;:25;;;;;;;;;;;2924:6;2907:8;:13;2916:3;2907:13;;;;;;;;;;;;;;;;:23;;;;;;;;;;;3036:6;3006:7;:14;3014:5;3006:14;;;;;;;;;;;;;;;:26;3021:10;3006:26;;;;;;;;;;;;;;;;:36;;;;;;;;;;;3073:3;3057:28;;3066:5;3057:28;;;3078:6;3057:28;;;;;;;;;;;;;;;;;;3102:4;3095:11;;2450:660;;;;;:::o;146:23::-;;;;;;;;;;;;;:::o;816:106::-;872:15;901:8;:16;910:6;901:16;;;;;;;;;;;;;;;;894:23;;816:106;;;:::o;4769:491::-;4810:12;4881:1;4869:9;:13;4861:51;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4992:9;4962:14;:26;4977:10;4962:26;;;;;;;;;;;;;;;;:39;;;;;;;;;;;5111:9;5080:15;:27;5096:10;5080:27;;;;;;;;;;;;;;;;:40;;;;;;;;;;;5171:9;5150:17;;:30;;;;;;;;;;;5212:28;5219:10;5230:9;5212:28;;;;;;;;;;;;;;;;;;;;;;;;;;;;5252:4;5245:11;;4769:491;:::o;4403:20::-;;;;;;;;;;;;;:::o;119:21::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::o;1231:736::-;1294:12;1332:1;1323:6;:10;1315:20;;;;;;1449:6;1425:8;:20;1434:10;1425:20;;;;;;;;;;;;;;;;:30;;1417:39;;;;;;1557:8;:13;1566:3;1557:13;;;;;;;;;;;;;;;;1548:6;1532:8;:13;1541:3;1532:13;;;;;;;;;;;;;;;;:22;:38;1524:47;;;;;;1643:6;1619:8;:20;1628:10;1619:20;;;;;;;;;;;;;;;;:30;;;;;;;;;;;1731:6;1714:8;:13;1723:3;1714:13;;;;;;;;;;;;;;;;:23;;;;;;;;;;;1866:3;1845:33;;1854:10;1845:33;;;1871:6;1845:33;;;;;;;;;;;;;;;;;;1953:4;1946:11;;1231:736;;;;:::o;5279:571::-;5325:12;5410:14;:26;5425:10;5410:26;;;;;;;;;;;;;;;;5401:5;:35;;5393:72;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;5666:5;5636:14;:26;5651:10;5636:26;;;;;;;;;;;;;;;;:35;;;;;;;;;;;5697:5;5675:17;;:27;;;;;;;;;;;5748:10;:19;;:26;5768:5;5748:26;;;;;;;;;;;;;;;;;;;;;;;;8:9:-1;5:2;;;45:16;42:1;39;24:38;77:16;74:1;67:27;5:2;5748:26:0;5806:24;5813:10;5824:5;5806:24;;;;;;;;;;;;;;;;;;;;;;;;;;;;5842:4;5835:11;;5279:571;;;:::o;3993:139::-;4067:17;4103:7;:15;4111:6;4103:15;;;;;;;;;;;;;;;:25;4119:8;4103:25;;;;;;;;;;;;;;;;4096:32;;3993:139;;;;:::o',
      source: 'pragma solidity >=0.4.25 <0.7.0;\n' +
        '\n' +
        'contract Token{\n' +
        '\tmapping (address => uint256) balances;\n' +
        '    string  public name;\n' +
        '    string  public symbol;\n' +
        '    uint8   public decimals;\n' +
        '    uint256 public totalSupply;\n' +
        '    mapping (address => mapping (address => uint256)) allowed;\n' +
        '\n' +
        '    //ERC20 Token Standard: https://eips.ethereum.org/EIPS/eip-20\n' +
        '    constructor() public { \n' +
        '        name = "Token";                                         \t// Set the name \n' +
        '        symbol = "TK";                                              // Set the symbol \n' +
        '        decimals = 18;                                              // Amount of decimals for display purposes\n' +
        '//\t\ttotalSupply = 1000000000000000000000;                       // Not set total supply\t\n' +
        '    }\n' +
        '\t\n' +
        '    //Returns the account balance of another account with address _owner.\n' +
        '    function balanceOf(address _owner) public view returns (uint256 balance) {\n' +
        '\t\t return balances[_owner];\t\n' +
        '\t}\n' +
        '\n' +
        '    /* Transfers _value amount of tokens to address _to, and MUST fire the Transfer event. \n' +
        '       The function SHOULD throw if the message caller’s account balance does not have enough tokens to spend.\n' +
        '\t   Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.*/\n' +
        '    function transfer(address _to, uint256 _value) public returns (bool success) {\n' +
        "\t    require(_value > 0 );                                      // Check if token's value to be send > 0\n" +
        '\t\trequire(balances[msg.sender] >= _value);                   // Check if the sender has enough token\n' +
        '        require(balances[_to] + _value > balances[_to]);           // Check for overflows\t\t\t\t\t\t\t\t\t\t\t\n' +
        '\t\tbalances[msg.sender] -= _value;                            // Subtract token from the sender\n' +
        '\t\tbalances[_to] += _value;                                   // Add the same amount to the receiver                      \n' +
        '\t\t \n' +
        '\t\temit Transfer(msg.sender, _to, _value); \t\t\t\t   // Notify anyone listening that this transaction happen.\n' +
        '\t\treturn true;      \n' +
        '\t}\n' +
        '\n' +
        '\t/* The transferFrom method is used for a withdraw workflow, \n' +
        '\t   allowing contracts to transfer tokens on your behalf. \n' +
        '\t   This can be used for example to allow a contract to transfer tokens on your behalf and/or to charge fees in sub-currencies. \n' +
        '\t   The function SHOULD throw unless the _from account has deliberately authorized the sender of the message via some mechanism.\n' +
        '\t   Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.*/\n' +
        '    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {\n' +
        '\t  \n' +
        '\t    require(balances[_from] >= _value);                 // Check if the sender has enough token\n' +
        '        require(balances[_to] + _value >= balances[_to]);   // Check for overflows\n' +
        '        require(_value <= allowed[_from][msg.sender]);      // Check allowance\n' +
        '        balances[_from] -= _value;                          // Subtract from the sender\n' +
        '        balances[_to] += _value;                            // Add the same amount to the receiver\n' +
        '        allowed[_from][msg.sender] -= _value;\n' +
        '        emit Transfer(_from, _to, _value);\n' +
        '        return true;\n' +
        '\t}\n' +
        '\n' +
        '\t/* Allows _spender to withdraw from your account multiple times, \n' +
        '\t   up to the _value amount. If this function is called again it overwrites the current allowance with _value.\n' +
        '\t   NOTE: To prevent attack vectors like the one described here and discussed here, \n' +
        '\t   clients SHOULD make sure to create user interfaces in such a way that they set the allowance first to 0 before setting it to another value for the same spender. \n' +
        '\t   THOUGH The contract itself shouldn’t enforce it, to allow backwards compatibility with contracts deployed before */\n' +
        '    function approve(address _spender, uint256 _value) public returns (bool success) {\n' +
        '\t\trequire(balances[msg.sender] >= _value);\n' +
        '\t\tallowed[msg.sender][_spender] = _value;\n' +
        '        emit Approval(msg.sender, _spender, _value);\n' +
        '\t\treturn true;\n' +
        '\t\n' +
        '\t}\n' +
        '\t\n' +
        '\t//Returns the amount which _spender is still allowed to withdraw from _owner.\n' +
        '    function allowance(address _owner, address _spender) public view returns (uint256 remaining) {\n' +
        '        return allowed[_owner][_spender];\n' +
        '\t}\n' +
        '\t\n' +
        '\n' +
        '\t//The event for tranfer and approve\n' +
        '    event Transfer(address indexed _from, address indexed _to, uint256 _value);\n' +
        '    event Approval(address indexed _owner, address indexed _spender, uint256 _value);\n' +
        '}\n' +
        '\n' +
        '\n' +
        'contract DeFi is Token{ \n' +
        '\n' +
        '\t//声明owner,暂时没作用\n' +
        '\taddress public owner ;\n' +
        '\t//声明 用户-抵押ETH数量 mapping\n' +
        '\tmapping (address => uint) pledgeETHAmount;\n' +
        '\t\n' +
        '\t//声明 抵押/赎回时的event\n' +
        '\tevent Pledge(address user, uint256 amount);\n' +
        '\tevent Redeem(address user, uint256 amount);\n' +
        '\t\n' +
        '\t//构造函数,只在合约被部署的时候执行一次\n' +
        '\tconstructor() public {\n' +
        '\t\towner = msg.sender ;\n' +
        '\t}\n' +
        '\n' +
        '\t//抵押功能\n' +
        '\tfunction pledge() public payable returns(bool success){\n' +
        '\n' +
        '\t\t//ETH抵押金额必须大于0\n' +
        '\t\trequire(msg.value > 0, "Not enough ETH to pledge.");\n' +
        '\t\t//抵押操作\n' +
        '\t\t// 1. 1:1贷出ERC20 Token\n' +
        '\t\tToken.balances[msg.sender] += msg.value;\n' +
        '\t\t// 2. 写入抵押信息map,记录用户抵押ETH的数量:单位wei\n' +
        '\t\tpledgeETHAmount[msg.sender] += msg.value;\n' +
        '\t\t// 3. 更新Token总量\n' +
        '\t\tToken.totalSupply += msg.value;\n' +
        '\t\t//记录抵押事件\n' +
        '\t\temit Pledge(msg.sender,msg.value);\n' +
        '\n' +
        '\t\treturn true;\n' +
        '\t}\n' +
        '\n' +
        '\t//赎回功能\n' +
        '\tfunction redeem(uint256 value) public returns(bool success){\n' +
        '\n' +
        '\t\t//要求赎回ETH的数量必须 <= Token余额\n' +
        '\t\trequire(value <= Token.balances[msg.sender],"Not enough ETH to redeem.");\n' +
        '\t\t//赎回操作\n' +
        '\t\t// 1. 在合约转出ETH到用户地址之前将待发金额清零,更新用户Token余额和Token总量,来防止重入(re-entrancy)攻击\n' +
        '\t\tToken.balances[msg.sender] -= value;\n' +
        '\t\tToken.totalSupply -=  value;\n' +
        '\t\t// 2. 从合约里转ETH到对应用户\n' +
        '\t\tmsg.sender.transfer(value);\n' +
        '\t\t//记录赎回事件\n' +
        '\t\temit Redeem(msg.sender,value);\n' +
        '\n' +
        '\t\treturn true;\n' +
        '\t}\n' +
        '}',
      sourcePath: '/Users/wujinquan/workspace/eth/truffle/defi/contracts/DeFi.sol',
      ast: [Object],
      legacyAST: [Object],
      compiler: [Object],
      networks: [Object],
      schemaVersion: '3.0.23',
      updatedAt: '2020-03-17T02:03:48.180Z',
      networkType: 'ethereum',
      devdoc: [Object],
      userdoc: [Object]
    },
    configureNetwork: [Function: bound configureNetwork],
    setProvider: [Function: bound setProvider],
    new: [Function: bound new] {
      estimateGas: [Function: bound estimateDeployment]
    },
    at: [Function: bound at] AsyncFunction,
    deployed: [Function: bound deployed] AsyncFunction,
    defaults: [Function: bound defaults],
    hasNetwork: [Function: bound hasNetwork],
    isDeployed: [Function: bound isDeployed],
    detectNetwork: [Function: bound detectNetwork] AsyncFunction,
    setNetwork: [Function: bound setNetwork],
    setNetworkType: [Function: bound setNetworkType],
    setWallet: [Function: bound setWallet],
    resetAddress: [Function: bound resetAddress],
    link: [Function: bound link],
    clone: [Function: bound clone],
    addProp: [Function: bound addProp],
    toJSON: [Function: bound toJSON],
    decodeLogs: [Function: bound decodeLogs],
    class_defaults: {
      from: '0x843ACfB41E5c0F1E0587C5B765d897cCDeA8c4DD',
      gas: 6721975,
      gasPrice: 20000000000
    },
    interfaceAdapter: Web3InterfaceAdapter { web3: [Web3Shim] },
    web3: Web3Shim {
      currentProvider: [Getter/Setter],
      _requestManager: [RequestManager],
      givenProvider: null,
      providers: [Object],
      _provider: [HttpProvider],
      setProvider: [Function (anonymous)],
      BatchRequest: [Function: bound Batch],
      extend: [Function],
      version: '1.2.1',
      utils: [Object],
      eth: [Eth],
      shh: [Shh],
      bzz: [Bzz],
      networkType: 'ethereum'
    },
    currentProvider: HttpProvider {
      host: 'http://127.0.0.1:9545/',
      httpAgent: [Agent],
      timeout: 0,
      headers: undefined,
      connected: true,
      send: [Function (anonymous)],
      _alreadyWrapped: true
    },
    network_id: '5777',
    ens: { enabled: false, registryAddress: null }
  },
  methods: {
    'allowance(address,address)': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'approve(address,uint256)': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'balanceOf(address)': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'decimals()': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'name()': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'owner()': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'symbol()': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'totalSupply()': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'transfer(address,uint256)': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'transferFrom(address,address,uint256)': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'pledge()': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    },
    'redeem(uint256)': [Function (anonymous)] {
      call: [Function (anonymous)],
      sendTransaction: [Function (anonymous)],
      estimateGas: [Function (anonymous)],
      request: [Function (anonymous)]
    }
  },
  abi: [
    {
      inputs: [],
      payable: false,
      stateMutability: 'nonpayable',
      type: 'constructor',
      constant: undefined
    },
    {
      anonymous: false,
      inputs: [Array],
      name: 'Approval',
      type: 'event',
      constant: undefined,
      payable: undefined,
      signature: '0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925'
    },
    {
      anonymous: false,
      inputs: [Array],
      name: 'Pledge',
      type: 'event',
      constant: undefined,
      payable: undefined,
      signature: '0x5e91ea8ea1c46300eb761859be01d7b16d44389ef91e03a163a87413cbf55b95'
    },
    {
      anonymous: false,
      inputs: [Array],
      name: 'Redeem',
      type: 'event',
      constant: undefined,
      payable: undefined,
      signature: '0x222838db2794d11532d940e8dec38ae307ed0b63cd97c233322e221f998767a6'
    },
    {
      anonymous: false,
      inputs: [Array],
      name: 'Transfer',
      type: 'event',
      constant: undefined,
      payable: undefined,
      signature: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
    },
    {
      constant: true,
      inputs: [Array],
      name: 'allowance',
      outputs: [Array],
      payable: false,
      stateMutability: 'view',
      type: 'function',
      signature: '0xdd62ed3e'
    },
    {
      constant: false,
      inputs: [Array],
      name: 'approve',
      outputs: [Array],
      payable: false,
      stateMutability: 'nonpayable',
      type: 'function',
      signature: '0x095ea7b3'
    },
    {
      constant: true,
      inputs: [Array],
      name: 'balanceOf',
      outputs: [Array],
      payable: false,
      stateMutability: 'view',
      type: 'function',
      signature: '0x70a08231'
    },
    {
      constant: true,
      inputs: [],
      name: 'decimals',
      outputs: [Array],
      payable: false,
      stateMutability: 'view',
      type: 'function',
      signature: '0x313ce567'
    },
    {
      constant: true,
      inputs: [],
      name: 'name',
      outputs: [Array],
      payable: false,
      stateMutability: 'view',
      type: 'function',
      signature: '0x06fdde03'
    },
    {
      constant: true,
      inputs: [],
      name: 'owner',
      outputs: [Array],
      payable: false,
      stateMutability: 'view',
      type: 'function',
      signature: '0x8da5cb5b'
    },
    {
      constant: true,
      inputs: [],
      name: 'symbol',
      outputs: [Array],
      payable: false,
      stateMutability: 'view',
      type: 'function',
      signature: '0x95d89b41'
    },
    {
      constant: true,
      inputs: [],
      name: 'totalSupply',
      outputs: [Array],
      payable: false,
      stateMutability: 'view',
      type: 'function',
      signature: '0x18160ddd'
    },
    {
      constant: false,
      inputs: [Array],
      name: 'transfer',
      outputs: [Array],
      payable: false,
      stateMutability: 'nonpayable',
      type: 'function',
      signature: '0xa9059cbb'
    },
    {
      constant: false,
      inputs: [Array],
      name: 'transferFrom',
      outputs: [Array],
      payable: false,
      stateMutability: 'nonpayable',
      type: 'function',
      signature: '0x23b872dd'
    },
    {
      constant: false,
      inputs: [],
      name: 'pledge',
      outputs: [Array],
      payable: true,
      stateMutability: 'payable',
      type: 'function',
      signature: '0x88ffe867'
    },
    {
      constant: false,
      inputs: [Array],
      name: 'redeem',
      outputs: [Array],
      payable: false,
      stateMutability: 'nonpayable',
      type: 'function',
      signature: '0xdb006a75'
    }
  ],
  address: '0xB378420cde84c7A73EecfAeE2fC91ED364F45E9F',
  transactionHash: undefined,
  contract: Contract {
    currentProvider: [Getter/Setter],
    _requestManager: RequestManager {
      provider: [HttpProvider],
      providers: [Object],
      subscriptions: {}
    },
    givenProvider: null,
    providers: {
      WebsocketProvider: [Function: WebsocketProvider],
      HttpProvider: [Function: HttpProvider],
      IpcProvider: [Function: IpcProvider]
    },
    _provider: HttpProvider {
      host: 'http://127.0.0.1:9545/',
      httpAgent: [Agent],
      timeout: 0,
      headers: undefined,
      connected: true,
      send: [Function (anonymous)],
      _alreadyWrapped: true
    },
    setProvider: [Function (anonymous)],
    BatchRequest: [Function: bound Batch],
    extend: [Function: ex] {
      formatters: [Object],
      utils: [Object],
      Method: [Function: Method]
    },
    clearSubscriptions: [Function (anonymous)],
    options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
    defaultAccount: [Getter/Setter],
    defaultBlock: [Getter/Setter],
    methods: {
      allowance: [Function: bound _createTxObject],
      '0xdd62ed3e': [Function: bound _createTxObject],
      'allowance(address,address)': [Function: bound _createTxObject],
      approve: [Function: bound _createTxObject],
      '0x095ea7b3': [Function: bound _createTxObject],
      'approve(address,uint256)': [Function: bound _createTxObject],
      balanceOf: [Function: bound _createTxObject],
      '0x70a08231': [Function: bound _createTxObject],
      'balanceOf(address)': [Function: bound _createTxObject],
      decimals: [Function: bound _createTxObject],
      '0x313ce567': [Function: bound _createTxObject],
      'decimals()': [Function: bound _createTxObject],
      name: [Function: bound _createTxObject],
      '0x06fdde03': [Function: bound _createTxObject],
      'name()': [Function: bound _createTxObject],
      owner: [Function: bound _createTxObject],
      '0x8da5cb5b': [Function: bound _createTxObject],
      'owner()': [Function: bound _createTxObject],
      symbol: [Function: bound _createTxObject],
      '0x95d89b41': [Function: bound _createTxObject],
      'symbol()': [Function: bound _createTxObject],
      totalSupply: [Function: bound _createTxObject],
      '0x18160ddd': [Function: bound _createTxObject],
      'totalSupply()': [Function: bound _createTxObject],
      transfer: [Function: bound _createTxObject],
      '0xa9059cbb': [Function: bound _createTxObject],
      'transfer(address,uint256)': [Function: bound _createTxObject],
      transferFrom: [Function: bound _createTxObject],
      '0x23b872dd': [Function: bound _createTxObject],
      'transferFrom(address,address,uint256)': [Function: bound _createTxObject],
      pledge: [Function: bound _createTxObject],
      '0x88ffe867': [Function: bound _createTxObject],
      'pledge()': [Function: bound _createTxObject],
      redeem: [Function: bound _createTxObject],
      '0xdb006a75': [Function: bound _createTxObject],
      'redeem(uint256)': [Function: bound _createTxObject]
    },
    events: {
      Approval: [Function: bound ],
      '0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925': [Function: bound ],
      'Approval(address,address,uint256)': [Function: bound ],
      Pledge: [Function: bound ],
      '0x5e91ea8ea1c46300eb761859be01d7b16d44389ef91e03a163a87413cbf55b95': [Function: bound ],
      'Pledge(address,uint256)': [Function: bound ],
      Redeem: [Function: bound ],
      '0x222838db2794d11532d940e8dec38ae307ed0b63cd97c233322e221f998767a6': [Function: bound ],
      'Redeem(address,uint256)': [Function: bound ],
      Transfer: [Function: bound ],
      '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef': [Function: bound ],
      'Transfer(address,address,uint256)': [Function: bound ],
      allEvents: [Function: bound ]
    },
    _address: '0xB378420cde84c7A73EecfAeE2fC91ED364F45E9F',
    _jsonInterface: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  Approval: [Function (anonymous)],
  Pledge: [Function (anonymous)],
  Redeem: [Function (anonymous)],
  Transfer: [Function (anonymous)],
  allowance: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  approve: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  balanceOf: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  decimals: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  name: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  owner: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  symbol: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  totalSupply: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  transfer: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  transferFrom: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  pledge: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  redeem: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  sendTransaction: [Function (anonymous)],
  send: [Function (anonymous)],
  allEvents: [Function (anonymous)],
  getPastEvents: [Function (anonymous)]
}
truffle(develop)>

5.3 查询account[0]余额,即truffle自带的10个账户中的第一个账户余额

ruffle(develop)> web3.eth.getBalance(accounts[0])
'99948512240000000000'
truffle(develop)>

5.4 调用合约

简单的DeFi.sol合约内容如下

pragma solidity >=0.4.25 <0.7.0;

contract Token{
	mapping (address => uint256) balances;
    string  public name;
    string  public symbol;
    uint8   public decimals;
    uint256 public totalSupply;
    mapping (address => mapping (address => uint256)) allowed;

    //ERC20 Token Standard: https://eips.ethereum.org/EIPS/eip-20
    constructor() public { 
        name = "Token";                                         	// Set the name 
        symbol = "TK";                                              // Set the symbol 
        decimals = 18;                                              // Amount of decimals for display purposes
//		totalSupply = 1000000000000000000000;                       // Not set total supply	
    }
	
    //Returns the account balance of another account with address _owner.
    function balanceOf(address _owner) public view returns (uint256 balance) {
		 return balances[_owner];	
	}

    /* Transfers _value amount of tokens to address _to, and MUST fire the Transfer event. 
       The function SHOULD throw if the message caller’s account balance does not have enough tokens to spend.
	   Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.*/
    function transfer(address _to, uint256 _value) public returns (bool success) {
	    require(_value > 0 );                                      // Check if token's value to be send > 0
		require(balances[msg.sender] >= _value);                   // Check if the sender has enough token
        require(balances[_to] + _value > balances[_to]);           // Check for overflows											
		balances[msg.sender] -= _value;                            // Subtract token from the sender
		balances[_to] += _value;                                   // Add the same amount to the receiver                      
		 
		emit Transfer(msg.sender, _to, _value); 				   // Notify anyone listening that this transaction happen.
		return true;      
	}

	/* The transferFrom method is used for a withdraw workflow, 
	   allowing contracts to transfer tokens on your behalf. 
	   This can be used for example to allow a contract to transfer tokens on your behalf and/or to charge fees in sub-currencies. 
	   The function SHOULD throw unless the _from account has deliberately authorized the sender of the message via some mechanism.
	   Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.*/
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
	  
	    require(balances[_from] >= _value);                 // Check if the sender has enough token
        require(balances[_to] + _value >= balances[_to]);   // Check for overflows
        require(_value <= allowed[_from][msg.sender]);      // Check allowance
        balances[_from] -= _value;                          // Subtract from the sender
        balances[_to] += _value;                            // Add the same amount to the receiver
        allowed[_from][msg.sender] -= _value;
        emit Transfer(_from, _to, _value);
        return true;
	}

	/* Allows _spender to withdraw from your account multiple times, 
	   up to the _value amount. If this function is called again it overwrites the current allowance with _value.
	   NOTE: To prevent attack vectors like the one described here and discussed here, 
	   clients SHOULD make sure to create user interfaces in such a way that they set the allowance first to 0 before setting it to another value for the same spender. 
	   THOUGH The contract itself shouldn’t enforce it, to allow backwards compatibility with contracts deployed before */
    function approve(address _spender, uint256 _value) public returns (bool success) {
		require(balances[msg.sender] >= _value);
		allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
		return true;
	
	}
	
	//Returns the amount which _spender is still allowed to withdraw from _owner.
    function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
        return allowed[_owner][_spender];
	}
	

	//The event for tranfer and approve
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}


contract DeFi is Token{ 

	//声明owner,暂时没作用
	address public owner ;
	//声明 用户-抵押ETH数量 mapping
	mapping (address => uint) pledgeETHAmount;
	
	//声明 抵押/赎回时的event
	event Pledge(address user, uint256 amount);
	event Redeem(address user, uint256 amount);
	
	//构造函数,只在合约被部署的时候执行一次
	constructor() public {
		owner = msg.sender ;
	}

	//抵押功能
	function pledge() public payable returns(bool success){

		//ETH抵押金额必须大于0
		require(msg.value > 0, "Not enough ETH to pledge.");
		//抵押操作
		// 1. 1:1贷出ERC20 Token
		Token.balances[msg.sender] += msg.value;
		// 2. 写入抵押信息map,记录用户抵押ETH的数量:单位wei
		pledgeETHAmount[msg.sender] += msg.value;
		// 3. 更新Token总量
		Token.totalSupply += msg.value;
		//记录抵押事件
		emit Pledge(msg.sender,msg.value);

		return true;
	}

	//赎回功能
	function redeem(uint256 value) public returns(bool success){

		//要求赎回ETH的数量必须 <= Token余额
		require(value <= Token.balances[msg.sender],"Not enough ETH to redeem.");
		//赎回操作
		// 1. 在合约转出ETH到用户地址之前将待发金额减少,更新用户Token余额和Token总量,来防止重入(re-entrancy)攻击
		Token.balances[msg.sender] -= value;
		Token.totalSupply -=  value;
		// 2. 从合约里转ETH到对应用户
		msg.sender.transfer(value);
		//记录赎回事件
		emit Redeem(msg.sender,value);

		return true;
	}
}

尝试用accounts[0]作为交易的发起方调用pledge函数,并向合约转入0.05 ethgas设置为 220000

truffle(develop)> defi.pledge.sendTransaction({ from: accounts[0], value: web3.utils.toWei("0.05", "ether"), gas: "220000"})
{
  tx: '0x1231db0725c3a29825b176baeddf8817d24d363f25b9405d3d1c9a5b4ea62216',
  receipt: {
    transactionHash: '0x1231db0725c3a29825b176baeddf8817d24d363f25b9405d3d1c9a5b4ea62216',
    transactionIndex: 0,
    blockHash: '0x50cf21460fcec3ead1f907c3d5ce65518ab0d461179738482f0514b2e355314c',
    blockNumber: 9,
    from: '0x843acfb41e5c0f1e0587c5b765d897ccdea8c4dd',
    to: '0xb378420cde84c7a73eecfaee2fc91ed364f45e9f',
    gasUsed: 85311,
    cumulativeGasUsed: 85311,
    contractAddress: null,
    logs: [ [Object] ],
    status: true,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000200000000000000000000000000000000000040000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    rawLogs: [ [Object] ]
  },
  logs: [
    {
      logIndex: 0,
      transactionIndex: 0,
      transactionHash: '0x1231db0725c3a29825b176baeddf8817d24d363f25b9405d3d1c9a5b4ea62216',
      blockHash: '0x50cf21460fcec3ead1f907c3d5ce65518ab0d461179738482f0514b2e355314c',
      blockNumber: 9,
      address: '0xB378420cde84c7A73EecfAeE2fC91ED364F45E9F',
      type: 'mined',
      id: 'log_f43bb4d5',
      event: 'Pledge',
      args: [Result]
    }
  ]
}
truffle(develop)>

查询合约余额,确认合约是否接收到0.05 eth

truffle(develop)> let contract = await defi.address
undefined
truffle(develop)> web3.eth.getBalance(contract)
'50000000000000000'  //单位wei
truffle(develop)>

结果如预期之后,再调用pledge函数,并向合约转入0.006 ethgas设置为 220000,并查看result结果

truffle(develop)> let result = await defi.pledge.sendTransaction({ from: accounts[0], value: web3.utils.toWei("0.006", "ether"), gas: "220000"})
undefined
truffle(develop)> result   
{
  tx: '0xae408007ed7268549ee312d79c2010d5c3961e381c12996636d334d877435705',
  receipt: {
    transactionHash: '0xae408007ed7268549ee312d79c2010d5c3961e381c12996636d334d877435705',
    transactionIndex: 0,
    blockHash: '0x03dddaab0d1eb8b50287785b3327a288517227cf278532ac3c0f0af02d40d274',
    blockNumber: 10,
    from: '0x843acfb41e5c0f1e0587c5b765d897ccdea8c4dd',
    to: '0xb378420cde84c7a73eecfaee2fc91ed364f45e9f',
    gasUsed: 40311,
    cumulativeGasUsed: 40311,
    contractAddress: null,
    logs: [ [Object] ],
    status: true,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000200000000000000000000000000000000000040000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    rawLogs: [ [Object] ]
  },
  logs: [
    {
      logIndex: 0,
      transactionIndex: 0,
      transactionHash: '0xae408007ed7268549ee312d79c2010d5c3961e381c12996636d334d877435705',
      blockHash: '0x03dddaab0d1eb8b50287785b3327a288517227cf278532ac3c0f0af02d40d274',
      blockNumber: 10,
      address: '0xB378420cde84c7A73EecfAeE2fC91ED364F45E9F',
      type: 'mined',
      id: 'log_5f234e76',
      event: 'Pledge',
      args: [Result]
    }
  ]
}
truffle(develop)> result.tx   //result.tx (string) - 交易哈希 hash
'0xae408007ed7268549ee312d79c2010d5c3961e381c12996636d334d877435705'
truffle(develop)> result.receipt   //result.receipt (object) - 交易收据 receipt(包括使用的gas)
{
  transactionHash: '0xae408007ed7268549ee312d79c2010d5c3961e381c12996636d334d877435705',
  transactionIndex: 0,
  blockHash: '0x03dddaab0d1eb8b50287785b3327a288517227cf278532ac3c0f0af02d40d274',
  blockNumber: 10,
  from: '0x843acfb41e5c0f1e0587c5b765d897ccdea8c4dd',
  to: '0xb378420cde84c7a73eecfaee2fc91ed364f45e9f',
  gasUsed: 40311,
  cumulativeGasUsed: 40311,
  contractAddress: null,
  logs: [
    {
      logIndex: 0,
      transactionIndex: 0,
      transactionHash: '0xae408007ed7268549ee312d79c2010d5c3961e381c12996636d334d877435705',
      blockHash: '0x03dddaab0d1eb8b50287785b3327a288517227cf278532ac3c0f0af02d40d274',
      blockNumber: 10,
      address: '0xB378420cde84c7A73EecfAeE2fC91ED364F45E9F',
      type: 'mined',
      id: 'log_5f234e76',
      event: 'Pledge',
      args: [Result]
    }
  ],
  status: true,
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000200000000000000000000000000000000000040000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  rawLogs: [
    {
      logIndex: 0,
      transactionIndex: 0,
      transactionHash: '0xae408007ed7268549ee312d79c2010d5c3961e381c12996636d334d877435705',
      blockHash: '0x03dddaab0d1eb8b50287785b3327a288517227cf278532ac3c0f0af02d40d274',
      blockNumber: 10,
      address: '0xB378420cde84c7A73EecfAeE2fC91ED364F45E9F',
      data: '0x000000000000000000000000843acfb41e5c0f1e0587c5b765d897ccdea8c4dd000000000000000000000000000000000000000000000000001550f7dca70000',
      topics: [Array],
      type: 'mined',
      id: 'log_5f234e76'
    }
  ]
}
truffle(develop)> result.logs  //result.logs (array) - 解码过的事件 (日志)
[
  {
    logIndex: 0,
    transactionIndex: 0,
    transactionHash: '0xae408007ed7268549ee312d79c2010d5c3961e381c12996636d334d877435705',
    blockHash: '0x03dddaab0d1eb8b50287785b3327a288517227cf278532ac3c0f0af02d40d274',
    blockNumber: 10,
    address: '0xB378420cde84c7A73EecfAeE2fC91ED364F45E9F',
    type: 'mined',
    id: 'log_5f234e76',
    event: 'Pledge',
    args: Result {
      '0': '0x843ACfB41E5c0F1E0587C5B765d897cCDeA8c4DD',
      '1': [BN],
      __length__: 2,
      user: '0x843ACfB41E5c0F1E0587C5B765d897cCDeA8c4DD',
      amount: [BN]
    }
  }
]
truffle(develop)>

再次查看合约eth余额,如预期:0.05 + 0.006 = 0.056 eth

扫描二维码关注公众号,回复: 9918649 查看本文章
truffle(develop)> web3.eth.getBalance(contract)
'56000000000000000'  //单位wei
truffle(develop)>

6. 进阶

以太坊ETH-智能合约开发-truffle框架使用进阶

发布了32 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u010159567/article/details/104864291