self.bot = self.node.eth.contract(self.node.toChecksumAddress(BOT_CONTRACT), abi=BOT_ABI) # router
def buy(self, bnb_amount, amountOutMin, token_address, router):
bnb_amount = self.node.toWei(bnb_amount, "ether")
token_address = self.node.toChecksumAddress(token_address)
router = self.node.toChecksumAddress(router)
tx = self.bot.functions.buyTokens(bnb_amount, amountOutMin, token_address, router).buildTransaction({
"value": bnb_amount,
'gas': 300000,
'gasPrice': 5000000000,
'nonce': self.bot.nonce
})
signed_tx = self.node.eth.account.sign_transaction(tx, self.pk)
tx_hash = self.node.eth.send_raw_transaction(signed_tx.rawTransaction)
return tx_hash.hex()
返回数据
tx {'chainId': 56, 'value': 10000000000000000, 'gas': 300000, 'gasPrice': 5000000000, 'nonce': 99, 'to': '0xa2D02F3557041b4125eE1BF6f9D062E886BF35B1', 'data': '0x4eecaedc000000000000000000000000000000000000000000000000002386f26fc100000000000000000000000000000000000000000000c1ded63574de0e466000000000000000000000000000000026193c7fa4354ae49ec53ea2cebc513dc39a10aa00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e'}
sign_tx 0xddc254eb1275e6fd1973383f476b5fe6460a139cfe95567eda8d65330044901a
python 中buyTokens2对应的Sodility方法如下:
function buyTokens(uint ethAmount,uint amountOutMin, address tokenAddress, address routerAddress) public payable onlyOwner {
uint buyAmount;
uint amountOutMin;
// if ethAmount > balance then change ethAmount to address balance
if(ethAmount > address(this).balance){
buyAmount = address(this).balance;
}else{
buyAmount = ethAmount;
}
amountOutMin = amountOutMin;
require(buyAmount <= address(this).balance, "Not enough ETH");
IERC20 token = IERC20(tokenAddress);
if(token.allowance(address(this), routerAddress) < 1){
require(token.approve(routerAddress, MAX_UINT),"FAIL TO APPROVE");
}
address spender = routerAddress;
IPancakeRouter02 router = IPancakeRouter02(routerAddress);
address[] memory path = new address[](2);
path[0] = router.WETH();
path[1] = tokenAddress;
router.swapExactETHForTokens{value: buyAmount}(amountOutMin, path, address(this), block.timestamp+60);
}
以上代码 可以交易成功,但是amountOutMin传入后看起来是无效的,是传的方法不对吗
====================================================
2、用Python代码调用提现方法,从合约提现到钱包不成功,但是用Remix在线执行是可以成功的 以下是Sodility代码:
function withdraw() public onlyOwner payable{
uint amount = pendingWithdrawals[msg.sender];
pendingWithdrawals[msg.sender] = 0;
owner.transfer(address(this).balance);
}
python 的调用如下:
def withdraw_test(self):
self.bot.functions.withdraw()