Can't initialise Contract for view after adding payable fallback or receive function - solidity

I have 2 contracts
contract BetContract {
address owner;
string title;
function GetTitle(string memory title) public view returns(string){
return this.title;
}
}
contract Main {
.
.
function GetBetTitle(address betAddress) public view returns(string){
BetContract currentBet = BetContract(betAddress);
return currentBet.GetTitle();
}
}
That works great, but as soon as I want add a fallback or receive function to BetContract, I can't initialise the BetContract anymore without making it payable?!
contract BetContract {
address owner;
string title;
receive()external payable{
}
function GetTitle(string memory title) public view returns(string){
return this.title;
}
}
contract Main {
.
.
function GetBetTitle(address betAddress) public view returns(string){
BetContract currentBet = BetContract(betAddress);
TypeError: Explicit type conversion not allowed from non-payable "address" to "contract BetContract", which has a payable fallback function.
return currentBet.GetTitle();
}}
Any solutions for that? Thank you all
I tried to call the function on other ways, but I need to know why this is the case

The address needs to be made payable first:
function GetBetTitle(address betAddress) public view returns(string){
BetContract currentBet = BetContract(payable(betAddress));
return currentBet.GetTitle();
}

Related

How to run multiples contracts functionalities in a single contract Solidity

I'm new to Solidity but I can't find much information about my problem.
For example, I want to make different contracts for different functionalities (I see them as classes)
For example
Main contract
// SPDX-License-Identifier: None
pragma solidity >=0.8.6;
import "./AuthContract.sol";
contract Contract {
string public message;
constructor() {
message = "test";
}
function getMessage() public view returns(string memory) {
return message;
}
}
and second contract
contract Auth {
struct UserDetail {
address addr;
string name;
string password;
string CNIC;
bool isUserLoggedIn;
}
mapping(address => UserDetail) user;
// user registration function
function register(
address _address,
string memory _name,
string memory _password,
string memory _cnic
) public returns (bool) {
require(user[_address].addr != msg.sender);
user[_address].addr = _address;
user[_address].name = _name;
user[_address].password = _password;
user[_address].CNIC = _cnic;
user[_address].isUserLoggedIn = false;
return true;
}
// user login function
function login(address _address, string memory _password)
public
returns (bool)
{
if (
keccak256(abi.encodePacked(user[_address].password)) ==
keccak256(abi.encodePacked(_password))
) {
user[_address].isUserLoggedIn = true;
return user[_address].isUserLoggedIn;
} else {
return false;
}
}
// check the user logged In or not
function checkIsUserLogged(address _address) public view returns (bool) {
return (user[_address].isUserLoggedIn);
}
// logout the user
function logout(address _address) public {
user[_address].isUserLoggedIn = false;
}
}
How could I use the functionalities from that contract in the main contract?
Is such a thing possible in the blockchain?
I am quite new here also but i can solve your problem, if not solve i can lead you to a good path .
so firstly you said you want to create multiple contract for different functionalities, this is good but keep in my you are going to exhaust a lot of gas.
so the answer to your problem is easy you can just read it and implement it.
if you want to use a contract in the Another contract (main in your case) you can do it in two ways(according to my knowledge there might be other).
using new keyword
using address of your previously deployed contract
we will be using the First case as i suppose you havenot deployed the second contract yet
In Order to do it you can use
Auth myObj=new Auth();
This will create a new instance of the contract Auth in your main contract and now you can use the Auth contract's function in your Main contract.you can create a function copy the above line and you can use the Functions using dot operator.
myObj.register(_address,_name,moreandmore);
I believe this will solve your problem if not you can ask it.
Thank You!

I need help how to fix the "else if" = only owner is allowed to call the _action from the main contract. this is just the logger

