Skip to content

Commit bb5873f

Browse files
author
James Calfee
committed
Single constructor (deprecates EosApi.Testnet and EosApi.Localnet) EOSIO#68
1 parent 69ae0fb commit bb5873f

8 files changed

+135
-132
lines changed

README.md

+24-26
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ General purpose library for the EOS blockchain.
1919
```javascript
2020
Eos = require('eosjs') // Eos = require('./src')
2121

22-
eos = Eos.Localnet() // 127.0.0.1:8888
22+
eos = Eos() // 127.0.0.1:8888
2323

2424
// All API methods print help when called with no-arguments.
2525
eos.getBlock()
@@ -39,7 +39,6 @@ eos.getBlock({block_num_or_id: 1}, callback)
3939

4040
// Provide an empty object or a callback if an API call has no arguments
4141
eos.getInfo({}).then(result => {console.log(result)})
42-
4342
```
4443

4544
API methods and documentation are generated from:
@@ -62,11 +61,11 @@ config = {
6261
},
6362
expireInSeconds: 60,
6463
broadcast: true,
65-
debug: false,
64+
debug: false, // API and transactions
6665
sign: true
6766
}
6867

69-
eos = Eos.Localnet(config)
68+
eos = Eos(config)
7069
```
7170

7271
* `mockTransactions` (optional)
@@ -107,12 +106,12 @@ options = {
107106

108107
### Usage (read/write)
109108

110-
If you use the Testnet, you'll need to replace the private key in keyProvider.
109+
You'll need to provide the private key in keyProvider.
111110

112111
```javascript
113112
Eos = require('eosjs') // Eos = require('./src')
114113

