Blockchain based auction using Goerli testnet. Part-III

Mohamed Nufail
5 min readJan 3, 2023

--

Implementing an auction system which frequently check the Inventory Control department when products in the warehouse is nearing its expiration date and conduct an auction to sell the products before the expiration.

MetaMask

MetaMask is a browser extension that allows you to interact with the Ethereum blockchain. It acts as a bridge between your browser and the Ethereum network.

With MetaMask, you can manage your Ethereum accounts, send and receive Ether and other Ethereum-based tokens, and interact with smart contracts. It also has built-in support for popular Ethereum test networks, like Rinkeby and Goerli, which makes it easy to test your dApps without using real Ether.

To use MetaMask, you will need to install it in your browser as an extension. It is available for Chrome, Firefox, Opera, and Brave. Once you have installed MetaMask, you will need to create an account and set a password to secure your account. You can then add Ethereum to your account by buying Ether from a cryptocurrency exchange or by receiving it from someone else.

Goerli Testnet

MetaMask supports the Goerli testnet, which is a public, proof-of-authority Ethereum testnet that is maintained by the Ethereum community. Goerli is a good choice for testing because it is fast, stable, and has a high transaction limit.

To use MetaMask with the Goerli testnet, you will need to switch to the Goerli network in your MetaMask extension. To do this, click on the network dropdown in the top-left corner of the MetaMask window and select “Goerli Test Network”. You may also need to add some test Ether to your Goerli account, which you can do by requesting it from a testnet faucet. Once you are connected to the Goerli testnet, you can deploy and test your own smart contracts.

Etherscan

You can use MetaMask and Etherscan together to interact with the Goerli testnet and explore the Ethereum blockchain. To view your transactions and contracts on Etherscan, you can use the Etherscan Goerli block explorer. This will allow you to see the details of your transactions and contracts, as well as the status and logs of your smart contracts.

Truffle HD wallet provier

Truffle HDWalletProvider is a plugin for Truffle, a popular development framework for Ethereum, that allows you to use an HDWallet (a Hierarchical Deterministic Wallet) as an account provider for your Ethereum transactions. This means you can use an HDWallet like Metamask or a Ledger hardware wallet to sign your transactions and deploy your contracts to the Ethereum network.

To use Truffle HDWalletProvider, you will first need to install it in your project by running the following command:

npm install truffle-hdwallet-provider

then you will have to create a project on infura.io in order to get an API KEY, here is my configuration:

Infura is a platform that provides secure and reliable access to Ethereum and IPFS (InterPlanetary File System) nodes for developers. It allows you to interact with the Ethereum blockchain and IPFS network without running your own full nodes, which makes it easy to develop and deploy decentralized applications (dApps).

To use Infura with the Goerli testnet and Truffle HDWalletProvider, you will need to sign up for a free Infura account and create a new project. This will give you an Infura API key, which you can use to connect to the Goerli testnet via Truffle HDWalletProvider. In this example, we are using Truffle HDWalletProvider to connect to the Goerli test network via Infura.

Here is an example of how you can configure Truffle HDWalletProvider to connect to the Goerli testnet via Infura and deploy a smart-contract.

const HDWalletProvider = require("@truffle/hdwallet-provider");
const Web3 = require('web3');


// ABI of the Smartcontract
let abi = [
{
"inputs": [
{
"internalType": "uint256",
"name": "biddingTime",
"type": "uint256"
},
{
"internalType": "string",
"name": "_productHash",
"type": "string"
},
{
"internalType": "string",
"name": "_couponCode",
"type": "string"
},
{
"internalType": "uint256",
"name": "_minimumBid",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},


]

// Bytecode of the Smartcontract
let bytecode = "60806040526000600a60006101000a81548160ff0219169083151502179055503480156200002c57600080fd5b5060405162001ded38038062001ded83398181016040528101906200005291906200022e565b3360008"



// Function to deploy the smartcontract
const deploy = async({MNEMONIC,account, biddingTime,productHash,couponCode,minimumBid}) => {
/* Inputs
- Private Key
- Public Key
- Aunction bidding time
- Hash of the product
- Release the coupon code for the highest bidder
- Minimum bid amount

*/


try{

// Define the "MNEMONIC" to generate the Ethereum accounts
let provider = new HDWalletProvider(MNEMONIC, "https://goerli.infura.io/v3/YOUR-API-KEY");

//Interact with the contract using Truffle or a web3 library like Web3.js
let web3 = new Web3(provider);

// Return the transaction address
let transactionHashValue = "";

// Send the bided value to beneficiary address
let beneficiaryAddress = account;

// Store the minimum bid value
let minimunBidValue = minimumBid+""


// Convert the floating point to ethers
const valueInWei = web3.utils.toWei(minimunBidValue, 'ether');

console.log('Attempting to deploy from account >>> ',beneficiaryAddress);

//Deploy the contract by mentioning the constructor parameters
const result = await new web3.eth.Contract(JSON.parse(JSON.stringify(abi)))
.deploy({data:bytecode, arguments:[biddingTime,productHash,couponCode,valueInWei]})
.send({gas:'9999999', from: account},(err,transactionHash)=>{
console.log("transaction hash >>> " + transactionHash)
transactionHashValue = transactionHash;
}).catch((e) => {
console.log('forceInterface.deploy failed: ');
console.log(e)
});


// Smart-contract address
console.log('To address >>> ', result.options.address);

// Return the required details
return {publickey:account, productHash:productHash, transactionHash:transactionHashValue, contractAddress:result.options.address, startTime:Date.now(), biddingTime: biddingTime, minimumBid:minimumBid }


}catch(e){
console.log(e)
}
}

module.exports = {deploy}

The mnemonic variable should contain your 12-word mnemonic phrase, which is used to generate the Ethereum accounts that Truffle HDWalletProvider will use to sign your transactions.

You can use Express.js, a popular web framework for Node.js, to pass details to a contract deployment function. Here is an example of how you might do this:

const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");

const { deploy } = require("./SmartContract/tradeContract");
const corsOptions = {
origin: "*",
credentials: true, //access-control-allow-credentials:true
optionSuccessStatus: 200,
};

const app = express();
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));


app.post("/contract", async (req, res) => {
contractDetails = await deploy({
MNEMONIC: req.body.MNEMONIC,
account: req.body.account,
biddingTime: req.body.biddingTime,
productHash: req.body.productHash,
couponCode: req.body.couponCode,
minimumBid: req.body.minimumBid
});

res.send(contractDetails);
});

app.listen(4000, () => {
console.log(`listening at localhost:4000`);

});

In this example, we have created an Express.js route that listens for POST requests to the /contract endpoint. When a request is received, the route parses the request body for the contractName, constructorArgs, and options parameters and passes them to the deployContract function.

You can then make a POST request to this endpoint from your client-side application to trigger the contract deployment. The output is:

--

--