波场tronWeb调用智能合约payable修饰的方法

这里将展示,如何在波场使用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

不要忘记给博主点个赞哟

猜你喜欢

转载自blog.csdn.net/weixin_39842528/article/details/108613674