-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
101 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1014,4 +1014,3 @@ mod tests { | |
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ pub mod api; | |
|
||
pub use crate::create_contract_wrappers; | ||
pub use runner::MultiTestRunner; | ||
pub mod test_addresses; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use anyhow::Result; | ||
use apollo_cw_multi_test::AddressGenerator; | ||
use cosmwasm_std::{instantiate2_address, Addr, Api, CanonicalAddr, Storage}; | ||
use sha2::digest::Update; | ||
use sha2::{Digest, Sha256}; | ||
#[derive(Default)] | ||
pub struct MockAddressGenerator; | ||
|
||
impl AddressGenerator for MockAddressGenerator { | ||
fn contract_address( | ||
&self, | ||
api: &dyn Api, | ||
_storage: &mut dyn Storage, | ||
code_id: u64, | ||
instance_id: u64, | ||
) -> Result<Addr> { | ||
let canonical_addr = Self::instantiate_address(code_id, instance_id); | ||
Ok(Addr::unchecked(api.addr_humanize(&canonical_addr)?)) | ||
} | ||
|
||
fn predictable_contract_address( | ||
&self, | ||
api: &dyn Api, | ||
_storage: &mut dyn Storage, | ||
_code_id: u64, | ||
_instance_id: u64, | ||
checksum: &[u8], | ||
creator: &CanonicalAddr, | ||
salt: &[u8], | ||
) -> Result<Addr> { | ||
let canonical_addr = instantiate2_address(checksum, creator, salt)?; | ||
Ok(Addr::unchecked(api.addr_humanize(&canonical_addr)?)) | ||
} | ||
} | ||
|
||
impl MockAddressGenerator { | ||
// non-predictable contract address generator, see `BuildContractAddressClassic` | ||
// implementation in wasmd: https://github.com/CosmWasm/wasmd/blob/main/x/wasm/keeper/addresses.go#L35-L42 | ||
fn instantiate_address(code_id: u64, instance_id: u64) -> CanonicalAddr { | ||
let mut key = Vec::<u8>::new(); | ||
key.extend_from_slice(b"wasm\0"); | ||
key.extend_from_slice(&code_id.to_be_bytes()); | ||
key.extend_from_slice(&instance_id.to_be_bytes()); | ||
let module = Sha256::digest("module".as_bytes()); | ||
Sha256::new() | ||
.chain(module) | ||
.chain(key) | ||
.finalize() | ||
.to_vec() | ||
.into() | ||
} | ||
} |