Skip to content

Instantly share code, notes, and snippets.

Created July 12, 2017 22:36
Show Gist options
  • Save anonymous/6e4f27981c7f6d3bbea66919e39f198a to your computer and use it in GitHub Desktop.
Save anonymous/6e4f27981c7f6d3bbea66919e39f198a to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from "@angular/http";
import { Observable } from "rxjs";
import Web3 from 'web3'; /** I think this is equivalent
to the require function. I'm also doing 'web3/lib because that is where the web3.js file is*/
let web3:Web3 = new Web3();
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} /** set the provider you want from Web3.providers */
else {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
@Injectable()
export class SharedService {
currencyURL = "http://api.fixer.io/latest?symbols=";
totReqsMade: number = 0;
constructor(private _http: Http) {
}
getBalance(Add){
return this._http.get(this.currencyURL + Add)
.map(response => {
{ return response.json() };
})
.catch(error => Observable.throw(error.json()));
}
getEtherBalance(PubAd) {
return web3.eth.getBlock(48, function(error, result){
if(!error)
console.log(result)
else
console.error(error);
})
}
/** let web3 = require('web3'); Trying to instantiate how the website says to do it.*/
}
@carlosm2011
Copy link

Now I'm having trouble with these lines

return  this.web3.eth.getBalance(PubAd, function(error, result){
    if(!error)

        /**console.log(String(result));*/
    	console.log(this.web3.fromWei(String(result), 'ether'));
    else
        console.error(error);
})
            .map(response => 
                response.json()
            )

I'm getting two errors

  1. ERROR TypeError: Cannot read property 'map' of undefined.
  2. Cannot read property 'fromWei' of undefined

@DeviateFish
Copy link

Looks like you're having scoping issues... Look into how scoping works in JavaScript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment