scatter使用eosjs 20以上版本

通常eos的dapp开发都是使用scatterjs来连接用户或者钱包,scatter的官方文档中的示例基本上都是使用eos 16版本的,eos20版本由于重构的关系,与eos 16版本完全不同,因此,这里给出eos 20的使用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import ScatterJS from 'scatterjs-core';
import ScatterEOS from 'scatterjs-plugin-eosjs2';
import {Api, JsonRpc} from 'eosjs'; // eos20


// Don't forget to tell ScatterJS which plugins you are using.
ScatterJS.plugins( new ScatterEOS() );

const network = {
blockchain:'eos',
protocol:'https',
host:'nodes.get-scatter.com',
port:443,
// protocol: 'http',
// host: '178.62.36.41',
// port: 8888,
chainId:'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906'
}

ScatterJS.scatter.connect("dappdemo").then(connected => {
// User does not have Scatter Desktop, Mobile or Classic installed.
if(!connected) return false;

console.log('connected!');
const scatter = ScatterJS.scatter;
let eosOptions = {};
eosOptions.rpc = new JsonRpc(jungle.protocol + '://' + jungle.host + ':' + jungle.port)
eosOptions.beta3 = true;
var eos = scatter.eos(jungle, Api, eosOptions);
eos.transact({
actions: [{
account: 'eosio.token',
name: 'transfer',
authorization: [{
actor: 'tianji111222',
permission: 'active',
}],
data: {
from: 'tianji111222',
to: 'tianji111333',
quantity: '1.0000 EOS',
memo: 'eos20',
},
}]}, {
blocksBehind: 3,
expireSeconds: 30,
}).then(function(data) {
console.log('transfer return data:', data);
}).catch(function(err) {
console.log('err:', err)
// console.log(data);
});

需要特别注意的是:

  1. 在创建eos实例时,eosOptions的rpc必须先初始化;
  2. eos20-beta3之后的版本(目前),eosOptions的rpc必须设置为true;