From ae081301580edc43113f8cba83c5f63f5bfe9448 Mon Sep 17 00:00:00 2001 From: Jorge-Lopes Date: Wed, 23 Oct 2024 12:16:54 +0100 Subject: [PATCH] chore(a3p): add helper methods and remove unnecessary comments rel: https://github.com/Agoric/BytePitchPartnerEng/issues/13 --- .../proposals/z:acceptance/kread.test.js | 7 -- .../proposals/z:acceptance/test-lib/kread.js | 89 ++++++++++--------- 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/a3p-integration/proposals/z:acceptance/kread.test.js b/a3p-integration/proposals/z:acceptance/kread.test.js index b0c1037a7dfb..53584bfd5e46 100644 --- a/a3p-integration/proposals/z:acceptance/kread.test.js +++ b/a3p-integration/proposals/z:acceptance/kread.test.js @@ -26,7 +26,6 @@ test.serial('Alice mints a new Character', async t => { await mintCharacter(Alice); const characterBalanceAfter = await getBalanceFromPurse(Alice, 'character'); - t.log(`Character minted: ${characterBalanceAfter}`); t.is( characterBalanceAfter.name, 'ephemeral_Ace', @@ -40,7 +39,6 @@ test.serial('Alice unequips all defaults Items', async t => { const characterId = 'ephemeral_Ace'; const characterInventoryBefore = await getCharacterInventory(characterId); - t.log(`Character inventory before unequipping: ${characterInventoryBefore}`); t.is( characterInventoryBefore.length, 3, @@ -50,7 +48,6 @@ test.serial('Alice unequips all defaults Items', async t => { await unequipAllItems(Alice); const characterInventoryAfter = await getCharacterInventory(characterId); - t.log(`Character inventory after unequipping: ${characterInventoryAfter}`); t.is( characterInventoryAfter.length, 0, @@ -82,7 +79,6 @@ test.serial('Alice sells one unequipped Item', async t => { itemNode => !itemListBefore.includes(itemNode), ); const soldItem = await getMarketItem(soldItemNode); - t.log(`Sold Item: ${soldItem}`); t.is( itemBefore.description, @@ -115,7 +111,6 @@ test.serial('Bob buys an Item on marketplace', async t => { ); const item = await getBalanceFromPurse(Bob, 'item'); - t.log(`Bought Item: ${item}`); t.is( item.description, marketItem.asset.description, @@ -148,7 +143,6 @@ test.serial('Alice sells a Character', async t => { const characterBalanceAfter = await getBalanceFromPurse(Alice, 'character'); t.is(characterBalanceAfter, null), 'Character should not be in purse after selling'; - t.log(`Sold Character: ${characterBalanceBefore}`); }); test.serial('Bob buys a Character on marketplace', async t => { @@ -166,5 +160,4 @@ test.serial('Bob buys a Character on marketplace', async t => { 'ephemeral_Ace', 'Character name should be ephemeral_Ace', ); - t.log(`Bought Character: ${characterBalance}`); }); diff --git a/a3p-integration/proposals/z:acceptance/test-lib/kread.js b/a3p-integration/proposals/z:acceptance/test-lib/kread.js index 02c577d3c60e..10ba5b21199a 100644 --- a/a3p-integration/proposals/z:acceptance/test-lib/kread.js +++ b/a3p-integration/proposals/z:acceptance/test-lib/kread.js @@ -9,6 +9,7 @@ import { import { makeFromBoard, boardSlottingMarshaller } from './rpc.js'; import { execFileSync } from 'child_process'; import { makeCopyBag } from '@agoric/store'; +import { AmountMath } from '@agoric/ertp'; const ISTunit = 1_000_000n; @@ -75,6 +76,16 @@ const unequipAllItemsOffer = async address => { const inventoryKeyId = kreadCharacter.keyId === 1 ? 2 : 1; const kreadCharacter2 = { ...kreadCharacter, keyId: inventoryKeyId }; + const kreadCharacterAmount = getAssetAmount( + brands.KREAdCHARACTER, + kreadCharacter, + ); + + const kreadCharacter2Amount = getAssetAmount( + brands.KREAdCHARACTER, + kreadCharacter2, + ); + const id = `KREAd-unequip-all-items-acceptance-test`; const body = { method: 'executeOffer', @@ -87,16 +98,10 @@ const unequipAllItemsOffer = async address => { }, proposal: { give: { - CharacterKey1: { - brand: brands.KREAdCHARACTER, - value: makeCopyBag([[kreadCharacter, 1n]]), - }, + CharacterKey1: kreadCharacterAmount, }, want: { - CharacterKey2: { - brand: brands.KREAdCHARACTER, - value: makeCopyBag([[kreadCharacter2, 1n]]), - }, + CharacterKey2: kreadCharacter2Amount, }, }, }, @@ -109,10 +114,8 @@ const buyItemOffer = async () => { const children = await getMarketItemsChildren(); const marketItem = await getMarketItem(children[0]); - const itemPrice = - marketItem.askingPrice.value + - marketItem.platformFee.value + - marketItem.royalty.value; + const itemAmount = getAssetAmount(brands.KREAdITEM, marketItem.asset); + const priceAmount = getAssetPriceAmount(marketItem); const id = `KREAd-buy-item-acceptance-test`; const body = { @@ -127,16 +130,10 @@ const buyItemOffer = async () => { offerArgs: { entryId: marketItem.id }, proposal: { give: { - Price: { - brand: brands.IST, - value: itemPrice, - }, + Price: priceAmount, }, want: { - Item: { - brand: brands.KREAdITEM, - value: makeCopyBag([[marketItem.asset, 1n]]), - }, + Item: itemAmount, }, }, }, @@ -151,6 +148,8 @@ const sellItemOffer = async address => { throw new Error('Item not found on user purse'); } + const itemAmount = getAssetAmount(brands.KREAdITEM, kreadItem); + const id = `KREAd-sell-item-acceptance-test`; const body = { method: 'executeOffer', @@ -163,10 +162,7 @@ const sellItemOffer = async address => { }, proposal: { give: { - Item: { - brand: brands.KREAdITEM, - value: makeCopyBag([[kreadItem, 1n]]), - }, + Item: itemAmount, }, want: { Price: { @@ -187,10 +183,11 @@ const buyCharacterOffer = async () => { const rawCharacterData = await agoric.follow('-lF', path, '-o', 'text'); const marketCharacter = marshaller.fromCapData(JSON.parse(rawCharacterData)); - const characterPrice = - marketCharacter.askingPrice.value + - marketCharacter.platformFee.value + - marketCharacter.royalty.value; + const kreadCharacterAmount = getAssetAmount( + brands.KREAdCHARACTER, + marketCharacter.asset, + ); + const priceAmount = getAssetPriceAmount(marketCharacter); const id = `KREAd-buy-character-acceptance-test`; const body = { @@ -204,16 +201,10 @@ const buyCharacterOffer = async () => { }, proposal: { give: { - Price: { - brand: brands.IST, - value: characterPrice, - }, + Price: priceAmount, }, want: { - Character: { - brand: brands.KREAdCHARACTER, - value: makeCopyBag([[marketCharacter.asset, 1n]]), - }, + Character: kreadCharacterAmount, }, }, }, @@ -228,6 +219,11 @@ const sellCharacterOffer = async address => { throw new Error('Character not found on user purse'); } + const kreadCharacterAmount = getAssetAmount( + brands.KREAdCHARACTER, + kreadCharacter, + ); + const id = `KREAd-sell-character-acceptance-test`; const body = { method: 'executeOffer', @@ -240,10 +236,7 @@ const sellCharacterOffer = async address => { }, proposal: { give: { - Character: { - brand: brands.KREAdCHARACTER, - value: makeCopyBag([[kreadCharacter, 1n]]), - }, + Character: kreadCharacterAmount, }, want: { Price: { @@ -344,3 +337,19 @@ export const getBalanceFromPurse = async (address, type) => { return assetPurse?.balance.value.payload[0]?.[0] || null; }; + +export const getAssetAmount = (brand, asset) => { + const assetValue = makeCopyBag([[asset, 1n]]); + const assetAmount = AmountMath.make( + brand, + //@ts-expect-error casting + assetValue, + ); + return assetAmount; +}; + +export const getAssetPriceAmount = asset => { + const fees = AmountMath.add(asset.platformFee, asset.royalty); + const price = AmountMath.add(asset.askingPrice, fees); + return price; +};