# Alchemy University 学习教程，Ethereum

By [冰糖vs橙子](https://paragraph.com/@bible666) · 2023-01-02

---

估值102亿融资5.45亿的Alchemy 项目，大学每周任务逐步开始。持有大学生早期卡的伙伴们可以开始大学生活了：

官方大佬的 lens ，可以关注下：

[https://lenster.xyz/u/vitto.lens](https://lenster.xyz/u/vitto.lens)

了解更多，请关注作者：[https://twitter.com/bitc2024](https://twitter.com/bitc2024)

这一期看文章的内容比较多，请各位自行查看。

Ethereum Features
-----------------

### Transactions Game

这里需要去下载一个钱包，创建一个钱包账户（注意不要导自己的助记词，直接新创建一个），注意最好用一个新的浏览器创建，不要在常用的浏览器创建（谷歌浏览器添加一个新账户创建即可）

![](https://storage.googleapis.com/papyrus_images/a0f721e0dcf643ef0d461061563bf249b3d4f10aac97d697c6f7709c86575fce.png)

官方这里有教程，可以看下，比较简单，我这里就不写了。

领完水后，需要进入官方discord群里，找一个钱包地址发送一些ETH过去，同时自己也要把自己刚刚创建的ETH地址发到这个频道去，内容包含：

1.写一个关于自己的有趣的事情，

2.钱包地址。

以下是频道：

[

Discord - Group Chat That's All Fun & Games
-------------------------------------------

Discord is great for playing games and chilling with friends, or even building a worldwide community. Customize your own space to talk, play, and hang out.

https://discord.com

![](https://storage.googleapis.com/papyrus_images/fd5daf4cc4569214cd87e650d89285a0aac548020f1d6734eaffa8b0add2160e.png)

](https://discord.com/channels/1039895401832128532/1050240006574317618)

点击上面链接后进不去频道的，需要在discord添加身份：

![](https://storage.googleapis.com/papyrus_images/41153c872e7c56c2bb72cc06005c01f4f4e8fe25c0db5903027dabbb56c0a128.png)

可以借鉴别人怎么发

![](https://storage.googleapis.com/papyrus_images/d494fe60a152de1df7389d7c4fa712b404b87869b2b3c6d9b8950952c8eca2b2.png)

随便找个地址发一些ETH给他就行。

注意用上面刚创建的钱包。

![](https://storage.googleapis.com/papyrus_images/d84cf084bac6c92da4eb7ce31f7158aa658a764c7259e1bca388061c73b36c76.png)

发送完第一节的游戏就完成了。

Reading Data from Ethereum
--------------------------

### Activity: Query Ethereum

这块内容，主要是查看文章，不是真正做区块链的，看完就好了。

Ethereum Transactions
---------------------

### JSON-RPC Requests

1: Current Block Number

查看要求：

![](https://storage.googleapis.com/papyrus_images/2c814e0517f9f16895df650f45a6461f61cee0abe6b3a6f4a08b2634a330ab8e.png)

输入：

    const provider = require('./provider');
    
    async function getBlockNumber() {
        const ret = await provider.send({
            jsonrpc: "2.0",
            id: 1,
            method: "eth_blockNumber",
        });
        return ret.result;
    }
    
    module.exports = getBlockNumber;
    

点击运行。

2: Get Balance

查看要求：

![](https://storage.googleapis.com/papyrus_images/663c5cd303ba24557a4b03fad8c9ad97658f3cc4b0ffa80f440d3af428cab5cb.png)

输入：

    const provider = require('./provider');
    
    async function getBalance(address) {
        const ret = await provider.send({
            jsonrpc: "2.0",
            id: 1,
            method: "eth_getBalance",
            params: [address, "latest"],
        });
        return ret.result;
    }
    
    module.exports = getBalance;
    

点击运行。

3: Get Nonce

查看要求：

![](https://storage.googleapis.com/papyrus_images/4485bbbfe3b16539d21b19bb071da3de309c9317c1dfa6b0e1aeee10d15a5e8b.png)

输入：

    const provider = require('./provider');
    
    async function getNonce(address) {
        const ret = await provider.send({
            jsonrpc: "2.0",
            id: 1,
            method: "eth_getTransactionCount",
            params: [address, "latest"],
        });
        return ret.result;
    }
    
    module.exports = getNonce;
    

点击运行。

4: Block Transactions

查看要求：

![](https://storage.googleapis.com/papyrus_images/bae263b6a5e8e12c98166b0631f7670834c14534aeb8af8c8c84f73841c113d8.png)

输入：

    const provider = require('./provider');
    
    async function getTotalTransactions(blockNumber) {
        const ret = await provider.send({
            jsonrpc: "2.0",
            id: 1,
            method: "eth_getBlockByNumber",
            params: [blockNumber, false], 
        });
        return ret.result.transactions.length;
    }
    
    module.exports = getTotalTransactions;
    

点击运行。

5: Total Wei

查看要求：

![](https://storage.googleapis.com/papyrus_images/1723ca4a6f6a041ffcb951dbd59ca6434cd14e9012ab45a7692a9bb0e2777d2a.png)

输入：

    const provider = require('./provider');
    
    async function getTotalBalance(addresses) {
        let reqs = [];
        for (let i = 0; i < addresses.length; i++) {
            reqs.push({
                jsonrpc: "2.0",
                id: 1,
                method: "eth_getBalance",
                params: [addresses[i], "latest"],
            });
        }
        const resps = await provider.send(reqs);
        let t = 0;
        for (let i = 0; i < resps.length; i++) {
            t = t + parseInt(resps[i].result);
        }
        return t;
    }
    
    module.exports = getTotalBalance;
    

点击运行。

Front-End Libraries
-------------------

### Intro to Ethers.js

1: Making Wallets

查看要求：

![](https://storage.googleapis.com/papyrus_images/bccf5ee9e851a07e3219556e8a0e7d8f30cf4e49e1a9a8d38fc36266d1fffb7f.png)

输入：

    const ethers = require('ethers');
    const { Wallet } = ethers;
    
    // create a wallet with a private key
    const wallet1 = new Wallet("0xf2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257d");
    
    // create a wallet from mnemonic
    const wallet2 = Wallet.fromMnemonic("plate lawn minor crouch bubble evidence palace fringe bamboo laptop dutch ice");
    
    module.exports = {
        wallet1,
        wallet2,
    }
    

点击运行。

2: Sign a Transaction

查看要求：

![](https://storage.googleapis.com/papyrus_images/b6d4d16a5298e872801e3bc9c723074c948b8f45093e6d88fd113a448d91ad67.png)

输入：

    const ethers = require('ethers');
    const { Wallet, utils } = ethers;
    const { wallet1 } = require('./wallets');
    
    const signaturePromise = wallet1.signTransaction({
        value: utils.parseEther('1', 'ether'),
        to: "0xdD0DC6FB59E100ee4fA9900c2088053bBe14DE92", 
        gasLimit: utils.parseUnits('21000', 'wei'),
    });
    
    module.exports = signaturePromise;
    

点击运行。

3: Connect to Ethereum

查看要求：

![](https://storage.googleapis.com/papyrus_images/6f29fa951faca85ace5b0a88b796f2869a1a484a89770bd8572d3b0043011018.png)

输入：

    const { Wallet, utils, providers } = require('ethers');
    const { ganacheProvider, PRIVATE_KEY } = require('./config');
    
    const provider = undefined; 
    
    const wallet = new Wallet(PRIVATE_KEY);
    
    async function sendEther({ value, to }) {
        const rawTx = await wallet.signTransaction({ 
            value, to, 
            gasLimit: 0x5208,
            gasPrice: 0x3b9aca00 
        });
    
        const provider = new providers.Web3Provider(ganacheProvider);
        resp = await provider.sendTransaction(rawTx);
        return resp;
    }
    
    module.exports = sendEther;
    

点击运行。

4: Account Nonce

查看要求：

![](https://storage.googleapis.com/papyrus_images/e5abc1a620c7e9c5ac015a03190220be49dcb99dccdca0871a2e1c1f05b33a3d.png)

输入：

    const { Wallet, utils, providers } = require('ethers');
    const { ganacheProvider, PRIVATE_KEY } = require('./config');
    
    const provider = new providers.Web3Provider(ganacheProvider);
    const wallet = new Wallet(PRIVATE_KEY, provider);
    
    async function sendEther({ value, to }) {
        return wallet.sendTransaction({ value, to });
    }
    
    module.exports = sendEther;
    

点击运行。

5: Find Balance

查看要求：

![](https://storage.googleapis.com/papyrus_images/789b1983e4d9ae25da1ea9ec1948533345a6395df6d871ae6a8a994e1fde80f5.png)

输入：

    const { Wallet, providers } = require('ethers');
    const { ganacheProvider } = require('./config');
    
    const provider = new providers.Web3Provider(ganacheProvider);
    
    function findMyBalance(privateKey) {
        const wallet = new Wallet(privateKey, provider);
        return wallet.getBalance();
    }
    
    module.exports = findMyBalance;
    

点击运行。

6: Charitable Donations

查看要求：

![](https://storage.googleapis.com/papyrus_images/bc330217d6cf232a43bfa8b3a47b7dc5684642b9d06f66c7c623463663e1534d.png)

输入：

    const { utils, providers, Wallet } = require('ethers');
    const { ganacheProvider } = require('./config');
    
    const provider = new providers.Web3Provider(ganacheProvider);
    
    /**
     * Donate at least 1 ether from the wallet to each charity
     * @param   {string} a hex string private key for a wallet with 10 ETH
     * @param   {array} an array of ethereum charity addresses 
     *
     * @returns {Promise} a promise that resolves after all donations have been sent
     */
    async function donate(privateKey, charities) {
        const wallet = new Wallet(privateKey, provider);
        let resps = [];
        nonce = await wallet.getTransactionCount();
        for (let i = 0; i < charities.length; i++) {
            const rawTx = await wallet.signTransaction({
                value: utils.parseEther('1', 'ether'),
                to: charities[i],
                gasLimit: utils.parseUnits('21000', 'wei'),
                nonce: nonce + i
            });
            resps.push(provider.sendTransaction(rawTx));
        }
    
        return Promise.all(resps);
    }
    
    module.exports = donate;
    

点击运行。

### Where is the Ether?

1: Where is the Ether?

查看要求：

![](https://storage.googleapis.com/papyrus_images/1c00dba358b097b4848d65568041f1a5cd612267a449b47b054f116492d70d99.png)

输入：

    const { providers } = require('ethers');
    const { ganacheProvider } = require('./config');
    
    const provider = new providers.Web3Provider(ganacheProvider);
    
    /**
     * Given an ethereum address find all the addresses
     * that were sent ether from that address
     * @param {string} address - The hexidecimal address for the sender
     * @async
     * @returns {Array} all the addresses that receieved ether
     */
    async function findEther(address) {
        let addrs = [];
        let blockNum = await provider.getBlockNumber();
        for (let i = 0; i <= blockNum; i++) {
            let block = await provider.getBlockWithTransactions(i);
            block.transactions.forEach((tx) => {
                if (tx.from === address) {
                    addrs.push(tx.to);
                }
            });
        }
        return addrs;
    }
    
    module.exports = findEther;
    

点击运行。

以上，Ethereum，课程学习完成。

未完待续……

了解更多，请关注作者：[https://twitter.com/bitc2024](https://twitter.com/bitc2024)

[https://mirror.xyz/bible666.eth/X3VB59DNaNU37nTwsKOFwEUPZvEtuzbM0GJlMkK8x40](https://mirror.xyz/bible666.eth/X3VB59DNaNU37nTwsKOFwEUPZvEtuzbM0GJlMkK8x40)

---

*Originally published on [冰糖vs橙子](https://paragraph.com/@bible666/alchemy-university-ethereum)*
