I already linked both web3 and the metamask API using script tags, and I do not seem to be getting any type of errors in my console, so why can I not find my smart contract on the etherscan.io?
My JS is as so:
var dataHandling = async function customResponse () {
const provider = await detectEthereumProvider();
if (provider) {
if (provider !== window.ethereum) {
console.error('Do you have multiple wallets installed?');
}
console.log('Access the decentralized web!');
} else {
console.log('Please install MetaMask!');
}
}
dataHandling();
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3($INFURA_LINK);
}
const SCabi = $ABI
const SCaddress = $address
async function connect(){
//Will Start the metamask extension
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
console.log(ethereum.selectedAddress)
var dat = {
fname: document.getElementById('name').value,
cert: document.getElementById('cert').value
}
var SC = new web3.eth.Contract(SCabi, SCaddress)
SC.methods.setMessage(JSON.stringify(dat)).call(function (err, res) {
if (err) {
console.log("An error occured", err)
}else{
console.log(SC.methods.getMessage())
return
}
})
My smart contract is as so:
contract Message {
string myMessage;
function setMessage(string x) public {
myMessage = x;
}
function getMessage() public view returns (string) {
return myMessage;
}
}
new web3.eth.Contractdoes not deploy the contract. If you provide a specific address, then it is saying "I want to interact with a contract with this ABI, already deployed at this address". To deploy it, you need to use thedeploymethod. You cannot choose the address you deploy to, it is returned to you when thePromisereturned fromdeployresolves.As an aside: I assume the
$values come from PHP or something? It might be worth checking they are correct before you try deploying, if you haven't already.Edit: assuming your contract is deployed, the problem is that
setMessageis a method that modifies the blockchain state, and as such you need to use a transaction (to pay for that change with a small amount of ETH/gas).The way to do this with Metamask/Web3 is a bit awkward in terms of API: