-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
800 additions
and
17 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
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
102 changes: 102 additions & 0 deletions
102
packages/orchestration/src/contracts/swapExample.contract.js
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,102 @@ | ||
// @ts-check | ||
import { Fail } from '@agoric/assert'; | ||
import { AmountMath, AmountShape } from '@agoric/ertp'; | ||
import { E, Far } from '@endo/far'; | ||
import { M } from '@endo/patterns'; | ||
import { makeOrchestrationFacade } from '../facade.js'; | ||
import { orcUtils } from '../utils/orc.js'; | ||
|
||
/** | ||
* @import {Orchestrator, ChainAccount, CosmosValidatorAddress} from '../types.js' | ||
* @import {TimerService} from '@agoric/time'; | ||
* @import {ERef} from '@endo/far' | ||
* @import {OrchestrationService} from '../service.js'; | ||
* @import {Zone} from '@agoric/zone'; | ||
*/ | ||
|
||
/** | ||
* @param {ZCF} zcf | ||
* @param {{ | ||
* orchestrationService: ERef<OrchestrationService>; | ||
* storageNode: ERef<StorageNode>; | ||
* timerService: ERef<TimerService>; | ||
* zone: Zone; | ||
* }} privateArgs | ||
*/ | ||
export const start = async (zcf, privateArgs) => { | ||
const { orchestrationService, storageNode, timerService, zone } = privateArgs; | ||
|
||
const { orchestrate } = makeOrchestrationFacade({ | ||
zone, | ||
timerService, | ||
zcf, | ||
storageNode, | ||
orchestrationService, | ||
}); | ||
|
||
/** deprecated historical example */ | ||
/** @type {OfferHandler} */ | ||
const swapAndStakeHandler = orchestrate( | ||
'LSTTia', | ||
{ zcf }, | ||
// eslint-disable-next-line no-shadow -- this `zcf` is enclosed in a membrane | ||
async (/** @type {Orchestrator} */ orch, { zcf }, seat, offerArgs) => { | ||
const { give } = seat.getProposal(); | ||
!AmountMath.isEmpty(give.USDC.value) || Fail`Must provide USDC.`; | ||
|
||
const celestia = await orch.getChain('celestia'); | ||
const agoric = await orch.getChain('agoric'); | ||
|
||
const [celestiaAccount, localAccount] = await Promise.all([ | ||
celestia.createAccount(), | ||
agoric.createAccount(), | ||
]); | ||
|
||
const tiaAddress = await celestiaAccount.getChainAddress(); | ||
|
||
// deposit funds from user seat to LocalChainAccount | ||
const seatKit = zcf.makeEmptySeatKit(); | ||
zcf.atomicRearrange(harden([[seat, seatKit.zcfSeat, give]])); | ||
// seat.exit() // exit user seat now, or later? | ||
const payment = await E(seatKit.userSeat).getPayout('USDC'); | ||
await localAccount.deposit(payment); | ||
|
||
// build swap instructions with orcUtils library | ||
const transferMsg = orcUtils.makeOsmosisSwap({ | ||
destChain: 'celestia', | ||
destAddress: tiaAddress, | ||
amountIn: give.USDC, | ||
brandOut: offerArgs.staked.brand, | ||
slippage: 0.03, | ||
}); | ||
|
||
await localAccount | ||
.transferSteps(give.USDC, transferMsg) | ||
.then(_txResult => | ||
celestiaAccount.delegate(offerArgs.validator, offerArgs.staked), | ||
) | ||
.catch(e => console.error(e)); | ||
|
||
// XXX close localAccount? | ||
return celestiaAccount; // should be continuing inv since this is an offer? | ||
}, | ||
); | ||
|
||
const makeSwapAndStakeInvitation = () => | ||
zcf.makeInvitation( | ||
swapAndStakeHandler, | ||
'Swap for TIA and stake', | ||
undefined, | ||
harden({ | ||
give: { USDC: AmountShape }, | ||
want: {}, // XXX ChainAccount Ownable? | ||
exit: M.any(), | ||
}), | ||
); | ||
|
||
const publicFacet = Far('SwapAndStake Public Facet', { | ||
makeSwapAndStakeInvitation, | ||
}); | ||
|
||
return harden({ publicFacet }); | ||
}; |
87 changes: 87 additions & 0 deletions
87
packages/orchestration/src/contracts/unbondExample.contract.js
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,87 @@ | ||
// @ts-check | ||
import { Fail } from '@agoric/assert'; | ||
import { AmountMath, AmountShape } from '@agoric/ertp'; | ||
import { Far } from '@endo/far'; | ||
import { M } from '@endo/patterns'; | ||
import { makeOrchestrationFacade } from '../facade.js'; | ||
|
||
/** | ||
* @import {Orchestrator, ChainAccount, CosmosValidatorAddress} from '../types.js' | ||
* @import {TimerService} from '@agoric/time'; | ||
* @import {ERef} from '@endo/far' | ||
* @import {OrchestrationService} from '../service.js'; | ||
* @import {Zone} from '@agoric/zone'; | ||
*/ | ||
|
||
/** | ||
* @param {ZCF} zcf | ||
* @param {{ | ||
* orchestrationService: ERef<OrchestrationService>; | ||
* storageNode: ERef<StorageNode>; | ||
* timerService: ERef<TimerService>; | ||
* zone: Zone; | ||
* }} privateArgs | ||
*/ | ||
export const start = async (zcf, privateArgs) => { | ||
const { orchestrationService, storageNode, timerService, zone } = privateArgs; | ||
|
||
const { orchestrate } = makeOrchestrationFacade({ | ||
zone, | ||
timerService, | ||
zcf, | ||
storageNode, | ||
orchestrationService, | ||
}); | ||
|
||
/** @type {OfferHandler} */ | ||
const unbondAndLiquidStake = orchestrate( | ||
'LSTTia', | ||
{ zcf }, | ||
// eslint-disable-next-line no-shadow -- this `zcf` is enclosed in a membrane | ||
async (/** @type {Orchestrator} */ orch, { zcf }, seat, _offerArgs) => { | ||
console.log('zcf within the membrane', zcf); | ||
const { give } = seat.getProposal(); | ||
!AmountMath.isEmpty(give.USDC.value) || Fail`Must provide USDC.`; | ||
|
||
// We would actually alreaady have the account from the orchestrator | ||
// ??? could these be passed in? It would reduce the size of this handler, | ||
// keeping it focused on long-running operations. | ||
const celestia = await orch.getChain('celestia'); | ||
const celestiaAccount = await celestia.createAccount(); | ||
|
||
const delegations = await celestiaAccount.getDelegations(); | ||
const [undelegation] = await celestiaAccount.undelegate(delegations); | ||
|
||
// wait for the undelegations to be complete (may take weeks) | ||
await undelegation.completion; | ||
|
||
// ??? should this be synchronous? depends on how names are resolved. | ||
const stride = await orch.getChain('stride'); | ||
const strideAccount = await stride.createAccount(); | ||
|
||
// TODO the `TIA` string actually needs to be the Brand from AgoricNames | ||
const tiaAmt = await celestiaAccount.getBalance('TIA'); | ||
await celestiaAccount.transfer(tiaAmt, strideAccount.getChainAddress()); | ||
|
||
await strideAccount.liquidStake(tiaAmt); | ||
}, | ||
); | ||
|
||
const makeUnbondAndLiquidStakeInvitation = () => | ||
zcf.makeInvitation( | ||
unbondAndLiquidStake, | ||
'Unbond and liquid stake', | ||
undefined, | ||
harden({ | ||
give: { USDC: AmountShape }, | ||
want: {}, // XXX ChainAccount Ownable? | ||
exit: M.any(), | ||
}), | ||
); | ||
|
||
const publicFacet = Far('SwapAndStake Public Facet', { | ||
makeUnbondAndLiquidStakeInvitation, | ||
}); | ||
|
||
return harden({ publicFacet }); | ||
}; |
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,47 @@ | ||
// @ts-check | ||
/** @file Orchestration service */ | ||
|
||
/** | ||
* @import {Zone} from '@agoric/zone'; | ||
* @import {TimerService} from '@agoric/time'; | ||
* @import {OrchestrationService} from './service.js'; | ||
* @import {OrchestrationHandlerMaker} from './types.js'; | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {{ | ||
* zone: Zone; | ||
* timerService: ERef<TimerService>; | ||
* zcf: ERef<ZCF>; | ||
* storageNode: ERef<StorageNode>; | ||
* orchestrationService: ERef<OrchestrationService>; | ||
* }} powers | ||
*/ | ||
export const makeOrchestrationFacade = ({ | ||
zone, | ||
timerService, | ||
zcf, | ||
storageNode, | ||
orchestrationService, | ||
}) => { | ||
console.log('makeOrchestrationFacade got', { | ||
zone, | ||
timerService, | ||
zcf, | ||
storageNode, | ||
orchestrationService, | ||
}); | ||
|
||
return { | ||
/** | ||
* @type {OrchestrationHandlerMaker} | ||
*/ | ||
orchestrate(durableName, ctx, fn) { | ||
console.log('orchestrate got', durableName, ctx, fn); | ||
throw new Error('Not yet implemented'); | ||
}, | ||
}; | ||
}; | ||
harden(makeOrchestrationFacade); | ||
/** @typedef {ReturnType<typeof makeOrchestrationFacade>} OrchestrationFacade */ |
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
Oops, something went wrong.