Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more json abi tests #89

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ None of these crates would have been possible without the great work done in:

- [`ethers.js`](https://github.com/ethers-io/ethers.js/)
- [`rust-web3`](https://github.com/tomusdrw/rust-web3/)
- [`ruint`](https://github.com/recmo/uint)
- [`ethabi`](https://github.com/rust-ethereum/ethabi)
- [`ethcontract-rs`](https://github.com/gnosis/ethcontract-rs/)
- [`guac_rs`](https://github.com/althea-net/guac_rs/)
- [`ruint`](https://github.com/recmo/uint)
1 change: 1 addition & 0 deletions crates/json-abi/tests/abi/Abiencoderv2Test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[],"name":"defaultPerson","outputs":[{"components":[{"internalType":"string","name":"name","type":"string"},{"internalType":"uint256","name":"age","type":"uint256"}],"internalType":"struct Hello.Person","name":"","type":"tuple"}],"stateMutability":"pure","type":"function"}]
1 change: 1 addition & 0 deletions crates/json-abi/tests/abi/EventWithStruct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"anonymous":false,"inputs":[{"components":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"uint256","name":"b","type":"uint256"}],"indexed":false,"internalType":"struct MyContract.MyStruct","name":"","type":"tuple"},{"indexed":false,"internalType":"uint256","name":"c","type":"uint256"}],"name":"MyEvent","type":"event"}]
1 change: 1 addition & 0 deletions crates/json-abi/tests/abi/LargeArray.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"uint64[128]","name":"longArray","type":"uint64[128]"}],"name":"callWithLongArray","outputs":[],"stateMutability":"view","type":"function"}]
1 change: 1 addition & 0 deletions crates/json-abi/tests/abi/LargeStruct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"bytes32","name":"id","type":"bytes32"}],"name":"getById","outputs":[{"components":[{"internalType":"uint128","name":"x","type":"uint128"},{"internalType":"int24","name":"y","type":"int24"},{"internalType":"int24","name":"z","type":"int24"},{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"int256","name":"b","type":"int256"},{"internalType":"int256","name":"c","type":"int256"},{"internalType":"int256","name":"d","type":"int256"},{"internalType":"uint256","name":"e","type":"uint256"},{"internalType":"uint256","name":"f","type":"uint256"}],"internalType":"struct Many.Info","name":"","type":"tuple"}],"stateMutability":"view","type":"function"}]
1 change: 1 addition & 0 deletions crates/json-abi/tests/abi/LargeStructs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[],"name":"getChainStorage","outputs":[{"components":[{"components":[{"internalType":"uint32","name":"shortFundingBaseRate8H","type":"uint32"},{"internalType":"uint32","name":"shortFundingLimitRate8H","type":"uint32"},{"internalType":"uint32","name":"fundingInterval","type":"uint32"},{"internalType":"uint32","name":"liquidityBaseFeeRate","type":"uint32"},{"internalType":"uint32","name":"liquidityDynamicFeeRate","type":"uint32"},{"internalType":"uint96","name":"mlpPriceLowerBound","type":"uint96"},{"internalType":"uint96","name":"mlpPriceUpperBound","type":"uint96"},{"internalType":"uint32","name":"lastFundingTime","type":"uint32"},{"internalType":"uint32","name":"sequence","type":"uint32"},{"internalType":"uint32","name":"strictStableDeviation","type":"uint32"}],"internalType":"struct IReader.PoolStorage","name":"pool","type":"tuple"},{"components":[{"internalType":"bytes32","name":"symbol","type":"bytes32"},{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"address","name":"muxTokenAddress","type":"address"},{"internalType":"uint8","name":"id","type":"uint8"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"uint56","name":"flags","type":"uint56"},{"internalType":"uint32","name":"initialMarginRate","type":"uint32"},{"internalType":"uint32","name":"maintenanceMarginRate","type":"uint32"},{"internalType":"uint32","name":"positionFeeRate","type":"uint32"},{"internalType":"uint32","name":"liquidationFeeRate","type":"uint32"},{"internalType":"uint32","name":"minProfitRate","type":"uint32"},{"internalType":"uint32","name":"minProfitTime","type":"uint32"},{"internalType":"uint96","name":"maxLongPositionSize","type":"uint96"},{"internalType":"uint96","name":"maxShortPositionSize","type":"uint96"},{"internalType":"uint32","name":"spotWeight","type":"uint32"},{"internalType":"uint32","name":"longFundingBaseRate8H","type":"uint32"},{"internalType":"uint32","name":"longFundingLimitRate8H","type":"uint32"},{"internalType":"uint8","name":"referenceOracleType","type":"uint8"},{"internalType":"address","name":"referenceOracle","type":"address"},{"internalType":"uint32","name":"referenceDeviation","type":"uint32"},{"internalType":"uint32","name":"halfSpread","type":"uint32"},{"internalType":"uint128","name":"longCumulativeFundingRate","type":"uint128"},{"internalType":"uint128","name":"shortCumulativeFunding","type":"uint128"},{"internalType":"uint96","name":"spotLiquidity","type":"uint96"},{"internalType":"uint96","name":"credit","type":"uint96"},{"internalType":"uint96","name":"totalLongPosition","type":"uint96"},{"internalType":"uint96","name":"totalShortPosition","type":"uint96"},{"internalType":"uint96","name":"averageLongPrice","type":"uint96"},{"internalType":"uint96","name":"averageShortPrice","type":"uint96"},{"internalType":"uint128","name":"collectedFee","type":"uint128"},{"internalType":"uint256","name":"deduct","type":"uint256"}],"internalType":"struct IReader.AssetStorage[]","name":"assets","type":"tuple[]"},{"components":[{"internalType":"uint8","name":"dexId","type":"uint8"},{"internalType":"uint8","name":"dexType","type":"uint8"},{"internalType":"uint8[]","name":"assetIds","type":"uint8[]"},{"internalType":"uint32[]","name":"assetWeightInDEX","type":"uint32[]"},{"internalType":"uint256[]","name":"totalSpotInDEX","type":"uint256[]"},{"internalType":"uint32","name":"dexWeight","type":"uint32"},{"internalType":"uint256","name":"dexLPBalance","type":"uint256"},{"internalType":"uint256[]","name":"liquidityBalance","type":"uint256[]"}],"internalType":"struct IReader.DexStorage[]","name":"dexes","type":"tuple[]"},{"internalType":"uint32","name":"liquidityLockPeriod","type":"uint32"},{"internalType":"uint32","name":"marketOrderTimeout","type":"uint32"},{"internalType":"uint32","name":"maxLimitOrderTimeout","type":"uint32"},{"internalType":"uint256","name":"lpDeduct","type":"uint256"},{"internalType":"uint256","name":"stableDeduct","type":"uint256"},{"internalType":"bool","name":"isPositionOrderPaused","type":"bool"},{"internalType":"bool","name":"isLiquidityOrderPaused","type":"bool"}],"internalType":"struct IReader.ChainStorage","name":"chain","type":"tuple"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint64[]","name":"orderIds","type":"uint64[]"}],"name":"getOrders","outputs":[{"internalType":"bytes32[3][]","name":"orders","type":"bytes32[3][]"},{"internalType":"bool[]","name":"isExist","type":"bool[]"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32[]","name":"subAccountIds","type":"bytes32[]"}],"name":"getSubAccounts","outputs":[{"components":[{"internalType":"uint96","name":"collateral","type":"uint96"},{"internalType":"uint96","name":"size","type":"uint96"},{"internalType":"uint32","name":"lastIncreasedTime","type":"uint32"},{"internalType":"uint96","name":"entryPrice","type":"uint96"},{"internalType":"uint128","name":"entryFunding","type":"uint128"}],"internalType":"struct IReader.SubAccountState[]","name":"subAccounts","type":"tuple[]"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32[]","name":"subAccountIds","type":"bytes32[]"},{"internalType":"uint64[]","name":"orderIds","type":"uint64[]"}],"name":"getSubAccountsAndOrders","outputs":[{"components":[{"internalType":"uint96","name":"collateral","type":"uint96"},{"internalType":"uint96","name":"size","type":"uint96"},{"internalType":"uint32","name":"lastIncreasedTime","type":"uint32"},{"internalType":"uint96","name":"entryPrice","type":"uint96"},{"internalType":"uint128","name":"entryFunding","type":"uint128"}],"internalType":"struct IReader.SubAccountState[]","name":"subAccounts","type":"tuple[]"},{"internalType":"bytes32[3][]","name":"orders","type":"bytes32[3][]"},{"internalType":"bool[]","name":"isOrderExist","type":"bool[]"}],"stateMutability":"pure","type":"function"}]
1 change: 1 addition & 0 deletions crates/json-abi/tests/abi/LargeTuple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"uint160","name":"input","type":"uint160"}],"name":"doSomething","outputs":[{"components":[{"internalType":"bytes","name":"output1","type":"bytes"},{"internalType":"bytes","name":"output2","type":"bytes"},{"internalType":"bytes","name":"output3","type":"bytes"},{"internalType":"bytes","name":"output4","type":"bytes"},{"internalType":"bytes","name":"output5","type":"bytes"},{"internalType":"bytes","name":"output6","type":"bytes"},{"internalType":"bytes","name":"output7","type":"bytes"},{"internalType":"bytes","name":"output8","type":"bytes"},{"internalType":"bytes","name":"output9","type":"bytes"},{"internalType":"bytes","name":"output10","type":"bytes"},{"internalType":"bytes","name":"output11","type":"bytes"},{"internalType":"bytes","name":"output12","type":"bytes"},{"internalType":"bytes","name":"output13","type":"bytes"}],"internalType":"struct Contract.Response","name":"","type":"tuple"}],"stateMutability":"view","type":"function"}]
1 change: 1 addition & 0 deletions crates/json-abi/tests/abi/console.json

