Deployment with Hardhat doesn't work correctly - deployment

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;
}
}

Related

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

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.

How Can I have multiples instances of a Spring boot Repository(Interface), to have a complete test-state-isolation?

1) Contextualization:
In order, to have a complete test-isolation-state in all test of my Test-Class;
I would like to have a new-instance-repository(DAO) for each individual test;
My Repository is a Interface, thats the why I can not simply instantiate that.
My Goal is:
Run all tests 'Parallelly', meaning 'at the same time';
That's the why, I need individual/multiple instances of Repository(DAO) in each test;
Those multiple instances will make sure that the tests' conclusion would not interfere on those that still is running.
Below is the code for the above situation:
1.1) Code:
Current working status: working, BUT with ths SAME-REPOSITORY-INSTANCE;
Current behaviour:
The tests are not stable;
SOMETIMES, they interfere in each other;
meaning, the test that finalize early, destroy the Repository Bean that still is being used, for the test that is still running.
public class ServiceTests2 extends ConfigTests {
private List<Customer> customerList;
private Flux<Customer> customerFlux;
#Lazy
#Autowired
private ICustomerRepo repo;
private ICustomerService service;
#BeforeEach
public void setUp() {
service = new CustomerService(repo);
Customer customer1 = customerWithName().create();
Customer customer2 = customerWithName().create();
customerList = Arrays.asList(customer1,customer2);
customerFlux = service.saveAll(customerList);
}
#Test
#DisplayName("Save")
public void save() {
StepVerifier.create(customerFlux)
.expectNextSequence(customerList)
.verifyComplete();
}
#Test
#DisplayName("Find: Objects")
public void find_object() {
StepVerifier
.create(customerFlux)
.expectNext(customerList.get(0))
.expectNext(customerList.get(1))
.verifyComplete();
}
}
2) The ERROR happening:
This ERROR happens in the failed-Tests:
3) Question:
How Can I create multiple instances of Repository
Even if, it being a Interface(does not allow instantation)?
In order, to have a COMPLETE TEST-ISOLATION
Meaning: ONE different instance of Repository in each test?
Thanks a lot for any help or idea
You can use the #DirtiesContext annotation on the test class that modifies the application context.
Java Doc
Spring documentation
By default, this will mark the application context as dirty after the entire test class is run. If you would like to mark the context as dirty after a single test method, then you can either annotate the test method instead or set the classMode property to AFTER_EACH_TEST_METHOD at your class level annotation.
#DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
When an application context is marked dirty, it is removed from the
testing framework's cache and closed; thus the underlying Spring
container is rebuilt for any subsequent test that requires a context
with the same set of resource locations.

Using NUnit, (how) can I test code that sets Thread.CurrentThread.Name?

Consider this Test
[TestFixture]
class Sample
{
[Test]
public void Test()
{
Thread.CurrentThread.Name = "Foo";
}
}
If I debug this test, it passes without error.
If I run this test, it fails with the following exception
System.InvalidOperationException : This property has already been set and cannot be modified.
In run mode, the test's thread's name is "NonParallelWorker".
In debug mode, the test's thread's name is null
As a constraint, assume the code-under-test is not allowed to change, and attempts to set the thread's name, without checking for null first.
E.g.
public void SampleMethodUnderTest()
{
// It is important that this method gets to set this field.
Thread.CurrentThread.Name = "Important Value";
}
My search through the documentation and other's posts has come up dry...
Question
Is there any way to disable/modify NUnit's thread-naming behavior?
Try adding the RequiresThreadAttribute.
[TestFixture]
class Sample
{
[Test, RequiresThread]
public void Test()
{
Thread.CurrentThread.Name = "Foo";
}
}
I think this will work currently, although the fact that this creates an unnamed thread may be an implementation detail, and not something that will necessarily work reliably going forward, I'm not sure. The alternative of course is to create your own user-controlled thread in the test, and pass any exceptions back to NUnit.

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

Testing repository with DbContext - mocking issue concerning IDbSet

I am trying to test a repository that uses DbContext
The issue I am running into is that DbCOntext wants to return DbSet for certain types
I can't even mock IDbSet as it has a PRIVATE CTOR?!?!?!
How is everyone getting over this?
You need to make use of an adapter or wrapper. The DbContext is third party code. Your code should hide this behind an abstraction, remember this is just an implementation detail about how the internals of your system works. You should be free to change this at any time.
public class ThatsHardToTest
{
// Private constructors, slow start up time etc...
public int AlwaysReturnOneExceptInSuperRareScnearios()
{
// Complex logic.
return 1;
}
}
Now if we want to test our code when the above method goes wrong, e.g database system is offline. I don't want the test to hit the database, so we need an adapter around this third party code. I would either make an interface or base class.
public class MyTestAdapter : IExampleAdapter
{
public int ReturnWhateverIWant()
{
return -1;
}
}
I would use MyTestAdapter for unit testing my code, as I can control what it does. For the production code you simply substitute this with an adapter which delegates to the real production system. For example:
public class MyRealAdapter : IExampleAdapter
{
public int ReturnWhateverIWant()
{
return new ThatsHardToTest().AlwaysReturnOneExceptInSuperRareScnearios();
}
}