我正在 Factory 合约中创建合约 (Exchange),我想通过 Web3 访问 Factory 合约。
?
例如,我想添加流动性,为此我需要访问 Exchange 合约。但在我想通过工厂合同中的 getExchange() 函数检查 Exchange() 合同是否已经创建之前。
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "./Exchange.sol";
contract Factory {
mapping(address => address) public tokenToExchange;
function createExchange(address _tokenAddress) public returns (address) {
require(_tokenAddress != address(0), "invalid token address");
require(
tokenToExchange[_tokenAddress] == address(0),
"exchange already exists"
);
Exchange exchange = new Exchange(_tokenAddress);
tokenToExchange[_tokenAddress] = address(exchange);
return address(exchange);
}
function getExchange(address _tokenAddress) public view returns (address) {
return tokenToExchange[_tokenAddress];
}
}
如果我运行函数 getExchange(tokenAddress) 总是返回 address(0) 0x0000000000000000000000000000000000000000 的地址