when trying to withdraw BUSD from a smart contract am gettting Gas Estimate Error - JSON-RPC : 32000 - deployment

i would like to send BUSD to a smart contract and then only owner can withdraw these BUSD from this smart contract. can someone help me to program the same?
i tried below but doesn't think it will work.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../IERC20.sol";
contract TestBUSD {
IERC20 tokenContract;
address private owner;
uint bal;
constructor() payable {
tokenContract = IERC20(0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56);
owner = msg.sender;
}
receive() external payable {}
modifier OnlyOwner() {
require(msg.sender == owner);
_;
}
function Approvetokens(uint256 _tokenamount) public returns(bool){
tokenContract.approve(address(this), _tokenamount);
return true;
}
function GetUserTokenBalance() public view returns(uint256){
return tokenContract.balanceOf(msg.sender);
}
function deposit(uint256 _tokenamount) payable public {
tokenContract.transferFrom(msg.sender, address(this), _tokenamount);
bal += msg.value;
}
function getOwner() public view returns (address) {
return owner;
}
function withdrawToken(address _tokenContract, uint256 _amount) payable external {
require(msg.sender == owner, "Only owner can withdraw!");
IERC20(_tokenContract);
tokenContract.transfer(msg.sender, _amount);
}
}

The tokenContract address does not hold any smart contract on the BSC testnet (there is the BUSD token on the same address on the mainnet).
Your withdrawToken() function sends an internal transaction to the tokenContract address, trying to execute its function transfer(), and expects to be returned a bool (as declared in the IERC20 interface) from this internal transaction. However, since there's no contract on this address, no response is generated - the internal transaction fails, which makes the main transaction fail as well.
If you want to test your contract specifically on the BSC testnet, you need to use a token address that has an ERC20 token deployed. As far as I'm aware, there's currently no official BUSD release on the testnet, so you might have to use another token or your own dummy token instead.
Or if you want to just test the contract interacting with BSUD on a non-mainnet network, I'd recommend you to fork the mainnet in your local emulator.

Related

ParserError: Source "project:/src/contracts/Interfaces/Libraries/IERC165.sol" not found

I have an error. I am trying to import my 'IERC165.sol' file to my 'ERC165.sol' and to my 'ERC721'. And I am receiving this error a long with two more error's that are the same just with different files.
I also made sure that the file was in the folder where it should be. And now I am just confused.
here is the code for the 'IERC165'
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IERC165 {
// #notice Query if a contract implements an interface
// #param interfaceID The interface identifier, as specified in ERC-165
// #dev Interface identification is specified in ERC-165. This function
// uses less than 30,000 gas.
// #retrun 'true' if the contract implements 'interfaceID' and
// 'interfaceId' is not 0xffffffff, 'false' otherwise
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}
here is the code for ERC165
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import './IERC165.sol';
contract ERC165 is IERC165 {
mapping (bytes4 => bool) private _supportedInterfaces;
constructor() {
_registerInterface(bytes4(keccak256('supportsInterface(bytes4)')));
}
function supportsInterface(bytes4 interfaceID) external view returns (bool) {
return _supportedInterfaces[interfaceID];
}
function _registerInterface(bytes4 interfaceId) internal{
require(interfaceId != 0xfffffff, 'Invalid interface request');
_supportedInterfaces[interfaceId] = true;
}
}
Here is the code for ERC721
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import '/.ERC165.sol';
import '/.IERC721.sol';
/*
Building out the minting function
a. nft to point an address
b. keep track of the token ids
c. keep track of token owner addresses to token ids
d. keep track of how many tokens an owner address has
e. create an event that emits a transfer log-
contact address, where it is being minted to, the id
*/
contract ERC721 is ERC165, IERC721 {
// mapping solidity creates a hash table of key
// pair values
// Mapping from token id to the owner
mapping(uint => address) private _tokenOwner;
// Mapping from owner to number of owned tokens
mapping(address => uint) private _OwnedTokensCount;
// Mapping from token id to approved addresses
mapping(uint256 => address) private _tokenApprovals;
/// #notice Count all NFTs assigned to an owner
/// #dev NFTs assigned to zero address are considered invalid
/// function throws for queries about the zero address.
/// #param _owner An address for whom to query the balance
/// #return The number of NFTs owned by '_owner', possibly zero
function balanceOf(address _owner) public override view returns(uint256) {
require(_owner != address(0), 'owner query for nonexistent token');
return _OwnedTokensCount[_owner];
}
/// #notice Find the owner of an NFT
/// #dev NFTs assigned to zero address are considered invalid, and queries
/// about them do throw.
/// #param _tokenId The identifier for an NFT
/// #return The address of the owner of the NFT
function ownerOf(uint256 _tokenId) public view override returns (address) {
address owner = _tokenOwner[_tokenId];
require(owner != address(0), 'owner query for nonexistent token');
return owner;
}
function _exists(uint256 tokenId) internal view returns (bool){
// setting the address of nft owner to check the mapping
// of the addres from the token owner at the tokenId
address owner = _tokenOwner[tokenId];
// return truthiness that address in not zero
return owner != address (0);
}
function _mint(address to, uint256 tokenId) internal virtual {
// requires the address isn't zero
require(to != address(0), 'ERC721: minting to the zero address');
// requires that he token does not already exist
require(!_exists(tokenId), 'ERC721: token already minted');
// we are adding a new address with a token id for minting
_tokenOwner[tokenId] = to;
// keeping track of each address that is minting and adding one to the count
_OwnedTokensCount[to] += 1;
emit Transfer(address (0), to, tokenId);
}
// #notice Transfer owner ship of an NFT -- THE REAL CALLER IS RESPOSIBLE
// TO CONFIRM THAT '_to' IS CAPABLE OF RECEIVING NFTS OR ELSE
// THEY MAY BE PERMANENTLY LOST
// #dev Throws untess 'msg.sender' is the current owner, an authorized
// operator, or the approved address for this NFT. Throws if '_from' is
// not current owner, Throws if '_to' is the zero address. Throws if
// '_tokenId' is not a valid NFT.
// #param _from The current owner of the NFT
// #param _to the new owner
// #param _tokenId The NFT to transfer
function _transferFrom(address _from, address _to, uint256 _tokenId) internal {
require(_to != address(0), 'Error - ERC721 Tr.ansfer to the zero address');
require(ownerOf(_tokenId) == _from, 'Trying to transfer a token address does not own');
_OwnedTokensCount[_from] -= 1;
_OwnedTokensCount[_to] += 1;
_tokenOwner[_tokenId] = _to;
emit Transfer(_from, _to, _tokenId);
}
function transferFrom(address _from, address _to, uint256 _tokenId) override public {
_transferFrom(_from, _to, _tokenId);
}
}
Typo error in the import of interfaces..
import './ERC165.sol';
import './IERC721.sol';
instead of
import '/.ERC165.sol';
import '/.IERC721.sol';

