test posting crypto and fiat offers with base and price inversion

This commit is contained in:
woodser 2022-05-11 13:17:41 -04:00
parent 7a4d3a375d
commit f2aca97e5c

View File

@ -923,21 +923,23 @@ test("Can create crypto payment accounts", async () => {
} }
}); });
test("Can post and remove an offer", async () => { test("Can post and remove offers", async () => {
// wait for alice to have unlocked balance to post offer // wait for alice to have at least 5 outputs of 0.5 XMR
const tradeAmount = BigInt("250000000000"); await waitForUnlockedOutputs([aliceWallet], BigInt("500000000000"), 5);
await waitForUnlockedBalance(tradeAmount * BigInt("2"), alice);
// get unlocked balance before reserving funds for offer // get unlocked balance before reserving funds for offer
const unlockedBalanceBefore = BigInt((await alice.getBalances()).getUnlockedBalance()); const unlockedBalanceBefore = BigInt((await alice.getBalances()).getUnlockedBalance());
// post offer // post crypto offer
const assetCode = getRandomAssetCode(); let assetCode = "ETH";
let offer: OfferInfo = await postOffer(alice, {assetCode: assetCode}); let price = 1 / 17;
if (isCrypto(assetCode)) assert.equal(offer.getBaseCurrencyCode(), assetCode); // TODO: crypto base/counter is inverted price = 1 / price; // TODO: price in crypto offer is inverted
else assert.equal(offer.getCounterCurrencyCode(),assetCode); let offer: OfferInfo = await postOffer(alice, {assetCode: assetCode, price: price});
assert.equal(offer.getState(), "AVAILABLE"); assert.equal(offer.getState(), "AVAILABLE");
assert.equal(offer.getBaseCurrencyCode(), assetCode); // TODO: base and counter currencies inverted in crypto offer
assert.equal(offer.getCounterCurrencyCode(), "XMR");
assert.equal(offer.getPrice(), price * 100000000); // TODO: price when posting crypto offer is inverted and * 100000000.
// has offer // has offer
offer = await alice.getMyOffer(offer.getId()); offer = await alice.getMyOffer(offer.getId());
@ -951,23 +953,28 @@ test("Can post and remove an offer", async () => {
// reserved balance released // reserved balance released
expect(BigInt((await alice.getBalances()).getUnlockedBalance())).toEqual(unlockedBalanceBefore); expect(BigInt((await alice.getBalances()).getUnlockedBalance())).toEqual(unlockedBalanceBefore);
});
test("Can prepare for trading", async () => { // post fiat offer
assetCode = "USD";
price = 180.0;
offer = await postOffer(alice, {assetCode: assetCode, price: price});
assert.equal(offer.getState(), "AVAILABLE");
assert.equal(offer.getBaseCurrencyCode(), "XMR");
assert.equal(offer.getCounterCurrencyCode(), "USD");
assert.equal(offer.getPrice(), price * 10000); // TODO: price = price * 10000
// create ethereum and revolut payment accounts // has offer
await createPaymentAccount(alice, "eth"); offer = await alice.getMyOffer(offer.getId());
await createRevolutPaymentAccount(alice); assert.equal(offer.getState(), "AVAILABLE");
await createPaymentAccount(bob, "eth");
await createRevolutPaymentAccount(bob);
// fund alice and bob with at least 5 outputs of 0.5 XMR // cancel offer
const numOutputs = 5; await alice.removeOffer(offer.getId());
const outputAmt = BigInt("500000000000");
const walletsToFund = []; // offer is removed from my offers
if (!await hasUnlockedOutputs(aliceWallet, outputAmt, numOutputs)) walletsToFund.push(aliceWallet); if (getOffer(await alice.getMyOffers(assetCode, "buy"), offer.getId())) throw new Error("Offer " + offer.getId() + " was found in my offers after removal");
if (!await hasUnlockedOutputs(bobWallet, outputAmt, numOutputs)) walletsToFund.push(bobWallet);
await fundWallets(walletsToFund, outputAmt, numOutputs); // reserved balance released
expect(BigInt((await alice.getBalances()).getUnlockedBalance())).toEqual(unlockedBalanceBefore);
}); });
// TODO (woodser): test grpc notifications // TODO (woodser): test grpc notifications
@ -1101,7 +1108,7 @@ test("Can resolve disputes", async () => {
// wait for alice and bob to have unlocked balance for trade // wait for alice and bob to have unlocked balance for trade
const tradeAmount = BigInt("250000000000"); const tradeAmount = BigInt("250000000000");
await fundWallets([aliceWallet, bobWallet], tradeAmount * BigInt("6"), 4); await waitForUnlockedOutputs([aliceWallet, bobWallet], tradeAmount * BigInt("6"), 4);
// register to receive notifications // register to receive notifications
const aliceNotifications: NotificationMessage[] = []; const aliceNotifications: NotificationMessage[] = [];
@ -1835,7 +1842,7 @@ async function hasUnlockedOutputs(wallet: any, amt: BigInt, numOutputs?: number)
* @param {BigInt} amt - the amount to fund * @param {BigInt} amt - the amount to fund
* @param {number?} numOutputs - the number of outputs of the given amount (default 1) * @param {number?} numOutputs - the number of outputs of the given amount (default 1)
*/ */
async function fundWallets(wallets: any[], amt: BigInt, numOutputs?: number): Promise<void> { async function waitForUnlockedOutputs(wallets: any[], amt: BigInt, numOutputs?: number): Promise<void> {
if (numOutputs === undefined) numOutputs = 1; if (numOutputs === undefined) numOutputs = 1;
// collect destinations // collect destinations