這里將展示,如何在波場使用tronWeb呼叫智能合約payable修飾的方法,并實作轉賬
波場合約代碼
pragma solidity ^0.5.0;
//請注意這個僅是Demo,請不要用到正式環境
contract PayTest is Ownable{
//向當前合約存款
function deposit(uint256 amount)public payable {
//msg.sender 全域變數,呼叫合約的發起方
//msg.value 全域變數,呼叫合約的發起方轉發的貨幣量,以wei為單位,
//send() 執行的結果
//address(this).send(amount);
//address(this).transfer(amount);
//lpToken.transferFrom(address(msg.sender), address(this), amount);
}
//取款
function withdraw(uint256 _amount)public {
//owner.transfer(address(this).balance);
msg.sender.transfer(address(this).balance/2);
}
}
下面tronWeb呼叫deposit方法,實作轉賬
const value = tronWeb.toBigNumber(amount * Math.pow(10, decimals))
let instance = await tronWeb.contract().at(contractAddress);
let res = await instance["deposit"](tronWeb.toHex(value.toNumber()));
let tx=res.send({
feeLimit:100000000,
callValue:tronWeb.toSun(amount),
tokenId:0,
shouldPollResponse:true
});
console.log(tx)
寫這邊文章當然也參考了:https://stackoverflow.com/questions/62015095/how-to-call-payable-functions-in-tron-web
不要忘記給博主點個贊喲
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/74301.html
標籤:其他
