Assume you have a function in your contract like this:
function deposit() public payable{
// do something here
}
Now, you want to call deposit()
and send some ether from your website.
On your website you have the form to input the Ether amount and a button to submit. First you get an instance of your contract:
var thecontract = web3.eth.contract(contractABI);
var MyContract = thecontract.at('contract_address');
Then "onclick" of the button you execute a function that contains this:
function makeDeposit(){
var etherAmount = web3.toBigNumber($("#id_of_field_with_ether_value").val());
var weiValue = web3.toWei(etherAmount,'ether');
MyContract.deposit({from: web3.eth.accounts[0], gas: gasValue, value: weiValue}, function(err, res){ })
}
Thats it.
Hope it helps.
Sending a transaction without signing it first is supported only if you unlock your account on the node that you're communicating with beforehand (for example, when you run your tests on Ganache).
On public nodes, this is obviously not feasible (neither would you want it to be, of course).
You'll need to sign the transaction first, and then send the signed-transaction instead.
For example (tested with web3.js v1.2.1):
const Web3 = require("web3");
const NODE_ADDRESS = "YourNodeAddress";
const PRIVATE_KEY = "0xYourPrivateKey";
async function scan(message) {
process.stdout.write(message);
return await new Promise(function(resolve, reject) {
process.stdin.resume();
process.stdin.once("data", function(data) {
process.stdin.pause();
resolve(data.toString().trim());
});
});
}
async function getGasPrice(web3) {
while (true) {
const nodeGasPrice = await web3.eth.getGasPrice();
const userGasPrice = await scan(`Enter gas-price or leave empty to use ${nodeGasPrice}: `);
if (/^\d+$/.test(userGasPrice))
return userGasPrice;
if (userGasPrice == "")
return nodeGasPrice;
console.log("Illegal gas-price");
}
}
async function getTransactionReceipt(web3) {
while (true) {
const hash = await scan("Enter transaction-hash or leave empty to retry: ");
if (/^0x([0-9A-Fa-f]{64})$/.test(hash)) {
const receipt = await web3.eth.getTransactionReceipt(hash);
if (receipt)
return receipt;
console.log("Invalid transaction-hash");
}
else if (hash) {
console.log("Illegal transaction-hash");
}
else {
return null;
}
}
}
async function send(web3, account, transaction) {
while (true) {
try {
const options = {
to : transaction._parent._address,
data : transaction.encodeABI(),
gas : await transaction.estimateGas({from: account.address}),
gasPrice: await getGasPrice(web3),
};
const signed = await web3.eth.accounts.signTransaction(options, account.privateKey);
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
return receipt;
}
catch (error) {
console.log(error.message);
const receipt = await getTransactionReceipt(web3);
if (receipt)
return receipt;
}
}
}
async function run() {
const web3 = new Web3(NODE_ADDRESS);
const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);
const contract = new web3.eth.Contract(abi, data.contractAddr);
const transaction = contract.methods.signAgreement();
const receipt = await send(web3, account, transaction);
console.log(JSON.stringify(receipt, null, 4));
if (web3.currentProvider.constructor.name == "WebsocketProvider")
web3.currentProvider.connection.close();
}
run();
Best Answer
I am not sure what does your
new metamask
mean, however I can show you how to call an contract method in 2022:( assuming you are calling a contract 's mint method )
refer to: http://siwei.me/blog/posts/blockchain-web3-metamask-contract