@ashetm/ng-ether
v15.4.0
Published
``@ashetm/ng-ether`` is a library that simplify manipulation of library ``ethers``, and has a good interaction with Angular with its stream based with ``RXJS``. It provides also a test module that provide mock of ``window.ethereum`` in a token (see below)
Downloads
48
Maintainers
Readme
@ashetm/ng-ether
@ashetm/ng-ether
is a library that simplify manipulation of library ethers
, and has a good interaction with Angular with its stream based with RXJS
. It provides also a test module that provide mock of window.ethereum
in a token (see below) for testing purpose.
Install
You can install it with npm:
npm install @ashetm/ng-ether
Import
You only need to import EtherModule
through forRoot
static method.
...
import { EtherModule } from '@ashetm/ng-ether';
...
@NgModule({
...
imports: [
...
EtherModule.forRoot(),
...
]
...
})
export class AppModule { }
API
@ashetm/ng-ether
exposes the following:
Modules
EtherModule
, that containsforRoot
static method to import in order to use the library withTEtherConfigurationRoot
optional configuration with abi and contract address of a deployed smart contractEtherTestModule
, that provide a mock ofwindow.ethereum
throughETHER_TOKEN
(See below) for testing purpose
Tokens
Tokens are InjectionToken<T>
that needs to be injected in order to get its value
ETHER_TOKEN
returnswindow.ethereum
object, if you wish a direct manipulationETHER_TOKEN_ABI
returns stringabi
provided in configuration forRoot static methodETHER_TOKEN_ADDRESS_CONTRACT
returnsaddressContract
string provided in configuration forRoot static methodETHER_TOKEN_IS_METAMASK_WALLET
returns boolean, true if MetaMask wallet is installed in browserETHER_TOKEN_IS_WALLET_INSTALLED
returns boolean, true if the wallet is installed in browserETHER_TOKEN_NETWORK_ID
returnsnetworkId
string provided in configuration forRoot static method, default value isany
Services
EtherNetworkService
EtherNetworkService
is a service concern all about network.
network$
isObservable<TEtherNetwork>
, it emitsTEtherNetwork
value of network information
Those following are Observable
and emits when account or network change
blockNumber$
isObservable<number>
that emits anumber
value of number of blockgasFee$
isObservable<TEtherBigNumber>
that emits aTEtherBigNumber
value, it concerns gas price of the current networkprice$
isObservable<number>
, it emitsnumber
value of price of ETH
And for methods, there is the following:
addNetwork(newNetwork: TEtherNetwork)
returnsObservable<true | TEtherError>
listen to if a new network is created successfully and emits a value, of type eithertrue
if success orTEtherNetworkChange = { newNetwork: TEtherNetwork; oldNetwork?: TEtherNetwork; }
for failure.onNetworkChange()
returnsObservable<TEtherNetworkChange>
listen to network change event and emitsTEtherNetworkChange = { newNetwork: TEtherNetwork; oldNetwork?: TEtherNetwork; }
value that contains informations of new network and old network.
or the second definition
onNetworkChange(fn: (newNetwork: TEtherNetwork, oldNetwork?: TEtherNetwork) => void)
returnsvoid
nothing, you need to provide a function with 2 arguments; first arguments is informùation of new network and second is information of old network.switchNetwork(chainId: EEtherNetworkChainId)
returnsObservable<true | TEtherError>
listen to if the switch to a specified network is done successfully and emits a value, of type eithertrue
if success orTEtherNetworkChange = { newNetwork: TEtherNetwork; oldNetwork?: TEtherNetwork; }
for failure.
EtherTransactionService
EtherTransactionService
is a service concern all about transaction.
For methods, there is the following:
prepareTransaction(addressWallet: string)
returnsEtherTransactionRef
that contains 3 methods:sendEth(amount: number)
,sendGWei(amount: number)
andsendWei(amount: number)
that you specify amount to send with its appropriate unit; each of one returnsObservable<TEtherTransactionResponse>
. It launch to sending transaction after subscribing to the obserable.
EtherWalletService
EtherWalletService
is a service concern all about wallet.
account$
isObservable<string>
that emits the address wallet of the connected accountbalance$
isObservable<TEtherBigNumber>
that emits aTEtherBigNumber
value, it concerns balance of the connected wallettransactionCount$
isObservable<number>
, it emitsnumber
value of transaction count
And for methods, there is the following:
connectWallet()
returnsObservable<string[]>
tries to connect the current wallet and returns the current connected wallet in arraygetBalanceOf(addressWallet: string)
isObservable<TEtherBigNumber>
that emits balance information of the provided address walletgetTransactionCountOf(addressWallet: string)
isObservable<number>
that emits transaction count of the provided address wallet
And listeners event, there is the following with 2 definitions each:
onAccountChange()
returnsObservable<string[]>
that emits list of wallet connected, if for example we switch from an account/address wallet to another one.
Or with second definition
onAccountChange(fn: (accounts: string[]) => void)
returnsvoid
, you need to provide a callback function with 1 arguments of account/address wallet list, it does emit list of wallet connected, if for example we switch from an account/address wallet to another one.onConnect()
returnsObservable<boolean>
that emitstrue
if wallet is connected,false
if not
Or with second definition
onConnect(fn: (isConnected: boolean) => void)
returnsvoid
, you need to provide a callback function with 1 arguments of boolean value is account connected or not;true
if wallet is connected,false
if notonDisconnect()
returnsObservable<void>
that emits avoid
value if wallet has disconnected
Or with second definition
onDisconnect(fn: () => void)
returnsvoid
, you need to provide a callback function without arguments; that emits avoid
value if wallet has disconnected
Classes
AEtherContract
AEtherContract
is an abstract class and a token to either to implement with 0 meethod to implement or to inject, the second case it will give you an instance of Contract.
Note: This abstract class is available if TEtherConfigurationRoot
is provided.
AEtherProvider
AEtherProvider
is an abstract class and a token to either to implement with 0 meethod to implement or to inject, the second case it will give you an instance of Web3Provider.
AEtherSigner
AEtherSigner
is an abstract class and a token to either to implement with 0 meethod to implement or to inject, the second case it will give you an instance of JsonRpcSigner.
ACustomContract
ACustomContract
is an abstract class to implement with your own custom smart contract call or transaction method, with Observable
as return type. It has a super protected member calld contract
that gives you access to the deployed smart contract provided in forRoot static method configuration.
Decorators
ContractInjectable
ContractInjectable
is a class decorator, it must annotates a class that extends ACustomContract
. It has a parameter just like Injectable
a scope to provide it in, by default is root
.
Usage
Use own deployed smart contract
You can interact with your custom deployed contract, like in the example below:
...
import { ACustomContract, ContractInjectable } from '@ashetm/ng-ether';
...
@ContractInjectable() // Compulsory, to annotate it
export class CustomContract extends ACustomContract { // Compulsory, extends ACustomContract
test(): Observable<any> {
return from(this.contract["decimals"]());
}
}
Issue
LOOKING FOR MAINTAINER OR IF THERE IS AN ISSUE OR ANY IDEA TO ADD. PLEASE CREATE ISSUE IN GITHUB REPOSITORY.