Deployment with Hardhat doesn't work correctly

I'm currently trying to deploy a solidity 0.7.6 contract with Hardhat 2.6.2. No errors are shown when compiling and deploying the contract to the Rinkeby testnet, and a contract address is returned. When trying to transact with this contract, it gets stuck, and checking Etherscan there is no contract creation transaction visible. When loading this contract address in Remix, I can transact with the contract, but when calling for a value, no value is returned.
Contract:
pragma solidity 0.7.6;
contract Greeter {
string public greeting;
constructor(string memory test) {
greeting = test;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}

What is the difference between creating a new solidity contract with and without the `new` keyword?

What is the use of the new keyword for creating new smart contracts? Why not just omit this keyword?
There are two ways you can create contracts
Using 'new' keyword
Using address of the contracts
Using new keyword you instantiate the new instance of the contract and use that newly created contract instance
While in latter option you use the address of the already deployed and instantiated contract. You can check below code for reference:
pragma solidity ^0.5.0;
contract Communication {
string public user_message;
function getMessage() public view returns (string memory) {
return user_message;
}
function setMessage(string memory _message) public {
user_message = _message;
}
}
contract GreetingsUsingNew {
function sayHelloUsingNew() public returns (string memory) {
Communication newObj = new Communication();
newObj.setMessage("Contract created using New!!!");
return newObj.getMessage();
}
}
contract GreetingsUsingAddress {
function sayHelloUsingAddress(address _addr) public returns (string memory) {
Communication addObj = Communication(_addr);
addObj.setMessage("Contract created using an Address!!!");
return addObj.getMessage();
}
}
You cannot omit the new keyword for creating new contracts.
In the case of: token = new Token;
A new contract is created and the address is passed to token.
In the case of: token = existingToken;
existingToken has to be an existing contract (already created) and token will be passed the current address of existingToken.
Creating a new contract with the new keyword deploys and creates a new contract instance. Deploying a contract involves checking whether the sender has provided enough gas to
complete the deployment. So you will be charged. Also, with this method, you can also send Ethereum in Wei during the creation of the contract.
NewContract myNewContract = (new NewContract){value: 1000000000000000000}()
in the second method, you are not deploying a new contract instead, instead, you are referring to an already deployed contract. With this method, you should already know the address beforehand

What do you think about that diagram?

Could you please tell me if my diagram is correct?
I especially care about relations between PaymentService and Customer,
Payment, Customer
I guess that:
class Customer {
private List<Payment> payments;
//..
public boolean pay() {
return PaymentService.pay(this.payments);
// calling the static method of the class PaymentService
}
}
interface Payment {
// knows nothing about Customer
}
class PaymentService {
public static boolean pay (List<ayment> payments) {
// the magic here is return result
}
}
UPD: Now, I noticed that why I use static member, but it does not touch my question.
What is the common way to construct payment systems (It looks like a general task)?
I guess that the FFCustomer should have only one account.
And an account exists only while an FFCustomer exists.
UPD:
It's pretty close. Make the association to Payment unidirectional. Make the account property an association end. In general, attributes should only be typed by datatypes, not classes. You are also missing an operation in the Payment interface.

How to send email to registered user in Java EE

I want to send an email activation link to a registered user. I already set up my Sendmail.class which work` perfectly.
Here is the scenario:
the user request for registration by providing information via a restful client
the restful endpoint gets the request to do some business operation and sends a computed code to the email of the registered user and returns a response saying 'successfully registered'
The problem is that I don't want to wait for the Sendmail.class to finish the sending process (it may fail) to return the 'successfully registered message
How can I handle this process using Java EE?
Put the code that sends the email in an #Asynchronous method.
Example:
#Stateless
public class EmailSender {
#Asynchronous
public void sendMail(...) {
// send mail here
}
}
From the place where you do your business logic:
#Inject
private EmailSender emailSender;
public Foo myBusiness() {
// Compute stuff
emailSender.sendMail(stuff); // returns immediately
// do other stuff if needed
}
See the Oracle tutorial for some extra info.
Put your e-mail sending code in a thread. Even you can easily use SwingWorker:
SwingWorker worker = new SwingWorker() {
#Override
protected void done() {
}
#Override
protected Object doInBackground() throws Exception {
// Send your e-mail here.
}
};
worker.execute();