Skip to content

Commit

Permalink
fix with parse U256 to Address
Browse files Browse the repository at this point in the history
  • Loading branch information
NanezX committed Nov 2, 2023
1 parent 6d9de0f commit 477c104
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
34 changes: 18 additions & 16 deletions subgraph/tests/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use utils::{
deploy::{deploy_erc20_mock, get_expression_deployer, get_orderbook, read_orderbook_meta},
events::{
_get_new_expression_event, get_add_order_event, get_after_clear_events, get_clear_events,
get_context_event, get_context_events, get_deposit_events, get_take_order_event,
get_take_order_events, get_withdraw_events,
get_context_event, get_deposit_events, get_take_order_event, get_take_order_events,
get_withdraw_events,
},
generate_random_u256, get_wallet, h256_to_bytes,
json_structs::{NewExpressionJson, OrderJson},
Expand All @@ -28,7 +28,7 @@ use utils::{
generate_signed_context_v1, get_block_data, get_decimals, mint_tokens, ContextIndex,
TestDepositConfig, TestWithdrawConfig,
},
u256_to_bytes,
u256_to_address,
};

use generated::{ClearCall, SignedContextV1, TakeOrderConfig, TakeOrdersConfigV2};
Expand Down Expand Up @@ -3088,31 +3088,33 @@ async fn context_entity_entity_take_order_test() -> anyhow::Result<()> {
// IF it's bigger than the last column, the have signed context
if context.len() > ContextIndex::VaultOutputsColumn as usize {
if let Some(ref resp_signed_contexts) = resp.signed_context {
let signed_context_vec = context[4..].to_vec();
let signed_context_vec = context[5..].to_vec();

let signers: &Vec<U256> = signed_context_vec.first().unwrap();

for index in 1..signed_context_vec.len() {
let signed_context_id = format!("{:?}-{}", take_order_tx_hash, index - 1);
// let current_signer = Address::from_slice(&u256_to_bytes(&signers[index])?);
let current_context = Some(signed_context_vec.get(index).unwrap().to_owned());
assert!(signers.len() == signed_context_vec.len() - 1);

let resp_context = resp_signed_contexts.get(index - 1).unwrap();
for index in 0..signers.len() {
let signed_context_id = format!("{:?}-{}", take_order_tx_hash, index);
let current_signer = u256_to_address(&signers[index]);
let current_context = Some(signed_context_vec.get(index + 1).unwrap().to_owned());

let resp_context = resp_signed_contexts.get(index).unwrap();

assert_eq!(
resp_context.id, signed_context_id,
"missing signed_context. \n- Expected: {}\n- Found: {}",
signed_context_id, resp_context.id
);
// assert_eq!(
// resp_context.signer, current_signer,
// "wrong signer. \n- Expected: {}\n- Found: {}",
// resp_context.signer, current_signer
// );
assert_eq!(
resp_context.signer, current_signer,
"wrong signer. \n- Expected: {:?}\n- Found: {:?}",
resp_context.signer, current_signer
);
assert_eq!(
resp_context.context, current_context,
"wrong context. \n- Expected: {:?}\n- Found: {:?}",
resp_context.context, current_context
"wrong signed context at {}. \n- Expected: {:?}\n- Found: {:?}",
index, resp_context.context, current_context
);
}
} else {
Expand Down
26 changes: 11 additions & 15 deletions subgraph/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub mod setup;
pub mod transactions;

use ethers::{
core::{abi::AbiEncode, k256::ecdsa::SigningKey, rand::random, utils::hex},
core::{k256::ecdsa::SigningKey, rand::random, utils::hex},
providers::Middleware,
signers::{coins_bip39::English, MnemonicBuilder, Wallet},
types::{Bytes, H256, U256, U64},
types::{Address, Bytes, H256, U256, U64},
};
use hex::FromHex;
use rust_bigint::BigInt;
Expand Down Expand Up @@ -161,22 +161,18 @@ pub fn h256_to_bytes(value: &H256) -> Bytes {
}

/// Take a U256 value and parse it to a Bytes
pub fn u256_to_bytes(value: &U256) -> anyhow::Result<Bytes> {
todo!("U256 convert to BYtes missing")
// let hex_string = format!("{:#x}", value);
pub fn u256_to_bytes(value: &U256) -> Bytes {
let mut bb: [u8; 32] = [0; 32];
value.to_big_endian(&mut bb);

// let a = U256::from_dec_str(&value.to_str_radix(16));

// println!("{value:#032X}");
// println!("{value:#032x}");

// println!("{value:#020X}");
// println!("{value:#020x}");
ethers::types::Bytes::from(bb)
}

// let ave = AbiEncode::encode(value.to_owned());
// println!("ave: {:?}", Bytes::from(ave));
/// Take a U256 value and parse it to an Address
pub fn u256_to_address(value: &U256) -> Address {
let bytes = u256_to_bytes(value);

// Ok(hex_string_to_bytes(&hex_string)?)
Address::from_slice(&bytes[12..])
}

/// Get a mock encoded rain document with hardcoded data.
Expand Down

0 comments on commit 477c104

Please sign in to comment.