-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fast-usdc): deposit, withdraw liquidity in exchange for shares
- Loading branch information
Showing
4 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* @import {Zone} from '@agoric/zone'; | ||
*/ | ||
|
||
import { AmountMath } from '@agoric/ertp/src/amountMath.js'; | ||
import { | ||
deposit, | ||
depositTransfers, | ||
makeParity, | ||
withdraw, | ||
withdrawTransfers, | ||
} from '../pool-share-math.js'; | ||
import { makeProposalShapes } from '../type-guards.js'; | ||
|
||
/** | ||
* @param {Zone} zone | ||
* @param {ZCF} zcf | ||
* @param {Record<'USDC', Brand<'nat'>>} brands | ||
*/ | ||
export const prepareLiquidityPoolKit = (zone, zcf, { USDC }) => { | ||
const interfaceToDo = undefined; | ||
return zone.exoClassKit( | ||
'Fast Liquidity Pool', | ||
interfaceToDo, | ||
/** | ||
* @param {ZCFMint<'nat'>} shareMint | ||
*/ | ||
shareMint => { | ||
const { brand: PoolShares } = shareMint.getIssuerRecord(); | ||
const proposalShapes = makeProposalShapes({ USDC, PoolShares }); | ||
const dust = AmountMath.make(USDC, 1n); | ||
const shareWorth = makeParity(dust, PoolShares); | ||
/* | ||
* TODO: should the pool be an orchestration account, | ||
* visible at the cosmos level? | ||
* | ||
* We could use a short-lived poolSeat for deposit / withdraw. | ||
*/ | ||
const { zcfSeat: poolSeat } = zcf.makeEmptySeatKit(); | ||
return { shareMint, shareWorth, poolSeat, PoolShares, proposalShapes }; | ||
}, | ||
{ | ||
depositHandler: { | ||
/** @param {ZCFSeat} lp */ | ||
handle(lp) { | ||
const { shareWorth, shareMint, poolSeat } = this.state; | ||
const { give, want } = lp.getProposal(); | ||
const { ToPool } = give; | ||
const post = deposit(shareWorth, give.ToPool, want.Shares); | ||
const { Shares } = post; | ||
const mint = shareMint.mintGains({ Shares }); | ||
zcf.atomicRearrange( | ||
depositTransfers({ pool: poolSeat, lp, mint }, { Shares, ToPool }), | ||
); | ||
this.state.shareWorth = post.shareWorth; | ||
lp.exit(); | ||
mint.exit(); | ||
return true; | ||
}, | ||
}, | ||
withdrawHandler: { | ||
/** @param {ZCFSeat} lp */ | ||
handle(lp) { | ||
const { shareWorth, shareMint, poolSeat } = this.state; | ||
const { give, want } = lp.getProposal(); | ||
const { Shares } = give; | ||
const post = withdraw(shareWorth, Shares, want.FromPool); | ||
const { FromPool } = post; | ||
const { zcfSeat: burn } = zcf.makeEmptySeatKit(); | ||
zcf.atomicRearrange( | ||
withdrawTransfers( | ||
{ pool: poolSeat, lp, burn }, | ||
{ Shares, FromPool }, | ||
), | ||
); | ||
shareMint.burnLosses({ Shares }, burn); | ||
this.state.shareWorth = post.shareWorth; | ||
lp.exit(); | ||
burn.exit(); | ||
return true; | ||
}, | ||
}, | ||
public: { | ||
makeDepositInvitation() { | ||
const { depositHandler } = this.facets; | ||
const { deposit: shape } = this.state.proposalShapes; | ||
const d = undefined; | ||
return zcf.makeInvitation(depositHandler, 'Deposit', d, shape); | ||
}, | ||
makeWithdrawInvitation() { | ||
const { withdrawHandler } = this.facets; | ||
const { withdraw: shape } = this.state.proposalShapes; | ||
const d = undefined; | ||
return zcf.makeInvitation(withdrawHandler, 'Withdraw', d, shape); | ||
}, | ||
}, | ||
}, | ||
); | ||
}; | ||
harden(prepareLiquidityPoolKit); |
This file contains 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
This file contains 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
This file contains 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