0

first of all thank you for your time reading this.

I'm trying to learn and play with solidity on polygon mumbai blockchain. I worked with internet forums, videos and chatgpt4.

Here is some details about where i'm at and what's the matter.

I made a smart contract that allows one to register paying some fees per "seats" (number of registrations). Nothing too complex, a struct to keep the address of people registered. A token delivery on buy, And company share taken out from the payment after registering the entry.

I can give you more details later if it looks usefull.

Everything works just perfectly in remix ide with injected provier metamask. There is no error, nor any kind of problem nor any bug. Everything works just as intended.

So i tryed to add an html page to allow any theorical user to interract with the contract.

The display functions work perfectly. I can retrieve data from the contract and diplay it in my html page from the struct or any variable.

But i can't make the transaction work from the webpage...

Here is some details of my html i can share more if you need. I try to understand what i do, that's why i try reasoning in linked pertinent elements.

Web3.js used : <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

Providers i tested :

 <script>
    // Set up web3
    //const web3 = new Web3(new Web3.providers.HttpProvider("https://polygon-mumbai-bor.publicnode.com"));
    //const web3 = new Web3(window.ethereum);
    //const web3 = new Web3(new Web3.providers.HttpProvider("https://polygon-mumbai.g.alchemy.com/v2/xxxxxxxxxxxxxxxxxxxxxxxxxxx"));
    const web3 = new Web3(window.ethereum || "https://polygon-mumbai-bor.publicnode.com");

Contract call :

const contract = new web3.eth.Contract(abi, contractAddress);

Example of working display function using an argument :

let launchNumber = getLaunchCount;
        
        // Call the getParticipants function with launchNumber = 0
        contract.methods.getParticipants(launchNumber).call().then(function(result) {
            const participants = result[0];
            const prizes = result[1];
            const entreesUtilisees = result[2];

            const tableBody = document.getElementById("participantsTable").getElementsByTagName("tbody")[0];
            
            for (let i = 0; i < participants.length; i++) {
                const newRow = tableBody.insertRow();

                const cellParticipant = newRow.insertCell(0);
                const textParticipant = document.createTextNode(participants[i]);
                cellParticipant.appendChild(textParticipant);

                const cellPrize = newRow.insertCell(1);
                const textPrize = document.createTextNode(prizes[i]);
                cellPrize.appendChild(textPrize);

                const cellEntreeUtilisee = newRow.insertCell(2);
                const textEntreeUtilisee = document.createTextNode(entreesUtilisees[i]);
                cellEntreeUtilisee.appendChild(textEntreeUtilisee);
            }
        });

        function updateData() {
            const launchNumber = document.getElementById("launchInput").value;

            // Effacez le contenu actuel du tableau avant de le mettre à jour
            const tableBody = document.getElementById("participantsTable").getElementsByTagName("tbody")[0];
            tableBody.innerHTML = '';

            // Appellez la méthode getParticipants avec le numéro de lancement choisi
            contract.methods.getParticipants(launchNumber).call().then(function(result) {
                const participants = result[0];
    
                if (participants.length === 0) {
                    const newRow = tableBody.insertRow();
                    const newCell = newRow.insertCell(0);
                    const newText = document.createTextNode("0");
                    newCell.appendChild(newText);
                    newRow.insertCell(1).appendChild(document.createTextNode("0"));  // For prizes
                    newRow.insertCell(2).appendChild(document.createTextNode("0"));  // For EntreeUtilisee
                } else {
                    // Votre code actuel pour afficher les données
                    const prizes = result[1];
                    const entreesUtilisees = result[2];
                
                    for (let i = 0; i < participants.length; i++) {
                        const newRow = tableBody.insertRow();

                        const cellParticipant = newRow.insertCell(0);
                        const textParticipant = document.createTextNode(participants[i]);
                        cellParticipant.appendChild(textParticipant);

                        const cellPrize = newRow.insertCell(1);
                        const textPrize = document.createTextNode(prizes[i]);
                        cellPrize.appendChild(textPrize);

                        const cellEntreeUtilisee = newRow.insertCell(2);
                        const textEntreeUtilisee = document.createTextNode(entreesUtilisees[i]);
                        cellEntreeUtilisee.appendChild(textEntreeUtilisee);
                    }
                }
            }).catch(function(error) {
                console.error("Erreur lors de la recuperation des donnees:", error);
                alert("Erreur lors de la récuperation des donnees pour ce lancement.");
            });
        }

The function i can't figure out what's wrong

