Skip to content

Instantly share code, notes, and snippets.

@dckc
Last active August 17, 2022 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dckc/73205178bd44cf3f4bbcf7db0fc280a8 to your computer and use it in GitHub Desktop.
Save dckc/73205178bd44cf3f4bbcf7db0fc280a8 to your computer and use it in GitHub Desktop.
/* 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;
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;
@dckc
Copy link
Author

dckc commented Jun 29, 2022

example run:

$ agoric deploy ./swap-on-amm.js 
Open CapTP connection to ws://127.0.0.1:8000/private/captp...o
agoric: deploy: running /home/connolly/projects/agoric/ag-admin/api/swap-on-amm.js
agoric: deploy: Deploy script will run with Node.js ESM
hello, world!
resolve home promise
amm pool brands: [ Object [Alleged: IbcATOM brand] {} ]
getting central brand
getting purses...
withdraw payments... { In: { brand: Object [Alleged: IbcATOM brand] {}, value: 5n } }
get invitation...
{ invitation: Object [Alleged: Zoe Invitation payment] {} }
make offer...
{ seat: Object [Alleged: userSeat] {} }
getOfferResult...
{ result: 'Swap successfully completed.' }
get payout...

@dckc
Copy link
Author

dckc commented Jun 29, 2022

Note: this uses purses directly rather than using the wallet API, which is risky.

@dckc
Copy link
Author

dckc commented Aug 14, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment