Alchemy University 学习教程,Ethereum

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

官方大佬的 lens ,可以关注下:

了解更多,请关注作者:https://twitter.com/bitc2024

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

Ethereum Features

Transactions Game

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

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

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

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

2.钱包地址。

以下是频道:

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

可以借鉴别人怎么发

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

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

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

Reading Data from Ethereum

Activity: Query Ethereum

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

Ethereum Transactions

JSON-RPC Requests

1: Current Block Number

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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

查看要求:

输入:

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?

查看要求:

输入:

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

Subscribe to 冰糖vs橙子
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.