Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active March 4, 2019 16:48
Show Gist options
  • Save alexytiger/818513185cc5ea99a51587fffda1e369 to your computer and use it in GitHub Desktop.
Save alexytiger/818513185cc5ea99a51587fffda1e369 to your computer and use it in GitHub Desktop.
injection token for Web3 and TruffleContract
import { InjectionToken} from '@angular/core';
import Web3 from 'web3';
import TruffleContract from 'truffle-contract';
import ContractAbi from '../../../../build/contracts/PokemonAttack.json';
export const WEB3 = new InjectionToken<Web3>('web3Token', {
providedIn: 'root',
factory: () => {
// based on https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8
try {
const provider = ('ethereum' in window) ? window['ethereum'] : Web3.givenProvider;
return new Web3(provider);
} catch (err) {
throw new Error('Unable to retrieve the injected Ethereum provider from MetaMask');
}
}
});
export const SmartContract = new InjectionToken<TruffleContract>('smartContract', {
providedIn: 'root',
factory: () => TruffleContract(ContractAbi),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment