@agoric/client-utils 0.1.1-dev-fbae24c.0 → 0.1.1-dev-8f9f075.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 +7 -7
- package/src/main.js +1 -0
- package/src/smart-wallet-kit.js +113 -0
- package/src/vstorage-kit.js +4 -5
- package/src/wallet-utils.js +5 -110
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agoric/client-utils",
|
|
3
|
-
"version": "0.1.1-dev-
|
|
3
|
+
"version": "0.1.1-dev-8f9f075.0+8f9f075",
|
|
4
4
|
"description": "Utilities for building Agoric clients",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"ts-blank-space": "^0.4.4"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@agoric/casting": "0.4.3-dev-
|
|
31
|
-
"@agoric/ertp": "0.16.3-dev-
|
|
32
|
-
"@agoric/internal": "0.3.3-dev-
|
|
33
|
-
"@agoric/smart-wallet": "0.5.4-dev-
|
|
34
|
-
"@agoric/vats": "0.15.2-dev-
|
|
30
|
+
"@agoric/casting": "0.4.3-dev-8f9f075.0+8f9f075",
|
|
31
|
+
"@agoric/ertp": "0.16.3-dev-8f9f075.0+8f9f075",
|
|
32
|
+
"@agoric/internal": "0.3.3-dev-8f9f075.0+8f9f075",
|
|
33
|
+
"@agoric/smart-wallet": "0.5.4-dev-8f9f075.0+8f9f075",
|
|
34
|
+
"@agoric/vats": "0.15.2-dev-8f9f075.0+8f9f075",
|
|
35
35
|
"@cosmjs/stargate": "^0.32.3",
|
|
36
36
|
"@cosmjs/tendermint-rpc": "^0.32.3",
|
|
37
37
|
"@endo/common": "^1.2.8",
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
],
|
|
59
59
|
"timeout": "20m"
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "8f9f07501c1e5f25103d0a015f2f2817275391f2"
|
|
62
62
|
}
|
package/src/main.js
CHANGED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { makeWalletStateCoalescer } from '@agoric/smart-wallet/src/utils.js';
|
|
2
|
+
import { pollBlocks } from './chain.js';
|
|
3
|
+
import { makeStargateClient } from './rpc.js';
|
|
4
|
+
import { makeAgoricNames, makeVstorageKit } from './vstorage-kit.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import {Amount, Brand} from '@agoric/ertp/src/types.js'
|
|
8
|
+
* @import {CurrentWalletRecord, UpdateRecord} from '@agoric/smart-wallet/src/smartWallet.js';
|
|
9
|
+
* @import {MinimalNetworkConfig} from './network-config.js';
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Augment VstorageKit with addtional convenience methods for working with
|
|
14
|
+
* Agoric smart wallets.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} root0
|
|
17
|
+
* @param {typeof globalThis.fetch} root0.fetch
|
|
18
|
+
* @param {(ms: number) => Promise<void>} root0.delay
|
|
19
|
+
* @param {MinimalNetworkConfig} networkConfig
|
|
20
|
+
*/
|
|
21
|
+
export const makeSmartWalletKit = async ({ fetch, delay }, networkConfig) => {
|
|
22
|
+
const vsk = makeVstorageKit({ fetch }, networkConfig);
|
|
23
|
+
|
|
24
|
+
const client = await makeStargateClient(networkConfig, { fetch });
|
|
25
|
+
|
|
26
|
+
const agoricNames = await makeAgoricNames(vsk.fromBoard, vsk.vstorage);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} from
|
|
30
|
+
* @param {number|string} [minHeight]
|
|
31
|
+
*/
|
|
32
|
+
const storedWalletState = async (from, minHeight = undefined) => {
|
|
33
|
+
const history = await vsk.vstorage.readFully(
|
|
34
|
+
`published.wallet.${from}`,
|
|
35
|
+
minHeight,
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
/** @type {{ Invitation: Brand<'set'> }} */
|
|
39
|
+
// @ts-expect-error XXX how to narrow AssetKind to set?
|
|
40
|
+
const { Invitation } = agoricNames.brand;
|
|
41
|
+
const coalescer = makeWalletStateCoalescer(Invitation);
|
|
42
|
+
// update with oldest first
|
|
43
|
+
for (const txt of history.reverse()) {
|
|
44
|
+
const { body, slots } = JSON.parse(txt);
|
|
45
|
+
const record = vsk.marshaller.fromCapData({ body, slots });
|
|
46
|
+
coalescer.update(record);
|
|
47
|
+
}
|
|
48
|
+
const coalesced = coalescer.state;
|
|
49
|
+
harden(coalesced);
|
|
50
|
+
return coalesced;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get OfferStatus by id, polling until available.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} from
|
|
57
|
+
* @param {string|number} id
|
|
58
|
+
* @param {number|string} minHeight
|
|
59
|
+
* @param {boolean} [untilNumWantsSatisfied]
|
|
60
|
+
*/
|
|
61
|
+
const pollOffer = async (
|
|
62
|
+
from,
|
|
63
|
+
id,
|
|
64
|
+
minHeight,
|
|
65
|
+
untilNumWantsSatisfied = false,
|
|
66
|
+
) => {
|
|
67
|
+
const poll = pollBlocks({
|
|
68
|
+
client,
|
|
69
|
+
delay,
|
|
70
|
+
retryMessage: 'offer not in wallet at block',
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const lookup = async () => {
|
|
74
|
+
const { offerStatuses } = await storedWalletState(from, minHeight);
|
|
75
|
+
const offerStatus = [...offerStatuses.values()].find(s => s.id === id);
|
|
76
|
+
if (!offerStatus) throw Error('retry');
|
|
77
|
+
harden(offerStatus);
|
|
78
|
+
if (untilNumWantsSatisfied && !('numWantsSatisfied' in offerStatus)) {
|
|
79
|
+
throw Error('retry (no numWantsSatisfied yet)');
|
|
80
|
+
}
|
|
81
|
+
return offerStatus;
|
|
82
|
+
};
|
|
83
|
+
return poll(lookup);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {string} addr
|
|
88
|
+
* @returns {Promise<UpdateRecord>}
|
|
89
|
+
*/
|
|
90
|
+
const getLastUpdate = addr => {
|
|
91
|
+
return vsk.readPublished(`wallet.${addr}`);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {string} addr
|
|
96
|
+
* @returns {Promise<CurrentWalletRecord>}
|
|
97
|
+
*/
|
|
98
|
+
const getCurrentWalletRecord = addr => {
|
|
99
|
+
return vsk.readPublished(`wallet.${addr}.current`);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
// pass along all of VstorageKit
|
|
104
|
+
...vsk,
|
|
105
|
+
agoricNames,
|
|
106
|
+
networkConfig,
|
|
107
|
+
getLastUpdate,
|
|
108
|
+
getCurrentWalletRecord,
|
|
109
|
+
storedWalletState,
|
|
110
|
+
pollOffer,
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
/** @typedef {Awaited<ReturnType<typeof makeSmartWalletKit>>} SmartWalletKit */
|
package/src/vstorage-kit.js
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
boardSlottingMarshaller,
|
|
3
3
|
makeBoardRemote,
|
|
4
4
|
} from '@agoric/vats/tools/board-utils.js';
|
|
5
|
+
import { assertAllDefined } from '@agoric/internal';
|
|
5
6
|
import { makeVStorage } from './vstorage.js';
|
|
6
7
|
|
|
7
8
|
export { boardSlottingMarshaller };
|
|
@@ -73,6 +74,7 @@ harden(storageHelper);
|
|
|
73
74
|
* @returns {Promise<import('@agoric/vats/tools/board-utils.js').AgoricNamesRemotes>}
|
|
74
75
|
*/
|
|
75
76
|
export const makeAgoricNames = async (ctx, vstorage) => {
|
|
77
|
+
assertAllDefined({ ctx, vstorage });
|
|
76
78
|
const reverse = {};
|
|
77
79
|
const entries = await Promise.all(
|
|
78
80
|
['brand', 'instance', 'vbankAsset'].map(async kind => {
|
|
@@ -96,12 +98,10 @@ export const makeAgoricNames = async (ctx, vstorage) => {
|
|
|
96
98
|
* @param {{ fetch: typeof window.fetch }} io
|
|
97
99
|
* @param {MinimalNetworkConfig} config
|
|
98
100
|
*/
|
|
99
|
-
export const makeVstorageKit =
|
|
100
|
-
await null;
|
|
101
|
+
export const makeVstorageKit = ({ fetch }, config) => {
|
|
101
102
|
try {
|
|
102
103
|
const vstorage = makeVStorage({ fetch }, config);
|
|
103
104
|
const fromBoard = makeFromBoard();
|
|
104
|
-
const agoricNames = await makeAgoricNames(fromBoard, vstorage);
|
|
105
105
|
|
|
106
106
|
const marshaller = boardSlottingMarshaller(fromBoard.convertSlotToVal);
|
|
107
107
|
|
|
@@ -131,7 +131,6 @@ export const makeVstorageKit = async ({ fetch }, config) => {
|
|
|
131
131
|
readLatestHead(`published.${subpath}`);
|
|
132
132
|
|
|
133
133
|
return {
|
|
134
|
-
agoricNames,
|
|
135
134
|
fromBoard,
|
|
136
135
|
marshaller,
|
|
137
136
|
readLatestHead,
|
|
@@ -143,4 +142,4 @@ export const makeVstorageKit = async ({ fetch }, config) => {
|
|
|
143
142
|
throw Error(`RPC failure (${config.rpcAddrs}): ${err.message}`);
|
|
144
143
|
}
|
|
145
144
|
};
|
|
146
|
-
/** @typedef {
|
|
145
|
+
/** @typedef {ReturnType<typeof makeVstorageKit>} VstorageKit */
|
package/src/wallet-utils.js
CHANGED
|
@@ -1,113 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
import { pollBlocks } from './chain.js';
|
|
3
|
-
import { makeStargateClient } from './rpc.js';
|
|
4
|
-
import { boardSlottingMarshaller, makeVstorageKit } from './vstorage-kit.js';
|
|
1
|
+
/** @file backwards compat */
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
* @import {Amount, Brand} from '@agoric/ertp/src/types.js'
|
|
8
|
-
* @import {CurrentWalletRecord, UpdateRecord} from '@agoric/smart-wallet/src/smartWallet.js';
|
|
9
|
-
* @import {MinimalNetworkConfig} from './network-config.js';
|
|
10
|
-
*/
|
|
3
|
+
import { makeSmartWalletKit } from './smart-wallet-kit.js';
|
|
11
4
|
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Augment VstorageKit with addtional convenience methods for working with
|
|
15
|
-
* Agoric smart wallets.
|
|
16
|
-
*
|
|
17
|
-
* @param {object} root0
|
|
18
|
-
* @param {typeof globalThis.fetch} root0.fetch
|
|
19
|
-
* @param {(ms: number) => Promise<void>} root0.delay
|
|
20
|
-
* @param {MinimalNetworkConfig} networkConfig
|
|
21
|
-
*/
|
|
22
|
-
export const makeWalletUtils = async ({ fetch, delay }, networkConfig) => {
|
|
23
|
-
const vsk = await makeVstorageKit({ fetch }, networkConfig);
|
|
5
|
+
/** @typedef {import('./smart-wallet-kit.js').SmartWalletKit} WalletUtils */
|
|
24
6
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const client = await makeStargateClient(networkConfig, { fetch });
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @param {string} from
|
|
31
|
-
* @param {number|string} [minHeight]
|
|
32
|
-
*/
|
|
33
|
-
const storedWalletState = async (from, minHeight = undefined) => {
|
|
34
|
-
const history = await vsk.vstorage.readFully(
|
|
35
|
-
`published.wallet.${from}`,
|
|
36
|
-
minHeight,
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
/** @type {{ Invitation: Brand<'set'> }} */
|
|
40
|
-
// @ts-expect-error XXX how to narrow AssetKind to set?
|
|
41
|
-
const { Invitation } = vsk.agoricNames.brand;
|
|
42
|
-
const coalescer = makeWalletStateCoalescer(Invitation);
|
|
43
|
-
// update with oldest first
|
|
44
|
-
for (const txt of history.reverse()) {
|
|
45
|
-
const { body, slots } = JSON.parse(txt);
|
|
46
|
-
const record = m.fromCapData({ body, slots });
|
|
47
|
-
coalescer.update(record);
|
|
48
|
-
}
|
|
49
|
-
const coalesced = coalescer.state;
|
|
50
|
-
harden(coalesced);
|
|
51
|
-
return coalesced;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Get OfferStatus by id, polling until available.
|
|
56
|
-
*
|
|
57
|
-
* @param {string} from
|
|
58
|
-
* @param {string|number} id
|
|
59
|
-
* @param {number|string} minHeight
|
|
60
|
-
* @param {boolean} [untilNumWantsSatisfied]
|
|
61
|
-
*/
|
|
62
|
-
const pollOffer = async (
|
|
63
|
-
from,
|
|
64
|
-
id,
|
|
65
|
-
minHeight,
|
|
66
|
-
untilNumWantsSatisfied = false,
|
|
67
|
-
) => {
|
|
68
|
-
const poll = pollBlocks({
|
|
69
|
-
client,
|
|
70
|
-
delay,
|
|
71
|
-
retryMessage: 'offer not in wallet at block',
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
const lookup = async () => {
|
|
75
|
-
const { offerStatuses } = await storedWalletState(from, minHeight);
|
|
76
|
-
const offerStatus = [...offerStatuses.values()].find(s => s.id === id);
|
|
77
|
-
if (!offerStatus) throw Error('retry');
|
|
78
|
-
harden(offerStatus);
|
|
79
|
-
if (untilNumWantsSatisfied && !('numWantsSatisfied' in offerStatus)) {
|
|
80
|
-
throw Error('retry (no numWantsSatisfied yet)');
|
|
81
|
-
}
|
|
82
|
-
return offerStatus;
|
|
83
|
-
};
|
|
84
|
-
return poll(lookup);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* @param {string} addr
|
|
89
|
-
* @returns {Promise<UpdateRecord>}
|
|
90
|
-
*/
|
|
91
|
-
const getLastUpdate = addr => {
|
|
92
|
-
return vsk.readPublished(`wallet.${addr}`);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @param {string} addr
|
|
97
|
-
* @returns {Promise<CurrentWalletRecord>}
|
|
98
|
-
*/
|
|
99
|
-
const getCurrentWalletRecord = addr => {
|
|
100
|
-
return vsk.readPublished(`wallet.${addr}.current`);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
// pass along all of VstorageKit
|
|
105
|
-
...vsk,
|
|
106
|
-
networkConfig,
|
|
107
|
-
getLastUpdate,
|
|
108
|
-
getCurrentWalletRecord,
|
|
109
|
-
storedWalletState,
|
|
110
|
-
pollOffer,
|
|
111
|
-
};
|
|
112
|
-
};
|
|
113
|
-
/** @typedef {Awaited<ReturnType<typeof makeWalletUtils>>} WalletUtils */
|
|
7
|
+
/** @deprecated use `makeSmartWalletKit` */
|
|
8
|
+
export const makeWalletUtils = makeSmartWalletKit;
|