[SOLVED] Speedy Node - method Send()

Hello,

I’m trying to write a specific method from a CA but, it fails while using the Speedy Nodes. Error below:

Access to XMLHttpRequest at 'https://speedy-nodes-nyc.moralis.io/{number}/bsc/mainnet' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
VM346:1          POST https://speedy-nodes-nyc.moralis.io/{number}/bsc/mainnet net::ERR_FAILED 401
Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (errors.js:43:1)
    at XMLHttpRequest.request.onreadystatechange (index.js:95:1)

Code:

await _contract.methods.increaseAllowance("0xd5f363f83b36e10e8a823166b992c0bdc3dede2c", allowance)
.send({ 'from': userAddress })
.then(console.log).catch(console.error);

and

       await _lottery.methods.getTicket()
.send({ from: userAddress })
.then(function (receipt) {
                console.log(receipt)
            }).catch(console.log);

Changed to https://bsc-dataseed1.binance.org:443, now I get:

errors.js:28 Uncaught (in promise) Error: Returned error: unknown account
    at Object.ErrorResponse (errors.js:28:1)
    at index.js:300:1
    at XMLHttpRequest.request.onreadystatechange (index.js:98:1)
    await _contract.methods.increaseAllowance("0xd5f363f83b36e10e8a823166b992c0bdc3dede2c", allowance)
.send({from: "<my wallet address>"});

very very strange, I don’t understand why I get this error.

it will require a private key to be able to send a transaction
if you want to do that in browser, you can do it directly by using metamask, and without having to hardcode a speedy node url and a private key

Hmmm, but I want the MetaMask window to popup to “accept” the transaction…

async function buyTicket() {

        try {

            if (elonBankTokens >= ticket) {

                const allowance = await _contract.methods.allowance(userAddress, "0xe47bF48B17A5F169bB2d4982349310410181E2fc").call();

                console.log(allowance + "outside")

                if (allowance < ticket) {

                    // we need to give allowance to lottery contract first0

                    console.log(ticket + "ticket inside")

                    await _contract.methods.increaseAllowance("0xe47bF48B17A5F169bB2d4982349310410181E2fc", allowance).send({ from: userAddress });

                }

                const receipt = await lotteryGetTicket.methods.getTicket().send({ from: userAddress });

                console.log(receipt)

            } else {

                return console.log('Your Raptor balance is not sufficient to buy a ticket');

            }

        }

        catch (e) {

            console.log(e)

        }

    }

It should work to make MetaMask pop up
Did you use Moralis.enableWeb3?
Also, in latest version of Moralis sdk it returns an ethers instance by default.

1 Like

I had to change the provider as per instructions to:

    async function buyTicket() {
        
        await Moralis.enableWeb3();

        try {


            const Web3Client = new Web3(Moralis.provider);

            const _lottery = new Web3Client.eth.Contract(require('./lottery-abi.json'), "0xe47bF48B17A5F169bB2d4982349310410181E2fc")
            const _contract = new Web3Client.eth.Contract(require('./contract-abi.json'), "0x9B1F1fA0Cb5f51BB6334509984751195129fDBc3")

            console.log(user.get('accounts')[0])
            if (elonBankTokens >= ticket) {
                const allowance = await _contract.methods.allowance(user.get('accounts')[0], "0xe47bF48B17A5F169bB2d4982349310410181E2fc").call();
                console.log(allowance + "outisde")
                if (allowance < ticket) {

                    // we need to give allowance to lottery contract first
                    console.log(allowance + "inside")
                    console.log(ticket + "ticket inside")
                    await _contract.methods.increaseAllowance("0xe47bF48B17A5F169bB2d4982349310410181E2fc", allowance).send({ from: user.get('accounts')[0] });

                }

                const receipt = await _lottery.methods.getTicket().send({ from: user.get('accounts')[0] });
                console.log(receipt)

            } else {
                return console.log('Your Raptor balance is not sufficient to buy a ticket');
            }
        }
        catch (e) {
            console.log(e)
        }


    }

Now it properly works. Thanks !!

1 Like