项目作者: decent-bet

项目描述 :
Vuex enhanced action to add @decent-bet/connex-entities capabilities to vuex
高级语言: TypeScript
项目地址: git://github.com/decent-bet/vuex-connex-entities.git
创建时间: 2019-03-08T14:18:39Z
项目社区:https://github.com/decent-bet/vuex-connex-entities

开源协议:MIT License

下载


Vuex Connex Entities

Vuex Connex Entities allows to add @decent-bet/connex-entities capabilities to vuex. It consist for now in an enhanced action that add three new parameters to every action created based on it.

  • getContract(): return a contract instance of a class based on @decent-bet/connex-entities.
  • requestExternalWalletAccess(): request access to an external wallet.
  • walletInfo: an object that store the public address and the current chain tag, check the interface WalletInfo.

Getting started

Install: npm i --save @decent-bet/vuex-connex-entities
Follow the instructions to setup a contract with @decent-bet/connex-entities.

Using the enhanced action

Frist of all you need to add some mutations to your store or module of your store:
``` typescript
import { connexEntitiesMutations } from ‘@decent-bet/vuex-connex-entities’;


mutations: {
// your mutations
…connexEntitiesMutations
},

  1. > Get the wallet access:
  2. ***You should call `requestExternalWalletAccess()` before any access to `walletInfo` or call to `getContract()`***
  3. ``` typescript
  4. import { ActionContext } from 'vuex';
  5. import { connexAction } from '@decent-bet/vuex-connex-entities';
  6. import { MySubState } from './MySubState'; // use your own state definitions
  7. import { MyRootState } from '../MyRootState'; // use your own state definitions
  8. import { MyConnexEntityContract } from '../MyConnexEntityContract'; // created using @decent-bet/connex-entities.
  9. // the connexAction receive a type of the return, in this case Promise<void>,
  10. // the second param is a function and can be awaitable like in this case
  11. const getWalletAccess = connexAction<Promise<void>>(async <MySubState, MyRootState>(
  12. context: ActionContext<MySubState, MyRootState>) => {
  13. const {
  14. commit,
  15. requestExternalWalletAccess
  16. } = context;
  17. const result: boolean = await requestExternalWalletAccess();
  18. if (result) {
  19. // the user allowed the wallet access
  20. } else {
  21. // the user not confirm or not has the installed extension
  22. }
  23. commit('WALLET_ACCESS_ACTION', result);
  24. });

Access to the wallet info:
``` typescript
// get the wallet info, you only be able to access the walletInfo after call to requestExternalWalletAccess() method
const setupWappet = connexAction>((
context: ActionContext, balance?: any
) => {
const {
commit,
walletInfo // WalletInfo
} = context;

// access to the public address and the chain tag
const { publicAddress, chainTag } = walletInfo;
console.log(My address is: ${publicAddress} and the chainTag is: ${chainTag});
commit(‘SETUP_WALLET_ACTION’, walletInfo);
});

  1. > Get an entity contract and call to a method:
  2. ``` typescript
  3. // get any created contract based on @decent-bet/connex-entities
  4. const getBalance = connexAction<Promise<void>>(async <MySubState, MyRootState>(
  5. context: ActionContext<MySubState, MyRootState>, balance?: any
  6. ) => {
  7. const {
  8. commit,
  9. getContract
  10. } = context;
  11. // get the class instace of MyConnexEntityContract class
  12. const contract = getContract(MyConnexEntityContract);
  13. const balance = await contract.myBalance();
  14. commit('GET_BALANCE_ACTION', balance);
  15. });