115-
eos = Eos.Localnet({keyProvider: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'})
114+
eos = Eos({keyProvider: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'})
116115

117116
// Run with no arguments to print usage.
118117
eos.transfer()
@@ -153,7 +152,7 @@ Eos = require('eosjs') // Eos = require('./src')
153152
wif = '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'
154153
pubkey = 'EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV'
155154

156-
eos = Eos.Localnet({keyProvider: wif})
155+
eos = Eos({keyProvider: wif})
157156

158157
eos.transaction(tr => {
159158
tr.newaccount({
@@ -200,7 +199,7 @@ Import and include the library when you configure Eos:
200199

201200
```javascript
202201
binaryen = require('binaryen')
203-
eos = Eos.Localnet({..., binaryen})
202+
eos = Eos({..., binaryen})
204203
```
205204

206205
Complete example:
@@ -215,9 +214,9 @@ keyProvider = '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'
215214
//
216215
// $ npm install [email protected]
217216
// binaryen = require('binaryen')
218-
// eos = Eos.Localnet({keyProvider, binaryen})
217+
// eos = Eos({keyProvider, binaryen})
219218

220-
eos = Eos.Localnet({keyProvider})
219+
eos = Eos({keyProvider})
221220

222221
wasm = fs.readFileSync(`docker/contracts/eosio.token/eosio.token.wasm`)
223222
abi = fs.readFileSync(`docker/contracts/eosio.token/eosio.token.abi`)
@@ -243,7 +242,7 @@ keyProvider = [
243242
Eos.modules.ecc.seedPrivate('currency')
244243
]
245244

246-
eos = Eos.Localnet({keyProvider})
245+
eos = Eos({keyProvider})
247246

248247
// if either transfer fails, both will fail (1 transaction, 2 messages)
249248
eos.transaction(eos =>
@@ -288,7 +287,7 @@ A manual transaction provides for more flexibility.
288287
```javascript
289288
Eos = require('eosjs') // Eos = require('./src')
290289

291-
eos = Eos.Localnet({keyProvider: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'})
290+
eos = Eos({keyProvider: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'})
292291

293292
// returns Promise
294293
eos.transaction({
@@ -324,28 +323,24 @@ import from `./src` instead.
324323

325324
```javascript
326325
Eos = require('./src')
327-
328-
// Creating the instance `eos` means that common blockchain data-structures are
329-
// available for a given network (Testnet, Mainnet, etc).
330-
eos = Eos.Localnet()
326+
eos = Eos()
331327
```
332328

333329
* Fcbuffer
334330

335-
The `eos` instance can provide more convenient serialization:
331+
The `eos` instance can provide serialization:
336332

337333
```javascript
338334
// 'asset' is a type but could be any struct or type like: transaction or uint8
339335
type = {type: 1, data: '00ff'}
340336
buffer = eos.fc.toBuffer('extensions_type', type)
341337
assert.deepEqual(type, eos.fc.fromBuffer('extensions_type', buffer))
342338

343-
// Serialization for a smart-contract's Abi:
344-
eos.contract('currency', (error, c) => currency = c)
345-
create = {issuer: 'inita', maximum_supply: '1.0000 4,CUR'}
346-
buffer = currency.fc.toBuffer('create', create)
347-
create.maximum_supply = '1.0000 CUR'
348-
assert.deepEqual(create, currency.fc.fromBuffer('create', buffer))
339+
// ABI Serialization
340+
eos.contract('eosio.token', (error, c) => eosio_token = c)
341+
create = {issuer: 'inita', maximum_supply: '1.0000 SYS'}
342+
buffer = eosio_token.fc.toBuffer('create', create)
343+
assert.deepEqual(create, eosio_token.fc.fromBuffer('create', buffer))
349344
```
350345

351346
Use Node v8+ to `package-lock.json`.
@@ -357,7 +352,7 @@ need to use them directly. They are exported here giving more API access or
357352
in some cases may be used standalone.
358353

359354
```javascript
360-
var {api, ecc, json, Fcbuffer, format} = Eos.modules
355+
var {format, api, ecc, json, Fcbuffer} = Eos.modules
361356
```
362357
* format [./format.md](./docs/format.md)
363358
* Blockchain name validation
@@ -393,13 +388,16 @@ git clone https://github.com/EOSIO/eosjs.git
393388
cd eosjs
394389
npm install
395390
npm run build_browser
396-
# builds: ./dist/eos.js
391+
# builds: ./dist/eos.js load with ./dist/index.html
392+
393+
npm run build_browser_test
394+
# builds: ./dist/test.js run with ./dist/test.html
397395
```
398396

399397
```html
400398
<script src="eos.js"></script>
401399
<script>
402-
var eos = Eos.Testnet()
400+
var eos = Eos()
403401
//...
404402
</script>
405403
```

dist/index.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
<meta charset="utf-8">
44
<script src="eos.js"></script>
55
<script>
6-
eos = Eos.Testnet()
6+
eos = Eos({
7+
httpEndpoint: 'http://ayeaye.cypherglass.com:8888',
8+
chainId: 'a628a5a6123d6ed60242560f23354c557f4a02826e223bb38aad79ddeb9afbca',
9+
debug: true
10+
})
711
</script>
812
</head>
913
<body>

package-lock.json

+3-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"babel-polyfill": "^6.26.0",
4747
"binaryen": "^37.0.0",
4848
"create-hash": "^1.1.3",
49-
"eosjs-api": "6.1.2",
49+
"eosjs-api": "6.1.3",
5050
"eosjs-ecc": "4.0.1",
5151
"fcbuffer": "2.2.0"
5252
},

src/index.js

+52-45
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,71 @@ try {
77
}
88

99
const ecc = require('eosjs-ecc')
10-
const json = {
11-
api: require('eosjs-api').api,
12-
schema: require('./schema')
13-
}
14-
1510
const Fcbuffer = require('fcbuffer')
16-
const api = require('eosjs-api')
11+
const EosApi = require('eosjs-api')
12+
const assert = require('assert')
1713

1814
const Structs = require('./structs')
1915
const AbiCache = require('./abi-cache')
2016
const AssetCache = require('./asset-cache')
2117
const writeApiGen = require('./write-api')
22-
const assert = require('assert')
2318
const format = require('./format')
24-
19+
const schema = require('./schema')
2520
const pkg = require('../package.json')
26-
const Eos = {
27-
version: pkg.version
28-
}
29-
30-
module.exports = Eos
31-
32-
Eos.modules = {
33-
json,
34-
ecc,
35-
api,
36-
Fcbuffer,
37-
format
38-
}
3921

4022
const configDefaults = {
4123
broadcast: true,
4224
debug: false,
4325
sign: true
4426
}
4527

46-
function development(Network) {
47-
Network.schema = json.schema
48-
return (config = {}) => {
49-
config = Object.assign({}, configDefaults, config)
50-
const network = Network(Object.assign({}, {
51-
apiLog: consoleObjCallbackLog(config.verbose)},
52-
config
53-
))
54-
const eosConfig = Object.assign({}, {
55-
transactionLog: consoleObjCallbackLog(config.verbose)},
56-
config
57-
)
58-
return createEos(eosConfig, Network, network)
28+
const Eos = (config = {}) => createEos(
29+
Object.assign(
30+
{},
31+
{
32+
apiLog: consoleObjCallbackLog(config.verbose),
33+
transactionLog: consoleObjCallbackLog(config.verbose),
34+
},
35+
configDefaults,
36+
config
37+
)
38+
)
39+
40+
module.exports = Eos
41+
42+
Object.assign(
43+
Eos,
44+
{
45+
version: pkg.version,
46+
modules: {
47+
format,
48+
api: EosApi,
49+
ecc,
50+
json: {
51+
api: EosApi.api,
52+
schema
53+
},
54+
Fcbuffer
55+
},
56+
57+
/** @deprecated */
58+
Testnet: function (config) {
59+
console.error('deprecated, change Eos.Testnet(..) to just Eos(..)')
60+
return Eos(config)
61+
},
62+
63+
/** @deprecated */
64+
Localnet: function (config) {
65+
console.error('deprecated, change Eos.Localnet(..) to just Eos(..)')
66+
return Eos(config)
67+
}
5968
}
60-
}
69+
)
6170

62-
Eos.Testnet = development(api.Testnet)
63-
Eos.Localnet = development(api.Localnet)
64-
// Eos.Mainnet = config => ..
6571

66-
function createEos(config, Network, network) {
67-
config = Object.assign({}, config, {network})
72+
function createEos(config) {
73+
const network = EosApi(config)
74+
config.network = network
6875

6976
config.assetCache = AssetCache(network)
7077
config.abiCache = AbiCache(network, config)
@@ -84,7 +91,7 @@ function createEos(config, Network, network) {
8491
}
8592

8693
const {structs, types, fromBuffer, toBuffer} = Structs(config)
87-
const eos = mergeWriteFunctions(config, Network, structs)
94+
const eos = mergeWriteFunctions(config, EosApi, structs)
8895

8996
Object.assign(eos, {fc: {
9097
structs,
@@ -121,18 +128,18 @@ function consoleObjCallbackLog(verbose = false) {
121128
name conflicts.
122129
123130
@arg {object} config.network - read-only api calls
124-
@arg {object} Network - api[Network] read-only api calls
131+
@arg {object} EosApi - api[EosApi] read-only api calls
125132
@return {object} - read and write method calls (create and sign transactions)
126133
@throw {TypeError} if a funciton name conflicts
127134
*/
128-
function mergeWriteFunctions(config, Network, structs) {
135+
function mergeWriteFunctions(config, EosApi, structs) {
129136
assert(config.network, 'network instance required')
130137
const {network} = config
131138

132139
const merge = Object.assign({}, network)
133140

134-
const writeApi = writeApiGen(Network, network, structs, config)
135-
throwOnDuplicate(merge, writeApi, 'Conflicting methods in Network Api and Transaction Api')
141+
const writeApi = writeApiGen(EosApi, network, structs, config, schema)
142+
throwOnDuplicate(merge, writeApi, 'Conflicting methods in EosApi and Transaction Api')
136143
Object.assign(merge, writeApi)
137144

138145
return merge

0 commit comments

Comments
 (0)