Skip to content

Commit

Permalink
add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Feb 4, 2024
1 parent 613a953 commit c8b7982
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 9 deletions.
4 changes: 4 additions & 0 deletions contracts/coretime_market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ pub mod coretime_market {
MarketError::NotAllowed
);

// Transfer the region to the seller.
PSP34Ref::transfer(&self.xc_regions_contract, listing.seller, id.clone(), Default::default())
.map_err(MarketError::XcRegionsPsp34Error)?;

// Remove the region from sale:
self.remove_from_sale(region_id)?;

Expand Down
6 changes: 0 additions & 6 deletions tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { Region } from 'coretime-utils';
const REGION_COLLECTION_ID = 42;

export async function createRegionCollection(api: ApiPromise, caller: KeyringPair): Promise<void> {
console.log(`Creating the region collection`);

const createCollectionCall = api.tx.uniques.create(REGION_COLLECTION_ID, caller.address);

const callTx = async (resolve: () => void, reject: ({ reason }) => void) => {
Expand Down Expand Up @@ -49,8 +47,6 @@ export async function mintRegion(
caller: KeyringPair,
region: Region,
): Promise<void> {
console.log(`Minting a region`);

const rawRegionId = region.getEncodedRegionId(api);
const mintCall = api.tx.uniques.mint(REGION_COLLECTION_ID, rawRegionId, caller.address);

Expand All @@ -76,8 +72,6 @@ export async function approveTransfer(
region: Region,
delegate: string,
): Promise<void> {
console.log(`Approving region to ${delegate}`);

const rawRegionId = region.getEncodedRegionId(api);
const approveCall = api.tx.uniques.approveTransfer(REGION_COLLECTION_ID, rawRegionId, delegate);

Expand Down
10 changes: 8 additions & 2 deletions tests/market/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CoreMask, Id, Region, RegionId, RegionRecord } from 'coretime-utils';
import { MarketErrorBuilder, PSP34ErrorBuilder } from '../../types/types-returns/coretime_market';
import {
approveTransfer,
balanceOf,
createRegionCollection,
expectEvent,
expectOnSale,
Expand Down Expand Up @@ -74,6 +75,8 @@ describe('Coretime market listing', () => {

await xcRegions.withSigner(alice).tx.approve(market.address, id, true);

const aliceBalance = await balanceOf(api, alice.address);

const timeslicePrice = 50;
const result = await market
.withSigner(alice)
Expand All @@ -91,6 +94,7 @@ describe('Coretime market listing', () => {
timeslicePrice * (region.getEnd() - region.getBegin()),
);
expect((await xcRegions.query.ownerOf(id)).value.unwrap()).to.deep.equal(market.address);
expect(await balanceOf(api, alice.address)).to.be.lessThan(aliceBalance - LISTING_DEPOIST);
});

it('Listing requires listing deposit', async () => {
Expand All @@ -115,8 +119,10 @@ describe('Coretime market listing', () => {
await xcRegions.withSigner(alice).tx.approve(market.address, id, true);

const timeslicePrice = 50;
const result = market.withSigner(alice).query.listRegion(id, timeslicePrice, alice.address);
expect((await result).value.unwrap().err).to.deep.equal(MarketErrorBuilder.MissingDeposit());
const result = await market
.withSigner(alice)
.query.listRegion(id, timeslicePrice, alice.address);
expect(result.value.unwrap().err).to.deep.equal(MarketErrorBuilder.MissingDeposit());
});

it('Listing requires region to be approved to the market', async () => {
Expand Down
8 changes: 7 additions & 1 deletion tests/market/purchase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ describe('Coretime market purchases', () => {
// Alice's balance is increased.
expect(await balanceOf(api, alice.address)).to.be.greaterThan(aliceBalance);

// TODO: ensure the region is removed from sale:
// Ensure the region is removed from sale:
expect(market.query.listedRegions()).to.eventually.be.equal([]);
expect((await market.query.listedRegion(id)).value.unwrap().ok).to.be.equal(null);
});

it('Purchasing fails when insufficient value is sent', async () => {
Expand Down Expand Up @@ -227,5 +229,9 @@ describe('Coretime market purchases', () => {
expect(await balanceOf(api, charlie.address)).to.be.equal(
charlieBalance + timeslicePrice * (region.getEnd() - region.getBegin()),
);

// Ensure the region is removed from sale:
expect(market.query.listedRegions()).to.eventually.be.equal([]);
expect((await market.query.listedRegion(id)).value.unwrap().ok).to.be.equal(null);
});
});
174 changes: 174 additions & 0 deletions tests/market/unlist.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { expect, use } from 'chai';
import { KeyringPair } from '@polkadot/keyring/types';
import XcRegions_Factory from '../../types/constructors/xc_regions';
import Market_Factory from '../../types/constructors/coretime_market';
import XcRegions from '../../types/contracts/xc_regions';
import Market from '../../types/contracts/coretime_market';
import chaiAsPromised from 'chai-as-promised';
import { CoreMask, Id, Region, RegionId, RegionRecord } from 'coretime-utils';
import {
approveTransfer,
balanceOf,
createRegionCollection,
expectEvent,
expectOnSale,
initRegion,
mintRegion,
} from '../common';
import { MarketErrorBuilder } from '../../types/types-returns/coretime_market';

use(chaiAsPromised);

const REGION_COLLECTION_ID = 42;
const LISTING_DEPOIST = 5 * Math.pow(10, 15);

const wsProvider = new WsProvider('ws://127.0.0.1:9944');
// Create a keyring instance
const keyring = new Keyring({ type: 'sr25519', ss58Format: 5 });

describe('Coretime market unlisting', () => {
let api: ApiPromise;
let alice: KeyringPair;
let bob: KeyringPair;

let xcRegions: XcRegions;
let market: Market;

beforeEach(async function (): Promise<void> {
api = await ApiPromise.create({ provider: wsProvider, noInitWarn: true, types: { Id } });

alice = keyring.addFromUri('//Alice');
bob = keyring.addFromUri('//Bob');

const xcRegionsFactory = new XcRegions_Factory(api, alice);
xcRegions = new XcRegions((await xcRegionsFactory.new()).address, alice, api);

const marketFactory = new Market_Factory(api, alice);
market = new Market(
(await marketFactory.new(xcRegions.address, LISTING_DEPOIST)).address,
alice,
api,
);

if (!(await api.query.uniques.class(REGION_COLLECTION_ID)).toHuman()) {
await createRegionCollection(api, alice);
}
});

it('Unlisting works', async () => {
const regionId: RegionId = {
begin: 30,
core: 20,
mask: CoreMask.completeMask(),
};
const regionRecord: RegionRecord = {
end: 60,
owner: alice.address,
paid: null,
};
const region = new Region(regionId, regionRecord);

await mintRegion(api, alice, region);
await approveTransfer(api, alice, region, xcRegions.address);

await initRegion(api, xcRegions, alice, region);

const id: any = api.createType('Id', { U128: region.getEncodedRegionId(api) });
await xcRegions.withSigner(alice).tx.approve(market.address, id, true);

const aliceBalance = await balanceOf(api, alice.address);

const timeslicePrice = 5 * Math.pow(10, 12);
await market
.withSigner(alice)
.tx.listRegion(id, timeslicePrice, alice.address, { value: LISTING_DEPOIST });

await expectOnSale(market, id, alice, timeslicePrice);
expect((await xcRegions.query.ownerOf(id)).value.unwrap()).to.deep.equal(market.address);

expect(await balanceOf(api, alice.address)).to.be.lessThan(aliceBalance - LISTING_DEPOIST);

const result = await market.withSigner(alice).tx.unlistRegion(id);
expectEvent(result, 'RegionUnlisted', {
regionId: id.toPrimitive().u128,
caller: alice.address,
});

// Ensure the region is removed from sale:
expect(market.query.listedRegions()).to.eventually.be.equal([]);
expect((await market.query.listedRegion(id)).value.unwrap().ok).to.be.equal(null);

// The caller receives the deposit back.
// TODO fix:
//expect(await balanceOf(api, alice.address)).to.be.greaterThan(aliceBalance - LISTING_DEPOIST);
});

it('Unlisting not listed region fails', async () => {
const regionId: RegionId = {
begin: 30,
core: 21,
mask: CoreMask.completeMask(),
};
const regionRecord: RegionRecord = {
end: 60,
owner: alice.address,
paid: null,
};
const region = new Region(regionId, regionRecord);

await mintRegion(api, alice, region);
await approveTransfer(api, alice, region, xcRegions.address);

await initRegion(api, xcRegions, alice, region);

const id: any = api.createType('Id', { U128: region.getEncodedRegionId(api) });
await xcRegions.withSigner(alice).tx.approve(market.address, id, true);

const result = await market.withSigner(alice).query.unlistRegion(id);
expect(result.value.unwrap().err).to.deep.equal(MarketErrorBuilder.RegionNotListed());
});

it('Only owner can unlisting unexpired region', async () => {
const regionId: RegionId = {
begin: 30,
core: 22,
mask: CoreMask.completeMask(),
};
const regionRecord: RegionRecord = {
end: 60,
owner: alice.address,
paid: null,
};
const region = new Region(regionId, regionRecord);

await mintRegion(api, alice, region);
await approveTransfer(api, alice, region, xcRegions.address);

await initRegion(api, xcRegions, alice, region);

const id: any = api.createType('Id', { U128: region.getEncodedRegionId(api) });
await xcRegions.withSigner(alice).tx.approve(market.address, id, true);

const timeslicePrice = 5 * Math.pow(10, 12);
await market
.withSigner(alice)
.tx.listRegion(id, timeslicePrice, alice.address, { value: LISTING_DEPOIST });

await expectOnSale(market, id, alice, timeslicePrice);
expect((await xcRegions.query.ownerOf(id)).value.unwrap()).to.deep.equal(market.address);

const bobUnlistResult = await market.withSigner(bob).query.unlistRegion(id);
expect(bobUnlistResult.value.unwrap().err).to.deep.equal(MarketErrorBuilder.NotAllowed());

const aliceUnlistResult = await market.withSigner(alice).tx.unlistRegion(id);
expectEvent(aliceUnlistResult, 'RegionUnlisted', {
regionId: id.toPrimitive().u128,
caller: alice.address,
});

// Ensure the region is removed from sale:
expect(market.query.listedRegions()).to.eventually.be.equal([]);
expect((await market.query.listedRegion(id)).value.unwrap().ok).to.be.equal(null);
});
});

0 comments on commit c8b7982

Please sign in to comment.