|
| 1 | +# Documentation |
| 2 | + |
| 3 | +The code should be documented enough to make this library easy to use for anyone familiar with blockchain technology and especially the Tendermint engine and the Cosmos SDK. |
| 4 | + |
| 5 | +You can find more details by browsing the [code documentation](./lib). |
| 6 | + |
| 7 | +## Examples |
| 8 | + |
| 9 | +A couple examples to help you get started. |
| 10 | + |
| 11 | +### Imports |
| 12 | + |
| 13 | +```typescript |
| 14 | +import { |
| 15 | + LumWallet, |
| 16 | + LumClient, |
| 17 | + LumTypes, |
| 18 | + LumUtils, |
| 19 | + LumConstants, |
| 20 | + LumMessages, |
| 21 | +} from '@lum-network/sdk-javascript' |
| 22 | +``` |
| 23 | + |
| 24 | +### Create a wallet |
| 25 | + |
| 26 | +#### Mnemonic |
| 27 | +```typescript |
| 28 | +// Create a new cryptographically secure random mnemonic |
| 29 | +const mnemonic = LumUtils.generateMnemonic(12); |
| 30 | + |
| 31 | +// Create a wallet instance based on this fresh mnemonic |
| 32 | +const wallet = await LumWallet.fromMnemonic(mnemonic); |
| 33 | +``` |
| 34 | + |
| 35 | +#### Private key |
| 36 | +```typescript |
| 37 | +// Create a new cryptographically secure random private key |
| 38 | +const privateKey = LumUtils.generatePrivateKey(); |
| 39 | + |
| 40 | +// Create a wallet instance based on this fresh private key |
| 41 | +const wallet = await LumWallet.fromPrivateKey(mnemonic); |
| 42 | +console.log(`Wallet address: ${wallet.address}`); |
| 43 | + |
| 44 | +// Create a wallet instance based on an hexadecimal private key (ex: user input - 0x is optional) |
| 45 | +const hexPrivateKey = '0xb8e62c34928025cdd3aef6cbebc68694b5ad9209b2aff6d3891c8e61d22d3a3b'; |
| 46 | +const existingWallet = await LumWallet.fromPrivateKey(LumUtils.keyFromHex(hexPrivateKey)); |
| 47 | +console.log(`Existing wallet address: ${wallet.address}`); |
| 48 | +``` |
| 49 | + |
| 50 | +#### Keystore |
| 51 | +```typescript |
| 52 | +// Create a random private key for the sake of this example |
| 53 | +const privateKey = LumUtils.generatePrivateKey(); |
| 54 | +// Create a keystore (or consume user input) |
| 55 | +const keystore = LumUtils.generateKeyStore(privateKey, 'some-password'); |
| 56 | +const wallet = await LumWallet.fromKeyStore(keystore, 'some-password'); |
| 57 | +console.log(`Wallet address: ${wallet.address}`); |
| 58 | +``` |
| 59 | + |
| 60 | +### Connect to the testnet |
| 61 | + |
| 62 | +```typescript |
| 63 | +const testnetClient = await LumClient.connect('http://localhost:26657'); |
| 64 | +``` |
| 65 | + |
| 66 | +### Account information & balance |
| 67 | + |
| 68 | +#### Get account information |
| 69 | +```typescript |
| 70 | +const account = await testnetClient.getAccount(wallet.address); |
| 71 | +if (account === null) { |
| 72 | + console.log('Account: not found'); |
| 73 | +} else { |
| 74 | + console.log(`Account: ${account.address}, ${account.accountNumber}, ${account.sequence}`); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +#### Get account balances |
| 79 | +```typescript |
| 80 | +const balances = await testnetClient.getBalancesUnverified(wallet.address); |
| 81 | +if (balances.length === 0) { |
| 82 | + console.log('Balances: empty account'); |
| 83 | +} else { |
| 84 | + console.log( |
| 85 | + `Balances: ${balances.map((coin) => { |
| 86 | + coin.denom + ': ' + coin.amount; |
| 87 | + })}`, |
| 88 | + ); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Transactions |
| 93 | + |
| 94 | +#### Get account transactions (sent and received) |
| 95 | +```typescript |
| 96 | +// The client search feature supports multiple searches and merge+store the results |
| 97 | +const transactions = await testnetClient.searchTx([ |
| 98 | + LumUtils.searchTxFrom(wallet.address), |
| 99 | + LumUtils.searchTxTo(wallet.address), |
| 100 | +]); |
| 101 | +console.log(`Transactions: ${transactions.map((tx) => tx.hash).join(', ')}`); |
| 102 | +``` |
| 103 | + |
| 104 | +#### Send transaction |
| 105 | +```typescript |
| 106 | +// Build transaction message (Send 100 LUM) |
| 107 | +const sendMsg = LumMessages.BuildMsgSend( |
| 108 | + wallet.address, |
| 109 | + toAddress, |
| 110 | + [{ denom: LumConstants.LumDenom, amount: '100' }], |
| 111 | +); |
| 112 | +// Define fees (1 LUM) |
| 113 | +const fee = { |
| 114 | + amount: [{ denom: LumConstants.LumDenom, amount: '1' }], |
| 115 | + gas: '100000', |
| 116 | +}; |
| 117 | +// Sign and broadcast the transaction using the client |
| 118 | +const broadcastResult = await clt.signAndBroadcastTx(w1, [sendMsg], fee, 'hello memo!'); |
| 119 | +// Verify the transaction was succesfully broadcasted and made it into a block |
| 120 | +console.log(`Broadcast success: ${LumUtils.broadcastTxCommitSuccess(broadcastResult)}`); |
| 121 | +``` |
0 commit comments