我有两个智能合约,DEXUserCoin
和DEXTransferCoinUserToUser
。
我想使用从DEXUserCoin
到DEXTransferCoinUserToUser
的一些函数。
DEXUserCoin:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import './DEXCoin.sol';
contract DEXUserCoin {
struct UserCoin {
uint coinId;
uint256 amount;
}
mapping(address => UserCoin) internal userCoins;
constructor() {
}
function ApproveTransferSend(address userAddress , uint coinId , uint256 amount) external view returns(bool) {
if(userCoins[userAddress].amount >= amount && userCoins[userAddress].coinId == coinId) {
return true;
} else {
return false;
}
}
}
需要在DEXTransferCoinUserToUser
中使用ApproveTransferSend
,我尝试这样做:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import './DEXUserCoin.sol';
contract DEXTransferCoinUserToUser {
DEXUserCoin private desxUserCoin;
constructor(){}
function TransferUserToUser(address from , address to ,uint coinId , uint256 amount)
public view returns(bool) {
return desxUserCoin.ApproveTransferSend(from,coinId,amount);
}
}
您只是在导入但没有继承任何东西:
contract DEXTransferCoinUserToUser is DEXUserCoin {
}
或者必须在构造函数中设置desxUserCoin
值
constructor (DEXUserCoin _dex){
desxUserCoin=_dex;
}
这个构造函数意味着在创建合约时,必须传递一个参数来初始化合约。