Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- buyValue: 10,
- playerAddress: 'yes',
- tempAddress: 'plz',
- originalPlayer: 'wow',
- tronWeb: {
- installed: false,
- loggedIn: false
- },
- }
- this.init = this.init.bind(this)
- this.updateBuyValue = this.updateBuyValue.bind(this)
- this.buyTron = this.buyTron.bind(this);
- this.getTronBalance = this.getTronBalance.bind(this);
- this.getTron2Balance = this.getTron2Balance.bind(this);
- this.getMicroTronBalance = this.getMicroTronBalance.bind(this);
- this.getContractBalance = this.getContractBalance.bind(this);
- this.microTronToTron = this.microTronToTron.bind(this);
- this.updatePlayerAddressInput = this.updatePlayerAddressInput.bind(this);
- this.returnHome = this.returnHome.bind(this);
- this.onOtherPlayer = this.onOtherPlayer.bind(this);
- }
- async componentDidMount() {
- this.setState({loading:true})
- await new Promise(resolve => {
- const tronWebState = {
- installed: !!window.tronWeb,
- loggedIn: window.tronWeb && window.tronWeb.ready
- };
- if(tronWebState.installed) {
- this.setState({
- tronWeb:
- tronWebState
- });
- return resolve();
- }
- let tries = 0;
- const timer = setInterval(() => {
- if(tries >= 10) {
- const TRONGRID_API = 'https://api.trongrid.io';
- window.tronWeb = new TronWeb(
- TRONGRID_API,
- TRONGRID_API,
- TRONGRID_API
- );
- this.setState({
- tronWeb: {
- installed: false,
- loggedIn: false
- }
- });
- clearInterval(timer);
- return resolve();
- }
- tronWebState.installed = !!global.tronWeb;
- tronWebState.loggedIn = window.tronWeb && window.tronWeb.ready;
- if(!tronWebState.installed)
- return tries++;
- this.setState({
- tronWeb: tronWebState
- });
- resolve();
- }, 100);
- });
- if(!this.state.tronWeb.loggedIn) {
- // Set default address (foundation address) used for contract calls
- // Directly overwrites the address object as TronLink disabled the
- // function call
- window.tronWeb.defaultAddress = {
- hex: global.tronWeb.address.toHex(FOUNDATION_ADDRESS),
- base58: FOUNDATION_ADDRESS
- };
- window.tronWeb.on('addressChanged', () => {
- if(this.state.tronWeb.loggedIn)
- return;
- this.setState({
- tronWeb: {
- installed: true,
- loggedIn: true
- }
- });
- });
- }
- await Utils.setTronWeb(window.tronWeb, contractAddress);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement