npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gapi/ethereum

v1.8.117

Published

#### @Gapi Ethereum module @StrongTyped

Downloads

113

Readme

@Gapi Ethereum

@Gapi Ethereum module @StrongTyped

For questions/issues you can write ticket here
This module is intended to be used with GAPI

Installation and basic examples:

To install this Gapi module, run:
$ npm install @gapi/ethereum --save

Consuming @gapi/ethereum

Basic usage

Import inside AppModule or CoreModule

import { Module } from '@rxdi/core';
import { EthereumModule } from '@gapi/ethereum';

@Module({
    imports: [
        EthereumModule.forRoot({
            port: 8545,
            rpc: 'http://localhost',
        }),
    ]
})
export class CoreModule { }

With this simple configuration you are connected to Ethereum Node and you can inject Web3Token and Web3Provider inside your Services, Controllers, Types, etc. There is a typing provided and tested inside this module link

Web3Token

    {
        provide: Web3Token,
        useValue: new Web3(null)
    },

Web3ProviderToken

    {
        provide: Web3ProviderToken,
        deps: [Web3Token],
        useFactory: (web3: Web3Token) => {
            const provider = new web3.providers.HttpProvider(`${config.rpc}:${config.port}`);
            web3.setProvider(provider);
            return provider;
        }
    },

More information you can find here

Usage:

import { Controller } from '@rxdi/core';
import { Web3Token, Web3ProviderToken } from '@gapi/ethereum';

@Controller() // or @Service()
export class EthereumQueriesController {

    constructor(
        @Inject(Web3Token) private web3: Web3Token,
        @Inject(Web3ProviderToken) private provider: Web3ProviderToken
    ) {
        this.web3;
        this.provider;
    }

}

Advanced usage

loading contracts to Gapi Dependency Injection

To Compile Solidity Contracts you can use truffle

$ npm i -g truffle

Example Contracts you can find here inside truffle-metacoin-example

To compile and migrate your contracts type:

$ truffle migrate

From compiled ABI files *.json you need to install TypeChain compiler to Typescript which will help us to create Methods and classes related with specific contracts that we created

Generate modules using TypeChain

To install it type:

$ npm i -g typechain

Use it as folow inside Gapi root project folder

$ typechain --force --outDir src/app/core/contracts './truffle-metacoin-example/build/contracts/*.json'

You can use contracts parameter inside forRoot configuration to import freshly generated contracts mmm.... smellss like Ethereum :D


import { Module } from '@gapi/core';
import { EthereumModule } from '@gapi/ethereum';
import { Coin } from '../core/contracts/Coin';
import { CoinCrowdsale } from '../core/contracts/CoinCrowdsale';

const CoinCrowdsaleABI = require('../../../truffle-metacoin-example/build/contracts/CoinCrowdsale.json');
const CoinABI = require('../../../truffle-metacoin-example/build/contracts/Coin.json');

@Module({
    imports: [
        EthereumModule.forRoot({
            port: process.env.ETHEREUM_PORT || 8545,
            rpc: process.env.ETHEREUM_HOST || 'http://localhost',
            contracts: [
                {
                    contract: Coin,
                    abi: CoinABI
                },
                {
                    contract: CoinCrowdsale,
                    abi: CoinCrowdsaleABI
                }
            ]
        })
    ]
})
export class CoreModule { }

Or you can import your contract like raw TypeChain contracts

import { Module, ModuleWithServices } from '@gapi/core';
import { Web3Token } from '@gapi/ethereum';
import { Coin } from '../core/contracts/Coin';
import { CoinCrowdsale } from '../core/contracts/CoinCrowdsale';

const CoinCrowdsaleABI = require('../../../truffle-metacoin-example/build/contracts/CoinCrowdsale.json');
const CoinABI = require('../../../truffle-metacoin-example/build/contracts/Coin.json');

@Module()
export class ContractsModule {
    public static forRoot(): ModuleWithServices {
        return {
            gapiModule: ContractsModule,
            services: [
                {
                    provide: Coin,
                    deps: [Web3Token],
                    lazy: true,
                    useFactory: async (web3: Web3Token) => {
                        return await Coin.createAndValidate(web3, CoinABI.networks[Object.keys(CoinABI.networks)[0]].address);
                    }
                },
                {
                    provide: CoinCrowdsale,
                    deps: [Web3Token],
                    lazy: true,
                    useFactory: async (web3: Web3Token) => {
                        return await CoinCrowdsale.createAndValidate(web3, CoinCrowdsaleABI.networks[Object.keys(CoinCrowdsaleABI.networks)[0]].address);
                    }
                }
            ]
        };
    }
}

Then import them inside your Core module


import { Module } from '@gapi/core';
import { EthereumModule } from '@gapi/ethereum';
import { ContractsModule } from './ethereum/contracts.module';

@Module({
    imports: [
        EthereumModule.forRoot({
            port: process.env.ETHEREUM_PORT || 8545,
            rpc: process.env.ETHEREUM_HOST || 'http://localhost'
        })
        ContractsModule.forRoot()
    ]
})
export class CoreModule { }

Then use them inside your controller

import {
    Query, GraphQLNonNull, Type,
    Controller, GraphQLInt, Public
} from '@gapi/core';
import { CoinCrowdsale } from '../core/contracts/CoinCrowdsale';

@ObjectType()
export class EthereumCrowdsaleType {
    startTime: number | GraphQLScalarType = GraphQLInt;
    endTime: number | GraphQLScalarType = GraphQLInt;
    hasEnded: boolean | GraphQLScalarType = GraphQLBoolean;
    token: string | GraphQLScalarType = GraphQLString;
    weiRaised: number | GraphQLScalarType = GraphQLInt;
    wallet: string | GraphQLScalarType = GraphQLString;
}


@Controller()
export class EthereumQueriesController {

    constructor(
        private crowdsale: CoinCrowdsale
    ) {}

    @Type(EthereumCrowdsaleType)
    @Public()
    @Query()
    async getCrowdsaleInfo(root, payload, context): Promise<EthereumCrowdsaleType>  {
        const crowdsaleType = {
            startTime: (await this.crowdsale.startTime).toNumber(),
            endTime: (await this.crowdsale.endTime).toNumber(),
            hasEnded: await this.crowdsale.hasEnded,
            token: await this.crowdsale.token,
            weiRaised: (await this.crowdsale.weiRaised).toNumber(),
            wallet: await this.crowdsale.wallet,
        };
        console.log('START TIME: ', crowdsaleType.startTime);
        console.log('END TIME: ', crowdsaleType.endTime);
        console.log('Has Ended: ', crowdsaleType.hasEnded);
        console.log('Token: ', crowdsaleType.token);
        console.log('WeiRaised: ', crowdsaleType.weiRaised);
        console.log('Owner Wallet: ', crowdsaleType.wallet);
        return crowdsaleType;
    }

}

Running private blockchain using Ganache with Docker

Docker

The Simplest way to get started with the Docker image:

docker run -d -p 8545:8545 trufflesuite/ganache-cli:latest

To pass options to ganache-cli through Docker simply add the arguments to the run command:

docker run -d -p 8545:8545 trufflesuite/ganache-cli:latest -a 10 --debug

To build the Docker container from source:

git clone https://github.com/trufflesuite/ganache-cli.git && cd ganache-cli
docker build -t trufflesuite/ganache-cli .

TODO: Better documentation...

Enjoy ! :)