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

Transaction Error. Exception thrown in contract code #11

Open
panoskor opened this issue Jul 1, 2019 · 0 comments
Open

Transaction Error. Exception thrown in contract code #11

panoskor opened this issue Jul 1, 2019 · 0 comments

Comments

@panoskor
Copy link

panoskor commented Jul 1, 2019

Hello i try to deploy my contract using truffle on my private ethereum network(geth). I have connect an account to metamask and i have mine ethers. So i deploy the contract and when react with a function the metamask gives me this error.

contract code
```
pragma solidity ^0.5.8;

contract Creation  {
    
    int basiclife=50;
    
    struct hero  {
        string name;
        int life;
        int lvl;
        int xp;
        int attack;
        int winCount;
        int trophies;
        int lossCount;
    }
    hero[] public heroes; 
    
    
    struct enemy {  
        string name;
        int life;
        int lvl;
        int xp;
        int attack;
    }
    enemy[] public enemies;

  mapping (uint => address) public herotoowner;
  mapping (address => uint) public  heroCount;
  
  mapping (uint => address) public monstertoowner;
  uint public monsterCount; 
    
    function createhero(string memory _name) public  { 
     require(heroCount[msg.sender] == 0);
       uint id= heroes.push(hero(_name,basiclife,1,0,20,0,0,0))-1; 
       herotoowner[id] = msg.sender;
       heroCount[msg.sender]++;
       
    }
     function enemyRage(int _herolvl) public  { 
       uint id=enemies.push(enemy("Rage",20*_herolvl,_herolvl,7*_herolvl,5*_herolvl))-1;
       monstertoowner[id] = msg.sender;
       monsterCount++;
       
    }
    
    function enemyDerpina(int _herolvl) public  { 
       uint id=enemies.push(enemy("Derpina",40*_herolvl,_herolvl,14*_herolvl,10*_herolvl))-1; 
       monstertoowner[id] = msg.sender;
       monsterCount++;
       
    }
     
       function getHerosByOwner(address owner) external view returns( uint ) { 
        uint result =3;
        for (uint i = 0; i < 3; i++) {
              if (herotoowner[i] == owner) {
                result = i;
                break;
          }
        }
        return result;
        }
        function getmonstersByOwner(address owner) external view returns( uint ) { 
        uint result =0;
        for (uint i = enemies.length ; i > 0 ; i--) {
              if (monstertoowner[i] == owner) {
                result = i;
                break;
          }
        }
        return result;
        }
        
        uint nonce=0;
   
   function randNum(int _num) private   returns(int) {  //sinartisi gia tixaies times
    int randomnumber = int(keccak256(abi.encodePacked(now, msg.sender, nonce))) % _num;
    if (randomnumber <=0) {
        randomnumber=(-1)*randomnumber;
    }
    nonce++;
    return randomnumber;
    }  
    
    function attack_hero(uint _heroid, uint _monsterid, int _num  ) external  returns(string memory, int,int,int,int ) { 
      
      hero storage myhero = heroes[_heroid];
      enemy storage monster = enemies[_monsterid];
      int attackofhero=0;
      if(_num==0) { 
       attackofhero=randNum(myhero.attack);
      }
      monster.life=monster.life - attackofhero;
      
      int attackofmonster=randNum(monster.attack);
      myhero.life=myhero.life - attackofmonster;
      
      if (monster.life <= 0 && myhero.life >0) { 
        myhero.winCount++;
        myhero.xp+=monster.xp;
        
        if (randNum(100) > 50 ){ 
              myhero.trophies++;
          }
        
        return("win",myhero.lvl,myhero.xp,myhero.life,myhero.trophies);
      }
      if (myhero.xp>=10+2*myhero.lvl){ 
          myhero.lvl++;
          myhero.xp=0; //reset you xp
          myhero.life=basiclife*myhero.lvl;  
          myhero.attack=myhero.attack*myhero.lvl;
        }
      if (myhero.life <= 0 ) { //ita 
          myhero.lossCount++; 
          return("dead",myhero.lossCount,myhero.life,myhero.lvl,myhero.trophies);
      } 
      
      return("attack",myhero.life,attackofhero,monster.life,attackofmonster);
      
     
    }
    function resurrection(uint _heroid) public { 
         hero storage myhero = heroes[_heroid];
         myhero.life=basiclife*myhero.lvl;
     }
    
      
    
}      
genesis.json 

{
"config":{
"chainId":15,
"homesteadBlock":0,
"eip155Block":0,
"eip158Block":0
},
"nonce":"0x0000000000000042",
"mixhash":"0x000000000000000000000000000000000000000000000000000000000
0000000",
"difficulty":"0x200",
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000
000000000",
"gasLimit":"0xffffffff",
"alloc":{
}
}
}

app.js 
`App = {
    web3Provider: null,
    contracts: {},
    account: '0x0',
    heros: [],
    enemies: [],
    heroId: null,
    monsterId: null,
    rand: null,
    rand2: 0,
    init: function() {

        return App.initWeb3();

    },

    initWeb3: function() {
        // TODO: refactor conditional
        if (typeof web3 !== 'undefined') {
            // If a web3 instance is already provided by Meta Mask.
            App.web3Provider = web3.currentProvider;
            web3 = new Web3(web3.currentProvider);
        } else {
            // Specify default instance if no web3 instance provided
            App.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');
            web3 = new Web3(App.web3Provider);
        }
        return App.initContract();
    },

    initContract: function() {
        $.getJSON("Creation.json", function(Creation) {
            // Instantiate a new truffle contract from the artifact
            App.contracts.Creation = TruffleContract(Creation);
            // Connect provider to interact with contract
            App.contracts.Creation.setProvider(App.web3Provider);

            return App.render();
        });
    },
    monsterRender: function() {
        var enemyspot = $("#enemyspot");
        enemyspot.empty();
        if (App.rand == 1) {
            App.contracts.Creation.deployed().then(function(instance) {
                creationInstance = instance;
                return creationInstance.getmonstersByOwner(App.account);
            }).then(function(getmonstersByOwner) {
                App.monsterId = getmonstersByOwner;
                console.log(App.monsterId)
                return creationInstance.enemies(getmonstersByOwner)
            }).then(function(enemies) {
                console.log("hey 1" + enemies)
                App.enemies[App.monsterId] = enemies;
                var name = enemies[0];
                var life = enemies[1];
                var lvl = enemies[2];
                var xp = enemies[3];
                var atk = enemies[4];

                var enemyTemplate = "<tr ><td colspan='2'><img src = './images/rage.jpg' width ='200px'></td></tr><tr><td> Name</td><td>: " + name + "</td></tr><tr><td>Health Points</td><td>: " + life + " HP</td></tr><tr><td>Level</td><td>: " + lvl + "</td></tr><tr><td>Experiance</td><td>: " + xp + "</td></tr><tr><td>Attack</td><td>: " + atk;
                enemyspot.append(enemyTemplate);

            }).then(function() {
                if (App.enemies[App.monsterId][1] <= 0) {
                    alert("You have won!!!");

                    enemyspot.empty();
                }
            });
        } else {
            App.contracts.Creation.deployed().then(function(instance) {
                creationInstance = instance;

                return creationInstance.getmonstersByOwner(App.account);
            }).then(function(getmonstersByOwner) {
                App.monsterId = getmonstersByOwner;
                return creationInstance.enemies(getmonstersByOwner)
            }).then(function(enemies) {
                console.log("hey 1" + enemies)

                App.enemies[App.monsterId] = enemies;
                var name = enemies[0];
                var life = enemies[1];
                var lvl = enemies[2];
                var xp = enemies[3];
                var atk = enemies[4];

                var enemyTemplate = "<tr ><td colspan='2'><img src = './images/derpina.jpg' width ='200px'></td></tr><tr><td> Name</td><td>: " + name + "</td></tr><tr><td>Health Points</td><td>: " + life + " HP</td></tr><tr><td>Level</td><td>: " + lvl + "</td></tr><tr><td>Experiance</td><td>: " + xp + "</td></tr><tr><td>Attack</td><td>: " + atk;
                enemyspot.append(enemyTemplate);
            }).then(function() {
                if (App.enemies[App.monsterId][1] <= 0) {
                    alert("You have won!!!");
                    isCreated =0;
                    var enemyspot = $("#enemyspot");
                    enemyspot.empty();
                }
            });
        }
    },


    render: function() {

        // Load account data
        web3.eth.getCoinbase(function(err, account) {
            if (err === null) {
                App.account = account;
                $("#accountAddress").html("Your Account: " + account);
            }
        });
        var herospot = $("#herospot");
        herospot.empty();
        // Load contract data
        App.contracts.Creation.deployed().then(function(instance) {
            creationInstance = instance;
            return creationInstance.getHerosByOwner(App.account);
        }).then(function(getHerosByOwner) {
            console.log("hey äóáä" + getHerosByOwner)
            App.heroId = getHerosByOwner;
            console.log(App.heroId)
            return creationInstance.heroes(getHerosByOwner);
        }).then(function(heroes) {
            $("#charform").hide();
            $("#story").hide();
            console.log(account)
            App.heroes = heroes;

            var name = heroes[0];
            var life = heroes[1];
            var lvl = heroes[2];
            var xp = heroes[3];
            var atk = heroes[4];
            var winCount = heroes[5];
            var trophies = heroes[6];
            var lossCount = heroes[7];
            var heroTemplate = "<tr ><td colspan='2'><img src = './images/happyfa.jpg' width ='200px'></td></tr><tr><td> Name</td><td>: " +
                name + "</td></tr><tr><td>Health Points</td><td>: " + life + " HP</td></tr><tr><td>Level</td><td>: " +
                lvl + "</td></tr><tr><td>Experiance</td><td>: " + xp + "</td></tr><tr><td>Attack</td><td>: " + atk + "</td></tr><tr><td>Win count</td><td>: " + winCount +
                "</td></tr><tr><td>trophies</td><td>: " + trophies + "</td></tr><tr><td>lossCount</td><td>: " + lossCount;
            herospot.append(heroTemplate);
        }).then(function() {
            console.log("hello"+App.heroes[1])
            if (App.heroes[1] <= 0) {
                console.log("sdasd" + App.heroes[1])
                alert("You have Lost!!!");
                var enemyspot = $("#enemyspot");
                enemyspot.empty();
                App.resurrection();
                isCreated =0;
            }
        }).then(App.loadMonsters());
    },
    fight: function() {
        App.contracts.Creation.deployed().then(function(instance){
            creationInstance = instance;
            return creationInstance.attack_hero(App.heroId, App.monsterId, App.rand2);
        }).then(function(attack_hero){
            App.monsterRender();
    App.render();
     App.rand2 = 0;
        })
    },
    resurrection:function() {
        
             App.contracts.Creation.deployed().then(function(instance){
            creationInstance = instance;
                return creationInstance.resurrection(App.heroId);
            }).then(function(resurrection){
                App.render();
            });
    },


    spawn: function() {
        console.log(App.heroes)
        console.log(App.account)
        var rand = Math.round(Math.random() * 1);
        App.rand = rand;
        var enemyspot = $("#enemyspot");
        enemyspot.empty();
        if (rand == 1) {
            App.contracts.Creation.deployed().then(function(instance) {
                creationInstance = instance;
                return creationInstance.enemyRage(App.heroes[2]);
            }).then(function(enemyRage) {
                console.log("hey1 " + enemyRage)
                return creationInstance.getmonstersByOwner(App.account);
            }).then(function(getmonstersByOwner) {
                App.monsterId = getmonstersByOwner;

                return creationInstance.enemies(getmonstersByOwner)
            }).then(function(enemies) {
                console.log("hey 1" + enemies)
                App.enemies.push(enemies);
                App.enemies[App.monsterId] = enemies;
                var name = enemies[0];
                var life = enemies[1];
                var lvl = enemies[2];
                var xp = enemies[3];
                var atk = enemies[4];

                var enemyTemplate = "<tr ><td colspan='2'><img src = './images/rage.jpg' width ='200px'></td></tr><tr><td> Name</td><td>: " + name + "</td></tr><tr><td>Health Points</td><td>: " + life + " HP</td></tr><tr><td>Level</td><td>: " + lvl + "</td></tr><tr><td>Experiance</td><td>: " + xp + "</td></tr><tr><td>Attack</td><td>: " + atk;
                enemyspot.append(enemyTemplate);
                isCreated = 1;
            });
        } else {
            App.contracts.Creation.deployed().then(function(instance) {
                creationInstance = instance;
                return creationInstance.enemyDerpina(App.heroes[2]);
            }).then(function(enemyDerpina) {
                console.log("hey1 " + enemyDerpina)
                return creationInstance.getmonstersByOwner(App.account);
            }).then(function(getmonstersByOwner) {
                App.monsterId = getmonstersByOwner;
                return creationInstance.enemies(getmonstersByOwner)
            }).then(function(enemies) {
                console.log("hey 1" + enemies)
                App.enemies.push(enemies);
                App.enemies[App.monsterId] = enemies;
                var name = enemies[0];
                var life = enemies[1];
                var lvl = enemies[2];
                var xp = enemies[3];
                var atk = enemies[4];

                var enemyTemplate = "<tr ><td colspan='2'><img src = './images/derpina.jpg' width ='200px'></td></tr><tr><td> Name</td><td>: " + name + "</td></tr><tr><td>Health Points</td><td>: " + life + " HP</td></tr><tr><td>Level</td><td>: " + lvl + "</td></tr><tr><td>Experiance</td><td>: " + xp + "</td></tr><tr><td>Attack</td><td>: " + atk;
                enemyspot.append(enemyTemplate);
                isCreated = 1;
            });
        }


    },

    validate: function() {
        var name = document.getElementById("name").value;

        App.contracts.Creation.deployed().then(function(instance) {
            alert("Your name is " + name);
            return instance.createhero(name, {
                from: App.account
            });
        }).then(function(result) {

            App.render();
        });
    },
    loadMonsters: function() {
        App.contracts.Creation.deployed().then(function(instance) {
            creationInstance = instance;
            return creationInstance.monsterCount();
        }).then(function(monsterCount) {
            console.log("äóöä" + monsterCount);
            for (var i = 0; i < monsterCount; i++) {

                creationInstance.enemies(i).then(function(enemies) {
                    App.enemies.push(enemies);

                });
            }
        })
    },

};
var enemyspot = $("#enemyspot");
let startFlag;
var account;
var accountInterval = setInterval(function() {
    // Check if account has changed
    if (web3.eth.accounts[0] !== account) {
        account = web3.eth.accounts[0];
        // Call some function to update the UI with the new account
        App.render();
        location.reload();
    }
}, 100);

