ERC20 代币 API 调用-前期准备(一)

说在前面:

如果你正在为关于 ERC20 相关问题而发愁,写了一些关于 ERC20 系列的文档,请看笔者的大纲链接,应该能够帮助你走出现在的困境。

大纲链接:ERC20 代币整体分类大纲与实现

注意:笔者以下操作皆在主网。

一、运行 Geth(go-ethereum) 钱包节点

1.打开 liunx 连接窗口,并输入以下命令:

# 后台运行并启动 geth 快速同步主网区块
nohup geth --syncmode "fast" --datadir "同步区块的目录"  --rpc --rpcapi "eth,web3,personal" --rpcport 8545--rpcaddr "0.0.0.0" --cache=1024 --maxpeers 150 &

2.另起一个连接窗口,输入以下命令:

# 启动交互式 JavaScript 环境
geth --datadir "同步区块的目录" attach

启动成功后,大致如下图:

ERC20 代币 API 调用-前期准备(一)_第1张图片

你可以输入各种命令来进行交互,例如

0cad255af45e639a22abbb3a140cab72a9d.jpg

 

二、获取代币的  contract abi

contract abi 中保存了智能合约代币的实现方法以及方法需要的参数,笔者后面调用代币 api 进行转账等等一些其他操作,其转账函数是必须要存在于 abi 中,而想要 abi 存在该转账函数,就与发币的智能合约有关,需要合约里面写了转账函数才行。

简单来说:abi 就是发币智能合约代码的缩影,它里面浓缩了该发币合约代码的所有变量、函数以及参数。

eth主网浏览器地址:https://etherscan.io

3a6acf037f17200048636380e12934c462a.jpg

在搜索框中输入你要查找代币的合约地址,得到如下图:

ERC20 代币 API 调用-前期准备(一)_第2张图片

如果读者将 abi 的 json 格式化一下,会很清晰的看出合约代码中的函数等等,为了读者能更好的理解,笔者就辛苦一下,将自己的 json 贴出来,如下:

[
    {
        "constant": true, 
        "inputs": [ ], 
        "name": "name", 
        "outputs": [
            {
                "name": "", 
                "type": "string"
            }
        ], 
        "payable": false, 
        "stateMutability": "view", 
        "type": "function"
    }, 
    {
        "constant": false, 
        "inputs": [
            {
                "name": "_spender", 
                "type": "address"
            }, 
            {
                "name": "_value", 
                "type": "uint256"
            }
        ], 
        "name": "approve", 
        "outputs": [
            {
                "name": "success", 
                "type": "bool"
            }
        ], 
        "payable": false, 
        "stateMutability": "nonpayable", 
        "type": "function"
    }, 
    {
        "constant": true, 
        "inputs": [ ], 
        "name": "totalSupply", 
        "outputs": [
            {
                "name": "", 
                "type": "uint256"
            }
        ], 
        "payable": false, 
        "stateMutability": "view", 
        "type": "function"
    }, 
    {
        "constant": false, 
        "inputs": [
            {
                "name": "_from", 
                "type": "address"
            }, 
            {
                "name": "_to", 
                "type": "address"
            }, 
            {
                "name": "_value", 
                "type": "uint256"
            }
        ], 
        "name": "transferFrom", 
        "outputs": [
            {
                "name": "success", 
                "type": "bool"
            }
        ], 
        "payable": false, 
        "stateMutability": "nonpayable", 
        "type": "function"
    }, 
    {
        "constant": true, 
        "inputs": [ ], 
        "name": "decimals", 
        "outputs": [
            {
                "name": "", 
                "type": "uint8"
            }
        ], 
        "payable": false, 
        "stateMutability": "view", 
        "type": "function"
    }, 
    {
        "constant": false, 
        "inputs": [
            {
                "name": "amount", 
                "type": "uint256"
            }
        ], 
        "name": "withdrawEther", 
        "outputs": [ ], 
        "payable": false, 
        "stateMutability": "nonpayable", 
        "type": "function"
    }, 
    {
        "constant": false, 
        "inputs": [
            {
                "name": "_value", 
                "type": "uint256"
            }
        ], 
        "name": "burn", 
        "outputs": [
            {
                "name": "success", 
                "type": "bool"
            }
        ], 
        "payable": false, 
        "stateMutability": "nonpayable", 
        "type": "function"
    }, 


    篇幅太多了,少贴一点,后面的就省略了。
    ............
]

 

三、获取智能合约对象

在JS交互窗口中输入:

1.将上一步我们得到的 abi 存放到变量内


var cfcAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"withdrawEther","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"unfreeze","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"freezeOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"freeze","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"decimalUnits","type":"uint8"},{"name":"tokenSymbol","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Freeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Unfreeze","type":"event"}];

 

2.实例化智能合约

# 在 at 后面填写你发送代币的合约地址
var cfcContract = web3.eth.contract(cfcAbi).at('0xd4b33b74cec5141c0c40be4a664641495c6afc7e');

到这里,前期准备已经完了,接下来可直接调用api,具体调用请看下一节:ERC20 代币 API 调用-代币功能函数(二)

 

 

 

 

如果搭建过程中遇到任何问题,请发送问题致邮件:[email protected],我会尽我所能帮您解答。

 

 

打赏(加密货币)

BTC地址:1MHAPXoydTMTXi9r5mHYp3kGFYkKGcpXf

ETH地址:0x8d345c08805c1e0d21f518bb301640e937b8c2c6

ADA地址:DdzFFzCqrhsqmBCy6zTcdy1bshH3p5LtfmssqvL8AUcPjf55q7LteaJtx7eztaayftSj3aKWS4qWqUPJVt3ZiPVmtoKHdRYrfMHDzwGn

EOS地址:0x8d345c08805c1e0d21f518bb301640e937b8c2c6

 

转载于:https://my.oschina.net/u/3050295/blog/2050343

你可能感兴趣的:(ERC20 代币 API 调用-前期准备(一))