I have deployed a smart contract using the Remix editor on the Mumbai testnet. Do I still need hardhat in order to interact with the smart contract? - solidity

I have deployed a smart contract using the remix editor. I need to know that if I have to have the hardhat extension as well.
I want a user to be able to set up their profile using the UI with some info like availability, profile picture hourly rate etc. In remix I have been able to achieve this by adding a new instance after deploying the smart contract. I am not too sure how I(or someone else) would be able to interact with the smart contract using the UI. I am intending use Moralis and the Web3uikit I also want the pictures to be uploaded to IPFS aswell.

No, hardhat is used to compile, test and deploy smart contracts, on your own local hardhat blockchain instance, or a testnet or mainnet. If you have already deployed the contract to a blockchain then you don't need hardhat anymore.
If you verified the contract, you should be able to interact with it via https://mumbai.polygonscan.com/ > search your contract's address > contract tab.
Yet, from what I can grasp from your question, I think what you'll need to do is create a website that would be used as an interface for the contract.

You do not need hardhat.
If you want to interact with a smart contract from your UI, you will need to connect a wallet like metamask from the application's UI. Using JS libraries like etherJs or web3JS you can connect and interact with the smart contract.
For example using etherjs:
async function payUser (amount){
//connect to metamask
const { ethereum } = window;
//if ethereum is not found, it means that a user does not
//have metamask installed on their browser
if (!ethereum) {
return;
}
//Get wallet provider and signer
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
//contract initialization: create an instance of the //contract
const contractInstance = new ethers.Contract(contractAddress, abi, signer);
//Interact with the contract using appropriate methods
const transaction = await
contractInstance.pay(ethers.utils.parseEther(amount))
}

Related

Can I use smart contract to mint my NFT without confirmation?

I would like to let my users reserve my NFT with cash (through things like PayPal or Stripe). When the reservation happens, the NFT should be minted by my wallet and they can contact me once they have set up their own wallet, so I can transfer the NFT to their wallet.
But it seems each minting (which would create a transaction) will need the confirmation of the owner of the wallet. Is there a way to skip it, or do I have to do it manually?
If by "automatically" you mean by doing it without using a wallet like Metamask, then yes, you can automate the transaction, but you'll need to specify your private key and an RPC URL to a node when calling the contract's mint method.
For that, you'll need to securely manage that transaction in a different place besides your frontend, since if you type your private key on your frontend app ( website ), anyone can access it and access your wallet.
You'll need an external service ( node app for example running an ethers script ) on a server where you can securely manage that data, and execute the transaction, and have the frontend call this service.
This script can, for example, listen for HTTP requests from the frontend ( using expressjs ), once received the request, get a provider from the RPC URL, a signer using the private key ( using the method new ethers.Wallet( privateKey [ , provider ] ) and then, get a contract instance like usual. After that, just call the method, await for the transaction's promise, and then return a response to the frontend.
And from the frontend, you can simply send the request using fetch once the payment through stripe has been confirmed for example.

New contracts visibility

first of all, excuse me because I am starting to learn Solidity programming, and this question is surely trivial for most of you, but I haven't found any answer yet.
When I create a simple smart contract from within another one (using "new"), and I try to check the new contract visibility, I cannot find it on etherscan (Rinkeby), even though I can interact with it from within Remix IDE. Is there any reason for that?
Thank you very much in advance!!
First, how do you know which address the new contract has? You can try to log it via event emitting and Remix will show it on the console.
Secondly, on which network are you deploying your contract? By default Remix use an EVM VM that some mimic a fake network, it is not a public test net, just something that runs locally in your browser, meaning, you can not see in etherscan.
To achieve this, you have to choose "injected web3" in the environment dropdown during the deployment process.
There are a lot of gotchas but here is a good guide on how to connect your metamastk testnet.
when you create Contract that Creates other Contracts,It doesn't create or what you are trying to say that it doesn't deploying any
new contract on Rinkeby.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Account {
address public owner;
constructor(address _owner) {
owner = _owner;
}
}
contract AccountFactory{
function createAccount(address _owner) public {
Account account = new Account(_owner);
}
}
When you are deploying the written smart contract both Account and AccountFactory will be deployed you will be able to find it on (Rinkeby)
Therefore each time you hit the public createAccount function it's just making an transaction on with interact with the deployed Account smart contract
which will definitely not create Account smart contract again for each function call.
Have a look HERE
Thank you all!
For example, if this piece of code
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
is suposed to "deploy" a new instance of a contract (storing their addresses in an array), each time I call the function, so we can interact with to store or retrieve values, or whatever, my question came because I figured out that the address of the new "deployed" contract (contained in simpleStorageArray) should be found in Etherscan, but it actually does not.

Is smart contract receive, hold and send different kinds of ERC-20 tokens

I'm creating a smart contract using solidity and publishing it to the Mumbai Test Network.
Right now when I'm interacting with the contract on the client-side using ethers and metamask it's automatically using the MATIC token.
I want the smart contract to be able to receive different kinds of ERC-20 tokens and afterward be able to transfer them.
const parsedAmount = ethers.utils.parseUnits(amount.toString(), 'ether');
const contract = await tokenContract.createTransfer({ value: parsedAmount });
For example, I want to be able to send the test token shown in the picture.
Link to the faucet
I've searched online and read docs but couldn't find an answer for this problem...
If needed, I will add any info you think will clarify this question!
Thanks in advance :)
The ERC-20 standard doesn't define any way to let the receiving contract know about a transfer that is not initiated by the receiver. It "only" emits an event but that's not readable from onchain.
All ERC-20 balances are stored in the respective token contracts. For example, if an address holds 10 USDC, this information is stored on the USDC contract - no matter if the holder is an end user address or a contract.
Combined these two things together, you can send a transaction to the USDC (or any other ERC-20) token contract, invoking the transfer() function where the receiver is your contract. This will effectively transfer USDC from the user to your contract address.
const usdcContract = new ethers.Contract(usdcAddress, usdcAbi, metamaskSigner);
const to = "0x123..."; // your contract address
const amount = "1000000"; // 1 token with six decimals
await usdcContract.transfer(to, amount);

