-
Notifications
You must be signed in to change notification settings - Fork 208
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
7 changed files
with
247 additions
and
165 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
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 |
---|---|---|
@@ -1,77 +1,131 @@ | ||
import { M } from '@endo/patterns'; | ||
import { provideLazy } from '@agoric/store'; | ||
|
||
import { AmountMath, AmountShape } from '@agoric/ertp'; | ||
|
||
import { AmountMath } from '@agoric/ertp'; | ||
import { DenomAmountShape } from '@agoric/orchestration'; | ||
import { | ||
MinOrchAccountAddressI, | ||
MinChainI, | ||
MinChainShape, | ||
MinDenomInfoShape, | ||
MinOrchAccountI, | ||
MinOrchChainI, | ||
MinOrchAccountShape, | ||
MinOrchestratorI, | ||
} from './typeGuards.js'; | ||
|
||
/** | ||
* @import {Zone} from '@agoric/zone' | ||
* | ||
* @import {MinOrchAccountAddress} from './types.js' | ||
* @import {Zone} from '@agoric/base-zone' | ||
*/ | ||
|
||
const makeMinDenomInfo = (chain, _denom) => { | ||
const brand = {}; // TODO the whole thing | ||
return harden({ | ||
brand, | ||
chain, | ||
}); | ||
}; | ||
|
||
/** | ||
* @param {Zone} zone | ||
*/ | ||
export const prepareMockOrchChain = zone => { | ||
let amp; | ||
const makeAccountKit = zone.exoClassKit( | ||
'MockOrchAccount', | ||
{ | ||
account: MinOrchAccountI, | ||
address: MinOrchAccountAddressI, | ||
incrFacet: M.interface('Incr', { | ||
incr: M.call(AmountShape).returns(), | ||
}), | ||
export const prepareMinOrchestrator = zone => { | ||
const makeMinOrchAccount = zone.exoClass( | ||
'MinOrchAccount', | ||
MinOrchAccountI, | ||
(chain, ledger) => { | ||
const addrValue = `${ledger.size()}`; | ||
return { chain, ledger, addrValue }; | ||
}, | ||
brand => ({ | ||
fullBalance: AmountMath.makeEmpty(brand), | ||
}), | ||
{ | ||
account: { | ||
async getFullBalance() { | ||
return this.state.fullBalance; | ||
}, | ||
/** | ||
* @param {MinOrchAccountAddress} dest | ||
* @param {Amount} depositAmount | ||
*/ | ||
async transfer(dest, depositAmount) { | ||
const destIncrFacet = amp(dest).incrFacet; | ||
this.state.fullBalance = AmountMath.subtract( | ||
this.state.fullBalance, | ||
depositAmount, | ||
); | ||
destIncrFacet.incr(depositAmount); | ||
}, | ||
getAddress() { | ||
return this.facets.address; | ||
}, | ||
getAddress() { | ||
const { chain, addrValue: value } = this.state; | ||
const { chainId } = chain.getChainInfo(); | ||
return harden({ chainId, value }); | ||
}, | ||
address: {}, | ||
incrFacet: { | ||
incr(amount) { | ||
this.state.fullBalance = AmountMath.add( | ||
this.state.fullBalance, | ||
amount, | ||
); | ||
}, | ||
getBalances() { | ||
const { ledger, addrValue } = this.state; | ||
const { balances } = ledger.get(addrValue); | ||
return [...balances.values()]; | ||
}, | ||
getBalance(denom) { | ||
const { ledger, addrValue } = this.state; | ||
const { balances } = ledger.get(addrValue); | ||
return balances.get(denom); | ||
}, | ||
transfer(destAddr, denomAmount) { | ||
|
||
}, | ||
}, | ||
); | ||
|
||
const makeMinChain = zone.exoClass( | ||
'MinChain', | ||
MinChainI, | ||
chainName => ({ | ||
chainId: chainName, | ||
denoms: zone.mapStore('denoms', { | ||
keyShape: M.string(), // denom | ||
valueShape: MinDenomInfoShape, | ||
}), | ||
ledger: zone.mapStore('accounts', { | ||
keyShape: M.string(), // addrValue | ||
valueShape: { | ||
account: MinOrchAccountShape, | ||
balances: M.remotable('balances'), | ||
}, | ||
}), | ||
}), | ||
{ | ||
receiveAmplifier(a) { | ||
amp = a; | ||
getChainInfo() { | ||
const { chainId } = this.state; | ||
return harden({ chainId }); | ||
}, | ||
makeAccount() { | ||
const { ledger } = this.state; | ||
const { self } = this; | ||
const account = makeMinOrchAccount(self, ledger); | ||
const { value: addrValue } = account.getAddress(); | ||
ledger.init( | ||
addrValue, | ||
harden({ | ||
account, | ||
balances: zone.mapStore('balances', { | ||
keyShape: M.string(), // denom | ||
valueShape: DenomAmountShape, | ||
}), | ||
}), | ||
); | ||
return account; | ||
}, | ||
getDenomInfo(denom) { | ||
const { denoms } = this.state; | ||
const { self } = this; | ||
return provideLazy(denoms, denom, d => makeMinDenomInfo(self, d)); | ||
}, | ||
asAmount(denomAmount) { | ||
const { self } = this; | ||
const { denom, value } = denomAmount; | ||
const { brand } = self.getDenomInfo(denom); | ||
return AmountMath.make(brand, value); | ||
}, | ||
}, | ||
); | ||
|
||
return zone.exo('MockOrchChain', MinOrchChainI, { | ||
makeAccount(brand) { | ||
return Promise.resolve(makeAccountKit(brand).account); | ||
const makeMinOrchestrator = zone.exoClass( | ||
'MinOrchestrator', | ||
MinOrchestratorI, | ||
() => ({ | ||
chains: zone.mapStore('chains', { | ||
keyShape: M.string(), | ||
valueShape: MinChainShape, | ||
}), | ||
}), | ||
{ | ||
getChain(chainName) { | ||
const { chains } = this.state; | ||
return provideLazy(chains, chainName, makeMinChain); | ||
}, | ||
}, | ||
}); | ||
); | ||
return makeMinOrchestrator; | ||
}; | ||
harden(prepareMinOrchestrator); |
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.