async function enter() {
            console.log("1 Function enter called");

            // nombre de places voulu
            //const numSeats = document.getElementById("numSeats").value;
            //console.log("numSeats:", numSeats);

            const numSeatsRaw = document.getElementById("numSeats").value.trim();
            console.log("2 : numSeatsRaw :", numSeatsRaw);
            console.log("Type of numSeatsRaw devrait être string :", typeof numSeatsRaw); // devrait être "string"

            console.log("2b : accounts[0] :", accounts[0]);
            console.log("Type of accounts[0] devrait être  :", typeof accounts[0]); // devrait être "string"

            if (!numSeatsRaw || isNaN(numSeatsRaw) || parseInt(numSeatsRaw) <= 0) {
                alert("2 c : Veuillez entrer un nombre valide de places.");
                return;
            }

            const numSeats = parseInt(numSeatsRaw);
            console.log("3 : numSeats:", numSeats);
            console.log("Type of numSeats devrait être number :", typeof numSeats); // devrait être "number"
            

            const etherValue = (numSeats * 0.02).toString();
            console.log("4 : etherValue:", etherValue);
            console.log("Type of etherValue devrait être string :", typeof etherValue); // devrait être "string"


            // Set up the contract instance
            const contract = new web3.eth.Contract(abi, contractAddress);
            console.log("5 : Contract set up done");



            //test de la fonction
            try {
                const result = await contract.methods.enter(numSeats).call({
                    from: accounts[0],
                    value: web3.utils.toWei(etherValue, 'ether')
                });
                console.log("5a1 :  Transaction réussie:",result);
            } catch (error) {
                console.error("5b1 : Simulation error:", error);
            }

            const estimatedGas = await contract.methods.enter(numSeats).estimateGas({from: accounts[0], value: web3.utils.toWei(etherValue, 'ether')});
            
            console.log("6 : estimatedGas", estimatedGas);

            const estimatedGasNumeric = Number(estimatedGas);  // Convertir BigInt en Number
            const estimatedGas2 = Math.ceil(estimatedGasNumeric * 1.2);  // Multiplier par 1.2 et arrondir au nombre entier le plus proche
            console.log("7 : estimatedGas 2", estimatedGas2);
            console.log("Type of estimatedGas2 devrait être number : ", typeof estimatedGas2);  // devrait être "number"

            const weiValue = web3.utils.toWei(etherValue, 'ether');
            console.log("Ether value:", etherValue);
            console.log("Wei value:", weiValue);

            // Call the contract's enter function
            contract.methods.enter(numSeats).send({
                from: accounts[0],
                value: weiValue,
                gas: estimatedGas2,
                gasPrice: '20000000000'
            }).then((receipt) => {
                console.log("8a : Transaction reussie:", receipt);
            }).catch((error) => {
                console.log("8b : Erreur lors de l'envoi de la transaction:", error.message);
            });
        }

I tested some things but did not seam to work any beter. I tryed ethers.js, but did not figure out how to do it. I also tryed to add as many logs as i could to check everything. Here is what i get :

1 Function enter called
index9a.html:672 
2 : numSeatsRaw : 1
index9a.html:673 
Type of numSeatsRaw devrait être string : string
index9a.html:675 
2b : accounts[0] : 0x93d57a4de289ca3ca934a3dc6e47970994e59ad1
index9a.html:676 
Type of accounts[0] devrait être  : string
index9a.html:684 
3 : numSeats: 1
index9a.html:685 
Type of numSeats devrait être number : number
index9a.html:689 
4 : etherValue: 0.02
index9a.html:690 
Type of etherValue devrait être string : string
index9a.html:695 
5 : Contract set up done
index9a.html:705 
5a1 :  Transaction réussie: {__length__: 0}
index9a.html:712 
6 : estimatedGas 274425n
index9a.html:716 
7 : estimatedGas 2 329310
index9a.html:717 
Type of estimatedGas2 devrait être number :  number
index9a.html:720 
Ether value: 0.02
index9a.html:721 
Wei value: 20000000000000000
index9a.html:732
8b : Erreur lors de l'envoi de la transaction: Transaction has been reverted by the EVM:
{"blockHash":"0x392961f988a5ec48c272847ac580563238972c1e3f982485d60ae3a6eb3860f3","blockNumber":"40611995","cumulativeGasUsed":"21046","effectiveGasPrice":"20000000000","from":"0x93d57a4de289ca3ca934a3dc6e47970994e59ad1","gasUsed":"21046","logs":[{"address":"0x0000000000000000000000000000000000001010","topics":["0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63","0x0000000000000000000000000000000000000000000000000000000000001010","0x00000000000000000000000093d57a4de289ca3ca934a3dc6e47970994e59ad1","0x0000000000000000000000005082f249cdb2f2c1ee035e4f423c46ea2dab3ab1"],"data":"0x00000000000000000000000000000000000000000000000000017ed3130eba6a0000000000000000000000000000000000000000000000000472278eb04892ba000000000000000000000000000000000000000000000226d86e7e92f32f53aa0000000000000000000000000000000000000000000000000470a8bb9d39d850000000000000000000000000000000000000000000000226d86ffd66063e0e14","blockNumber":"40611995","transactionHash":"0x930a4b484f9fde80f2e2c5ec146e6ea59369aec01cb125c0c3ca712cd7a497e3","transactionIndex":"0","blockHash":"0x392961f988a5ec48c272847ac580563238972c1e3f982485d60ae3a6eb3860f3","logIndex":"0","removed":false}],"logsBloom":"0x00000000000000000000000000400000000000000000000000000000020000000000000002000000000000000000000000008000000000000000000000000000000000000000000000000000000000800000000000000000000100000000000000008000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000200000000000000000000000004000000000000000000001000000000000000000000000000000100000000000000000000000000080000000000000000000000000000000000000000000100000","status":"0","to":"0xee184d1bc6f9df3250a320119cfa30f0f69f47f0","transactionHash":"0x930a4b484f9fde80f2e2c5ec146e6ea59369aec01cb125c0c3ca712cd7a497e3","transactionIndex":"0","type":"2"}

But i do not understand the problem i get. I don't know where to look anymore as i feel like i checked all possibilities...

Changed gas fees, tryed more, less, no gas fees explicitly given, using metamask fee selection. Nothing seamed to work. Tryed to check the format of the data but it looked fine. Tryed to test the function before sending it, same it seams to work.

Do you have ideas, tests ? Or am i missing an update or something like that ?

Also i saw that the contract is on bnb and other chains at the same time. And when i tryed the button on bnb, it worked. Well not exactly because i don't know what happened but i lost some bnb and did not get an error... Even if it was absolutly not intended.

Last info, i used the exact same method i used on a earlier version of the contract that was working. But when i tryed to confirm again that the older version works, i did not work any more. Anything crucial changed in the way to do it ?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.