部署智能合约
上面介绍了在Nebulas中怎么去编写一个智能合约,现在我们需要把智能合约部署到链上。在Nebulas中部署一个智能合约其实也是发送一个transaction来实现.
我们可以通过WebWallet快速部署,详情信息请点击下面的链接
在本地测试,我们可以直接使用解锁 & 发送的方式来发送交易。 首先,我们在conf/default/genesis.conf中预分配过代币的账户里,选择账户n1H4MYms9F55ehcvygwWE71J8tJC4CRr2so作为本章的发送者账号,并检查该账户的状态。
> curl -i -H Accept:application/json -X POST http://localhost:8685/v1/user/accountstate -d '{"address":"n1H4MYms9F55ehcvygwWE71J8tJC4CRr2so"}'
{"result":{"balance":"5000000000000000000000000","nonce":"0","type":87}}该账户有足够的钱来支付手续费,接下来,我们解锁发送者账户n1H4MYms9F55ehcvygwWE71J8tJC4CRr2so,解锁12小时。
> curl -i -H 'Content-Type: application/json' -X POST http://localhost:8685/v1/admin/account/unlock -d '{"address":"n1H4MYms9F55ehcvygwWE71J8tJC4CRr2so","passphrase":"passphrase","duration":"43200000000000"}'
{"result":{"result":true}}然后,我们发送部署BankVault合约的交易。
> curl -i -H 'Accept: application/json' -X POST http://localhost:8685/v1/admin/transactionWithPassphrase -H 'Content-Type: application/json' -d '{"transaction": {"from":"n1H4MYms9F55ehcvygwWE71J8tJC4CRr2so","to":"n1H4MYms9F55ehcvygwWE71J8tJC4CRr2so", "value":"0","nonce":1,"gasPrice":"1000000","gasLimit":"2000000","contract":{"source":"\"use strict\";var DepositeContent=function(text){if(text){var o=JSON.parse(text);this.balance=new BigNumber(o.balance);this.expiryHeight=new BigNumber(o.expiryHeight);}else{this.balance=new BigNumber(0);this.expiryHeight=new BigNumber(0);}};DepositeContent.prototype={toString:function(){return JSON.stringify(this);}};var BankVaultContract=function(){LocalContractStorage.defineMapProperty(this,\"bankVault\",{parse:function(text){return new DepositeContent(text);},stringify:function(o){return o.toString();}});};BankVaultContract.prototype={init:function(){},save:function(height){var from=Blockchain.transaction.from;var value=Blockchain.transaction.value;var bk_height=new BigNumber(Blockchain.block.height);var orig_deposit=this.bankVault.get(from);if(orig_deposit){value=value.plus(orig_deposit.balance);} var deposit=new DepositeContent();deposit.balance=value;deposit.expiryHeight=bk_height.plus(height);this.bankVault.put(from,deposit);},takeout:function(value){var from=Blockchain.transaction.from;var bk_height=new BigNumber(Blockchain.block.height);var amount=new BigNumber(value);var deposit=this.bankVault.get(from);if(!deposit){throw new Error(\"No deposit before.\");} if(bk_height.lt(deposit.expiryHeight)){throw new Error(\"Can not takeout before expiryHeight.\");} if(amount.gt(deposit.balance)){throw new Error(\"Insufficient balance.\");} var result=Blockchain.transfer(from,amount);if(!result){throw new Error(\"transfer failed.\");} Event.Trigger(\"BankVault\",{Transfer:{from:Blockchain.transaction.to,to:from,value:amount.toString()}});deposit.balance=deposit.balance.sub(amount);this.bankVault.put(from,deposit);},balanceOf:function(){var from=Blockchain.transaction.from;return this.bankVault.get(from);},verifyAddress:function(address){var result=Blockchain.verifyAddress(address);return{valid:result==0?false:true};}};module.exports=BankVaultContract;","sourceType":"js", "args":""}}, "passphrase": "passphrase"}'
{"result":{"txhash":"aaebb86d15ca30b86834efb600f82cbcaf2d7aaffbe4f2c8e70de53cbed17889","contract_address":"n1rVLTRxQEXscTgThmbTnn2NqdWFEKwpYUM"}}from: 合约创建者账户地址
to: 和from一致,同为合约创建者地址
value:部署合约时为
"0"nonce: 比创建者当前的
nonce多1,可以通过GetAccountState获取创建前当前noncegasPrice:部署智能合约用到的gasPrice,可以通过
GetGasPrice获取,或者使用默认值:"1000000";gasLimit: 部署合约的gasLimit,通过
EstimateGas可以获取部署合约的gas消耗,不能使用默认值,也可以设置一个较大值,执行时以实际使用计算。contract: 合约信息,部署合约时传入的参数
source: 合约代码sourceType: 合约代码类型,支持js和ts(对应javaScript和typeScript代码)args: 合约初始化方法参数,无参数为空字符串,有参数时为JSON数组
部署智能合约的返回值是transaction的hash地址txhash和合约的部署地址contract_address。 得到返回值并不能保证合约已经部署成功,因为发送交易是一个异步的过程,需要经过矿工打包,正如之前的转账交易一样,转账并不能实时到账,依赖矿工打包的速度,所以需要等待一段时间,然后可以通过查询合约地址的信息或者调用智能合约来验证合约是否部署成功。
验证合约是否部署成功
在部署智能合约的时候得到了transaction的hash地址
txhash,我们可以很方便的使用console控制台查询transaction的hash信息来验证合约是否部署成功。如上所示,部署合约的交易的状态变成了1,表示合约部署成功了。
测试模式
通测试网CURL模式
Last updated