Large diffs are not rendered by default.

60 changes: 41 additions & 19 deletions crates/json-abi/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
use alloy_json_abi::{AbiJson, Error, Param, SimpleParam};

const JSON: &str = include_str!("./seaport_1_3.abi.json");

#[test]
fn deserialize() {
let abi: AbiJson = serde_json::from_str(JSON).unwrap();
assert_eq!(abi.len(), 67);
}

#[test]
fn round_trip() {
let abi: AbiJson = serde_json::from_str(JSON).unwrap();
let json = serde_json::to_string(&abi).unwrap();
let abi2: AbiJson = serde_json::from_str(&json).unwrap();
assert_eq!(abi, abi2);
}
use alloy_json_abi::{AbiItem, AbiJson, Error, Param, SimpleParam};

#[test]
fn complex_error() {
let err = r#"{
let json = r#"{
"inputs": [
{
"internalType": "string",
Expand All @@ -29,7 +13,7 @@ fn complex_error() {
"name": "SomeName",
"type": "error"
}"#;
let decoded: Error = serde_json::from_str(err).unwrap();
let decoded: Error = serde_json::from_str(json).unwrap();
assert_eq!(
decoded,
Error {
Expand All @@ -42,3 +26,41 @@ fn complex_error() {
}
);
}

macro_rules! abi_parse_tests {
($($name:ident($path:literal, $len:literal))*) => {$(
#[test]
fn $name() {
parse_test(include_str!($path), $len);
}
)*};
}

abi_parse_tests! {
abiencoderv2("abi/Abiencoderv2Test.json", 1)
console("abi/console.json", 379)
event_with_struct("abi/EventWithStruct.json", 1)
large_array("abi/LargeArray.json", 1)
large_struct("abi/LargeStruct.json", 1)
large_structs("abi/LargeStructs.json", 4)
large_tuple("abi/LargeTuple.json", 1)
seaport("abi/Seaport.json", 67)
}

#[track_caller]
fn parse_test(s: &str, len: usize) {
let abi_items: Vec<AbiItem> = serde_json::from_str(s).unwrap();
assert_eq!(abi_items.len(), len);

let json = serde_json::to_string(&abi_items).unwrap();
let abi1: AbiJson = serde_json::from_str(&json).unwrap();

let abi2: AbiJson = serde_json::from_str(s).unwrap();

assert_eq!(abi_items.len(), abi2.len());
assert_eq!(abi1, abi2);

let json = serde_json::to_string(&abi2).unwrap();
let abi3: AbiJson = serde_json::from_str(&json).unwrap();
assert_eq!(abi2, abi3);
}