solidity 中的继承问题。不能使用其他智能合约的任何功能
Problem with Inheritance in solidity . can not use any function from other smart contract
我有两个智能合约,DEXUserCoin
和 DEXTransferCoinUserToUser
。
我想将 DEXUserCoin
中的某些函数用于 DEXTransferCoinUserToUser
。
DEX用户币:
// 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;
}
这个构造函数意味着当你创建合约时,你必须传递一个参数来初始化合约。
我有两个智能合约,DEXUserCoin
和 DEXTransferCoinUserToUser
。
我想将 DEXUserCoin
中的某些函数用于 DEXTransferCoinUserToUser
。
DEX用户币:
// 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;
}
这个构造函数意味着当你创建合约时,你必须传递一个参数来初始化合约。