说在前面:
请先阅读此文章:ERC20 代币 API 调用-前期准备(一)
一、查看合约对象可调用的 api 接口
在JS交互窗口中输入:
cfcContract
得到以下输出:
1> cfcContract 2{ 3 abi: [{ 4 constant: true, 5 inputs: [], 6 name: "name", 7 outputs: [{...}], 8 payable: false, 9 stateMutability: "view", 10 type: "function" 11 }, { 12 constant: false, 13 inputs: [{...}, {...}], 14 name: "approve", 15 outputs: [{...}], 16 payable: false, 17 stateMutability: "nonpayable", 18 type: "function" 19 }, { 20 constant: true, 21 inputs: [], 22 name: "totalSupply", 23 outputs: [{...}], 24 payable: false, 25 stateMutability: "view", 26 type: "function" 27 }, { 28 constant: false, 29 inputs: [{...}, {...}, {...}], 30 name: "transferFrom", 31 outputs: [{...}], 32 payable: false, 33 stateMutability: "nonpayable", 34 type: "function" 35 }, { 36 constant: true, 37 inputs: [], 38 name: "decimals", 39 outputs: [{...}], 40 payable: false, 41 stateMutability: "view", 42 type: "function" 43 }, { 44 constant: false, 45 inputs: [{...}], 46 name: "withdrawEther", 47 outputs: [], 48 payable: false, 49 stateMutability: "nonpayable", 50 type: "function" 51 }, { 52 constant: false, 53 inputs: [{...}], 54 name: "burn", 55 outputs: [{...}], 56 payable: false, 57 stateMutability: "nonpayable", 58 type: "function" 59 }, { 60 constant: false, 61 inputs: [{...}], 62 name: "unfreeze", 63 outputs: [{...}], 64 payable: false, 65 stateMutability: "nonpayable", 66 type: "function" 67 }, { 68 constant: true, 69 inputs: [{...}], 70 name: "balanceOf", 71 outputs: [{...}], 72 payable: false, 73 stateMutability: "view", 74 type: "function" 75 }, { 76 constant: true, 77 inputs: [], 78 name: "owner", 79 outputs: [{...}], 80 payable: false, 81 stateMutability: "view", 82 type: "function" 83 }, { 84 constant: true, 85 inputs: [], 86 name: "symbol", 87 outputs: [{...}], 88 payable: false, 89 stateMutability: "view", 90 type: "function" 91 }, { 92 constant: false, 93 inputs: [{...}, {...}], 94 name: "transfer", 95 outputs: [], 96 payable: false, 97 stateMutability: "nonpayable", 98 type: "function" 99 }, { 100 constant: true, 101 inputs: [{...}], 102 name: "freezeOf", 103 outputs: [{...}], 104 payable: false, 105 stateMutability: "view", 106 type: "function" 107 }, { 108 constant: false, 109 inputs: [{...}], 110 name: "freeze", 111 outputs: [{...}], 112 payable: false, 113 stateMutability: "nonpayable", 114 type: "function" 115 }, { 116 constant: true, 117 inputs: [{...}, {...}], 118 name: "allowance", 119 outputs: [{...}], 120 payable: false, 121 stateMutability: "view", 122 type: "function" 123 }, { 124 inputs: [{...}, {...}, {...}, {...}], 125 payable: false, 126 stateMutability: "nonpayable", 127 type: "constructor" 128 }, { 129 payable: true, 130 stateMutability: "payable", 131 type: "fallback" 132 }, { 133 anonymous: false, 134 inputs: [{...}, {...}, {...}], 135 name: "Transfer", 136 type: "event" 137 }, { 138 anonymous: false, 139 inputs: [{...}, {...}], 140 name: "Burn", 141 type: "event" 142 }, { 143 anonymous: false, 144 inputs: [{...}, {...}], 145 name: "Freeze", 146 type: "event" 147 }, { 148 anonymous: false, 149 inputs: [{...}, {...}], 150 name: "Unfreeze", 151 type: "event" 152 }], 153 address: "0xd4b33b74cec5141c0c40be4a664641495c6afc7e", 154 transactionHash: null, 155 Burn: function(), 156 Freeze: function(), 157 Transfer: function(), 158 Unfreeze: function(), 159 allEvents: function(), 160 allowance: function(), 161 approve: function(), 162 balanceOf: function(), 163 burn: function(), 164 decimals: function(), 165 freeze: function(), 166 freezeOf: function(), 167 name: function(), 168 owner: function(), 169 symbol: function(), 170 totalSupply: function(), 171 transfer: function(), 172 transferFrom: function(), 173 unfreeze: function(), 174 withdrawEther: function() 175} 176>
返回到控制台的结果就是你可以调用的各个接口。
二、简单实例如何调用接口
1将智能合约中的eth代币转出: 2- withdrawEther方法调用 3web3.eth.defaultAccount = "0xbb438852706212284ff8E1ED6dA7F000e9ecd1A6" 4personal.unlockAccount("私钥") 5cfcContract.withdrawEther(75000000000000000) 6 7代币销毁: 8- burn方法调用 9personal.unlockAccount("私钥") 10cfcContract.burn(100000000000000000000) // 由于该cfc代币的denamic为18,所以要销毁的数量=数量*10^18 11 12代币冻结: 13- freeze方法调用 14personal.unlockAccount("私钥") 15cfcContract.freeze(100000000000000000000) 16 17代币解冻: 18- unfreeze方法调用 19personal.unlockAccount("私钥") 20cfcContract.unfreeze(100000000000000000000)
以上只使用了部分接口,其他接口都雷同。
web3.eth.defaultAccount 方法是指定默认账户,可代替 from(发送方的账户)
如果搭建过程中遇到任何问题,请发送问题致邮件:Adamson_xgp@163.com,我会尽我所能帮您解答。
打赏(加密货币)
BTC地址:1MHAPXoydTMTXi9r5mHYp3kGFYkKGcpXf
ETH地址:0x8d345c08805c1e0d21f518bb301640e937b8c2c6
ADA地址:DdzFFzCqrhsqmBCy6zTcdy1bshH3p5LtfmssqvL8AUcPjf55q7LteaJtx7eztaayftSj3aKWS4qWqUPJVt3ZiPVmtoKHdRYrfMHDzwGn
EOS地址:0x8d345c08805c1e0d21f518bb301640e937b8c2c6