const
Base
=
require
(
'./base.js'
);
var
Web3
=
require
(
"web3"
);
//创建web3对象
// 连接到以太坊节点
if
(
typeof
web3
!==
'undefined'
) {
web3
=
new
Web3
(
web3
.
currentProvider
);
}
else
{
// set the provider you want from Web3.providers
web3
=
new
Web3
(
new
Web3
.
providers
.
HttpProvider
(
"http://localhost:8545"
));
}
var
abi
=
[ {
"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"
:
""
,
"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"
:
""
,
"type"
:
"bool"
} ],
"payable"
:
false
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"function"
}, {
"constant"
:
true
,
"inputs"
:
[],
"name"
:
"INITIAL_SUPPLY"
,
"outputs"
:
[ {
"name"
:
""
,
"type"
:
"uint256"
} ],
"payable"
:
false
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}, {
"constant"
:
true
,
"inputs"
:
[],
"name"
:
"decimals"
,
"outputs"
:
[ {
"name"
:
""
,
"type"
:
"uint8"
} ],
"payable"
:
false
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}, {
"constant"
:
false
,
"inputs"
:
[ {
"name"
:
"_spender"
,
"type"
:
"address"
}, {
"name"
:
"_subtractedValue"
,
"type"
:
"uint256"
} ],
"name"
:
"decreaseApproval"
,
"outputs"
:
[ {
"name"
:
""
,
"type"
:
"bool"
} ],
"payable"
:
false
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"function"
}, {
"constant"
:
true
,
"inputs"
:
[ {
"name"
:
"_owner"
,
"type"
:
"address"
} ],
"name"
:
"balanceOf"
,
"outputs"
:
[ {
"name"
:
"balance"
,
"type"
:
"uint256"
} ],
"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"
:
[ {
"name"
:
""
,
"type"
:
"bool"
} ],
"payable"
:
false
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"function"
}, {
"constant"
:
false
,
"inputs"
:
[ {
"name"
:
"_spender"
,
"type"
:
"address"
}, {
"name"
:
"_addedValue"
,
"type"
:
"uint256"
} ],
"name"
:
"increaseApproval"
,
"outputs"
:
[ {
"name"
:
""
,
"type"
:
"bool"
} ],
"payable"
:
false
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"function"
}, {
"constant"
:
true
,
"inputs"
:
[ {
"name"
:
"_owner"
,
"type"
:
"address"
}, {
"name"
:
"_spender"
,
"type"
:
"address"
} ],
"name"
:
"allowance"
,
"outputs"
:
[ {
"name"
:
""
,
"type"
:
"uint256"
} ],
"payable"
:
false
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}, {
"inputs"
:
[],
"payable"
:
false
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"constructor"
}, {
"anonymous"
:
false
,
"inputs"
:
[ {
"indexed"
:
true
,
"name"
:
"owner"
,
"type"
:
"address"
}, {
"indexed"
:
true
,
"name"
:
"spender"
,
"type"
:
"address"
}, {
"indexed"
:
false
,
"name"
:
"value"
,
"type"
:
"uint256"
} ],
"name"
:
"Approval"
,
"type"
:
"event"
}, {
"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"
}];
var
address
=
'0x3be29127330474399a7449b16a1ec01980e71690'
;
//通过abi以及合约部署的地址实例化一个coin
var
coin
=
web3
.
eth
.
contract
(
abi
).
at
(
address
);
//必须先定义defaultAccount,不然会报invalid address错
web3
.
eth
.
defaultAccount
=
web3
.
eth
.
accounts
[
0
];
module
.
exports
=
class
extends
Base
{
async
indexAction
() {
console
.
log
(
web3
.
eth
.
defaultAccount
);
this
.
assign
(
'title11'
,
web3
.
eth
.
coinbase
);
await
this
.
assign
(
'xianjin'
,
coin
.
balanceOf
.
call
(
web3
.
eth
.
coinbase
));
// console.log(coin.balanceOf(web3.eth.accounts[1]));
// coin.transferFrom(web3.eth.accounts[0],web3.eth.accounts[1], 800);
coin
.
transfer
(
web3
.
eth
.
accounts
[
1
],
200
);
// console.log(web3.eth.accounts[1]);
return
this
.
display
();
}
};