Hi to everyone)
Can someone help me pls?
I tried to add liquidity from function in my smart contract on pancakeSwap BSC testnet, but its aways Fail.
Im using this router 0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3
If from their UI web site (pancake.kiemtienonline360.com) its works fine, but from my contract can't.
This is my contract:
https://testnet.bscscan.com/address/0x942799b172842fd466be9152870043a4566837d1#code
This is tx with error:
https://testnet.bscscan.com/tx/0xc26af890a15dcdab7fe3d83e32847bd4eb5f66f67836e338c2747db2962dcc99
I tried already so many things, but still stuck on that.
Router contract approved also, but still cant.
Any advice, opinion will be really appreciate, Tq
Mar 7, 2022, 6:18 AM
This no official pancakeswap website buddy
Mar 7, 2022, 6:47 AM
I had same issue on testnet, I couldn't add test liquidity bnb, in your code did u add the correct testnet router address?
Testnet one
Mar 7, 2022, 6:49 AM
Hello mate , how create ico website like pinksale? Any open source ?
Mar 7, 2022, 6:49 AM
What a question. Can't be less precise.
Mar 7, 2022, 7:24 AM
yeah, correct address
yeah, its from community. official pancakeswap not support testnet
did you figured out this issue? or maybe you tried it in mainnet?
and remix showed to me thiss error
Mar 7, 2022, 8:05 AM
Just use simple contact hu
Send me your token and simple name i will give you simple contract like baby doge contract
Mar 7, 2022, 8:07 AM
im not really understand what you mean.
I already deployed my contract and there already have token.
The issue for me is how to add liquidity from function in my contract
I already deployed my contract and there already have token.
The issue for me is how to add liquidity from function in my contract
Mar 7, 2022, 8:09 AM
Maybe this function not working just add on pancakeswap not testnet
Mar 7, 2022, 8:09 AM
hmm. its works in testnet, have the same router like in mainnet
but not work for me if i integrate this contract in my contract
i find my error in pancake Router (testnet):
library TransferHelper {
function safeApprove(
address token,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('approve(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(
success && (data.length == 0 abi.decode(data, (bool))),
'TransferHelper::safeApprove: approve failed'
);
}
function safeTransfer(
address token,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('transfer(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(
success && (data.length == 0 abi.decode(data, (bool))),
'TransferHelper::safeTransfer: transfer failed'
);
}
function safeTransferFrom(
address token,
address from,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::transferFrom: transferFrom failed'
);
}
function safeTransferETH(address to, uint256 value) internal {
(bool success, ) = to.call{value: value}(new bytes(0));
require(success, 'TransferHelper::safeTransferETH: ETH transfer failed');
}
}
function safeApprove(
address token,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('approve(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(
success && (data.length == 0 abi.decode(data, (bool))),
'TransferHelper::safeApprove: approve failed'
);
}
function safeTransfer(
address token,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('transfer(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(
success && (data.length == 0 abi.decode(data, (bool))),
'TransferHelper::safeTransfer: transfer failed'
);
}
function safeTransferFrom(
address token,
address from,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::transferFrom: transferFrom failed'
);
}
function safeTransferETH(address to, uint256 value) internal {
(bool success, ) = to.call{value: value}(new bytes(0));
require(success, 'TransferHelper::safeTransferETH: ETH transfer failed');
}
}
this part:
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::transferFrom: transferFrom failed'
);
}
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::transferFrom: transferFrom failed'
);
}
Mar 7, 2022, 8:12 AM
Did you grant your contract approval to spend your tokens?
Mar 7, 2022, 8:14 AM
nope. can you help how to do it?
its not just normall approve, right?
its not just normall approve, right?
i have approved router contract already
tokens in owner wallet
hmm
Mar 7, 2022, 8:16 AM
function addMyLiquidity(uint256 _tokensAmoun)public payable ownerOnly returns(bool){
_pancakeRouter.addLiquidityETH {value: msg.value}(
address(this),
_tokensAmoun,
0,
0,
msg.sender,
block.timestamp
);
return true;
}
This function will be called by your address (external account). But when addLiquidityETH is actually called, it's being called by the contract you're calling. And Im prtty sure it uses msg.sender when determining the owner of the tokens to transfer. And in this case, msg.sender will be your contract that's calling addLiquidityETH.
_pancakeRouter.addLiquidityETH {value: msg.value}(
address(this),
_tokensAmoun,
0,
0,
msg.sender,
block.timestamp
);
return true;
}
This function will be called by your address (external account). But when addLiquidityETH is actually called, it's being called by the contract you're calling. And Im prtty sure it uses msg.sender when determining the owner of the tokens to transfer. And in this case, msg.sender will be your contract that's calling addLiquidityETH.
Mar 7, 2022, 8:18 AM
Thanx, yeah, i understand what you mean. Will change it now to contract creator from msg.sender
still the same, i tried to change msg.sender to creator (which is the account deployed this contract)
or how to make it to call this function from my address (external account), not from contract?
Mar 7, 2022, 8:39 AM
I ended up just deploying on mainnet (with mainnet pcs router) and worked fine
Mar 7, 2022, 11:07 AM
nice. maybe i will try it late if will not figured out here
Thanks man! Im really appreciate for your advice!
So, in conclusion what i did.
first im add function for approve spend from contract:
//for granted contract to approve to spend
function approveContract(address _spender, uint256 _value)public returns(bool){
_approve(address(this), _spender, _value);
return true;
}
and then im deployed my contract, approved router for externall account and approved router for my contract, and then i send my tokens for add liquidity to my contract, and BINGO!! its works now and i already add liquidity on this PCS BSC testnet.
Really happy for that.
I'm being try it more then one month and almost give up.
So, in conclusion what i did.
first im add function for approve spend from contract:
//for granted contract to approve to spend
function approveContract(address _spender, uint256 _value)public returns(bool){
_approve(address(this), _spender, _value);
return true;
}
and then im deployed my contract, approved router for externall account and approved router for my contract, and then i send my tokens for add liquidity to my contract, and BINGO!! its works now and i already add liquidity on this PCS BSC testnet.
Really happy for that.
I'm being try it more then one month and almost give up.
Mar 7, 2022, 12:21 PM