function buyTokens() public payable {
//CHECK FOR INPUT BALLANCE
require(msg.value > 0, "RGP: INSUFFICIENT INPUT BALLANCE, YOU NEED TO SEND SOME BUSDT");

//CALCULATE THE AMOUNT OF TOKEN TO PURCHASE
uint256 tokensToBuy = mul(msg.value, uint256(10) IERC20(token).decimals()) / mul(price, uint256(10) V2(rgp).decimals());

//ALLOWANCE FOR BOTH ADDRESS(THIS) AND MSG.SENDER
V2(rgp).allowance(msg.sender, address(this));
IERC20(token).allowance(address(this), msg.sender);

//REQUIREMENT FOR ALLOWANCE
tokensToBuy <= V2(rgp).allowance(address(this), msg.sender); // "RGP: CHECK THE TOKEN ALLOWANCE OF ADMIN";
msg.value <= IERC20(token).allowance(msg.sender, address(this)); // "RGP: CHECK THE TOKEN BALANCE OF BUYER";

//APPROVE FOR BOTH MSG.SENDER AND ADDRESS(THIS)
V2(rgp).approve(msg.sender, tokensToBuy);
IERC20(token).approve(address(this), msg.value);

//TRANSFER FROM MSG.SENDER TO ADDRESS(THIS)
IERC20(token).transferFrom(msg.sender, address(this), msg.value);

//CHECK FOR BALANCE OF ADMIN
uint256 rgpBalance = V2(rgp).balanceOf(address(this));

//REQUIRE THAT THE BALANCE APPROVED IS GREATER THAN THE TOKENS TO BUY.
require(rgpBalance >= tokensToBuy, "RGP: NOT ENOUGH TOKEN IN THE RESERVE");

//TRANSFER FROM RGP TO MSG.SENDER
V2(rgp).transferFrom(address(this), msg.sender, tokensToBuy);

//REDUCE THE ALLOWANCE OF BOTH PARTIES
IERC20(token).allowance(msg.sender, address(this)) + msg.value;
V2(rgp).allowance(address(this), msg.sender) + tokensToBuy;

//INCREASE THE ALLOWANCE OF BOTH PARTIES.
IERC20(token).allowance(msg.sender, address(this)) + msg.value;
V2(rgp).allowance(address(this), msg.sender) + tokensToBuy;

//
tokensSold += msg.value;
emit sold(msg.sender, msg.value * price, address(token));

}

function endSale() public onlyOwner{
// Send unsold tokens to the owner.
require(V2(rgp).transfer(owner, address(this).balance), "RGP: Sales ended.");
require(IERC20(token).transfer(owner, address(this).balance), "RGP: Sales ended.");

msg.sender.transfer(address(this).balance);
}
}

please what wrong with it?

Jan 8, 2021, 8:55 PM
you gotta be more specific
what's the output of the compiler?
Jan 8, 2021, 9:04 PM
really appreciate the prompt response.
Jan 8, 2021, 9:05 PM
do we not have combot in here?
Jan 8, 2021, 9:19 PM
all i want to achieve with this smartcontract is that.
i want Bob to bring token A and deposit to the smartcontract address then in return earned token B from the smartcontract address.
first error: The called function should be payable if you send value and the value you send should be less than your current balance.
when i test on remix
Jan 8, 2021, 9:21 PM
what happens when you test? Do you have an error?
You can copy and paste or use a screenshot.
Jan 8, 2021, 9:24 PM
transact to tokenSales.buyTokens errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
also i have my buy token function, that am not certain of it fuctionability.
function buyTokens() public payable {
//CHECK FOR INPUT BALLANCE
require(msg.value > 0, "RGP: INSUFFICIENT INPUT BALLANCE, YOU NEED TO SEND SOME BUSDT");
//CALCULATE THE AMOUNT OF TOKEN TO PURCHASE
uint256 tokensToBuy = mul(msg.value, uint256(10) IERC20(token).decimals()) / mul(price, uint256(10) V2(rgp).decimals());
//ALLOWANCE FOR BOTH ADDRESS(THIS) AND MSG.SENDER
V2(rgp).allowance(msg.sender, address(this));
IERC20(token).allowance(address(this), msg.sender);
//REQUIREMENT FOR ALLOWANCE
tokensToBuy <= V2(rgp).allowance(address(this), msg.sender); // "RGP: CHECK THE TOKEN ALLOWANCE OF ADMIN";
msg.value <= IERC20(token).allowance(msg.sender, address(this)); // "RGP: CHECK THE TOKEN BALANCE OF BUYER";
//APPROVE FOR BOTH MSG.SENDER AND ADDRESS(THIS)
V2(rgp).approve(msg.sender, tokensToBuy);
IERC20(token).approve(address(this), msg.value);
//TRANSFER FROM MSG.SENDER TO ADDRESS(THIS)
IERC20(token).transferFrom(msg.sender, address(this), msg.value);
//CHECK FOR BALANCE OF ADMIN
uint256 rgpBalance = V2(rgp).balanceOf(address(this));
//REQUIRE THAT THE BALANCE APPROVED IS GREATER THAN THE TOKENS TO BUY.
require(rgpBalance >= tokensToBuy, "RGP: NOT ENOUGH TOKEN IN THE RESERVE");
//TRANSFER FROM RGP TO MSG.SENDER
V2(rgp).transferFrom(address(this), msg.sender, tokensToBuy);
//REDUCE THE ALLOWANCE OF BOTH PARTIES
IERC20(token).allowance(msg.sender, address(this)) + msg.value;
V2(rgp).allowance(address(this), msg.sender) + tokensToBuy;
IERC20(token).allowance(msg.sender, address(this)) + msg.value;
V2(rgp).allowance(address(this), msg.sender) + tokensToBuy;
tokensSold += msg.value;
emit sold(msg.sender, msg.value * price, address(token));
}
Jan 8, 2021, 9:34 PM
well.... if you;re not certain
test it
hardhat!
Jan 8, 2021, 9:35 PM
the scope of the smartcontract is for Bob to bring token A and the contract address releases token B to Bob
Jan 8, 2021, 9:36 PM
I am so sorry that I cannot provide additional help right now
Jan 8, 2021, 9:38 PM
Thanks for the little you have provided it count.
Jan 8, 2021, 9:49 PM
if you can paste the error,
Please consider also running the code in a test container
such as hardhat
Jan 8, 2021, 9:50 PM
alright.
Jan 8, 2021, 9:50 PM
So what you are trying to do is..
Bob sends token A to contract
Function buyTokens() gets called
Contract sends token B back to Bob
Is that correct?
Jan 8, 2021, 9:52 PM
Exactly.
Jan 8, 2021, 9:53 PM
1) Contract cannot detect token transactions directly
2) msg.value returns ether value, not token value
Jan 8, 2021, 9:54 PM
great point
for first correction, how can i solve this?
Jan 8, 2021, 9:57 PM
Either use the approve-transferfrom combo or use tokens which support token fallback
Jan 8, 2021, 9:58 PM
alright, now for more clearifications, my intention is to interact with BUSD and USDT, which will you suggest?
Jan 8, 2021, 10:00 PM
Already deployed tokens, so approve-transferFrom
Jan 8, 2021, 10:10 PM
cant thank you enough.
here is my approve and transferFrom.
is this the best approach?

