-
Notifications
You must be signed in to change notification settings - Fork 61
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
Connect with Decentraland IPFS node #75
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b59d0a
0.2.10
cazala 71cf7d6
:arrow_upper_right: env var for ipfs url
nachomazzara ea7b467
:arrow_upper_right: update decentraland commons
nachomazzara 1cc9347
:new: beforeUnload event
nachomazzara b98d30a
:twisted_rightwards_arrows: refactor
nachomazzara 5b6cc39
:arrow_upper_right: move ipfs url to gh-pages, scene.html as default …
nachomazzara f6c6f32
:twisted_rightwards_arrows: refactor and prettier
nachomazzara 876b310
:recycle: merge with master
nachomazzara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
LAND_REGISTRY_CONTRACT_ADDRESS= | ||
IPFS_GATEWAY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
import "babel-polyfill"; | ||
import React from 'react'; | ||
import Router from 'next/router'; | ||
import { eth } from 'decentraland-commons'; | ||
import { LANDRegistry } from 'decentraland-commons/dist/contracts/LANDRegistry'; | ||
import { eth, txUtils, contracts } from 'decentraland-commons'; | ||
const { LANDRegistry } = contracts | ||
|
||
async function ethereum() { | ||
const { address } = await getContractAddress() | ||
const land = new LANDRegistry(address) | ||
|
||
await eth.connect({ contracts: [land]}) | ||
await eth.connect({ | ||
contracts: [land] | ||
}) | ||
|
||
return { | ||
address: await eth.getAddress(), | ||
land, | ||
web3: eth.web3 | ||
web3: eth.web3, | ||
land | ||
} | ||
} | ||
|
||
|
@@ -33,38 +35,62 @@ async function getIPFSKey() { | |
return ipfsKey; | ||
} | ||
|
||
async function getPeerId() { | ||
const res = await fetch('/api/get-ipfs-peerid'); | ||
const peerId = await res.json(); | ||
return peerId; | ||
} | ||
|
||
async function closeServer(ok, message) { | ||
console.log('closing server:', message) | ||
await fetch(`/api/close?ok=${ok}&reason=${message}`); | ||
} | ||
|
||
async function pinFiles (peerId, x, y) { | ||
const res = await fetch(`/api/pin-files/${peerId}/${x}/${y}`); | ||
const { ok } = await res.json() | ||
return ok | ||
} | ||
|
||
export default class Page extends React.Component { | ||
constructor(...args) { | ||
super(...args); | ||
|
||
this.state = { | ||
loading: true, | ||
transactionLoading: false, | ||
pinningLoading: false, | ||
error: false, | ||
address: null, | ||
tx: null | ||
} | ||
|
||
this.onUnload = this.onUnload.bind(this) | ||
} | ||
|
||
onUnload(event) { | ||
event.returnValue = 'Please, wait until the transaction and pinning are completed' | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener("beforeunload", this.onUnload) | ||
} | ||
|
||
async componentDidMount() { | ||
try { | ||
window.addEventListener("beforeunload", this.onUnload) | ||
try { | ||
let land, address, web3 | ||
|
||
try { | ||
const res = await ethereum() | ||
land = res.land | ||
address = res.address | ||
web3 = res.web3 | ||
|
||
this.setState({ | ||
loading: false, | ||
address | ||
}) | ||
} catch(err) { | ||
console.log(err.message) | ||
this.setState({ | ||
error: `Could not connect to MetaMask` | ||
}); | ||
|
@@ -84,7 +110,8 @@ export default class Page extends React.Component { | |
|
||
try { | ||
const ipfsKey = await getIPFSKey(); | ||
this.setState({ ipfsKey }); | ||
const peerId = await getPeerId(); | ||
this.setState({ ipfsKey, peerId }); | ||
} catch(err) { | ||
this.setState({ | ||
error: `There was a problem getting IPNS hash of your scene.\nTry to re-upload with dcl upload.` | ||
|
@@ -106,7 +133,7 @@ export default class Page extends React.Component { | |
let oldData | ||
try { | ||
console.log('oldData coordinates', coordinates[0].x, coordinates[0].y) | ||
oldData = await land.getData(coordinates[0].x, coordinates[0].y) | ||
oldData = await land.landData(coordinates[0].x, coordinates[0].y) | ||
console.log('oldData data', oldData) | ||
} catch(e) { | ||
console.error('oldData error', e) | ||
|
@@ -129,8 +156,8 @@ export default class Page extends React.Component { | |
try { | ||
console.log('update land data', coordinates, data) | ||
const tx = await land.updateManyLandData(coordinates, data) | ||
this.setState({ tx }) | ||
closeServer(true, 'transaction successful') | ||
this.watchTransactions(tx, coordinates[0].x, coordinates[0].y) | ||
this.setState({ tx, transactionLoading: true }) | ||
} catch(err) { | ||
this.setState({loading: false, error: 'Transaction Rejected'}) | ||
closeServer(false, 'transaction rejected') | ||
|
@@ -141,6 +168,22 @@ export default class Page extends React.Component { | |
} | ||
} | ||
|
||
async watchTransactions (txId, x, y) { | ||
const {peerId} = this.state | ||
const tx = await txUtils.waitForCompletion(txId) | ||
if (!txUtils.isFailure(tx)) { | ||
this.setState({ transactionLoading: false, pinningLoading: true }) | ||
const success = await pinFiles(peerId, x, y) | ||
this.setState({ | ||
pinningLoading: false, | ||
error: !success ? 'Failed pinning files to ipfs' : null | ||
}) | ||
} else { | ||
this.setState({transactionLoading: false, error: 'Transaction failed'}) | ||
} | ||
window.removeEventListener("beforeunload", this.onUnload) | ||
} | ||
|
||
renderTxHash = () => ( | ||
this.state.tx ? ( | ||
<p>Transaction:<br /> | ||
|
@@ -155,6 +198,22 @@ export default class Page extends React.Component { | |
this.state.error ? <p style={{ color: 'red' }}>{this.state.error}</p> : null | ||
) | ||
|
||
renderTransactionStatus = () => ( | ||
this.state.tx ? | ||
!this.state.transactionLoading ? | ||
<p style={{ color: 'green' }}>{`Transaction confirmed.`}</p> | ||
: <p style={{ color: 'orange' }}>{`Transaction pending. Will take a while...`}</p> | ||
: null | ||
) | ||
|
||
renderPinningIPFSStatus = () => ( | ||
!this.state.error && this.state.tx && !this.state.transactionLoading ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if(..) {
return
} else if(..) {
return
}
return null Is probably easier to read |
||
!this.state.pinningLoading ? | ||
<p style={{ color: 'green' }}>{`Pinning Success.`}</p> | ||
: <p style={{ color: 'orange' }}>{`Pinning pending. Will take a while...`}</p> | ||
: null | ||
) | ||
|
||
render() { | ||
return ( | ||
<div className="dcl-linker-main"> | ||
|
@@ -163,8 +222,10 @@ export default class Page extends React.Component { | |
<p>MetaMask address:<br /> | ||
{this.state.loading ? "loading..." : this.state.address} | ||
</p> | ||
{this.renderTxHash()} | ||
{this.renderError()} | ||
{ this.renderTxHash() } | ||
{ this.renderTransactionStatus() } | ||
{ this.renderPinningIPFSStatus() } | ||
{ this.renderError() } | ||
<style jsx>{` | ||
.dcl-icon { | ||
width: 52px; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import axios from 'axios'; | ||
import { env } from 'decentraland-commons'; | ||
env.load(); | ||
|
||
export const getIPFSURL = async () => { | ||
let ipfsURL: string = null; | ||
try { | ||
const { data } = await axios.get('https://decentraland.github.io/ipfs-node/url.json'); | ||
ipfsURL = data.staging; | ||
} catch (error) { | ||
// fallback to ENV | ||
} | ||
|
||
return env.get('IPFS_GATEWAY', () => ipfsURL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this watch the transaction for the whole Estate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait until transaction is completed by success or fail