1.1.41 • Published 1 month ago

@metis.io/middleware-client v1.1.41

Weekly downloads
-
License
ISC
Repository
github
Last release
1 month ago

Nuvo sdk

Change log

  • v1.1.41
    • support bitget wallet

Sdk install

npm install --save-dev @metis.io/middleware-client

To enable a user of a website/app to log into Nuvo and access its functionalities such as checking balance and sending transaction, follows these high level steps:

  1. Acquire access token using Oauth2 client
  2. Use the acquired access token to initialize a PolisProvider or PolisClient Object
  3. Use the methods provided by PolisProvider or PolisClient to execute user's Nuvo account's functionalities.

First we use this code example to demostrate how to initialize an Oauth2 client and acquire an access token from a user's account:

Oauth2 client

import & create

    import { Oauth2Client } from '@metis.io/middleware-client'

    let oauth2Client = new Oauth2Client()

start oauth

/**
 * switchAccount = true:  Does not automatically log in,default:false
 * newWindow: default false
 */

oauth2Client.startOauth2(`APPID`, `RETURN URL`, `newWindow`,`switchAccount`); 

The APPID and RETURN URL can get from Polis Developer User page

request access token & refresh token on RETURN URL page in backend

    get(`https://polis.metis.io/api/v1/oauth2/access_token?app_id=${this.appid}&app_key=${this.appsecret}&code=${this.code}`)

    // if success
    res => {
        if (res.status == 200 && res.data && res.data.code == 200) {
          // res.data.data.access_token
          // res.data.data.refresh_token
          // res.data.data.expires_in
        }
    }      

refresh token

    const oauth2User = await oauth2Client.refreshTokenAsync(`APPID`, `RefreshToken`)

get user info

    const userInfo = await oauth2Client.getUserInfoAsync(`AccessToken`)

    // user info struct {
        'display_name': '',
        'username': '',
        'email': '',
        'eth_address': '',
        'last_login_time': timestamp
    }

oauth logout

// refreshToken:options When refreshtoken is not empty, refreshtoken will also be deleted and cannot be used.
 logout(appId:string, accessToken:string, refreshToken:string="").then(res=>{
    //res = {
    //     status: 200 
    //     msg: ""
    // }
})
.catch(res=>{
    // res = {
    //     status: -90016
    //     msg: ""
    // }
})

Once we acquired the access token, we can use either a PolisProvider object that is compatible with ethers provider, or a PolisClient object to access user account's functionalities.

1、Use Ethers Web3Provider

step 1 IPolisProviderOpts

const opts: IPolisProviderOpts = {
            apiHost: 'https://api.nuvosphere.io/',  // api host
            oauthHost?: "", //oauth login host, options
            token?: {accessToken}, //optional oauth2 access token 
            chainId: 4,
        }
const polisprovider = new PolisProvider(opts)

step 2 Ethers Web3 Provider

ethers.js

ethersProvider = new ethers.providers.Web3Provider(polisprovider)

2、 Use Polis Client

step 1

const clientOps:IPolisClientOpts = {
    chainId: CHAIN_ID,
    appId:APP_ID,
    apiHost :apiHost
}
client = new PolisClient(clientOps);
client.web3Provider.getBalance("address")
/**
 * oauthInfo:  get from api: api/v1/oauth2/access_token or token string
 * 
 */
client.connect(oauthInfo);
// 1.1.17 later
await client.connect(oauthInfo);

Polis Client Events

//event of debug
 this.polisclient.on('debug', function (data) {
        console.log('debug data:%s', JSON.stringify(data));
 })
// event of error
this.polisclient.on('error', function (data) {
    console.log('error:', data instanceof Error)
});
//when metamask wallet
this.polisclient.on('chainChanged', (chainId) => {
    console.log('polis-client print chainId =>', chainId);
});
this.polisclient.on('accountsChanged', (account) => {
    console.log('polis-client print account =>', account);
});

step 2 get Web3 Provider

ethersProvider=client.web3Provider // ethers.providers.Web3Provider

ethersProvider.getSinger().signMessage("aa")


const daiAddress = this.contract.address;

const daiAbi = [
    // Some details about the token
    "function name() view returns (string)",
    "function symbol() view returns (string)",

    // Get the account balance
    "function balanceOf(address) view returns (uint)",

    // Send some of your tokens to someone else
    "function transfer(address to, uint amount)",

    // An event triggered whenever anyone transfers to someone else
    "event Transfer(address indexed from, address indexed to, uint amount)"
];

const daiContract = client.getContract(daiAddress, daiAbi);
await daiContract.name();
1.1.41

1 month ago

1.1.40

4 months ago

1.1.39

4 months ago

1.1.34

8 months ago

1.1.33

9 months ago

1.1.38

6 months ago

1.1.37

6 months ago

1.1.36

6 months ago

1.1.35

8 months ago

1.1.3-6.beta-2

6 months ago

1.1.3-6.beta-3

6 months ago

1.1.36-beta.2

6 months ago

1.1.36-beta.1

6 months ago

1.1.32

12 months ago

1.1.32-beta.5

12 months ago

1.1.32-beta.4

12 months ago

1.1.32-beta.7

12 months ago

1.1.32-beta.6

12 months ago

1.1.3-2.beta.1

1 year ago

1.1.32-beta.1

1 year ago

1.1.32-beta.3

1 year ago

1.1.32-beta.2

1 year ago

1.1.31-beta.2

1 year ago

1.1.31

1 year ago

1.1.31-beta.1

1 year ago

1.1.29

1 year ago

1.1.28

1 year ago

1.1.30

1 year ago

1.1.29-beta.0

1 year ago

1.1.29-beta

1 year ago

1.1.27

1 year ago

1.1.26

1 year ago

1.1.23

2 years ago

1.1.22

2 years ago

1.1.25

2 years ago

1.1.24

2 years ago

1.1.18

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.12

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.17

2 years ago

1.1.1

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.0

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.19

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago