|
| 1 | +# namada-sdk |
| 2 | + |
| 3 | +The Namada SDK package |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +### Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @namada/sdk |
| 11 | + |
| 12 | +# Or, via yarn |
| 13 | +yarn add @namada/sdk |
| 14 | +``` |
| 15 | + |
| 16 | +### Initializing the SDK |
| 17 | + |
| 18 | +As this package depends on Wasm compiled from Rust to integrate with Namada, this must be loaded asynchronously when |
| 19 | +developing for the Web. The following is a quick overview of some of the features of the SDK package: |
| 20 | + |
| 21 | +```typescript |
| 22 | +import { Sdk, getSdk } from "@namada/sdk/web"; |
| 23 | +import sdkInit from "@namada/sdk/web-init"; |
| 24 | + |
| 25 | +// Load Tx props from types package |
| 26 | +import { BondProps, WrapperTxProps } from "@namada/types"; |
| 27 | + |
| 28 | +// Amounts are defined as BigNumber |
| 29 | +import BigNumber from "bignumber.js"; |
| 30 | + |
| 31 | +// Define the following values: |
| 32 | +const rpcUrl = "https://rpc.example.net"; |
| 33 | +const tokenAddress = "tnam1qxgfw7myv4dh0qna4hq0xdg6lx77fzl7dcem8h7e"; // bech32m encoded NAM address |
| 34 | +const chainId = "namada-testnet-1"; |
| 35 | + |
| 36 | +const init = async (): Promise<void> => { |
| 37 | + const { cryptoMemory } = await sdkInit(); |
| 38 | + const sdk = getSdk(rpcUrl, tokenAddress, cryptoMemory); |
| 39 | + |
| 40 | + // Access various modules of the SDK |
| 41 | + const { keys, mnemonic, rpc, signing, tx } = sdk; // Alternatively, invoke getters directly, e.g., sdk.getRpc(), etc. |
| 42 | + |
| 43 | + // Examples: |
| 44 | + |
| 45 | + // Generate a 24 word mnemonic |
| 46 | + const mnemonicPhrase = mnemonic.generate(24).join(" "); |
| 47 | + |
| 48 | + // Mnemonic to seed |
| 49 | + const seed = mnemonic.toSeed(mnemonicPhrase); |
| 50 | + |
| 51 | + // Derive a keypair and address |
| 52 | + const bip44Path = { |
| 53 | + account: 0, |
| 54 | + change: 0, |
| 55 | + index: 0, |
| 56 | + }; |
| 57 | + const { address, publicKey, privateKey } = keys.deriveFromSeed( |
| 58 | + seed, |
| 59 | + bip44Path |
| 60 | + ); |
| 61 | + |
| 62 | + // Construct a Bond transaction |
| 63 | + const bondProps: BondProps = { |
| 64 | + source: address, |
| 65 | + validator: "tnam1q9vhfdur7gadtwx4r223agpal0fvlqhywylf2mzx", |
| 66 | + amount: BigNumber(123), |
| 67 | + }; |
| 68 | + |
| 69 | + // Define Wrapper Tx props |
| 70 | + const wrapperTxProps: WrapperTxProps = { |
| 71 | + token: tokenAddress, |
| 72 | + feeAmount: BigNumber(1), |
| 73 | + gasLimit: BigNumber(1000), |
| 74 | + chainId: "", |
| 75 | + publicKey, |
| 76 | + memo: "A bond transaction", |
| 77 | + }; |
| 78 | + |
| 79 | + // Build a Bond transaction |
| 80 | + const bondTx = await tx.buildBond(bondProps, wrapperTxProps); |
| 81 | + |
| 82 | + // Sign a Bond transaction with a private key |
| 83 | + const signedBondTxBytes = await signing.sign(bondTx, privateKey); |
| 84 | + |
| 85 | + // Broadcast the signed transaction to a node |
| 86 | + const txResponse = await rpc.broadcastTx(signedBondTxBytes, wrapperTxProps); |
| 87 | +}; |
| 88 | + |
| 89 | +init(); |
| 90 | +``` |
| 91 | + |
| 92 | +### Types |
| 93 | + |
| 94 | +Explore the generated type docs here: [modules.md](./docs/modules.md) |
| 95 | + |
| 96 | +## Development |
| 97 | + |
| 98 | +In order to compile the required Rust libraries locally (`@namada/crypto` and `@namada/shared`), issue one |
| 99 | +of the following commands, depending on the environment you are targeting: |
| 100 | + |
| 101 | +```bash |
| 102 | +# Build wasm for single core, release mode |
| 103 | +yarn wasm:build[:node] |
| 104 | + |
| 105 | +# Build wasm for multicore, release mode |
| 106 | +yarn wasm:build[:node]:multicore |
| 107 | + |
| 108 | +# Build wasm with debugging for single core |
| 109 | +yarn wasm:build[:node]:dev |
| 110 | + |
| 111 | +# Build wasm with debugging for multicore |
| 112 | +yarn wasm:build[:node]:dev:multicore |
| 113 | + |
| 114 | +# Generate new docs |
| 115 | +yarn build:docs |
| 116 | +``` |
0 commit comments