|
| 1 | +--- |
| 2 | +tags: ["multitest", "AppBuilder"] |
| 3 | +--- |
| 4 | + |
| 5 | +[AppBuilder]: https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.AppBuilder.html |
| 6 | +[Builder Pattern]: https://en.wikipedia.org/wiki/Builder_pattern |
| 7 | +[BlockInfo]: https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.BlockInfo.html |
| 8 | +[with_block]: |
| 9 | + https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.AppBuilder.html#method.with_block |
| 10 | +[build]: #build |
| 11 | + |
| 12 | +# `AppBuilder` |
| 13 | + |
| 14 | +[`AppBuilder{:rust}`][AppBuilder] is an implementation of the [Builder Pattern] that provides a flexible |
| 15 | +and modular way to construct the [`App{:rust}`](app) blockchain simulator. It allows smart contract developers |
| 16 | +to configure various components of the blockchain simulator (e.g., Bank, Staking, Gov, IBC) individually |
| 17 | +through dedicated `with_*` methods. Each method modifies and returns a new instance of the builder, enabling |
| 18 | +method chaining for a fluent interface. The [`build{:rust}`][build] method finalizes the construction |
| 19 | +process, ensuring that all components are correctly initialized and integrated. |
| 20 | + |
| 21 | +The following sections detail all builder functions, providing specific usage examples. |
| 22 | + |
| 23 | +## `default` |
| 24 | + |
| 25 | +(WIP) |
| 26 | + |
| 27 | +## `new` |
| 28 | + |
| 29 | +(WIP) |
| 30 | + |
| 31 | +## `new_custom` |
| 32 | + |
| 33 | +(WIP) |
| 34 | + |
| 35 | +## `with_api` |
| 36 | + |
| 37 | +(WIP) |
| 38 | + |
| 39 | +## `with_bank` |
| 40 | + |
| 41 | +(WIP) |
| 42 | + |
| 43 | +## `with_block` |
| 44 | + |
| 45 | +While the default block configuration is sufficient for most test cases, you can initialize the |
| 46 | +chain with a custom [`BlockInfo{:rust}`][BlockInfo] using the [`with_block{:rust}`][with_block] |
| 47 | +method provided by [`AppBuilder{:rust}`][AppBuilder]. |
| 48 | + |
| 49 | +The following example demonstrates this use case in detail. |
| 50 | + |
| 51 | +```rust showLineNumbers copy {15} /with_block/ |
| 52 | +use cosmwasm_std::{BlockInfo, Timestamp}; |
| 53 | +use cw_multi_test::{no_init, AppBuilder}; |
| 54 | + |
| 55 | +// create the chain builder |
| 56 | +let builder = AppBuilder::default(); |
| 57 | + |
| 58 | +// prepare the custom block |
| 59 | +let block = BlockInfo { |
| 60 | + height: 1, |
| 61 | + time: Timestamp::from_seconds(1723627489), |
| 62 | + chain_id: "starship-testnet".to_string(), |
| 63 | +}; |
| 64 | + |
| 65 | +// build the chain initialized with the custom block |
| 66 | +let app = builder.with_block(block).build(no_init); |
| 67 | + |
| 68 | +// get the current block properties |
| 69 | +let block = app.block_info(); |
| 70 | + |
| 71 | +// now the block height is 21 |
| 72 | +assert_eq!(1, block.height); |
| 73 | + |
| 74 | +// now the block timestamp is Wed Aug 14 2024 09:24:49 GMT+0000 |
| 75 | +assert_eq!(1723627489, block.time.seconds()); |
| 76 | + |
| 77 | +// now the chain identifier is "starship-testnet" |
| 78 | +assert_eq!("starship-testnet", block.chain_id); |
| 79 | +``` |
| 80 | + |
| 81 | +The [`AppBuilder{:rust}`][AppBuilder] is initialized with default settings in line 5. A custom block |
| 82 | +is created in lines 8-12 and passed to the [`with_block{:rust}`][with_block] method in line 15. |
| 83 | +Since this is the only customization in this example, the blockchain construction is finalized in |
| 84 | +the same line by calling the [`build{:rust}`][build] method. |
| 85 | + |
| 86 | +The current block metadata is retrieved in line 18, followed by value checks: |
| 87 | + |
| 88 | +- line 21: the block height is now `1{:rust}`, |
| 89 | +- line 24: the block time is set to the Unix timestamp `1723627489{:rust}`, representing |
| 90 | + `Wednesday, August 14, 2024, 09:24:49 GMT`, |
| 91 | +- line 27: the chain identifier is now `"starship-testnet"{:rust}`. |
| 92 | + |
| 93 | +The [`with_block{:rust}`][with_block] method of [`AppBuilder{:rust}`][AppBuilder] can be combined |
| 94 | +with any other `with_*` methods to configure a custom starting block. |
| 95 | + |
| 96 | +## `with_custom` |
| 97 | + |
| 98 | +(WIP) |
| 99 | + |
| 100 | +## `with_distribution` |
| 101 | + |
| 102 | +(WIP) |
| 103 | + |
| 104 | +## `with_gov` |
| 105 | + |
| 106 | +(WIP) |
| 107 | + |
| 108 | +## `with_ibc` |
| 109 | + |
| 110 | +(WIP) |
| 111 | + |
| 112 | +## `with_staking` |
| 113 | + |
| 114 | +(WIP) |
| 115 | + |
| 116 | +## `with_stargate` |
| 117 | + |
| 118 | +(WIP) |
| 119 | + |
| 120 | +## `with_storage` |
| 121 | + |
| 122 | +(WIP) |
| 123 | + |
| 124 | +## `with_wasm` |
| 125 | + |
| 126 | +(WIP) |
| 127 | + |
| 128 | +## `build` |
| 129 | + |
| 130 | +(WIP) |
0 commit comments