@ashetm/ng-ether v15.4.0
@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-etherImport
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 containsforRootstatic method to import in order to use the library withTEtherConfigurationRootoptional configuration with abi and contract address of a deployed smart contractEtherTestModule, that provide a mock ofwindow.ethereumthroughETHER_TOKEN(See below) for testing purpose
Tokens
Tokens are InjectionToken<T> that needs to be injected in order to get its value
ETHER_TOKENreturnswindow.ethereumobject, if you wish a direct manipulationETHER_TOKEN_ABIreturns stringabiprovided in configuration forRoot static methodETHER_TOKEN_ADDRESS_CONTRACTreturnsaddressContractstring provided in configuration forRoot static methodETHER_TOKEN_IS_METAMASK_WALLETreturns boolean, true if MetaMask wallet is installed in browserETHER_TOKEN_IS_WALLET_INSTALLEDreturns boolean, true if the wallet is installed in browserETHER_TOKEN_NETWORK_IDreturnsnetworkIdstring provided in configuration forRoot static method, default value isany
Services
EtherNetworkService
EtherNetworkService is a service concern all about network.
network$isObservable<TEtherNetwork>, it emitsTEtherNetworkvalue of network information
Those following are Observable and emits when account or network change
blockNumber$isObservable<number>that emits anumbervalue of number of blockgasFee$isObservable<TEtherBigNumber>that emits aTEtherBigNumbervalue, it concerns gas price of the current networkprice$isObservable<number>, it emitsnumbervalue 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 eithertrueif 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)returnsvoidnothing, 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 eithertrueif 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)returnsEtherTransactionRefthat 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 aTEtherBigNumbervalue, it concerns balance of the connected wallettransactionCount$isObservable<number>, it emitsnumbervalue 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 emitstrueif wallet is connected,falseif 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;trueif wallet is connected,falseif notonDisconnect()returnsObservable<void>that emits avoidvalue if wallet has disconnected
Or with second definition
onDisconnect(fn: () => void)returnsvoid, you need to provide a callback function without arguments; that emits avoidvalue 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.
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago