@makkii/app-eth
v0.1.2
Published
Ethereum application client implemented makkii-core.
Downloads
12
Readme
@makkii/app-eth
Ethereum application client.
This library uses some third-party service:
- Web3 JsonRPC - you can pass in jsonrpc in EthApiClient Constructor
- Explorer Api - To get transaction history, token history, we have two solutions:
http://api.ethplorer.io
http://api.etherscan.io/api
- Transaction Explorer - To show transaction detail page:
https://www.etherchain.org/tx/<txHash>
https://api.etherscan.io/tx/<txHash>
- Remote Api - we setup our own server to provide token list and icons.
Installation
$ npm install @makkii/app-eth
Usage
import { EthApiClient, EthKeystoreClient, EthLocalSigner } from '@makkii/app-eth';
const api_client = new EthApiClient({
network: 'mainnet',
jsonrpc: '***'
});
api_client.getBalance('0x...')
.then(console.log)
.catch(error=>console.log(error));
const keystore_client = new EthKeystoreClient();
api_client.buildTransaction(
'0x...', // from address
'0x...', // to address
0, // amount
{
gasPrice: 10,
gasLimit: 21000,
isTokenTransfer: false
}
).then(function(unsignedTx) {
keystore_client.signTransaction(unsignedTx, new EthLocalSigner(), {
private_key: '***'
}).then(function(signedTx) {
console.log(signedTx);
});
});
API
Table of Contents
EthLocalSinger
Ethereum's signer using private key, implements IkeystoreSigner.
signTransaction
Sign transaction
Parameters
transaction
EthUnsignedTxparams
{private_key: string} parameters object, example: { private_key: '' }}tx
EthUnsignedTx transaction object to sign.
Returns Promise<string> transaction hash string
IEthConfig
Ethereum configuration interface
network
Network name
Type: ("mainnet"
| "ropsten"
)
jsonrpc
JsonRPC endpoint
Type: string
explorer_api
api endpoint that used to query transaction information
Type: {provider: string, url: string, key: string}
explorer
Transaction explorer page
Type: {provider: string, url: string}
remote_api
app server endpoint that provides token, icons, etc.
Type: string
EthKeystoreClient
Ethereum keystore client that implements IsingleKeystoreClient
validatePrivateKey
throws not implemented error
Parameters
getAccountFromMnemonic
Get account from mnemonic
Parameters
Returns any account object: { private_key: '', public_key: '', address: '', index: '' }
EthUnsignedTx
Ethereum unsigned transaction
- to: string;
- from: string;
- nonce: string;
- value: BigNumber;
- gasPrice: number;
- gasLimit: number;
- data?: any;
- network: string;
EthPendingTx
Ethereum pending transaction
- hash: string;
- status: "PENDING";
- to: string;
- from: string;
- value: BigNumber;
- tknTo?: string;
- tknValue: BigNumber;
- gasPrice: number;
- gasLimit: number;
EthApiClient
Ethereum api client that implements IsingleApiFullClient
Parameters
config
IEthConfig
getNetwork
Get network name: mainnet, amity.
getBlockByNumber
Get block by number, block information doesn't contains transaction details
Parameters
blockNumber
string block number's hex string
Returns any eth_getBlockNumber response's result
getBlockNumber
Get latest block number
Returns any eth_getBlockNumber response's result
getTransactionStatus
Get transaction status.
Parameters
hash
string transaction hash
Returns Promise<any> if eth_getTransactionReceipt is null, returns null; else return object { status: true/false, blockNumber: intger, gasUsed: integer }
getTransactionsByAddress
Get transactions by the given address
Parameters
address
string account addresspage
number page numbersize
number page sizetimestamp
number? earlier than this timestamp
Returns any array of object structure which contains:- hash: string, with prefix 0x
- timestamp: milli-seconds from 1970
- from: sender
- to: receiver
- value: transfer amount
- status: 'CONFIRMED' or 'FAILED'
- blockNumber: hex string
- fee: integer
sendTransaction
Send transaction
Parameters
unsignedTx
EthUnsignedTx unsigned transaction build by buildTransactionsigner
T localSigner or hardwaresignerParams
any localSigner: {private_key} hardware:{derivationIndex}
Returns Promise<EthPendingTx>
getTopTokens
Get top tokens
Parameters
topN
number? default 20