0
votes

I know this question may be better suited for the Ethereum stack exchange but I've gotten no responses over there and I'm wondering if it's more a React issue or something.

I'm trying to run 3 async methods

1: Push a file to IPFS:


    pushToIPFS = async(e) => {
            return new Promise((resolve, reject) => {
                ipfs.add(this.state.buffer, (err, ipfsHash) => {
                    resolve(ipfsHash[0].hash);
                })
            });
        }

2: Write to smart contract and Blockchain:


    addToBlockchain = async(_ipfsLink) => {

            return new Promise((resolve, reject) => {
                const rand = uniqueRandom(1, 10000000)
                var key = rand()

                let newDate = new Date()
                newDate = newDate.getTime()
                var _account = this.state.account[0]
                var _uid = this.state.uid

                storehash.methods.sendDocument(_ipfsLink, newDate, key, _uid).send({from: _account}) 

                resolve(key)
            })
        }

3: Add an entry to a firebase database:


    createStudent = async(_key) => {
            //get student details from state variables & current user uid
            var _uid = this.state.uid
            var _studentName = this.state.StudentName
            var _studentNumber = this.state.StudentNumber
            var _courseCode = this.state.CourseCode
            var _courseName = this.state.CourseName
            var _idForBlockchain = _key

            // database.ref.students.uid.studentNumber 
            const db = firebase.database()
            db.ref().child("students").child(_uid).child(_studentNumber).set(
                {   studentName: _studentName,
                    courseCode: _courseCode,
                    courseName: _courseName,
                    blockchainKey: _idForBlockchain 
                }
            );

            alert("Student added")

        }

These are called in that order by this function:


    AddMyStuff = async (e) => {
            e.preventDefault()
            const ipfsHash = await this.pushToIPFS()
            //await this.createStudent()
            const _key = await this.addToBlockchain(ipfsHash)
            await this.createStudent(_key)
        }

The problem is the student is being added to the database before the metamask (smart contract) transaction has been confirmed. And also After the smart contract executes, all the correct details are written to the Blockchain, but the following error appears in the console:

web3-core-method.umd.js:1191 Uncaught (in promise) Error: Transaction has been reverted by the EVM: { "transactionHash": "0xe3f411d872bb5f42bd7bd15676647f5056421c5accb5aa6fa22d75eadd09973a", "transactionIndex": 0, "blockHash": "0x749ccd76a6888b261b00d8851dca36545fde563fce4c8513407a180d6cac3a00", "blockNumber": 6, "from": "0xa5fcbc63d6bcb8e07750cb75073ec3ff7b98c4f5", "to": "0xadb13cc1a32b64f938be7c1d3447dfcd20c09ae9", "gasUsed": 175983,
"cumulativeGasUsed": 175983, "contractAddress": null, "logs": [], "status": true, "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "v": "0x2d46", "r": "0xa1dfe4e9a4ac8dbbdf49186741cd74bda4fed78b280458ed2cd7f979a3020ccd", "s": "0x477fa20a2176975f3db9fb1e375fceeedb6ae0e7be35177d4908ad1744dddd1a" } at SafeSubscriber._next (web3-core-method.umd.js:1191) at SafeSubscriber.__tryOrUnsub (Subscriber.js:245) at SafeSubscriber.next (Subscriber.js:174) at Subscriber._next (Subscriber.js:99) at Subscriber.next (Subscriber.js:68) at TransactionObserver.emitNext (web3-core-method.umd.js:510) at _callee$ (web3-core-method.umd.js:357) at tryCatch (runtime.js:63) at Generator.invoke [as _invoke] (runtime.js:282) at Generator.prototype.(anonymous function) [as next] (http://localhost:3000/static/js/0.chunk.js:276708:21) at asyncGeneratorStep (asyncToGenerator.js:3) at _next (asyncToGenerator.js:25)

Scary stuff indeed, does anyone know what could be causing this? Again sorry if this is better suited for Ethereum stack exchange, but I've gotten no responses and I don't know what else to try. Thanks in advance for any help!

1
I guess because you dont wait for storehash.methods.sendDocument ? You call resolve() before it ends its execution - Alexandr Zavalii
Also you don't need async in addToBlockchain - Alexandr Zavalii
I'll need async though if I make it await storehash.methods.sendDocument ? - Neil Grogan
thats right, you need async if you want to use await inside. In that case you don't need to create promise. - Alexandr Zavalii
Okay so I used that and the order is now correct and the metamask prompt runs before the student is added to the firebase DB - but, that same error about the transaction being reverted appears and it doesn't reach the createStudent method - Neil Grogan

1 Answers

0
votes

in case sendDocument is a promise, you can try that..

  addToBlockchain = async (_ipfsLink) => {

    const rand = uniqueRandom(1, 10000000)
    var key = rand()

    let newDate = new Date()
    newDate = newDate.getTime()
    var _account = this.state.account[0]
    var _uid = this.state.uid

    await storehash.methods.sendDocument(_ipfsLink, newDate, key, _uid).send({from: _account}) 

    return key
}