diff --git a/mock/brand.mock.ts b/mock/brand.mock.ts index f26b323..6a69c86 100644 --- a/mock/brand.mock.ts +++ b/mock/brand.mock.ts @@ -8,4 +8,5 @@ export const mockBrand = () => getDisplayInfo: () => ({ assetKind: AssetKind.NAT, }), + getAmountShape: () => '', }); diff --git a/package.json b/package.json index f68b41f..52342a5 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "eslint-plugin-react": "^7.30.1", "eslint-plugin-react-hooks": "^4.6.0", "framer-motion": "^7.2.1", + "happy-dom": "^10.7.0", "jotai": "^1.8.0", "lodash-es": "4.17.21", "patch-package": "^7.0.0", diff --git a/src/services/swap.ts b/src/services/swap.ts index 44c0ed0..44b1e75 100644 --- a/src/services/swap.ts +++ b/src/services/swap.ts @@ -1,8 +1,8 @@ import type { PursesJSONState } from '@agoric/wallet-backend'; +import type { WalletBridge } from 'store/app'; import { E } from '@endo/eventual-send'; - import { SwapDirection } from 'store/swap'; -import { WalletBridge } from 'store/app'; +import { AmountMath } from '@agoric/ertp'; type SwapContext = { wallet: WalletBridge; @@ -30,7 +30,11 @@ export const makeSwapOffer = async ({ ? 'makeWantMintedInvitation' : 'makeGiveMintedInvitation'; - const serializedInstance = await E(marshal).serialize(instanceId); + const [serializedInstance, serializedIn, serializedOut] = await Promise.all([ + E(marshal).serialize(instanceId), + E(marshal).serialize(AmountMath.make(fromPurse.brand, fromValue)), + E(marshal).serialize(AmountMath.make(toPurse.brand, toValue)), + ]); const offerConfig = { publicInvitationMaker: method, @@ -38,14 +42,12 @@ export const makeSwapOffer = async ({ proposalTemplate: { give: { In: { - pursePetname: fromPurse.pursePetname, - value: Number(fromValue), + amount: serializedIn, }, }, want: { Out: { - pursePetname: toPurse.pursePetname, - value: Number(toValue), + amount: serializedOut, }, }, }, diff --git a/test/services/swap.test.ts b/test/services/swap.test.ts new file mode 100644 index 0000000..4021501 --- /dev/null +++ b/test/services/swap.test.ts @@ -0,0 +1,49 @@ +import { expect, it, describe } from 'vitest'; +import { mockBrand } from '../../mock/brand.mock'; +import { makeMarshal } from '@endo/marshal'; +import { makeSwapOffer } from '../../src/services/swap'; +import { SwapDirection } from '../../src/store/swap'; +import { AmountMath } from '@agoric/ertp'; + +describe('helpers', () => { + const minted = mockBrand(); + const anchor = mockBrand(); + + it('serializes the amounts properly', async () => { + let config: any; + const wallet = { + addOffer: offerConfig => { + config = offerConfig; + }, + }; + + const marshal = makeMarshal(); + + const d = 10n ** 18n; + const fromValue = 5000n * d; + const toValue = 50_000n * d; + + expect(Number(fromValue) > Number.MAX_SAFE_INTEGER).toBe(true); + + await makeSwapOffer({ + wallet, + instanceId: '123', + fromPurse: { brand: anchor }, + fromValue, + toPurse: { brand: minted }, + toValue, + swapDirection: SwapDirection.WantMinted, + marshal, + }); + + const fromAmount = marshal.fromCapData( + config.proposalTemplate.give.In.amount + ); + const toAmount = marshal.fromCapData( + config.proposalTemplate.want.Out.amount + ); + + expect(AmountMath.isEqual(AmountMath.make(anchor, fromValue), fromAmount)); + expect(AmountMath.isEqual(AmountMath.make(minted, toValue), toAmount)); + }); +}); diff --git a/vitest.config.ts b/vitest.config.ts index 3659233..021c617 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -7,6 +7,7 @@ export default mergeConfig( defineConfig({ test: { setupFiles: ['src/installSesLockdown.ts'], + environment: 'happy-dom', }, }) ); diff --git a/yarn.lock b/yarn.lock index 91814b6..cbb18c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2844,6 +2844,11 @@ css-to-react-native@^3.0.0: css-color-keywords "^1.0.0" postcss-value-parser "^4.0.2" +css.escape@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/css.escape/-/css.escape-1.5.1.tgz#42e27d4fa04ae32f931a4b4d4191fa9cddee97cb" + integrity sha512-YUifsXXuknHlUsmlgyY0PKzgPOr7/FjCePfHNt0jxm83wHZi44VDMQ7/fGNkjY3/jV1MC+1CmZbaHzugyeRtpg== + cssesc@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-3.0.0.tgz#37741919903b868565e1c09ea747445cd18983ee" @@ -3066,6 +3071,11 @@ end-of-stream@^1.1.0, end-of-stream@^1.4.1: dependencies: once "^1.4.0" +entities@^4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48" + integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw== + es-abstract@^1.19.0, es-abstract@^1.19.1, es-abstract@^1.19.2, es-abstract@^1.19.5: version "1.20.4" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.20.4.tgz#1d103f9f8d78d4cf0713edcd6d0ed1a46eed5861" @@ -3840,6 +3850,18 @@ grapheme-splitter@^1.0.4: resolved "https://registry.yarnpkg.com/grapheme-splitter/-/grapheme-splitter-1.0.4.tgz#9cf3a665c6247479896834af35cf1dbb4400767e" integrity sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ== +happy-dom@^10.7.0: + version "10.7.0" + resolved "https://registry.yarnpkg.com/happy-dom/-/happy-dom-10.7.0.tgz#923236e1377b983c51eee51b0b0035acde361f90" + integrity sha512-dJFPbA7KW4kkH3MFJBPTX73ywOfcrJcu6CAUw0CnCH0aEDcnPNG8QVmOAxsJSvTZFzLCnmSx/jMzAp00hdm0gw== + dependencies: + css.escape "^1.5.1" + entities "^4.5.0" + iconv-lite "^0.6.3" + webidl-conversions "^7.0.0" + whatwg-encoding "^2.0.0" + whatwg-mimetype "^3.0.0" + has-bigints@^1.0.1, has-bigints@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/has-bigints/-/has-bigints-1.0.2.tgz#0871bd3e3d51626f6ca0966668ba35d5602d6eaa" @@ -3910,6 +3932,13 @@ hoist-non-react-statics@^3.0.0: dependencies: react-is "^16.7.0" +iconv-lite@0.6.3, iconv-lite@^0.6.3: + version "0.6.3" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.6.3.tgz#a52f80bf38da1952eb5c681790719871a1a72501" + integrity sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw== + dependencies: + safer-buffer ">= 2.1.2 < 3.0.0" + iconv-lite@^0.4.24: version "0.4.24" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" @@ -5411,7 +5440,7 @@ safe-stable-stringify@^2.3.1: resolved "https://registry.yarnpkg.com/safe-stable-stringify/-/safe-stable-stringify-2.4.3.tgz#138c84b6f6edb3db5f8ef3ef7115b8f55ccbf886" integrity sha512-e2bDA2WJT0wxseVd4lsDP4+3ONX6HpMXQa1ZhFQ7SU+GjvORCmShbCMltrtIDfkYhVHrOcPtj+KhmDBdPdZD1g== -"safer-buffer@>= 2.1.2 < 3": +"safer-buffer@>= 2.1.2 < 3", "safer-buffer@>= 2.1.2 < 3.0.0": version "2.1.2" resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== @@ -5424,19 +5453,19 @@ scheduler@^0.23.0: loose-envify "^1.1.0" semver@^5.5.0, semver@^5.6.0: - version "5.7.1" - resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" - integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== + version "5.7.2" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.2.tgz#48d55db737c3287cd4835e17fa13feace1c41ef8" + integrity sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g== semver@^6.3.0: - version "6.3.0" - resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" - integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw== + version "6.3.1" + resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4" + integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA== semver@^7.0.0, semver@^7.3.5, semver@^7.3.7: - version "7.3.8" - resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.8.tgz#07a78feafb3f7b32347d725e33de7e2a2df67798" - integrity sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A== + version "7.5.4" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e" + integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA== dependencies: lru-cache "^6.0.0" @@ -6056,6 +6085,23 @@ webidl-conversions@^3.0.0: resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== +webidl-conversions@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a" + integrity sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g== + +whatwg-encoding@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/whatwg-encoding/-/whatwg-encoding-2.0.0.tgz#e7635f597fd87020858626805a2729fa7698ac53" + integrity sha512-p41ogyeMUrw3jWclHWTQg1k05DSVXPLcVxRTYsXUk+ZooOCZLcoYgPZ/HL/D/N+uQPOtcp1me1WhBEaX02mhWg== + dependencies: + iconv-lite "0.6.3" + +whatwg-mimetype@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz#5fa1a7623867ff1af6ca3dc72ad6b8a4208beba7" + integrity sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q== + whatwg-url@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d" @@ -6107,9 +6153,9 @@ winston@3.3.3: winston-transport "^4.4.0" word-wrap@^1.2.3: - version "1.2.3" - resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c" - integrity sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ== + version "1.2.4" + resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.4.tgz#cb4b50ec9aca570abd1f52f33cd45b6c61739a9f" + integrity sha512-2V81OA4ugVo5pRo46hAoD2ivUJx8jXmWXfUkY4KFNw0hEptvN0QfH3K4nHiwzGeKl5rFKedV48QVoqYavy4YpA== wrap-ansi@^7.0.0: version "7.0.0"