Testing Token with Uniswap liquidity provisioning using hardhat - testing

I'm attempting to fork Safemoon (or really NotSafeMoon), and use it as a vehicle to learn smart contract development. (I've got a substantial amount of what you might call "Web 2.0" dev experience).
So say I have something like so in my constructor:
constructor () {
_rOwned[_msgSender()] = _rTotal;
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E); // binance PANCAKE V2
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
When I run my tests with npx hardhat test I get the following failure:
Compilation finished successfully
TestToken contract
Deployment
1) "before each" hook for "Has the right name"
0 passing (807ms)
1 failing
1) TestToken contract
"before each" hook for "Has the right name":
Error: Transaction reverted: function call to a non-contract account
Now, this does make perfect sense, after all I am attempting to call the Pancakeswap v2 router contract. How do I get around this limitation? Is there a way to inject the contract address for the router as an environment variable perhaps? Is there a mock constructor for the UniswapRouter I can be using? Generally, how is this sort of thing done in a way that remains testable (and how is it therefore tested) with smart contract development?

A hardhat test deploys the contracts to the hardhat local network (by default). This local network only has few pre-funded and unlocked accounts, but there are no smart contracts deployed. Including the PancakeSwap v2 Router (0x10ED43...).
Instead of deploying and configuring local copy of the router contract, as well as all of its dependencies, you can create a new hardhat network forked from the production BSC.
https://hardhat.org/guides/mainnet-forking.html
This will run a local network with the Router contract available, but your actions will only effect the local network (not the mainnet).

Related

What is different between Aptos devnet and testnet?

I'm starting to build a dapp on Aptos and I notice that there are two development networks, devnet and testnet. What are the differences between these two?
Update - 2022-01-26: Previously the testnet faucet required the user to complete a captcha. This is no longer true, the faucets of both networks work similarly, so that section has been removed from the answer.
Release cadence
Devnet is generally released every week. Testnet is generally released every two weeks, after devnet.
This means devnet gets new features sooner and more frequently.
Persistence
With devnet, the chain is reset every release. All data is wiped, including any deployed modules, accounts, etc. and the chain restarts from genesis with a new chain ID. If you're building on devnet this means you must redeploy your Move modules and accounts every week.
Testnet is never wiped, similar to mainnet.
Faucet access
On both devnet and testnet you can create new accounts and get new APT easily by either:
Using the "Faucet" button in your wallet (e.g. in Petra).
Using the FaucetClient in the SDK.
Using the aptos CLI:
aptos account fund-with-faucet --account 0xd0f523c9e73e6f3d68c16ae883a9febc616e484c4998a72d8899a1009e5a89d6
Hitting the faucet directly:
curl -X POST 'https://faucet.devnet.aptoslabs.com/mint?amount=100&address=0xd0f523c9e73e6f3d68c16ae883a9febc616e484c4998a72d8899a1009e5a89d6'
Which should you use?
Generally speaking testnet is a friendlier developer experience because you don't need to keep redeploying your code / recreating accounts. For standard development the amount of APT the testnet faucets give you should be more than sufficient.
Devnet is good for rapid experimentation where you don't care about data persistence or if you're running tests that require programmatic access to APT.

cannot consume a scoped service from singleton

I am getting the above error and I realize why, but the odd behavior I see that it only happens in our Dev environment and not in for example our staging or Production environment and it is the exact same code.
In the startup there is :
services.AddSingleton<ExcahngeService>();
services.AddScoped<ITradingService, TradingService>();
This throws the error:
"cannot consume a scoped service from singleton"
but it only happens in a development environment and it is the same code in all environments
Could this be due to a timing issue, where the environment is slower ?
Scoped instance lifetime is limited, It is only available per request. So you are consuming an instance from Scoped service into Singleton and the error throws because of the scoped service is disposed. Either make both Singleton or Scoped
As Microsoft best practice you should create new scope and get scoped service from ServiceProvider
https://learn.microsoft.com/en-us/dotnet/core/extensions/scoped-service

Any way to deploy a solana program with an offline/cold authority?

The CLI command (solana program deploy) doesn't seem to permit offline signing.
Is there any other way to deploy a solana program without putting the contract authority on an internet-connected computer?
It's almost possible. You'll need to follow the instructions for deploying to a buffer before performing the upgrade in one transaction.
Essentially, you have a hot key write the program buffer with solana program write-buffer, and then the upgrade authority only authorizes the upgrade transaction with solana program deploy --program-id <> --buffer <>.
It wouldn't be a lot of work to add the sign_only flag to the deploy command to have the last bit done offline. Feel free to submit an issue for that! https://github.com/solana-labs/solana/issues
More information at: https://docs.solana.com/cli/deploy-a-program#using-an-intermediary-buffer-account

Link Javascript to Solidity?

Is it possible to link/connect/ send a signal from JavaScript code into Solidity/Ethereum?
You can use web3.
There is a different syntax for interfacing with contracts. Depending on if the contract is already deployed or you need to do that yourself.
When you have the contract instance you can then call contract methods.
const ChessGame = web3.eth.contract(abiArray);
const contractInstance = ChessGame.at(address);
contractInstance.declareWinner(winner, function (err, res) {
// do something
});
I personally think web3 is a little cumbersome. I recommend checking out Truffle and following some of their tutorials to get up to speed.
You could install the official Go implementation of the Ethereum protocol, geth.
Geth is a tool for running a local node/miner and also allows you to connect to a running blockchain via the console (which then becomes a Javascript console) and input RPC calls natively via the provided web3 package.
Geth also supports connecting to test nets (such as Ropsten or Rinkeby) or even a private blockchain on localhost.
Additionally to a user interacting directly with the console via the command line, geth can be configured from a shell script/batch file to both run or preload javascript files containing scripted commands for testing!
yes just create an ABI of your smart contract and then interact with your smart contracts however you see fit via web3.

Testing fasthttp with httptest

I am wondering how I can test an application that's written with fasthttp using the httptest package in the base library of Go.
I found this guide which explains the testing pretty well, but the issue is that httptest does not satisfy the http.Handler interface so I have no idea how to do the http.HandlerFunc since fasthttp uses it's own fasthttp.ListenAndServe that's incompatible.
Any ideas on how to create a wrapper, or how to otherwise test a fasthttp written library end to end?
There are two possible approaches. Unit testing a handler isn't really viable as you would need to create a RequestCtx and stub/mock all necessary fields.
Instead, I would unit test the code that your fasthttp handlers call out
to. I would do e2e testing of the actual handlers themselves.
There is an in memory listener implementation that you could use to avoid actually listening on a TCP port or Unix socket. You would initialise the server but serve on this listener instead of on a network connection.
You would then create a HTTP client and call the relevant methods as normal but use this listener as the transport.
If you stub/fake anything that your handlers interact with then you could make this in-memory only with no external dependencies, i.e. like a unit test but it will actually doing a full system test.