forked from jl777/SuperNET
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement "Mixed-case checksum address encoding" for ETH. #394
- Loading branch information
1 parent
dbcd12e
commit 0039f23
Showing
5 changed files
with
166 additions
and
92 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
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,102 @@ | ||
use super::*; | ||
|
||
fn check_sum(addr: &str, expected: &str) { | ||
let actual = checksum_address(addr); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#test-cases | ||
fn test_check_sum_address() { | ||
check_sum("0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"); | ||
check_sum("0x52908400098527886e0f7030069857d2e4169ee7", "0x52908400098527886E0F7030069857D2E4169EE7"); | ||
check_sum("0x8617e340b3d01fa5f11f306f4090fd50e238070d", "0x8617E340B3D01FA5F11F306F4090FD50E238070D"); | ||
check_sum("0xde709f2102306220921060314715629080e2fb77", "0xde709f2102306220921060314715629080e2fb77"); | ||
check_sum("0x27b1fdb04752bbc536007a920d24acb045561c26", "0x27b1fdb04752bbc536007a920d24acb045561c26"); | ||
check_sum("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"); | ||
check_sum("0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"); | ||
check_sum("0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"); | ||
check_sum("0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"); | ||
} | ||
|
||
#[test] | ||
fn test_is_valid_checksum_addr() { | ||
assert!(is_valid_checksum_addr("0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359")); | ||
assert!(is_valid_checksum_addr("0x52908400098527886E0F7030069857D2E4169EE7")); | ||
assert!(!is_valid_checksum_addr("0x8617e340B3D01FA5F11F306F4090FD50E238070D")); | ||
assert!(!is_valid_checksum_addr("0xd1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb")); | ||
} | ||
|
||
#[test] | ||
fn display_u256_with_point() { | ||
let number = U256::from_dec_str("1000000000000000000").unwrap(); | ||
let string = display_u256_with_decimal_point(number, 18); | ||
assert_eq!("1.", string); | ||
|
||
let number = U256::from_dec_str("1234567890000000000").unwrap(); | ||
let string = display_u256_with_decimal_point(number, 18); | ||
assert_eq!("1.23456789", string); | ||
|
||
let number = U256::from_dec_str("1234567890000000000").unwrap(); | ||
let string = display_u256_with_decimal_point(number, 16); | ||
assert_eq!("123.456789", string); | ||
|
||
let number = U256::from_dec_str("1234567890000000000").unwrap(); | ||
let string = display_u256_with_decimal_point(number, 0); | ||
assert_eq!("1234567890000000000.", string); | ||
|
||
let number = U256::from_dec_str("1000000000000000").unwrap(); | ||
let string = display_u256_with_decimal_point(number, 18); | ||
assert_eq!("0.001", string); | ||
|
||
let number = U256::from_dec_str("0").unwrap(); | ||
let string = display_u256_with_decimal_point(number, 18); | ||
assert_eq!("0.", string); | ||
|
||
let number = U256::from_dec_str("0").unwrap(); | ||
let string = display_u256_with_decimal_point(number, 0); | ||
assert_eq!("0.", string); | ||
} | ||
|
||
#[test] | ||
fn test_wei_from_f64() { | ||
let amount = 0.000001; | ||
let wei = wei_from_f64(amount, 18).unwrap(); | ||
let expected_wei: U256 = 1000000000000u64.into(); | ||
assert_eq!(expected_wei, wei); | ||
|
||
let amount = 1.000001; | ||
let wei = wei_from_f64(amount, 18).unwrap(); | ||
let expected_wei: U256 = 1000001000000000000u64.into(); | ||
assert_eq!(expected_wei, wei); | ||
|
||
let amount = 1.; | ||
let wei = wei_from_f64(amount, 18).unwrap(); | ||
let expected_wei: U256 = 1000000000000000000u64.into(); | ||
assert_eq!(expected_wei, wei); | ||
|
||
let amount = 0.000000000000000001; | ||
let wei = wei_from_f64(amount, 18).unwrap(); | ||
let expected_wei: U256 = 1u64.into(); | ||
assert_eq!(expected_wei, wei); | ||
|
||
let amount = 1234.; | ||
let wei = wei_from_f64(amount, 9).unwrap(); | ||
let expected_wei: U256 = 1234000000000u64.into(); | ||
assert_eq!(expected_wei, wei); | ||
|
||
let amount = 1234.; | ||
let wei = wei_from_f64(amount, 0).unwrap(); | ||
let expected_wei: U256 = 1234u64.into(); | ||
assert_eq!(expected_wei, wei); | ||
|
||
let amount = 1234.; | ||
let wei = wei_from_f64(amount, 1).unwrap(); | ||
let expected_wei: U256 = 12340u64.into(); | ||
assert_eq!(expected_wei, wei); | ||
|
||
let amount = 1234.12345; | ||
let wei = wei_from_f64(amount, 1).unwrap(); | ||
let expected_wei: U256 = 12341u64.into(); | ||
assert_eq!(expected_wei, wei); | ||
} |
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