How to avoid hardcoding contract address in Solidity

When looking at other team's smart contracts, I often see code like this:
address constant public token = address(0xabc123...);
Where the hexadecimal number is the address of an earlier-deployed smart contract. Coming from JS and C++ background, I'm not a fan of this because it effectively hardcodes what should be in a configuration file directly into the smart contract code. Several questions come to mind when I see code like this:
What if I want to deploy this to another EVM-compatible network?
What if I want to deploy this to testnet?
I'm still relatively new to Solidity, so it's possible I'm missing some feature of truffle that allows me to insert these strings at the time of deployment, but I didn't see this mentioned in any of the tutorials I went through. I would much rather have something like a JSON configuration file for testnet/mainnet/L2-chain/etc instead of having N versions of the same file with minor differences. How should I handle these cases?
You can have a variable (instead of a constant) containing the token address. And this variable can be set from a constructor.
So you can effectively pass the value from an environment variable to the contract constructor, to the contract storage.
Example:
.env
TOKEN_ADDRESS=0x123
deploy.js using Truffle (docs) for example
MyContract.new(process.env.TOKEN_ADDRESS)
You can also use Hardhat (docs) or any other library allowing you to deploy to any network depending on the config.
MyContract.sol
pragma solidity ^0.8;
contract MyContract {
address token;
constructor(address _token) {
token = _token;
}
}

How to mint ERC20 token using web3.js library?

I have been working on ERC20 token development. My code is written using solidity and zeppelin frameworks.
So far I have used the test networks like Rinkeby, Ropsten to deploy and test all the ERC20 methods. Last night, I have to deploy the smart contract in the mainnet where 10000000 tokens had to be deployed but I deployed only 1000000 (missed a zero).
As it is deployed in the mainnet, mint is the only way to top up the initial amount instead of redeploying. Mint is achievable using remix by removing the internal keyword in the mint method. But it requires to redeploy the smart contract in order to use mint method, which customer would not agree to redo the same.
The only way I think is to use web3js API to achieve the same. But there are no content given in the web3js document to how to pragmatically mint (to top up the initial amount).
If any of you have faced a similar situation, please let me know how you tackled it.
Thanks,
Sriram
The mint using web3 would be just a call to the contract function just like from remix, nothing special. All you would have to do would be to call the function. But since this function internal you cannot call it directly not from remix, not from web3, not from any other library. To put it simply if you cannot call the function from remix you cannot do it from web3 either. Web3 offers nothing more than remix in terms access rights to the contract.