Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't call my smart contract functions #76

Open
PabloBlanco10 opened this issue Apr 16, 2019 · 0 comments
Open

Can't call my smart contract functions #76

PabloBlanco10 opened this issue Apr 16, 2019 · 0 comments

Comments

@PabloBlanco10
Copy link

PabloBlanco10 commented Apr 16, 2019

I have a simple smart contract deployed in rinkeby :

pragma solidity ^0.4.18;

contract CarChain {
    
    struct Car {
        uint id;
        string plate;
        uint userId;
        bool registered;
    }
    
    struct User{
        uint id;
        uint credit;
        uint carId;
        bool registered;
    }

    mapping(uint => User) public users;
    mapping(uint => Car) public cars;
    
    uint public minimumRentCredit;
    
    address manager;
    
    function CarChain(uint _minimumRentCredit) public {
        manager = msg.sender;
        minimumRentCredit = _minimumRentCredit;
    }
    
    function registerNewUser(uint _id, uint _credit) public{
        //Check there is no User registered with this id
        require(_id != 0);
        require(users[_id].registered == false);
        
        User storage newUser = users[_id];
        newUser.id = _id;
        newUser.credit = _credit;
        newUser.carId = 0;
        newUser.registered = true;
    }
    
    function registerNewCar(uint _id, string _plate) public restricted{
        //Check there is no Car registered with this id
        require(_id != 0);
        require(cars[_id].registered == false);
        
        Car storage newCar = cars[_id];
        newCar.id = _id;
        newCar.plate = _plate;
        newCar.registered = true;
    }
    
    function rentCar(uint _carId, uint _userId) public {
        require(cars[_carId].registered == true);
        require(users[_userId].registered == true);
        require(cars[_carId].userId == 0);
        require(users[_userId].carId == 0);
        require(users[_userId].credit >= minimumRentCredit);
        
        User storage user = users[_userId];
        user.carId = _carId;
        user.credit = user.credit - minimumRentCredit;
        Car storage car = cars[_carId];
        car.userId = _userId;
    }
    
    function returnCar(uint _carId, uint _userId) public {
        require(cars[_carId].registered == true);
        require(users[_userId].registered == true);
        require(cars[_carId].userId != 0);
        require(users[_userId].carId != 0);

        User storage user = users[_userId];
        user.carId = 0;
        Car storage car = cars[_carId];
        car.userId = 0;
    }
    
    function getUserCredit(uint _id) public view returns (uint){
        return users[_id].credit;
    }
    
    modifier restricted(){
        require(msg.sender == manager);
        _;
    }
}

But I think I am not calling the functions correctly.


 let contractJsonABI = "[{\"constant\":false,\"inputs\":[{\"name\":\"_id\",\"type\":\"uint256\"},{\"name\":\"_credit\",\"type\":\"uint256\"}],\"name\":\"registerNewUser\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"users\",\"outputs\":[{\"name\":\"id\",\"type\":\"uint256\"},{\"name\":\"credit\",\"type\":\"uint256\"},{\"name\":\"carId\",\"type\":\"uint256\"},{\"name\":\"registered\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_id\",\"type\":\"uint256\"},{\"name\":\"_plate\",\"type\":\"string\"}],\"name\":\"registerNewCar\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_id\",\"type\":\"uint256\"}],\"name\":\"getUserCredit\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_carId\",\"type\":\"uint256\"},{\"name\":\"_userId\",\"type\":\"uint256\"}],\"name\":\"rentCar\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_carId\",\"type\":\"uint256\"},{\"name\":\"_userId\",\"type\":\"uint256\"}],\"name\":\"returnCar\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"cars\",\"outputs\":[{\"name\":\"id\",\"type\":\"uint256\"},{\"name\":\"plate\",\"type\":\"string\"},{\"name\":\"userId\",\"type\":\"uint256\"},{\"name\":\"registered\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"minimumRentCredit\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]".data(using: .utf8)!

let web3 = Web3(rpcURL: "https://rinkeby.infura.io/v3/9...")
let contractAddress = try! EthereumAddress(hex: "0x526F5F06D6f1f9e63c6f344Ab5b160DE7b1eaCEa", eip55: true)
let contract = try! web3.eth.Contract(json: contractJsonABI, abiKey: nil, address: contractAddress)
let myPrivateKey = try! EthereumPrivateKey(hexPrivateKey: "***************")

        do {
            
            let c = contract["registerNewUser"]?(contractAddress, BigUInt(1), BigUInt(100))
            let transaction: EthereumTransaction = c!
                .createTransaction(nonce: 14,
                                   from: myPrivateKey.address,
                                   value: 0,
                                   gas: 210000,
                                   gasPrice: EthereumQuantity(quantity: 1.gwei))!
            
            let signedTx: EthereumSignedTransaction = try transaction.sign(with: myPrivateKey, chainId: 4)
            
            firstly {
                web3.eth.sendRawTransaction(transaction: signedTx)
                }.done { txHash in
                    print(txHash)
                }.catch { error in
                    print(error)
            }
            
        } catch {
            print(error)
        }

It enters in .done section with a txHash but when I test at remix the deployed contract there is no proof that something got setted.

Any help to call the functions?
Am I doing it right?

Thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant