Skip to content

Commit

Permalink
feat(loadgen): Use fallback token if no vaultFactoryCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman committed Mar 21, 2022
1 parent 52c735c commit 5b80462
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 22 deletions.
4 changes: 2 additions & 2 deletions loadgen/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const collateralToken = 'USDC';
export const fallbackCollateralToken = 'USDC';

export const tradeToken = 'USDC';
export const fallbackTradeToken = 'USDC';
59 changes: 51 additions & 8 deletions loadgen/contract/agent-prepare-loadgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { fallback } from './fallback.js';
* wallet: ERef<import('../types.js').HomeWallet>,
* zoe: ERef<ZoeService>,
* mintBundle: BundleSource,
* fallbackCollateralToken?: string | undefined,
* fallbackTradeToken?: string | undefined,
* }} startParam
*/

Expand Down Expand Up @@ -126,6 +128,8 @@ export default async function startAgent({
zoe,
wallet,
mintBundle,
fallbackTradeToken,
fallbackCollateralToken,
}) {
const walletAdmin = E(wallet).getAdminFacet();

Expand Down Expand Up @@ -400,14 +404,53 @@ export default async function startAgent({
},
);

return harden(
await allValues({
tokenKit,
runKit,
amm,
vaultManager,
vaultFactory: vaultFactoryPublicFacet,
}),
return E.when(vaultManager, async (vaultManagerPresence) => {
const collateralTokenPetname =
fallbackCollateralToken || fallbackTradeToken;

if (vaultManagerPresence) {
return { vaultTokenKit: tokenKit, ammTokenKit: tokenKit };
} else if (collateralTokenPetname) {
// Make sure the finder knows about all purses by finding the
// LGT purse we created
await purseFinder.find({ brandPetname: tokenBrandPetname });

const { kit: vaultTokenKit } = E.get(
purseFinder.find({
brandPetname: collateralTokenPetname,
existingOnly: true,
}),
);

let ammTokenKit = vaultTokenKit.then((value) => value || tokenKit);
if (
(fallbackTradeToken && fallbackTradeToken !== collateralTokenPetname) ||
!(await fundingResult)
) {
({ kit: ammTokenKit } = E.get(
purseFinder.find({
brandPetname: fallbackTradeToken,
existingOnly: true,
}),
));
}

return { vaultTokenKit, ammTokenKit };
} else {
return { vaultTokenKit: null, ammTokenKit: null };
}
}).then(async ({ vaultTokenKit, ammTokenKit }) =>
harden(
await allValues({
tokenKit,
runKit,
amm,
ammTokenKit,
vaultManager,
vaultFactory: vaultFactoryPublicFacet,
vaultTokenKit,
}),
),
);

// TODO: exit here?
Expand Down
4 changes: 4 additions & 0 deletions loadgen/prepare-loadgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import path from 'path';
import { E } from '@agoric/eventual-send';

import { fallbackCollateralToken, fallbackTradeToken } from './config.js';

const key = 'loadgenKit';

/**
Expand Down Expand Up @@ -64,6 +66,8 @@ export async function prepareLoadgen(home, deployPowers) {
wallet,
zoe,
mintBundle,
fallbackCollateralToken,
fallbackTradeToken,
});
loadgenKit = await E(installerP).spawn(agentParam);

Expand Down
21 changes: 15 additions & 6 deletions loadgen/task-create-vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ export async function prepareVaultCycle(home, deployPowers) {

// create the solo-side agent to drive each cycle, let it handle zoe
const installerP = E(spawner).install(agentBundle);
const { runKit, tokenKit, vaultFactory } = await loadgenKit;
/** @type {import('./contract/agent-create-vault').startParam} */
const startParam = { tokenKit, runKit, vaultFactory, zoe };
agent = await E(installerP).spawn(startParam);
await E(scratch).set(key, agent);
console.log(`create-vault: prepare: agent installed`);
const { runKit, vaultTokenKit: tokenKit, vaultFactory } = await loadgenKit;
if (runKit && tokenKit && vaultFactory) {
/** @type {import('./contract/agent-create-vault').startParam} */
const startParam = { tokenKit, runKit, vaultFactory, zoe };
agent = await E(installerP).spawn(startParam);
await E(scratch).set(key, agent);
console.log(`create-vault: prepare: agent installed`);
} else {
console.error(
`create-vault: prepare: couldn't install agent, missing prerequisites`,
);
}
}

async function vaultCycle() {
if (!agent) {
throw new Error('No agent available');
}
const {
newRunBalanceDisplay,
newCollateralBalanceDisplay,
Expand Down
21 changes: 15 additions & 6 deletions loadgen/task-trade-amm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ export async function prepareAMMTrade(home, deployPowers) {

// create the solo-side agent to drive each cycle, let it handle zoe
const installerP = E(spawner).install(agentBundle);
const { runKit, tokenKit, amm } = await loadgenKit;
/** @type {import('./contract/agent-trade-amm').startParam} */
const startParam = { tokenKit, runKit, amm, zoe };
agent = await E(installerP).spawn(startParam);
await E(scratch).set(key, agent);
console.log(`trade-amm: prepare: agent installed`);
const { runKit, ammTokenKit: tokenKit, amm } = await loadgenKit;
if (runKit && tokenKit && amm) {
/** @type {import('./contract/agent-trade-amm').startParam} */
const startParam = { tokenKit, runKit, amm, zoe };
agent = await E(installerP).spawn(startParam);
await E(scratch).set(key, agent);
console.log(`trade-amm: prepare: agent installed`);
} else {
console.error(
`trade-amm: prepare: couldn't install agent, missing prerequisites`,
);
}
}

async function tradeAMMCycle() {
if (!agent) {
throw new Error('No agent available');
}
const { newRunBalanceDisplay, newTargetBalanceDisplay, targetToken } =
await E(agent).doAMMCycle();
console.log(
Expand Down

0 comments on commit 5b80462

Please sign in to comment.