0

I'm trying to create a simple web application. It should take an user input (Ethereum wallet) and then, with the help of Alchemy API, it should show a list of transactions from that address. To do this I used the following documentation:

I have to use HTML and JavaScript. And I have to understand how to take user input and give it to a JavaScript function. At the end I want to see transactions in JSON to the Browser Console.

But I always find same error in the browser:

Uncaught ReferenceError: fun is not defined
    onsubmit

I tried to understand the problem. I changed browsers and I used different approaches, but nothing. I'll show you HTML and JS code. I hope that someone can help me, please!

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Project</title>

</head>

<body>

    <h3>Etereum Blockchain Query</h3>

    <form name="dati" style="border:0px" action="" method="post" onsubmit="fun()"> <br>

        Wallet address: <input type="text" id="address" name="address" placeholder="Enter address"><br><br>

        <input type="submit" value="Submit"><br>
        <input name="Reset Data" type="reset" value="Reset Data">   

    </form>
    <br><br>

    <script type="alchemy-sdk" src="alchemy-test.mjs"></script>

</body>
</html>

alchemy-test.mjs - JS Code

// Setup: npm install alchemy-sdk
import { Alchemy, Network } from "alchemy-sdk";

const settings = {
    apiKey: "######################",
    network: Network.ETH_MAINNET,
};

const alchemy = new Alchemy(settings);

function fun() {
    var addr = document.getElementById("address").value;
    return addr;
};

const res = await alchemy.core.getAssetTransfers({
    fromBlock: "0x0",
    toBlock: "latest",
    fromAddress: addr,
    //toAddress: toAddress,
    excludeZeroValue: true,
    category: ["erc20", "external"],
    maxCount: "0x3e8"
});


console.log(res);
z3f1r0
  • 1

1 Answers1

0

I noticed that you are using ES Modules. For your code to work as is you would need to bundle it using something like webpack. You cannot write javascript code that imports third party libraries in the browser.

Daniel Smith
  • 179
  • 8