此文档为个人整理学习使用,部分内容来源于网络,也期待能给你带来益处。
以太坊的机制、基本合约都已经看过了,怎么再进一步做一个线上的合约呢?把以下内容都看一遍,不进阶都难吧!
solidity不懂就查
http://solidity.readthedocs.io/en/develop/index.html
solidity中文,聊胜于无
http://blog.csdn.net/xiatiancc/article/details/78910374
创建TOKEN示例
https://www.ethereum.org/token
https://www.ethereum.org/dao#the-shareholder-association
ROLL应用
https://etheroll.com/#tab7
帐号信息如下:
account1:0x54b865714068f5f03574ace39a1f3279c4e83e2c
account2:0x469f07278600b1cb031448fd25f187f24a89ecb6
account3:0x068a7a0022a0e20d78682f39aa75547a0a260a2a
contract addr:0xF4eAF74F94c84D8195Ee73BB0015e4855722731b
重新用https://www.ethereum.org/token此处完整例子部署合约,设置88个珍惜标志,小数位数为0
先在mist设置好买价格为5Ether,卖为0Ether,在web3js中调用buy,调用成果但一直没法真正买成功,输出如下:
confirmation: 0
receipt: { blockHash: '0x3d9657b5f9c6d687bf21d1958a0d1affb236e4b87ed0f0970514183557094b1e',
blockNumber: 8470,
contractAddress: null,
cumulativeGasUsed: 90000,
from: '0x54b865714068f5f03574ace39a1f3279c4e83e2c',
gasUsed: 90000,
logsBloom: '...',
root: '0x9eaf64c3d9946e4a29a6d8df176447c994cd9f53ad77ec108313d78b909a457f',
to: '0xf4eaf74f94c84d8195ee73bb0015e4855722731b',
transactionHash: '0x1a244972a4cf03b747334ec77ba50a8b0bdf1457b4d2724d157cddd489b2e714',
transactionIndex: 0,
events: {} }
开始以为是参数设置问题,调整几次也没用,其实从输出可以看出是从account1到contract addr,再次查询buy代码:
/// @notice Buy tokens from contract by sending ether
function buy() payable public {
uint amount = msg.value / buyPrice; // calculates the amount
_transfer(this, msg.sender, amount); // makes the transfers
}
没有收到Transfer事件,也就是说这个过程可能出错了,_transfer中第一个参数是this,这个是合约地址,也就是说需要从合约地址转代币“GF88”到account1,而刚才建立代币“GF88”时是用account3建立的,合约地址上并没有这种代币!
从mist转了37个代币“GF88”到合约地址,再次调用web3js,这次成功了!成功输出如下:
receipt: { blockHash: '0x68019fe35bda68b5e1ab26d1de239d203b470828f05f5429ff9cde94ed4eae7c',
blockNumber: 8513,
contractAddress: null,
cumulativeGasUsed: 36009,
from: '0x54b865714068f5f03574ace39a1f3279c4e83e2c',
gasUsed: 36009,
logsBloom: '...',
root: '0x81523d3f9cc640c80b3b97339c0caa06a075adbd91621cdc16983badca10f3bc',
to: '0xf4eaf74f94c84d8195ee73bb0015e4855722731b',
transactionHash: '0x076c89af087d4589bdc1c1a579c214252434b8b057ec617e93b7009ddfd46b32',
transactionIndex: 0,
events:
{ Transfer:
{ address: '0xF4eAF74F94c84D8195Ee73BB0015e4855722731b',
blockNumber: 8513,
transactionHash: '0x076c89af087d4589bdc1c1a579c214252434b8b057ec617e93b7009ddfd46b32',
transactionIndex: 0,
blockHash: '0x68019fe35bda68b5e1ab26d1de239d203b470828f05f5429ff9cde94ed4eae7c',
logIndex: 0,
removed: false,
id: 'log_6b233e6a',
returnValues: [Object],
event: 'Transfer',
signature: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
raw: [Object] } } }
web3js调用代码如下:
contract.methods.buy().send({from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c", value:web3.utils.toWei("10")})
.on('transactionHash', function(hash){
console.log("transactionHash:", hash);
})
.on('confirmation', function(confirmationNumber, receipt){
console.log("confirmation:", confirmationNumber);
})
.on('receipt', function(receipt){
// receipt example
console.log("receipt:", receipt); //查询这里可以得到结果
})
.on('error', console.error); // If a out of gas error, the second parameter is the receipt.
function要求使用view声明
solidity某版本要求用view或pure,view为只读不改,pure为不读不改,类似math函数
调用方法时报gas不够
试了多次,如果被执行函数中require条件不满足则直接提示此错误,当把条件改成满足后则可以调用执行!难道调用前先在本地做过一次计算,发现条件不满足?
***************************************
老鱼,从事互联网技术十余年,知名上市公司CTO;炒股十余年,从亏损几十万到略有盈利;顺应创业大潮多次创业,在传统行业和手游行业有所获;研习佛学易理,性格淡泊清净!
欢迎有兴趣的朋友加入微信群和电报群,一起学习探讨,一起实现咱们的小目标!