-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add invariant helper and constant product use example (#3090)
- Loading branch information
1 parent
cdf158b
commit f533f76
Showing
7 changed files
with
141 additions
and
13 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 |
---|---|---|
|
@@ -36,6 +36,7 @@ export { | |
withdrawFromSeat, | ||
saveAllIssuers, | ||
offerTo, | ||
checkZCF, | ||
} from './zoeHelpers'; | ||
|
||
export { | ||
|
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
38 changes: 38 additions & 0 deletions
38
packages/zoe/src/contracts/multipoolAutoswap/constantProduct.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,38 @@ | ||
// @ts-check | ||
|
||
import { assert, details as X } from '@agoric/assert'; | ||
import { natSafeMath } from '../../contractSupport'; | ||
|
||
// A pool seat has Central and Secondary keywords, and a swap seat has | ||
// In and Out keywords | ||
const isPoolSeat = allocation => { | ||
return allocation.Central !== undefined || allocation.Secondary !== undefined; | ||
}; | ||
|
||
const calcK = allocation => { | ||
return natSafeMath.multiply( | ||
allocation.Secondary.value, | ||
allocation.Central.value, | ||
); | ||
}; | ||
|
||
/** | ||
* | ||
* @param {SeatStaging[]} stagings | ||
*/ | ||
export const assertConstantProduct = stagings => { | ||
stagings.forEach(seatStaging => { | ||
const seat = seatStaging.getSeat(); | ||
const priorAllocation = seat.getCurrentAllocation(); | ||
const stagedAllocation = seatStaging.getStagedAllocation(); | ||
if (isPoolSeat(stagedAllocation)) { | ||
const oldK = calcK(priorAllocation); | ||
const newK = calcK(stagedAllocation); | ||
console.log('oldK', oldK, 'newK', newK); | ||
assert( | ||
newK >= oldK, | ||
X`the product of the pool tokens must not decrease as the result of a trade. ${oldK} decreased to ${newK}`, | ||
); | ||
} | ||
}); | ||
}; |
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
64 changes: 64 additions & 0 deletions
64
packages/zoe/test/unitTests/contracts/multipoolAutoswap/test-constantProduct.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,64 @@ | ||
// @ts-check | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { test } from '@agoric/zoe/tools/prepare-test-env-ava'; | ||
|
||
import { AmountMath } from '@agoric/ertp'; | ||
|
||
import { checkZCF } from '../../../../src/contractSupport'; | ||
import { assertConstantProduct } from '../../../../src/contracts/multipoolAutoswap/constantProduct'; | ||
import { setupZCFTest } from '../../zcf/setupZcfTest'; | ||
|
||
test('constantProduct invariant', async t => { | ||
const { zcf } = await setupZCFTest(); | ||
|
||
const checkedZCF = checkZCF(zcf, assertConstantProduct); | ||
|
||
const { zcfSeat: poolSeat } = checkedZCF.makeEmptySeatKit(); | ||
const { zcfSeat: swapSeat } = checkedZCF.makeEmptySeatKit(); | ||
|
||
// allocate some secondary and central to the poolSeat | ||
const centralMint = await checkedZCF.makeZCFMint('Central'); | ||
const { brand: centralBrand } = centralMint.getIssuerRecord(); | ||
const secondaryMint = await checkedZCF.makeZCFMint('Secondary'); | ||
const { brand: secondaryBrand } = secondaryMint.getIssuerRecord(); | ||
centralMint.mintGains( | ||
{ Central: AmountMath.make(centralBrand, 10n ** 6n) }, | ||
poolSeat, | ||
); | ||
secondaryMint.mintGains( | ||
{ Secondary: AmountMath.make(secondaryBrand, 10n ** 6n) }, | ||
poolSeat, | ||
); | ||
|
||
const poolSeatAllocation = poolSeat.getCurrentAllocation(); | ||
t.deepEqual(poolSeatAllocation, { | ||
Central: AmountMath.make(centralBrand, 10n ** 6n), | ||
Secondary: AmountMath.make(secondaryBrand, 10n ** 6n), | ||
}); | ||
|
||
// const oldK = | ||
// poolSeatAllocation.Secondary.value * poolSeatAllocation.Central.value; | ||
|
||
// const newK = 0; | ||
|
||
// Let's give the swap user all the tokens and take | ||
// nothing, a clear violation of the constant product | ||
t.throws( | ||
() => | ||
checkedZCF.reallocate( | ||
poolSeat.stage({ | ||
Central: AmountMath.make(centralBrand, 0n), | ||
Secondary: AmountMath.make(secondaryBrand, 0n), | ||
}), | ||
swapSeat.stage({ | ||
In: poolSeatAllocation.Central, | ||
Out: poolSeatAllocation.Secondary, | ||
}), | ||
), | ||
{ | ||
message: | ||
'the product of the pool tokens must not decrease as the result of a trade. "[1000000000000n]" decreased to "[0n]"', | ||
}, | ||
); | ||
}); |
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