Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

fix(abigen): SolStruct derives edge case #2335

Merged
merged 3 commits into from
Apr 12, 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
9 changes: 5 additions & 4 deletions ethers-contract/ethers-contract-abigen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,12 @@ fn _derive_builtin_traits_struct(
for (field, ty) in fields.iter().zip(params) {
match &field.ty {
FieldType::Struct(s_ty) => {
// a tuple here is actually a sol struct so we skip it
if !matches!(ty, ParamType::Tuple(_)) {
*def &= can_derive_default(ty);
*others &= can_derive_builtin_traits(ty);
// `ty` here can only be `Tuple`, `Array(Tuple)`, or `FixedArray(Tuple(), len)`.
// We recurse on the Tuple's fields and check the FixedArray's length.
if let StructFieldType::FixedArray(_, len) = s_ty {
*def &= *len <= MAX_SUPPORTED_ARRAY_LEN;
}

let id = s_ty.identifier();
// TODO: InternalStructs does not contain this field's ID if the struct and field
// are in 2 different modules, like in `can_generate_internal_structs_multiple`
Expand Down
9 changes: 9 additions & 0 deletions ethers-contract/tests/it/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,15 @@ fn can_generate_large_output_struct() {
let _r = GetByIdReturn(Info::default());
}

#[test]
fn can_generate_large_structs() {
abigen!(LargeStructs, "ethers-contract/tests/solidity-contracts/LargeStructs.json");

assert_struct::<PoolStorage>();
assert_struct::<AssetStorage>();
assert_struct::<ChainStorage>();
}

#[test]
fn can_generate_complex_function() {
abigen!(
Expand Down
1 change: 1 addition & 0 deletions ethers-contract/tests/solidity-contracts/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"}]