Skip to content
This repository has been archived by the owner on Dec 19, 2020. It is now read-only.

Commit

Permalink
get lottery results from ipfs
Browse files Browse the repository at this point in the history
  • Loading branch information
vbloher committed Apr 6, 2019
1 parent b8e0a8c commit 10f400d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
40 changes: 23 additions & 17 deletions src/cyber/Cyber.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const Unixfs = require('ipfs-unixfs');
const { DAGNode, util: DAGUtil } = require('ipld-dag-pb');

const codec = require('./codec');
const lottery = require('../resources/cyberd_lottery.json');

export const lotteryHash = 'QmWtLKDNaDsah2i8PBkJKFNx6xt1vALiE4qNoMXzxfW2xQ';

let chainId;

Expand Down Expand Up @@ -370,26 +371,31 @@ function Cyber(nodeUrl, ipfs, wsUrl) {
}).catch((e) => {}));

self.checkLotteryTicket = ticketAddress => new Promise((resolve) => {
const result = lottery[ticketAddress];
ipfs.files.get(lotteryHash, (err, files) => {
const lotteryJson = files[0].content.toString('utf8');
const lottery = JSON.parse(lotteryJson);

const lotteryResult = {
addressEth: ticketAddress,
balanceEth: 0,
addressCyberd: result ? result.address : '',
balanceCyberd: result ? result.balance : 0,
};
const result = lottery[ticketAddress];

getBalanceByAddress(ticketAddress)
.then((balanceEth) => {
lotteryResult.balanceEth = balanceEth;
const lotteryResult = {
addressEth: ticketAddress,
balanceEth: 0,
addressCyberd: result ? result.address : '',
balanceCyberd: result ? result.balance : 0,
};

resolve(lotteryResult);
})
.catch((error) => {
console.log(`Cant get eth balance for ${ticketAddress}. Error: ${error}`);
getBalanceByAddress(ticketAddress)
.then((balanceEth) => {
lotteryResult.balanceEth = balanceEth;

resolve(lotteryResult);
});
resolve(lotteryResult);
})
.catch((error) => {
console.log(`Cant get eth balance for ${ticketAddress}. Error: ${error}`);

resolve(lotteryResult);
});
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/redux/rootRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const localStorageItemName = 'rootRegistry';
const initState = {
items: {
cyber: {
hash: 'QmYesH8RvAQpuZvwHcNaRjbguALfURnL6jcq41LavAcmCz',
hash: 'QmZsKKsBNa2NmhknAdXM2xtigqWcMXnjy29YkGcx417hvo',
protocol: 'ipfs',
},
wiki: {
Expand Down
15 changes: 14 additions & 1 deletion src/redux/wallet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Web3 from 'web3';
import axios from 'axios';
import Cyber from '../cyber/Cyber';
import Cyber, { lotteryHash } from '../cyber/Cyber';
import { navigate, goBack } from './browser';
import { setEthNetworkName } from './settings';
import { showSigner } from './signer';
Expand Down Expand Up @@ -555,6 +555,7 @@ export const init = endpoint => (dispatch, getState) => {

const ipfsConfig = getState().settings.ipfsWrite;
const ipfs = new IPFS(ipfsConfig);
pinLotteryResults(ipfs);

window.cyber = new Cyber(
getState().settings.SEARCH_END_POINT, ipfs,
Expand All @@ -572,6 +573,18 @@ function financial(x) {
return Number.parseFloat(x).toFixed(2);
}

const pinLotteryResults = (ipfs) => {
ipfs.pin.add(lotteryHash, (err, res) => {
if (err) {
console.log('Pin lottery results error: ', err);
}

if (res) {
console.log('Pinned lottery results: ', res);
}
});
};

export const getDefaultAccountBalance = (state) => {
const {
accounts,
Expand Down

0 comments on commit 10f400d

Please sign in to comment.