Coder Social home page Coder Social logo

substrate_ts's Introduction

第六课作业

使用polkadot js api来写一个订阅Event的程序

main.js

import { ApiPromise, WsProvider, Keyring } from "@polkadot/api";

const WS_ADDRESS = "ws://127.0.0.1:9944"

// Connect to substrate
const connectSubstrate = async () => {
    console.log("Connecting to Substrate...")
    const wsProvider = new WsProvider(WS_ADDRESS);
    const api = await ApiPromise.create({ provider: wsProvider, types: {} });
    await api.isReady;
    console.log("Connected.");
    return api;
};

// Get Constant
const getConst = async (api) => {
    return api.consts.balances.existentialDeposit.toHuman();
};

// Get balance
const getFreeBalance = async (api, address) => {
    const account = await api.query.system.account(address);
    return account["data"]["free"].toHuman();
};

// Print balance
const printBalance = async (api) => {
    const keyring = new Keyring({ type: 'sr25519' });
    const alice = keyring.addFromUri('//Alice');
    const bob = keyring.addFromUri('//Bob');
    console.log("Alice's balance is ", await getFreeBalance(api, alice.address));
    console.log("Bob's balance is ", await getFreeBalance(api, bob.address));
};

// Subscribe to the balance of Alice
const subscribeAlice = async (api) => {
    const keyring = new Keyring({ type: 'sr25519' });
    const alice = keyring.addFromUri('//Alice');
    await api.query.system.account(alice.address, (aliceAcc) => {
        console.log("Subscribe to Alice account");
        const aliceFreeSub = aliceAcc.data.free;
        console.log(`Alice Account (sub): ${aliceFreeSub}`);
    });
};

// Subscribe events
const subscribeEvents = async (api) => {
    // Subscribe to system events via storage
    api.query.system.events((events) => {
        console.log(`\nReceived ${events.length} events:`);

        // Loop through the Vec<EventRecord>
        events.forEach((record) => {
            // Extract the phase, event and the event types
            const { event, phase } = record;
            const types = event.typeDef;

            // Show what we are busy with
            console.log(`\t${event.section}:${event.method}:: (phase=${phase})`);
            console.log(`\t\t${event.meta.documentation}`);

            // Loop through each of the parameters, displaying the type and data
            event.data.forEach((data, index) => {
                console.log(`\t\t\t${types[index].type}: ${data.toString()}`);
            });
        });
    });
};

// Sleep for a while
const sleep = async (time) => {
    return new Promise(resolve => setTimeout(resolve, time));
};

const main = async () => {
    const api = await connectSubstrate();
    const t = await getConst(api)
    console.log("existentialDeposit " + t)
    // const d = await printBalance(api)
    console.log("Connection executed.")
    await subscribeAlice(api)
    await subscribeEvents(api)
    await sleep(600000)
    console.log("Successfuly exit.")
};
main().then(() => {
    console.log("Connection succefully!");
    process.exit(0);
}).catch((err) => {
    console.log("Connection failed: " + err);
    process.exit(1);
});

运行截图:

substrate_ts's People

Contributors

keikei99 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.