//APPROVE FOR BOTH MSG.SENDER AND ADDRESS(THIS)
V2(rgp).approve(msg.sender, tokensToBuy);
IERC20(token).approve(address(this), _tokenAmount);

//TRANSFER FROM MSG.SENDER TO ADDRESS(THIS)
IERC20(token).transferFrom(msg.sender, address(this), _tokenAmount);

//CHECK FOR BALANCE OF ADMIN
uint256 rgpBalance = V2(rgp).balanceOf(address(this));

//REQUIRE THAT THE BALANCE APPROVED IS GREATER THAN THE TOKENS TO BUY.
require(rgpBalance >= tokensToBuy, "RGP: NOT ENOUGH TOKEN IN THE RESERVE");

//TRANSFER FROM RGP TO MSG.SENDER
V2(rgp).transfer(msg.sender, tokensToBuy);
Jan 8, 2021, 10:13 PM
Seems alright.
Jan 8, 2021, 10:16 PM
alright will make little changes then see the outcome.
thanks
Jan 8, 2021, 10:18 PM
This>
IERC20(token).approve(address(this), _tokenAmount);

Won't work. Keep in mind that every internal transaction to an other contract than the one the user interacted with is called "in the name" of the contract the user interacts with. In the above case the contract will basically approve itself. You have to make the approve outside the contract via a call through the UI.
Jan 9, 2021, 3:39 AM
yea, thanks so much.
i have effected the change. but still got an error from the V2 token
in the transferFrom //ERC20: transfer amount exceeds allowance.
Jan 9, 2021, 3:43 AM
This>
V2(rgp).approve(msg.sender, tokensToBuy);

seems to be very dangerous. The approve will allow the msg.sender to transfer rpg tokens(?), however, in the last step via
V2(rgp).transfer(msg.sender, tokensToBuy);
Transfer tokens. Basically the user can now transfer tokens again from the contract balance. No need to approve the msg.sender here since you transfer from the contract balance (which does not require an allowance)
Use https://ethfiddle.com/ to post code please, is easier for the eyes
(when finished posting, click on "Share" at the top, not "Compile")
Jan 9, 2021, 3:46 AM
alright on it
Jan 9, 2021, 3:49 AM
Is the V2 an interface too?
Jan 9, 2021, 3:49 AM
yes
Jan 9, 2021, 3:50 AM
What's the difference between V2 and IERC20?
Jan 9, 2021, 3:50 AM
Bob brings IERC0 and in return get V2
encountering restriction in sending so i DM the link.
onlyOwner can also be used when setting up a require function that only the owner of the contract can have access to
Jan 9, 2021, 5:29 AM
That means wallet owner whose address is used to create contract ?
Jan 9, 2021, 5:30 AM
yes
Jan 9, 2021, 5:31 AM
Can anyone withdraw tokens except owner ?
Jan 9, 2021, 5:32 AM
if the modifier is set to onlyOwner or add this to the function
require(owner == msg.sender);
address owner
and passing the owner address into the constructor.
Jan 9, 2021, 5:34 AM
Thanks for making me understand
i will come after reading more 🙏have a nice day 😊
Jan 9, 2021, 5:36 AM

© 2024 Draquery.com All rights reserved.