-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblockchain_interactions.js
100 lines (85 loc) · 3.35 KB
/
blockchain_interactions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Configuration variables */
var contractAddress = "0x43833c47907be9780581c0fbee97cc6e4f0a99c9";
var ABI = require("./build/contracts/MindMathGame.json").abi;
var web3Host = 'http://localhost';
var web3Port = '8545';
var exports = module.exports = {};
/* web3 initialization */
var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(web3Host + ':' + web3Port));
if (!web3.isConnected()) {
console.error("Ethereum - no connection to RPC server");
} else {
console.log("Ethereum - connected to RPC server");
}
// Get first default Ethereum account. This will be contract owner account.
var account = web3.eth.accounts[0];
var sendDataObject = {
from: account,
gas: 300000,
};
// creation of contract object
var MindMathGameContract = web3.eth.contract(ABI);
// initiate contract for an address
exports.MindMathGameInstance = MindMathGameContract.at(contractAddress); // export MindMathGameInstance to be visible in other js files
// Call game MindMathGameSetOwner with var account = web3.eth.accounts[0], no other account can change data inside the contract
var result = exports.MindMathGameInstance.MindMathGameSetOwner.sendTransaction(sendDataObject, function (err, result) {
if (err) {
console.error("Ethereum contract. MindMathGameSetOwner, Transaction submission error:", err);
} else {
console.log("Ethereum contract. MindMathGameSetOwner success. Transaction hash:", result);
}
});
/* Call Ethereum contract to create new game
*/
exports.mmgCreateGame = function(_game_address)
//function mmgCreateGame( _game_address )
{
var result = exports.MindMathGameInstance.createGame.sendTransaction(_game_address, sendDataObject, function (err, result) {
if (err) {
console.error("Ethereum contract. createGame, Transaction submission error:", err);
} else {
console.log("Ethereum contract. createGame success. Transaction hash:", result);
}
});
}
/* Call Ethereum contract to update/store the game score
*/
exports.mmgUpdateGameScore = function( _new_score, _game_address)
{
var result = exports.MindMathGameInstance.updateGameScore.sendTransaction(_new_score, _game_address, sendDataObject, function (err, result) {
if (err) {
console.error("Ethereum contract. updateGameScore submission error:", err);
} else {
console.log("Ethereum contract. updateGameScore success. Transaction hash:", result);
}
});
return result;
}
/* Call Ethereum contract to buy power up.
* @return true if success, false if not score funds on the _game_address
*/
exports.mmgBuyPowerUp = function( _desired_power_up, _game_address )
{
var result = exports.MindMathGameInstance.buyPowerUp.sendTransaction(_desired_power_up, _game_address, sendDataObject, function (err, result) {
if (err) {
console.error("Ethereum contract. buyPowerUp submission error:", err);
} else {
console.log("Ethereum contract. buyPowerUp success. Transaction hash:", result);
}
});
return result;
}
/* Get from Ethereum contract to get game_score and game_power_up
*/
exports.mmgGetGameDetails = function( _game_address )
{
var result = exports.MindMathGameInstance.getGameDetails.sendTransaction(_game_address, sendDataObject, function (err, result) {
if (err) {
console.error("Ethereum contract. getGameDetails submission error:", err);
} else {
console.log("Ethereum contract. getGameDetails success. Transaction hash:", result);
}
});
}