1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

move requestFromFaucet function, typed response

This commit is contained in:
Matthias Kretschmann 2019-04-15 14:20:37 +02:00
parent 6ed3cb7842
commit 7203b88b5e
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 57 additions and 42 deletions

View File

@ -1,17 +1,9 @@
import React, { Component } from 'react' import React, { PureComponent } from 'react'
import Web3 from 'web3' import Web3 from 'web3'
import { Logger } from '@oceanprotocol/squid' import { Logger } from '@oceanprotocol/squid'
import { User } from '.' import { User } from '.'
import { provideOcean } from '../ocean' import { provideOcean, requestFromFaucet, FaucetResponse } from '../ocean'
import { nodeHost, nodePort, nodeScheme } from '../config/config'
import {
nodeHost,
nodePort,
nodeScheme,
faucetHost,
faucetPort,
faucetScheme
} from '../config/config'
const POLL_ACCOUNTS = 1000 // every 1s const POLL_ACCOUNTS = 1000 // every 1s
const POLL_NETWORK = POLL_ACCOUNTS * 60 // every 1 min const POLL_NETWORK = POLL_ACCOUNTS * 60 // every 1 min
@ -42,34 +34,11 @@ interface UserProviderState {
network: string network: string
web3: Web3 web3: Web3
ocean: any ocean: any
requestFromFaucet(): Promise<{}> requestFromFaucet(account: string): Promise<FaucetResponse>
message: string message: string
} }
export default class UserProvider extends Component<{}, UserProviderState> { export default class UserProvider extends PureComponent<{}, UserProviderState> {
private accountsInterval: any = null
private networkInterval: any = null
private requestFromFaucet = async () => {
try {
const url = `${faucetScheme}://${faucetHost}:${faucetPort}/faucet`
const response = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
address: this.state.account,
agent: 'commons'
})
})
return response.json()
} catch (error) {
Logger.log('requestFromFaucet', error)
}
}
public state = { public state = {
isLogged: false, isLogged: false,
isLoading: true, isLoading: true,
@ -87,10 +56,13 @@ export default class UserProvider extends Component<{}, UserProviderState> {
), ),
account: '', account: '',
ocean: {} as any, ocean: {} as any,
requestFromFaucet: this.requestFromFaucet, requestFromFaucet: () => requestFromFaucet(''),
message: 'Connecting to Ocean...' message: 'Connecting to Ocean...'
} }
private accountsInterval: any = null
private networkInterval: any = null
public async componentDidMount() { public async componentDidMount() {
await this.bootstrap() await this.bootstrap()
@ -162,7 +134,11 @@ export default class UserProvider extends Component<{}, UserProviderState> {
// Get accounts // Get accounts
await this.fetchAccounts() await this.fetchAccounts()
this.setState({ isLoading: false }) this.setState({
isLoading: false,
requestFromFaucet: () =>
requestFromFaucet(this.state.account)
})
// Set proper network names now that we have Ocean // Set proper network names now that we have Ocean
this.fetchNetwork() this.fetchNetwork()
@ -218,10 +194,14 @@ export default class UserProvider extends Component<{}, UserProviderState> {
const accounts = await ocean.accounts.list() const accounts = await ocean.accounts.list()
if (accounts.length > 0) { if (accounts.length > 0) {
const account = accounts[0].getId() const account = await accounts[0].getId()
if (account !== this.state.account) { if (account !== this.state.account) {
this.setState({ account, isLogged: true }) this.setState({
account,
isLogged: true,
requestFromFaucet: () => requestFromFaucet(account)
})
} }
const balance = await accounts[0].getBalance() const balance = await accounts[0].getBalance()

View File

@ -1,4 +1,4 @@
import { Ocean } from '@oceanprotocol/squid' import { Ocean, Logger } from '@oceanprotocol/squid'
import Web3 from 'web3' import Web3 from 'web3'
import { import {
@ -9,6 +9,9 @@ import {
brizoPort, brizoPort,
brizoScheme, brizoScheme,
brizoAddress, brizoAddress,
faucetHost,
faucetPort,
faucetScheme,
nodeHost, nodeHost,
nodePort, nodePort,
nodeScheme, nodeScheme,
@ -43,3 +46,32 @@ export async function provideOcean(web3provider: Web3) {
return { ocean } return { ocean }
} }
//
// Faucet
//
export interface FaucetResponse {
success: boolean
message: string
trxHash?: string
}
export async function requestFromFaucet(account: string) {
try {
const url = `${faucetScheme}://${faucetHost}:${faucetPort}/faucet`
const response = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
address: account,
agent: 'commons'
})
})
return response.json()
} catch (error) {
Logger.log('requestFromFaucet', error)
}
}

View File

@ -1,4 +1,5 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import { FaucetResponse } from '../ocean'
import Route from '../components/templates/Route' import Route from '../components/templates/Route'
import Button from '../components/atoms/Button' import Button from '../components/atoms/Button'
import Spinner from '../components/atoms/Spinner' import Spinner from '../components/atoms/Spinner'
@ -21,7 +22,9 @@ export default class Faucet extends PureComponent<{}, FaucetState> {
trxHash: undefined trxHash: undefined
} }
private getTokens = async (requestFromFaucet: () => any) => { private getTokens = async (
requestFromFaucet: () => Promise<FaucetResponse>
) => {
this.setState({ isLoading: true }) this.setState({ isLoading: true })
try { try {