Skip to content
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

Considering rank service at server side #29

Closed
shingonu opened this issue Mar 25, 2020 · 10 comments
Closed

Considering rank service at server side #29

shingonu opened this issue Mar 25, 2020 · 10 comments

Comments

@shingonu
Copy link
Contributor

before user sign in, we can get all the accounts that have power. but after user sign in, vapp can't detect event for other user's deposit.

so we have to consider to make rank service at server side.

shingonu added a commit that referenced this issue Mar 25, 2020
@shingonu
Copy link
Contributor Author

have to make event listener

@shingonu
Copy link
Contributor Author

myContract.events.MyEvent([options][, callback]) doesn't work.

const sub = this.DepositManager.events.Deposited({
  fromBlock: this.blockNumber,
}, function (error, event){
  console.log(event);
})
  .on('data', function (event){
    console.log(event); // same results as the optional callback above
  })
  .on('changed', function (event){
    // remove event from local database
  })
  .on('error', console.error);

@shingonu
Copy link
Contributor Author

also not working

const subscription = this.web3.eth.subscribe('logs', {
  address: this.DepositManager._address,
}, function (error, result){
  if (error)
    console.log('error', error);
  console.log('result', result);
});

@shingonu
Copy link
Contributor Author

shingonu commented Mar 31, 2020

it works: this.web3.currentProvider.on('data', console.log);

make rootchain blocktime 10 seconds. otherwise we can meet this error again.

@shingonu
Copy link
Contributor Author

reference: MetaMask/metamask-extension#5458

@shingonu
Copy link
Contributor Author

shingonu commented Mar 31, 2020

after block time set 10s, all event listeners work.

so we use this one.

this.DepositManager.events.Deposited({
  fromBlock: this.blockNumber,
}, function (error, event){
  console.log(event);
})
  .on('data', function (event){
    console.log(event); // same results as the optional callback above
  })
  .on('changed', function (event){
    // remove event from local database
  })
  .on('error', console.error);

@shingonu
Copy link
Contributor Author

Klaytn..? 1s?

@shingonu
Copy link
Contributor Author

unsubscribe event listener

@shingonu
Copy link
Contributor Author

shingonu commented Mar 31, 2020

we need to check subscribe function, when same deposited function called under other circumstance.

@shingonu
Copy link
Contributor Author

before sign in:

async setAccountsDepositedWithPower (context) {
  const web3 = context.state.web3;
  const PowerTON = context.state.PowerTON;
  const DepositManager = context.state.DepositManager;

  const depositedEvent = web3.eth.abi.encodeEventSignature('Deposited(address,address,uint256)');
  const deployedAt = DepositManager.transactionConfirmationBlocks;

  // event Deposited(address indexed rootchain, address depositor, uint256 amount);
  const events = await DepositManager.getPastEvents('Deposited', {
    fromBlock: deployedAt,
    toBlock: 'latest',
    topics: [depositedEvent],
  });

  const depositors = uniq(events.map(event => event.returnValues.depositor));
  const accounts = depositors.map(async depositor => {
    const power = await PowerTON.methods.powerOf(depositor).call();
    return {
      address: depositor.toLowerCase(),
      power: _POWER.ray(power.toString()),
    };
  });
  context.commit('SET_ACCOUNTS_DEPOSITED_WITH_POWER', await Promise.all(accounts));
},

after sign in:

async subscribe () {
  this.depositedEventSubscription = this.DepositManager.events.Deposited({
    fromBlock: 'latest',
  }, async (error, event) => {
    if (error) {
      //
    }
    const result = event.returnValues;
    this.$store.dispatch('addAccountDepositedWithPower', result.depositor);
  });
},
async addAccountDepositedWithPower (context, depositor) {
  const PowerTON = context.state.PowerTON;
  const power = await PowerTON.methods.powerOf(depositor).call();
  context.commit('ADD_ACCOUNT_DEPOSITED_WITH_POWER', {
    address: depositor.toLowerCase(),
    power: _POWER.ray(power.toString()),
  });
ADD_ACCOUNT_DEPOSITED_WITH_POWER: (state, accountWithPower) => {
  const findAccount = (account) => account.address.toLowerCase() === accountWithPower.address.toLowerCase();
  const index = state.accountsDepositedWithPower.findIndex(findAccount);

  if (index > -1) {
    Vue.set(state.accountsDepositedWithPower, index, accountWithPower);
  } else {
    state.accountsDepositedWithPower.push(accountWithPower);
  }
},

shingonu added a commit that referenced this issue Mar 31, 2020
@shingonu shingonu closed this as completed Apr 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant