0

Actually i am trying to create a betting application so when i get the result that Home team has won Before this i will be taking both address as input (betting do two address whoever wins gets the money)

But how to transfer money? Between these accounts after taking input? I have written code to check who has won home or away team and then will call function to transfer money but how to process the transfer Please if you can help? Or provide code example ? I am new to solidity programming and finding it difficult to understand.

Solidity File

pragma solidity ^0.4.22;

import "installed_contracts/strings.sol";
import "installed_contracts/oraclize-api/contracts/usingOraclize.sol";

contract OraclizeTest is usingOraclize {

    using strings for *;        //strings import requirement
   // string public matchId; 
    uint public amount; 

//remove url traces
    address public homeBet;  
    address public awayBet;


    //string public ETHUSD;

    event LogInfo(string description);      //getting from update function
//event LogPriceUpdate(string price);     //waiting for price in callback 
    //then calling update function 
    //event LogUpdate(address indexed _owner, uint indexed _balance);     //getting from constructor 

    // Constructor
    function OraclizeTest (uint _amount) public {      //adding args and not matchid
        amount = _amount;        

    //    emit LogUpdate(owner, address(this).balance); //owner was in original

        // Replace the next line with your version:
       OAR = OraclizeAddrResolverI(0x04De331D6fFe6079cDD635780Ce588263D682D31);



        oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
        update();
    }

    // Fallback function
    function()
    public{
        revert();
    }
    //--------------------------------------------------------------------------------------
     //1 indicates home team    //2 indicates away team
    function betOnTeam(uint team) payable public {
        // update();
        if(team == 1)
        {
            if(homeBet == 0)
            {
                if(msg.value == amount)
                {
                    homeBet = msg.sender;
                    if(homeBet != 0 && awayBet != 0)
                    {
                        //oraclize_query("URL", url);

                        update();
                       // oraclize_query("URL", "json(https://api.crowdscores.com/v1/matches/123945?api_key=93c4c515196741beaf5e4528b64ea511).outcome.winner");
                    }
                }
                else
                {
                    throw;
                }
            }
            else
            {
                throw;
            }
        }
        else if(team == 2)
        {
            if(awayBet == 0)
            {
                if(msg.value == amount)
                {
                    awayBet = msg.sender;

                    if(homeBet != 0 && awayBet != 0)
                    {
                        //oraclize_query("URL", url);
                        update();
                      //  oraclize_query("URL", "json(https://api.crowdscores.com/v1/matches/123945?api_key=93c4c515196741beaf5e4528b64ea511).outcome.winner");
                    }
                }
                else
                {
                    throw;
                }
            }
            else
            {
                throw;
            }
        }
        else
        {
            throw;
        }
    }
    //--------------------------------------------------------------------------------------
    function __callback(bytes32 id, string result, bytes proof) public {
    require(msg.sender == oraclize_cbAddress());        // just to be sure the calling address is the Oraclize authorized one
    update();
    //string lib being used to compare
    if (result.toSlice().equals("home".toSlice()))
    {
        homeBet.transfer(address(this).balance);
    }
    else if(result.toSlice().equals("away".toSlice()))
    {
        awayBet.transfer(address(this).balance);
    }
    else if(result.toSlice().equals("draw".toSlice()))
    {
        homeBet.transfer(address(this).balance / 2);
        awayBet.transfer(address(this).balance / 2);
    }

    // update();
    }

    function update()
    payable
    public {
        // Check if we have enough remaining funds
        if (oraclize_getPrice("URL") > address(this).balance) {
            emit LogInfo("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
        } else {
            emit LogInfo("Oraclize query was sent, standing by for the answer..");

            // Using XPath to to fetch the right element in the JSON response
            oraclize_query("URL", "json(https://api.crowdscores.com/v1/matches/123945?api_key=93c4c515196741beaf5e4528b64ea511).outcome.winner");

        }
    }

}
``````````````````

App.js
``````````````````

function ready(){
document.getElementById("bet").addEventListener("submit", function(e){
    e.preventDefault();

    var fromAddress1 = document.querySelector("#bet #fromAddress1").value;
    var privateKey1 = document.querySelector("#bet #privateKey1").value;
    var fromAddress2 = document.querySelector("#bet #fromAddress2").value;
    var privateKey2 = document.querySelector("#bet #privateKey2").value;

    var betAmount = document.querySelector("#bet #betAmount").value;

    var contract = web3.eth.contract(OraclizeContract.abi).at('0x0FBfeF5861C5315226A04865c942D97208D33e3C');

    console.log(contract.abi)

    //var sendata = contract.new.getData(web3.toWei(betAmount, "ether"));

    var team = document.querySelector("#bet #team").value;
    console.log(team)
    if(team == "Home")
    {
      console.log("inside----home-----")
      team = 1;
    }
    else
    {
      team = 2;
    }
    console.log(team)
````````````````
Now as you can see i have taken address as input connected with truffle but i need to call the betonTeam function and transfer money to the winner address

0

From the code I see you're using web3 0.2.x correct? I looked at your contract code and got a bit confused what is method betOnTeam doing, I didn't see ETH transferring inside of it. Anyway if you are using MetaMask this is the way to fire transaction:

function ready(){
document.getElementById("bet").addEventListener("submit", function(e){
    e.preventDefault();

    var fromAddress1 = document.querySelector("#bet #fromAddress1").value;
    var privateKey1 = document.querySelector("#bet #privateKey1").value;
    var fromAddress2 = document.querySelector("#bet #fromAddress2").value;
    var privateKey2 = document.querySelector("#bet #privateKey2").value;

    var betAmount = document.querySelector("#bet #betAmount").value;

    var contract = web3.eth.contract(OraclizeContract.abi).at('0x0FBfeF5861C5315226A04865c942D97208D33e3C');

    console.log(contract.abi)

    //var sendata = contract.new.getData(web3.toWei(betAmount, "ether"));

    var team = document.querySelector("#bet #team").value;
    console.log(team)
    if(team == "Home")
    {
        console.log("inside----home-----")
        team = 1;
        contract.betOnTeam(team, {
            from: <SENDER_ADDRESS>,
            gas: <GAS_AMOUNT>
        }, function(err, result){
            if(!err) {
                console.log(result, 'result');
            } else {
                console.log(err, 'err');
            }
        });
    }
    else
    {
      team = 2;
    }
    console.log(team)

If you want to fire transactions without using MetaMask you need to take a look at this example.

2
  • I am using Oraclize to fetch JSON and when the callback function will be called then only upon whatever team has won i wish to transfer ether
    – raghav
    Jan 16 '20 at 5:53
  • How to use metamask to send ether like HOW CAN I specify where to send ether in this
    – raghav
    Jan 16 '20 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.