I need help how to fix the "else if" = only owner is allowed to call the _action from the main contract. this is just the logger. below is the contract logger.
contract logger {
function log(address _caller, uint _amount, string memory _action) public {
if (equal(_action, "withdraw")) {
revert("It's a frank!");
else if (equal(_caller, "owner"));
assert();
}
}
function equal(string memory _a, string memory _b) public pure returns (bool) {
return keccak256(abi.encode(_a)) == keccak256(abi.encode(_b));
So I am guessing you want to call the function from the main contract in the logger contract. So we need to know the address of the main contract and the function signature of the action function in the main contract. Suppose the function signature of action function in the main contract is action(unit256). And let's initialize the main contract in the constructor.
So the contract logger would look like this:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import "./MainContract.sol";
contract Logger {
address private owner;
MainContract mainContract;
constructor(address _mainContract){
owner = msg.sender;
mainContract = new MainContract(_mainContract);
}
function log(address _caller, uint _amount, string memory _action, uint256 value) public {
if (equal(_action, "withdraw")) {
// Do whatever you want
}
else if (_caller == owner){
mainContract.action(value);
// Do whatever you want
}
}
function equal(string memory _a, string memory _b) public pure returns (bool) {
return keccak256(abi.encode(_a)) == keccak256(abi.encode(_b));
}
}

Solidity, is there gas-wise difference between transfer and call with 2300?

I am trying the below code, elementary reentrancy example.
The current code works but transfer or send doesn't work. It reverts. What are the reasons?
Note: I chose setCaller to reenter to test the fact that updating the dirty state variable (already modified var in the same tx) takes less gas.
// SPDX-License-Identifier: Unlicensed
pragma solidity 0.8.7;
contract TestFund {
// success, if caller is itself.
address public caller;
constructor() payable {}
function getBalance() public view returns (uint) {
return address(this).balance;
}
function emptyFn(address dummy) public {}
function setCaller(address _caller) public {
caller = _caller;
}
function withdraw() external {
caller = msg.sender;
// require(payable(msg.sender).send(1 wei));
// payable(msg.sender).transfer(1 wei);
(bool success, bytes memory data) = payable(msg.sender).call{gas: 2300, value:1 wei}("");
require(success);
}
}
contract Attack {
function getBalance() public view returns (uint) {
return address(this).balance;
}
function attack(address target) external {
TestFund(target).withdraw();
}
receive() external payable {
if (getBalance() == 1 wei) {
TestFund(msg.sender).setCaller(msg.sender);
}
}
}

Can someone please give working example of interface in solidity?

I am getting error "This contract does not implement all functions and thus cannot be created." while using interface in solidity can someone help me on this or please give me working example of interface in solidity?
pragma solidity ^0.4.0;
interface Audit {
function CheckBlance() public returns(bool);
function lending() public returns(bool);
}
contract Fin_Inst is Audit{
uint public Money;
function Fin_Inst(uint Value) public {
Money = Value;
}
function Deposit(uint DepAmount) public{
Money = Money + DepAmount;
}
function Withdraw(uint WithdAmount) public{
if(CheckBlance(WithdAmount) == true){
Money = Money - WithdAmount;
}
}
function Balance() public constant returns (uint){
return Money;
}
function CheckBlance(uint WithdAmount) public returns (bool){
return Money >= WithdAmount;
}
function lending() public returns (bool){
return Money > 0;
}
}
I've made small changes and now when you run Create Fin_Inst it executes without any error messages.
Seems like an error was in function parameter in interface method CheckBalance(uint WithdAmount).
Here is the edited code:
pragma solidity ^0.4.19;
interface Audit {
function CheckBalance(uint WithdAmount) public view returns(bool);
function lending() public view returns(bool);
}
contract Fin_Inst is Audit{
uint public Money;
function Fin_Inst(uint Value) public {
Money = Value;
}
function Deposit(uint DepAmount) public {
Money = Money + DepAmount;
}
function Withdraw(uint WithdAmount) public {
if(CheckBalance(WithdAmount) == true){
Money = Money - WithdAmount;
}
}
function Balance() external view returns (uint) {
return Money;
}
function CheckBalance(uint WithdAmount) public view returns (bool) {
return Money >= WithdAmount;
}
function lending() public view returns (bool) {
return Money > 0;
}
}
Yes, in order to implement a method you must respect the signature (same name and params) completly.
For example, you can have in your Contract 3 different methods like
method1(), method1(unit x) and method1(unit[] xs)
To implement this interface, you must code the 3 methods in the child Contract. So, if you only implement method1() they are still pending method1(unit x) and method1(unit[] xs).
This is called Function overloading. Therefore CheckBalance(uint WithdAmount) is not the same as CheckBalance()

Transfer ownership web3

I am creating a dapp to transfer ownership of the contract from one address to another using testrpc. However,I keep encountering this problem. I have tried using sentransaction method to do perform this ownership change.Perhaps I'm calling the exchange in a wrong manner.
Solidity version 0.4.4
web3 "version": "0.20.2"
web3.js:3127 Uncaught Error: VM Exception while processing transaction: invalid opcode
at Object.InvalidResponse (web3.js:3127)
at RequestManager.send (web3.js:6332)
at Eth.send [as sendTransaction] (web3.js:5066)
at SolidityFunction.sendTransaction (web3.js:4122)
at SolidityFunction.execute (web3.js:4208)
at transferOwnership (luxcure_manu.html:309)
at HTMLButtonElement.onclick (luxcure_manu.html:378
Full solidity contract as of yet.
pragma solidity ^0.4.4;
// TODO: Hash of the cert through IPFS Hash
// Transfer ownership of smart contract
contract LuxSecure {
address public contract_owner; //Manufacturer/owner
//string public current_owner; //Current Owner of good
bytes32 public model; //Model
mapping(uint => address) public owners; //list of owners
uint256 public owners_count;
bytes32 public status; // (Public(Owned by no one), Private(Bought by another entity),stolen(Stolen from public or private))
bytes32 public date_manufactured; //Time
// Set manufacturer of the Good RUN ONCE ONLY
function manufacturer() public{
if(owners_count == 0){
contract_owner = msg.sender;
}
}
//Modifier that only allows owner of the bag to Smart Contract AKA Good to use the function
modifier onlyOwner(){
require(msg.sender == contract_owner);
_;
}
// Add a new product to the blockchain with a new serial
function addNewGoods(bytes32 _model,bytes32 _status, bytes32 _date_manufactured) public returns(bool made) {//Declare Goods struct
setOwner(msg.sender);
model = _model;
status = _status;
date_manufactured = _date_manufactured;
return true;
}
//This function transfer ownership of contract from one entity to another
function transferOwnership(address _newOwner) public onlyOwner(){
require(_newOwner != address(0));
contract_owner = _newOwner;
}
//Set the KEY to uint256 and VALUE owner Ethereum Address
function setOwner(address owner)public{
owners_count += 1 ;
owners[owners_count] = owner;
}
//Get the previous owner in the mappings
function previousOwner()constant public returns(address){
if(owners_count != 0){
uint256 previous_owner = owners_count - 1;
return owners[previous_owner];
}
}
// Getter Methods
function getManufacturer() constant public returns(address){
return contract_owner;
}
function getCurrentOwner() constant public returns(address){
return owners[owners_count] ;
}
function getOwnerCount() constant public returns(uint256){
return owners_count;
}
function getModel() constant public returns(bytes32){
return model;
}
function getStatus() constant public returns(bytes32){
return status;
}
function getDateManufactured() constant public returns(bytes32){
return date_manufactured;
}
}// end of LuxSecure
Javascript to perform the transfer of ownership
function transferOwnership(){
var account_to_transfer = document.getElementById("ethereumaddress").value;
contract.transferOwnership(account_to_transfer,{
from:web3.eth.accounts[0],
gas:4000000
});
}
I don't see any particular mistake in your code. Maybe a bad formatting on the front-side, but can't guess for sure as we have partial front here.
I don't know if it will be some help but sometimes, using truffle, it happened to me to have some functions that returned bad opcode from testrpc/ganache-cli while no apparent error was in the code.
Deleting the ABI, recompiling the smart-contracts to get a brand new ABI and then redeploying the contracts solved the problem.