@agoric/client-utils 0.2.0-u19.2 → 0.2.0-u20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +17 -15
- package/src/types.d.ts +12 -4
- package/src/vstorage.js +78 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agoric/client-utils",
|
|
3
|
-
"version": "0.2.0-
|
|
3
|
+
"version": "0.2.0-u20.0",
|
|
4
4
|
"description": "Utilities for building Agoric clients",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -27,19 +27,21 @@
|
|
|
27
27
|
"ts-blank-space": "^0.4.4"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@agoric/casting": "^0.
|
|
31
|
-
"@agoric/
|
|
32
|
-
"@agoric/
|
|
33
|
-
"@agoric/
|
|
34
|
-
"@agoric/
|
|
35
|
-
"@
|
|
36
|
-
"@cosmjs/
|
|
37
|
-
"@
|
|
38
|
-
"@endo/
|
|
39
|
-
"@endo/
|
|
40
|
-
"@endo/
|
|
41
|
-
"@endo/
|
|
42
|
-
"@endo/
|
|
30
|
+
"@agoric/casting": "^0.5.0-u20.0",
|
|
31
|
+
"@agoric/cosmic-proto": "^0.5.0-u20.0",
|
|
32
|
+
"@agoric/ertp": "^0.16.3-u20.0",
|
|
33
|
+
"@agoric/internal": "^0.4.0-u20.0",
|
|
34
|
+
"@agoric/smart-wallet": "^0.5.4-u20.0",
|
|
35
|
+
"@agoric/vats": "^0.16.0-u20.0",
|
|
36
|
+
"@cosmjs/stargate": "^0.33.0",
|
|
37
|
+
"@cosmjs/tendermint-rpc": "^0.33.0",
|
|
38
|
+
"@endo/base64": "^1.0.9",
|
|
39
|
+
"@endo/common": "^1.2.10",
|
|
40
|
+
"@endo/errors": "^1.2.10",
|
|
41
|
+
"@endo/marshal": "^1.6.4",
|
|
42
|
+
"@endo/pass-style": "^1.5.0",
|
|
43
|
+
"@endo/patterns": "^1.5.0",
|
|
44
|
+
"@endo/promise-kit": "^1.1.10"
|
|
43
45
|
},
|
|
44
46
|
"ava": {
|
|
45
47
|
"extensions": {
|
|
@@ -58,5 +60,5 @@
|
|
|
58
60
|
],
|
|
59
61
|
"timeout": "20m"
|
|
60
62
|
},
|
|
61
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "8e4207fa19dabf76c1f91f8779b5b5b93570ecea"
|
|
62
64
|
}
|
package/src/types.d.ts
CHANGED
|
@@ -10,7 +10,11 @@ import type {
|
|
|
10
10
|
CurrentWalletRecord,
|
|
11
11
|
UpdateRecord,
|
|
12
12
|
} from '@agoric/smart-wallet/src/smartWallet.js';
|
|
13
|
-
import type {
|
|
13
|
+
import type {
|
|
14
|
+
ContractRecord,
|
|
15
|
+
PoolMetrics,
|
|
16
|
+
TransactionRecord,
|
|
17
|
+
} from '@agoric/fast-usdc';
|
|
14
18
|
|
|
15
19
|
// For static string key types. String template matching has to be in the ternary below.
|
|
16
20
|
type PublishedTypeMap = {
|
|
@@ -41,6 +45,10 @@ export type TypedPublished<T extends string> = T extends keyof PublishedTypeMap
|
|
|
41
45
|
? Array<[string, Brand]>
|
|
42
46
|
: T extends 'fastUsdc'
|
|
43
47
|
? ContractRecord
|
|
44
|
-
: T extends 'fastUsdc.
|
|
45
|
-
?
|
|
46
|
-
:
|
|
48
|
+
: T extends 'fastUsdc.feeConfig'
|
|
49
|
+
? FeeConfig
|
|
50
|
+
: T extends 'fastUsdc.poolMetrics'
|
|
51
|
+
? PoolMetrics
|
|
52
|
+
: T extends `fastUsdc.txns.${string}`
|
|
53
|
+
? TransactionRecord
|
|
54
|
+
: unknown;
|
package/src/vstorage.js
CHANGED
|
@@ -1,26 +1,81 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
|
+
QueryChildrenRequest,
|
|
3
|
+
QueryChildrenResponse,
|
|
4
|
+
QueryDataRequest,
|
|
5
|
+
QueryDataResponse,
|
|
6
|
+
} from '@agoric/cosmic-proto/agoric/vstorage/query.js';
|
|
7
|
+
import { decodeBase64 } from '@endo/base64';
|
|
8
|
+
import { encodeHex } from '@agoric/internal/src/hex.js';
|
|
2
9
|
|
|
3
10
|
/**
|
|
4
11
|
* @import {MinimalNetworkConfig} from './network-config.js';
|
|
5
12
|
*/
|
|
6
13
|
|
|
14
|
+
const codecs = {
|
|
15
|
+
children: {
|
|
16
|
+
rpc: '/agoric.vstorage.Query/Children',
|
|
17
|
+
request: QueryChildrenRequest,
|
|
18
|
+
response: QueryChildrenResponse,
|
|
19
|
+
},
|
|
20
|
+
data: {
|
|
21
|
+
rpc: '/agoric.vstorage.Query/Data',
|
|
22
|
+
request: QueryDataRequest,
|
|
23
|
+
response: QueryDataResponse,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** @typedef {(typeof codecs)[keyof typeof codecs]['response']} ResponseCodec */
|
|
28
|
+
|
|
7
29
|
/**
|
|
8
30
|
* @param {object} powers
|
|
9
31
|
* @param {typeof window.fetch} powers.fetch
|
|
10
32
|
* @param {MinimalNetworkConfig} config
|
|
11
33
|
*/
|
|
12
34
|
export const makeVStorage = ({ fetch }, config) => {
|
|
13
|
-
/**
|
|
14
|
-
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} path
|
|
37
|
+
*/
|
|
38
|
+
const getJSON = async path => {
|
|
15
39
|
const url = config.rpcAddrs[0] + path;
|
|
16
40
|
// console.warn('fetching', url);
|
|
17
|
-
|
|
41
|
+
const res = await fetch(url, { keepalive: true });
|
|
42
|
+
return res.json();
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @template {keyof codecs} T
|
|
47
|
+
* @param {string} [path]
|
|
48
|
+
* @param {object} [opts]
|
|
49
|
+
* @param {T} [opts.kind]
|
|
50
|
+
* @param {number | bigint} [opts.height] 0 is the same as omitting height and implies the highest block
|
|
51
|
+
*/
|
|
52
|
+
const url = (
|
|
53
|
+
path = 'published',
|
|
54
|
+
{ kind = /** @type {T} */ ('children'), height = 0 } = {},
|
|
55
|
+
) => {
|
|
56
|
+
const codec = codecs[kind];
|
|
57
|
+
if (!codec) {
|
|
58
|
+
throw Error(`unknown rpc kind: ${kind}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const { rpc, request } = codec;
|
|
62
|
+
const buf = request.toProto({ path });
|
|
63
|
+
|
|
64
|
+
const hexData = encodeHex(buf);
|
|
65
|
+
return `/abci_query?path=%22${rpc}%22&data=0x${hexData}&height=${height}`;
|
|
18
66
|
};
|
|
19
|
-
// height=0 is the same as omitting height and implies the highest block
|
|
20
|
-
const url = (path = 'published', { kind = 'children', height = 0 } = {}) =>
|
|
21
|
-
`/abci_query?path=%22/custom/vstorage/${kind}/${path}%22&height=${height}`;
|
|
22
67
|
|
|
23
|
-
|
|
68
|
+
/**
|
|
69
|
+
* @template {keyof codecs} T
|
|
70
|
+
* @param {string} [path]
|
|
71
|
+
* @param {object} [opts]
|
|
72
|
+
* @param {T} [opts.kind]
|
|
73
|
+
* @param {number | bigint} [opts.height] 0 is the same as omitting height and implies the highest block
|
|
74
|
+
*/
|
|
75
|
+
const readStorage = (
|
|
76
|
+
path = 'published',
|
|
77
|
+
{ kind = /** @type {T} */ ('children'), height = 0 } = {},
|
|
78
|
+
) =>
|
|
24
79
|
getJSON(url(path, { kind, height }))
|
|
25
80
|
.catch(err => {
|
|
26
81
|
throw Error(`cannot read ${kind} of ${path}: ${err.message}`);
|
|
@@ -38,18 +93,29 @@ export const makeVStorage = ({ fetch }, config) => {
|
|
|
38
93
|
err.codespace = response?.codespace;
|
|
39
94
|
throw err;
|
|
40
95
|
}
|
|
41
|
-
|
|
96
|
+
|
|
97
|
+
const codec = codecs[kind];
|
|
98
|
+
if (!codec) {
|
|
99
|
+
throw Error(`unknown codec for kind: ${kind}`);
|
|
100
|
+
}
|
|
101
|
+
return { ...data, codec: codec.response };
|
|
42
102
|
});
|
|
43
103
|
|
|
44
104
|
const vstorage = {
|
|
45
105
|
url,
|
|
46
|
-
|
|
106
|
+
/**
|
|
107
|
+
* @param {{ result: { response: { value: string, code: number } }, codec?: ResponseCodec }} param0
|
|
108
|
+
*/
|
|
109
|
+
decode({ result: { response }, codec }) {
|
|
47
110
|
const { code } = response;
|
|
48
111
|
if (code !== 0) {
|
|
49
112
|
throw response;
|
|
50
113
|
}
|
|
51
|
-
const { value } = response;
|
|
52
|
-
|
|
114
|
+
const { value: b64Value } = response;
|
|
115
|
+
if (!codec) {
|
|
116
|
+
return atob(b64Value);
|
|
117
|
+
}
|
|
118
|
+
return JSON.stringify(codec.decode(decodeBase64(b64Value)));
|
|
53
119
|
},
|
|
54
120
|
/**
|
|
55
121
|
*
|