-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprovider.js
145 lines (122 loc) · 3.98 KB
/
provider.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import oslikiClassifieds from './contracts/OslikiClassifieds.json'
import oslikToken from './contracts/OslikToken.json'
//import IPFS from 'ipfs'
const network = process.env.NODE_ENV === 'production' ? 'main' : 'rinkeby'
const contractAbi = oslikiClassifieds.abi
const contractAddress = oslikiClassifieds.networks[network].address
const contractTokenAbi = oslikToken.abi
const contractTokenAddress = oslikToken.networks[network].address
let web3, contract, contractToken, onChangeAccount, account, getIpfs, isMetaMaskAvail = false
//if (window.web3) {
if (window.web3) {
isMetaMaskAvail = true
web3 = new window.Web3(window.web3.currentProvider)
} else {
web3 = new window.Web3(new window.Web3.providers.HttpProvider(`https://${network === 'main' ? 'mainnet' : 'rinkeby'}.infura.io/8pRTWGvu6RdXVYyLnOqv`))
}
contract = new web3.eth.Contract(contractAbi, contractAddress)
contractToken = new web3.eth.Contract(contractTokenAbi, contractTokenAddress)
//web3.bzz.setProvider('http://swarm-gateways.net')
const subscribers = []
onChangeAccount = (callback) => {
subscribers.push(callback)
}
const loop = async () => {
let accounts
try {
accounts = await web3.eth.getAccounts() // if tab is opened on open browser, getAccounts never return result and loop will die
console.log('accounts[0]', accounts[0])
} catch(error) {
console.log('loop', error)
}
if (accounts && accounts[0] !== account) {
account = accounts[0]
subscribers.forEach(callback => callback(account))
}
setTimeout(loop, 2000)
}
if (isMetaMaskAvail) loop()
/*** IPFS ***/
const config = {
repo: 'osliki-classi',
EXPERIMENTAL: {
pubsub: true,
relay: {
enabled: true,
hop: {
enabled: true,
//active: true
}
}
},
config: {
Addresses: {
Swarm: [
'/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star',
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
]
},
Bootstrap: [
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/dns4/wss0.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
'/dns4/wss1.bootstrap.libp2p.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
'/dns4/bootstrap.osliki.net/tcp/443/wss/ipfs/QmfJB77qXfiEdJkaSxpZgiMh9kAPiDBj3ga7TxF72QdWtf',
'/p2p-circuit/ipfs/QmfJB77qXfiEdJkaSxpZgiMh9kAPiDBj3ga7TxF72QdWtf',
]
}
}
let ipfs = new window.Ipfs(config)
ipfs.on('error', (error) => {
console.error('js-ipfs node errored', error)
})
ipfs.on('start', () => {
ipfs.id().then(i=>console.log('ipfs id', i))
ipfs.swarm.peers().then(i=>console.log('ipfs peers', i))
console.log('ipfs Online status: ', ipfs.isOnline() ? 'online' : 'offline')
ipfs.bootstrap.list().then(i=>console.log('ipfs list', i))
})
//
// ipfs.once('ready', async () => {
// // ipfs.start()
// /*ipfs.once('start', () => {
// timeout()
// })*/
// })
/*
const timeout = () => {
setTimeout(async () => {
ipfs.stop()
ipfs.once('stop', () => {
ipfs.start()
ipfs.once('start', () => {
timeout()
})
})
}, 60000)
}*/
getIpfs = () => {
return new Promise(resolve => {
if (ipfs.isOnline()) {
resolve(ipfs)
} else {
ipfs.once('start', () => {
resolve(ipfs)
})
}
})
}
//}
export {
contract,
contractToken,
web3,
account,
onChangeAccount,
getIpfs,
isMetaMaskAvail
}