如何从另一个合约中读取结构数组 (UniswapV3 TickLens.sol)
How to read array of structs from another contract in solidity (UniswapV3 TickLens.sol)
如果我要读取的数据是结构体数组,如何读取contractA
在contractB
中的数据?
我想从 ContractA
:
中获取数据
function getPopulatedTicksInWord(address pool, int16 tickBitmapIndex)
public
view
override
returns (PopulatedTick[] memory populatedTicks)
link 到合约:https://etherscan.io/address/0xbfd8137f7d1516D3ea5cA83523914859ec47F573#code
(这是TickLens.sol中唯一的功能)
我在 ContractB 中尝试了以下操作,它将调用 ContractA 的 getPopulatedTicksInWord,然后 return 在我的代码中进一步调用结构数组:
contract Lib_UniswapV3
{
...
struct PopulatedTick {
int24 tick;
int128 liquidityNet;
uint128 liquidityGross;
}
}
abstract contract UniswapV3TickLens is Lib_UniswapV3
{
function getPopulatedTicksInWord(address poolAddress, int16 tickBitmapIndex) external virtual returns (PopulatedTick[] memory);
}
contract ContractB is
Lib_UniswapV3
{
function getExchangePriceInputData(
address poolAddress
)
public
returns (PriceInputData[] memory priceInputData)
{
...
PopulatedTick[] memory ticks;
(ticks) = UniswapV3TickLens(0xbfd8137f7d1516D3ea5cA83523914859ec47F573).getPopulatedTicksInWord(address(0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8), int16(12));
...
}
}
目前,我只是对输入数据进行了硬编码。如果您执行该调用(在 etherscan 上),您将收到该结构数组。
通过
解决了
- 您想将 ContractA 的 getPopulatedTicksInWord 函数实现到您的 contractB 中,而不仅仅是从 ContractB 中调用它。
- 将其标记为内部
- 如果使用安全帽,请不要使用会产生大量调用的输入参数进行测试。它无法处理太多请求。
如果我要读取的数据是结构体数组,如何读取contractA
在contractB
中的数据?
我想从 ContractA
:
function getPopulatedTicksInWord(address pool, int16 tickBitmapIndex)
public
view
override
returns (PopulatedTick[] memory populatedTicks)
link 到合约:https://etherscan.io/address/0xbfd8137f7d1516D3ea5cA83523914859ec47F573#code (这是TickLens.sol中唯一的功能)
我在 ContractB 中尝试了以下操作,它将调用 ContractA 的 getPopulatedTicksInWord,然后 return 在我的代码中进一步调用结构数组:
contract Lib_UniswapV3
{
...
struct PopulatedTick {
int24 tick;
int128 liquidityNet;
uint128 liquidityGross;
}
}
abstract contract UniswapV3TickLens is Lib_UniswapV3
{
function getPopulatedTicksInWord(address poolAddress, int16 tickBitmapIndex) external virtual returns (PopulatedTick[] memory);
}
contract ContractB is
Lib_UniswapV3
{
function getExchangePriceInputData(
address poolAddress
)
public
returns (PriceInputData[] memory priceInputData)
{
...
PopulatedTick[] memory ticks;
(ticks) = UniswapV3TickLens(0xbfd8137f7d1516D3ea5cA83523914859ec47F573).getPopulatedTicksInWord(address(0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8), int16(12));
...
}
}
目前,我只是对输入数据进行了硬编码。如果您执行该调用(在 etherscan 上),您将收到该结构数组。
通过
解决了- 您想将 ContractA 的 getPopulatedTicksInWord 函数实现到您的 contractB 中,而不仅仅是从 ContractB 中调用它。
- 将其标记为内部
- 如果使用安全帽,请不要使用会产生大量调用的输入参数进行测试。它无法处理太多请求。