Interact with a live deployed smart contract using Remix IDE

Takshil Patil
Coinmonks
3 min readJul 26, 2022

--

Summary: This article is related to interacting with a smart contract this deployed on either testnet or mainnet of a blockchain. We can always write test cases or scripts to interact with the contract, but remix provides a quick method to query and interact with a live smart contract easily.

In this example, I have already deployed a smart contract on polygon (mumbai) testnet.

Requirements

  1. Deployed Smart Contract Source Code: Please find the code below.
  2. Address of the deployed contract: 0x68B1D87F95878fE05B998F19b66F4baba5De1aed
  3. Metamask; configured with the blockchain, along with owner of token contract wallet connected to it, and having some token in wallet.
// NFTToken.sol
//SPDX-License-Identifier: Unlicense
pragma solidity 0.8.4;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract NFTToken is ERC721URIStorage {
/// @dev auto-increment field for each token
/// @param
using Counters for Counters.Counter;
Counters.Counter private _tokenIds; //this variable can be viewed by public, but cannot be called.

//owner of NFT Token and Marketplace are same.
address nftMarketAddress; //address of the NFT marketplace

constructor(address marketplaceAddress) ERC721("Nifty Tokens", "IFTY") {
nftMarketAddress = marketplaceAddress;
}

/// @notice create a new token and assign it to the caller of the function.
/// @param tokenURI: token URI
function createToken(string memory tokenURI) public returns (uint256) {
//sets a new token id for the token to be minted. Like: 0, 1, 2, 3, 4, ...
_tokenIds.increment();

//this stores the newly assinged tokenID
uint256 newItemId = _tokenIds.current();

//mint a new token
_mint(msg.sender, newItemId);

//given the token is valid, tokenURI is created using baseURI and tokenId
_setTokenURI(newItemId, tokenURI);

//give marketplace authority to make transaction on behalf of the user.
//If this is not done, any the marketplace cannot buy and sell
// this token in the marketplace, here both marketplace and token belong
//to the same owner so it was easy to supply marketplace address
//and give the required approval.
setApprovalForAll(nftMarketAddress, true);

//returns the new token id as uint
return newItemId;
}
}

Step 1: Compile the contract in Remix IDE

  • Visit https://remix.ethereum.org in browser, to open the remix IDE.
  • Copy the source code of the smart contract that you have already deployed and compile it again. After a successful compile you should see something like this,
successful compile

Step 2: Connect your Remix IDE to metamask

Set the environment in deploy configurations.

Set the envrionment as Metamask

Select yes / connect for the pop up permissions so that you can link remix IDE to metamask wallet, make sure you select the correct account while connecting.

Step 3: Connect your Remix IDE to Metamask

We don't want to deploy this contract, as it is already deployed. We need to simply connect the already deployed contract.

Paste the address of the smart contract in “At Address” section.

Finally, click the “At Address” blue button.

click the blue “At Address” button after pasting the address.

From the results I came to know that there are no tokens created in the smart contract. This is how you can interact with a live smart contract.

As this function was a simple read function, so there will be no gas fees incurred.

New to trading? Try crypto trading bots or copy trading

--

--