function startAdventure() {
    $("#adventure").hide();
    var instructions = $("#advWrap");
    var heroT = "<p id='instructions'>Press W to walk <br> Press A to attack <br> Press R to run<p>";
    instructions.append(heroT);
   document.getElementById("#advWrap").style.color = "yellow";
    startFlag = 1;
};
let isCreated;

console.log('fasdfas' + App.monsterId)
document.onkeyup = function(e) {
    if (e.which == 87 /*&& startFlag == 1*/ ) {
        console.log("iscreated is" + isCreated)
        let randomNum = Math.round(Math.random() * 3);
        console.log(randomNum)
        if (randomNum == 1) {

            if (isCreated == 0 || isCreated == null || isCreated == undefined  && App.heroes[1]>0) {
                alert("You have been ambushed!!!");
                App.spawn()
                isCreated =1;
                
                
            } else if (App.enemies[App.monsterId][1] <= 0  && App.heroes[1]>0) {

                App.spawn();
                isCreated =1;
            } else if (isCreated == 1) {
                alert("You must kill this monster first")
            }
        } else if(randomNum !== 1 && isCreated !==1) {
            alert("You didn't encounter any monster");
        }else if (isCreated == 1) {
                alert("You must kill this monster first")
            }
    }
    if (e.which == 65 && isCreated == 1 && App.enemies[App.monsterId][1] > 0 && App.heroes[1]>0) {
        console.log('123       ' + App.enemies[App.monsterId][1])
        
        App.fight()
    }
    if (e.which == 82 && isCreated == 1) {
         App.rand2 =Math.round(Math.random() * 1);
        if (App.rand2 == 0) {
            isCreated = 0;
            enemyspot.empty();
            App.rand2 = 1;
        } else if (App.rand2 ==1 ) {
            App.fight();

        }
    }
};

$(function() {
    $(window).load(function() {

        App.init();
    });
});`
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