Skip to content

Commit

Permalink
chore(a3p): add helper methods and remove unnecessary comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge-Lopes committed Oct 23, 2024
1 parent a05465b commit ae08130
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
7 changes: 0 additions & 7 deletions a3p-integration/proposals/z:acceptance/kread.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand All @@ -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}`);
});
89 changes: 49 additions & 40 deletions a3p-integration/proposals/z:acceptance/test-lib/kread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
Expand All @@ -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,
},
},
},
Expand All @@ -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 = {
Expand All @@ -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,
},
},
},
Expand All @@ -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',
Expand All @@ -163,10 +162,7 @@ const sellItemOffer = async address => {
},
proposal: {
give: {
Item: {
brand: brands.KREAdITEM,
value: makeCopyBag([[kreadItem, 1n]]),
},
Item: itemAmount,
},
want: {
Price: {
Expand All @@ -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 = {
Expand All @@ -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,
},
},
},
Expand All @@ -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',
Expand All @@ -240,10 +236,7 @@ const sellCharacterOffer = async address => {
},
proposal: {
give: {
Character: {
brand: brands.KREAdCHARACTER,
value: makeCopyBag([[kreadCharacter, 1n]]),
},
Character: kreadCharacterAmount,
},
want: {
Price: {
Expand Down Expand Up @@ -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;
};

0 comments on commit ae08130

Please sign in to comment.