-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathcontexts.js
139 lines (126 loc) · 4.29 KB
/
contexts.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { BridgeId, deeplyFulfilledObject } from '@agoric/internal';
import { unsafeMakeBundleCache } from '@agoric/swingset-vat/tools/bundleTool.js';
import { makeStorageNodeChild } from '@agoric/internal/src/lib-chainStorage.js';
import { E } from '@endo/far';
import path from 'path';
import { createPriceFeed } from '../../src/proposals/price-feed-proposal.js';
import { withAmountUtils } from '../supports.js';
/**
* @param {import('ava').ExecutionContext} t
* @param {(logger) => Promise<ChainBootstrapSpace>} makeSpace
*/
export const makeDefaultTestContext = async (t, makeSpace) => {
// To debug, pass t.log instead of null logger
const log = () => null;
const { consume, produce } = await makeSpace(log);
const { agoricNames, zoe } = consume;
// #region Installs
const pathname = new URL(import.meta.url).pathname;
const dirname = path.dirname(pathname);
const bundleCache = await unsafeMakeBundleCache('bundles/');
const bundle = await bundleCache.load(
`${dirname}/../../../smart-wallet/src/walletFactory.js`,
'walletFactory',
);
/** @type {Promise<Installation<import('@agoric/smart-wallet/src/walletFactory.js').start>>} */
const installation = E(zoe).install(bundle);
// #endregion
// copied from makeClientBanks()
const storageNode = await makeStorageNodeChild(
consume.chainStorage,
'wallet',
);
const assetPublisher = await E(consume.bankManager).getBankForAddress(
'anyAddress',
);
const bridgeManager = await consume.bridgeManager;
const walletBridgeManager = await (bridgeManager &&
E(bridgeManager).register(BridgeId.WALLET));
const walletFactory = await E(zoe).startInstance(
installation,
{},
{
agoricNames,
board: consume.board,
assetPublisher,
},
{ storageNode, walletBridgeManager },
);
/** @param {string} address */
const provideWalletAndBalances = async address => {
// copied from makeClientBanks()
const bank = await E(consume.bankManager).getBankForAddress(address);
const [wallet, _isNew] = await E(
walletFactory.creatorFacet,
).provideSmartWallet(address, bank, consume.namesByAddressAdmin);
/**
* Read-only facet of bank
*
* @param {Brand<'nat'>} brand
*/
const getBalanceFor = brand =>
E(E(bank).getPurse(brand)).getCurrentAmount();
return { getBalanceFor, wallet };
};
const simpleProvideWallet = address =>
provideWalletAndBalances(address).then(({ wallet }) => wallet);
/**
*
* @param {string[]} oracleAddresses
* @param {string} inBrandName
* @param {string} outBrandName
*/
const simpleCreatePriceFeed = async (
oracleAddresses,
inBrandName = 'ATOM',
outBrandName = 'USD',
) => {
// copied from coreProposalBehavior: Publish the installations for behavior dependencies.
/** @type {ERef<import('@agoric/vats').NameAdmin>} */
const installAdmin = E(consume.agoricNamesAdmin).lookupAdmin(
'installation',
);
const paBundle = await bundleCache.load(
'../inter-protocol/src/price/fluxAggregator.js',
'priceAggregator',
);
/** @type {Promise<Installation<import('@agoric/inter-protocol/src/price/fluxAggregator.js').start>>} */
const paInstallation = E(zoe).install(paBundle);
await E(installAdmin).update('priceAggregator', paInstallation);
await createPriceFeed(
{ consume, produce },
{
options: {
priceFeedOptions: {
AGORIC_INSTANCE_NAME: `${inBrandName}-${outBrandName} price feed`,
contractTerms: {
minSubmissionCount: 2,
minSubmissionValue: 1,
maxSubmissionCount: 5,
maxSubmissionValue: 99999,
restartDelay: 1n,
timeout: 10,
},
oracleAddresses,
IN_BRAND_NAME: inBrandName,
OUT_BRAND_NAME: outBrandName,
},
},
},
);
};
const anchor = withAmountUtils(
// @ts-expect-error xxx type debt
await deeplyFulfilledObject(consume.testFirstAnchorKit),
);
return {
anchor,
invitationBrand: await E(E(zoe).getInvitationIssuer()).getBrand(),
sendToBridge:
walletBridgeManager && (obj => E(walletBridgeManager).toBridge(obj)),
consume,
provideWalletAndBalances,
simpleProvideWallet,
simpleCreatePriceFeed,
};
};