Withdraw 函数 send 仅适用于 "address payable" 类型的对象
Withdraw function send is only available for objects of type "address payable"
pragma solidity >=0.6.0 <0.9.0;
//import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; ao importar este contracto o debaixo nao seria necessario mas usamos para ter um melhor entendimento do contrato
interface AggregatorV3Interface {
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
// getRoundData and latestRoundData should both raise "No data present"
// if they do not have data to report, instead of returning unset values
// which could be misinterpreted as actual reported values.
function getRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
}
contract FundMe {
//track de todos os endereços que interagiram com o contracto
mapping(address => uint256) public addressToAmmountFunded;
address public owner;
//tudo o que é feito nos constructor() é automaticamente aplicado na criaçao do smart contract
constructor() {
//como o primeiro msg.sender é o criador do smart contract fica automaticamente como owner
owner = msg.sender;
}
function fund() public payable {
uint256 minimunUSD = 500 * 10 ** 18; //define que minimunUSD tem de ser 50 mas é preciso multiplicar por 10^18 por estarmos a usar WEI
require(getConversionRate(msg.value) >= minimunUSD, "You need to invest more than 500USD in xxx tokens!"); // Se valor enviado nao for suficiente o ele reverte a transacçao
addressToAmmountFunded[msg.sender] += msg.value;
//contrato que so aceita ETH por isso é necessario fazer uma conversao para um USD conversion rate
}
function getVersion() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e); // contrato definido encontrado em https://docs.chain.link/docs/ethereum-addresses/ que é ETH/USD
return priceFeed.version();
}
function getPrice() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 answer,,,) = priceFeed.latestRoundData(); // O numero de virgulas representam as variaves em latest round data que nao estao a ser usadas
return uint256(answer * 10000000000);
}
function getLatestUpdate() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,,,uint256 updatedAt,) = priceFeed.latestRoundData(); // O numero de virgulas representam as variaves em latest round data que nao estao a ser usadas
return updatedAt;
}
// 1000000000
function getConversionRate(uint256 ethAmount) public view returns(uint256){
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000 ; //divisao por 10^18 casas decimais do ethereum
return ethAmountInUsd;
}
//este modificador faz com que o codigo de withdraw() só corra depois de executar o require
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function withdraw() payable onlyOwner public {
msg.sender.transfer(address(this).balance);
}
}
在最后一个函数 withdraw() 中,我收到错误类型错误:“发送”和“转移”仅适用于“应付地址”类型的对象,而不适用于“地址”类型的对象。
我正在使用 REMIX 的编译器版本 0.8.7 IDE
我已经仔细检查了所有的智能合约,但我看不出任何解决方法。
我想用这个功能做的是检查存储在智能合约上的所有价值,并将其提取到部署智能合约的所有者地址
由于你修改了onlyOwner,只有所有者才能调用。所以
msg.sender
实际上是 owner
。
所有者应付款。如果您的代码中没有其他错误,这应该可以解决它:
address payable owner;
pragma solidity >=0.6.0 <0.9.0;
//import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; ao importar este contracto o debaixo nao seria necessario mas usamos para ter um melhor entendimento do contrato
interface AggregatorV3Interface {
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
// getRoundData and latestRoundData should both raise "No data present"
// if they do not have data to report, instead of returning unset values
// which could be misinterpreted as actual reported values.
function getRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
}
contract FundMe {
//track de todos os endereços que interagiram com o contracto
mapping(address => uint256) public addressToAmmountFunded;
address public owner;
//tudo o que é feito nos constructor() é automaticamente aplicado na criaçao do smart contract
constructor() {
//como o primeiro msg.sender é o criador do smart contract fica automaticamente como owner
owner = msg.sender;
}
function fund() public payable {
uint256 minimunUSD = 500 * 10 ** 18; //define que minimunUSD tem de ser 50 mas é preciso multiplicar por 10^18 por estarmos a usar WEI
require(getConversionRate(msg.value) >= minimunUSD, "You need to invest more than 500USD in xxx tokens!"); // Se valor enviado nao for suficiente o ele reverte a transacçao
addressToAmmountFunded[msg.sender] += msg.value;
//contrato que so aceita ETH por isso é necessario fazer uma conversao para um USD conversion rate
}
function getVersion() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e); // contrato definido encontrado em https://docs.chain.link/docs/ethereum-addresses/ que é ETH/USD
return priceFeed.version();
}
function getPrice() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 answer,,,) = priceFeed.latestRoundData(); // O numero de virgulas representam as variaves em latest round data que nao estao a ser usadas
return uint256(answer * 10000000000);
}
function getLatestUpdate() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,,,uint256 updatedAt,) = priceFeed.latestRoundData(); // O numero de virgulas representam as variaves em latest round data que nao estao a ser usadas
return updatedAt;
}
// 1000000000
function getConversionRate(uint256 ethAmount) public view returns(uint256){
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000 ; //divisao por 10^18 casas decimais do ethereum
return ethAmountInUsd;
}
//este modificador faz com que o codigo de withdraw() só corra depois de executar o require
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function withdraw() payable onlyOwner public {
msg.sender.transfer(address(this).balance);
}
}
在最后一个函数 withdraw() 中,我收到错误类型错误:“发送”和“转移”仅适用于“应付地址”类型的对象,而不适用于“地址”类型的对象。
我正在使用 REMIX 的编译器版本 0.8.7 IDE
我已经仔细检查了所有的智能合约,但我看不出任何解决方法。
我想用这个功能做的是检查存储在智能合约上的所有价值,并将其提取到部署智能合约的所有者地址
由于你修改了onlyOwner,只有所有者才能调用。所以
msg.sender
实际上是 owner
。
所有者应付款。如果您的代码中没有其他错误,这应该可以解决它:
address payable owner;