Last active
August 17, 2022 20:27
-
-
Save dckc/73205178bd44cf3f4bbcf7db0fc280a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-await-in-loop */ | |
// @ts-check | |
import { E } from '@endo/eventual-send'; | |
import { AmountMath } from '@agoric/ertp'; | |
const createVault = async ( | |
vfPubP, | |
collaterals, | |
deposit, | |
debt, | |
{ zoe, agoricNames, wallet }, | |
) => { | |
const depositInfo = | |
collaterals.find(({ brand }) => `${brand}`.match(/ATOM/)) || | |
assert.fail('no ATOM found'); | |
const displayInfo = await E(depositInfo.brand).getDisplayInfo(); | |
console.log('collateral info:', displayInfo); | |
console.log('!!! DANGER!!! getting purses...'); | |
const purses = await E(wallet).getPurses(); | |
console.log(purses); | |
const [_name2, depositPurse] = | |
purses.find(([name, _p]) => name.match(/ATOM/)) || | |
assert.fail('no ATOM purse'); | |
const [_name3, istPurse] = | |
purses.find(([name, _p]) => name.match(/stable/)) || | |
assert.fail('no IST purse'); | |
console.log('getting central brand'); | |
/** @type {Brand} */ | |
const centralBrand = await E(agoricNames).lookup('brand', 'IST'); | |
const depositUnit = 10n ** BigInt(displayInfo.decimalPlaces); | |
const istUnit = 1_000_000n; | |
const proposal = { | |
give: { | |
Collateral: AmountMath.make(depositInfo.brand, deposit * depositUnit), | |
}, | |
want: { | |
Minted: AmountMath.make(centralBrand, debt * istUnit), | |
}, | |
}; | |
console.log('get invitation...'); | |
const managerP = E(vfPubP).getCollateralManager(depositInfo.brand); | |
/** @type {ERef<Invitation>} */ | |
const invitationP = E(managerP).makeVaultInvitation(); | |
console.log({ invitation: invitationP }); | |
// console.log({ proposal }); | |
console.log('withdraw payments...', proposal.give); | |
const payments = { | |
Collateral: await E(depositPurse).withdraw(proposal.give.Collateral), | |
}; | |
console.log('make offer...'); | |
const seat = await E(zoe).offer(invitationP, proposal, payments); | |
console.log({ seat }); | |
console.log('getOfferResult...'); | |
const result = await E(seat).getOfferResult(); | |
console.log({ result }); | |
console.log('get payout...'); | |
const payout = await E(seat).getPayout('Minted'); | |
// await E(scratch).set('vaultPayout', payout); | |
const payoutAmount = await E(istPurse).deposit(payout); | |
console.log({ payoutAmount }); | |
return result; | |
}; | |
/** | |
* | |
* @param {ERef<import('@agoric/deploy-script-support/src/user-bundle').HomeBundle>} homeP | |
* @param {unknown} _endowments - see `agoric deploy` docs | |
*/ | |
const borrowFromVault = async (homeP, _endowments) => { | |
console.log('hello, world!'); | |
const { agoricNames, zoe, wallet, scratch } = E.get(homeP); | |
const vfInstanceP = E(agoricNames).lookup('instance', 'VaultFactory'); | |
const vfPubP = E(zoe).getPublicFacet(vfInstanceP); | |
console.log('getCollaterals...'); | |
const collaterals = await E(vfPubP).getCollaterals(); | |
console.log('VaultFactory collaterals:', collaterals); | |
const details = [ | |
{ deposit: 5n, debt: 30n }, | |
{ deposit: 4n, debt: 20n }, | |
// { id: 3, deposit: 10n, debt: 150n }, | |
]; | |
let myVaults = (await E(scratch).get('myVaults')) || []; | |
for (const { deposit, debt } of details) { | |
const vault = await createVault(vfPubP, collaterals, deposit, debt, { | |
zoe, | |
agoricNames, | |
wallet, | |
}); | |
const id = Object.keys(myVaults).length + 1; | |
myVaults = [...myVaults, vault]; | |
console.log('vault id:', id); | |
await E(scratch).set('myVaults', myVaults); | |
} | |
}; | |
export default borrowFromVault; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { E } from '@endo/eventual-send'; | |
import { AmountMath } from '@agoric/ertp'; | |
/** | |
* | |
* @param {*} homeP - see `agoric deploy` docs | |
* @param {*} _endowments - see `agoric deploy` docs | |
*/ | |
const swapOnAmm = async (homeP, _endowments) => { | |
console.log('hello, world!'); | |
console.log('resolve home promise'); | |
const home = await homeP; | |
// console.log('home is:', home); | |
// console.log('other toys:', endowments); | |
const ammInstanceP = E(home.agoricNames).lookup('instance', 'amm'); | |
const ammPubP = E(home.zoe).getPublicFacet(ammInstanceP); | |
const brands = await E(ammPubP).getAllPoolBrands(); | |
console.log('amm pool brands:', brands); | |
if (!brands.length) { | |
throw Error('no pools!'); | |
} | |
const [secondaryBrand] = brands; | |
console.log('getting central brand'); | |
const centralBrand = await E(home.agoricNames).lookup( | |
'brand', | |
// soon to be IST | |
'RUN', | |
); | |
const proposal = { | |
give: { | |
In: AmountMath.make(secondaryBrand, 5n), | |
}, | |
want: { | |
Out: AmountMath.make(centralBrand, 100n), | |
}, | |
}; | |
console.log('getting purses...'); | |
const purses = await E(home.wallet).getPurses(); | |
const [[_name, atomPurse]] = purses; // we know Atom is 1st | |
// console.log({ proposal }); | |
console.log('withdraw payments...', proposal.give); | |
const payments = { | |
In: await E(atomPurse).withdraw(proposal.give.In), | |
}; | |
console.log('get invitation...'); | |
const invitation = await E(ammPubP).makeSwapInvitation(); | |
console.log({ invitation }); | |
console.log('make offer...'); | |
const seat = await E(home.zoe).offer(invitation, proposal, payments); | |
console.log({ seat }); | |
console.log('getOfferResult...'); | |
const result = await E(seat).getOfferResult(); | |
console.log({ result }); | |
console.log('get payout...'); | |
const payout = await E(seat).getPayout('Out'); | |
E(home.scratch).set('swapResult', payout); | |
const [_name2, runPurse] = purses.find(([name, _p]) => name.match(/RUN/)); | |
await E(runPurse).deposit(payout); | |
}; | |
export default swapOnAmm; |
Note: this uses purses directly rather than using the wallet API, which is risky.
context:
- office hours, including recordings Agoric/documentation#576 (comment)
- Agoric/agoric-sdk#5663
- Agoric/agoric-sdk#5644
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example run: