Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Refactor ui/src/services and remove uncommon getters in BaseShop #434

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ export * from './CandyShopDropAPI';
export * from './CandyShopPay';
export * from './CandyShopInfoAPI';
export * from './CandyShopAuction';

/* Only export the vendor folder to control the exposure of some internal methods */
export * from './vendor';
/* ETH SDK */

/* Multi-Chain objects */
export * from './shop';
20 changes: 2 additions & 18 deletions core/sdk/src/shop/base/BaseShop.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import {
Blockchain,
CandyShop,
ListBase,
Nft,
OrdersEditionFilterQuery,
OrdersFilterQuery,
SingleBase,
Trade,
TradeQuery,
WhitelistNft
} from '@liqnft/candy-shop-types';
import { Order } from '@opensea/seaport-js/lib/types';
import { BlockchainType, CandyShopVersion, ExplorerLinkBase, ShopSettings } from './BaseShopModel';
import { Blockchain, ListBase, Nft, OrdersEditionFilterQuery, Trade, TradeQuery } from '@liqnft/candy-shop-types';
import { CandyShopVersion, ExplorerLinkBase, ShopSettings } from './BaseShopModel';

export abstract class BaseShop {
constructor(params: any) {}
Expand All @@ -21,20 +9,16 @@ export abstract class BaseShop {
abstract get candyShopCreatorAddress(): string;
abstract get treasuryMint(): string;
abstract get settings(): Partial<ShopSettings>;
abstract get blockchain(): BlockchainType;
abstract get baseUnitsPerCurrency(): number;
abstract get currencySymbol(): string;
abstract get currencyDecimals(): number;
abstract get priceDecimalsMin(): number;
abstract get priceDecimals(): number;
abstract get volumeDecimals(): number;
abstract get volumeDecimalsMin(): number;
abstract get programId(): string | undefined;
abstract get env(): Blockchain;
abstract get version(): CandyShopVersion;
abstract get explorerLink(): ExplorerLinkBase;
abstract get connection(): any;
abstract get isEnterprise(): boolean;

// Marketplace
abstract buy(params: any): Promise<string>;
Expand Down
4 changes: 2 additions & 2 deletions core/sdk/src/shop/base/BaseShopModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export enum CandyShopVersion {
* Blockchains that CandyShop supports
*/
export enum BlockchainType {
Ethereum = 'Ethereum',
Solana = 'Solana'
EVM = 'EVM',
SOL = 'SOL'
}

/**
Expand Down
20 changes: 1 addition & 19 deletions core/sdk/src/shop/eth/EthCandyShop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseShop } from '../base/BaseShop';
import { BlockchainType, CandyShopVersion, ExplorerLinkBase, ShopSettings } from '../base/BaseShopModel';
import { CandyShopVersion, ExplorerLinkBase, ShopSettings } from '../base/BaseShopModel';
import { AssetType, EthereumSDK } from '../../factory/conveyor/eth/conveyor';
import { constants, ethers } from 'ethers';
import { CreateOrderInterface, OrderInterface } from '../../factory/conveyor/eth/types/order.type';
Expand Down Expand Up @@ -55,7 +55,6 @@ export class EthCandyShop extends BaseShop {
private _env: Blockchain;
private _settings: ShopSettings;
private _baseUnitsPerCurrency: number;
private _blockchainType: BlockchainType;

set candyShopAddress(shopUuid: string) {
this._candyShopAddress = shopUuid;
Expand Down Expand Up @@ -89,10 +88,6 @@ export class EthCandyShop extends BaseShop {
return this._env;
}

get blockchain(): BlockchainType {
return this._blockchainType;
}

get baseUnitsPerCurrency(): number {
return this._baseUnitsPerCurrency;
}
Expand Down Expand Up @@ -121,18 +116,6 @@ export class EthCandyShop extends BaseShop {
return this._settings.volumeDecimalsMin;
}

get isEnterprise(): boolean {
throw new Error('Method not implemented.');
}

get programId(): string | undefined {
throw new Error('Method not implemented.');
}

get connection(): any {
throw new Error('Method not implemented.');
}

/**
* Instantiate a EthCandyShop object
*
Expand All @@ -159,7 +142,6 @@ export class EthCandyShop extends BaseShop {
explorerLink: settings?.explorerLink ?? ExplorerLinkBase.Polygon
};
this._baseUnitsPerCurrency = Math.pow(10, this._settings.currencyDecimals);
this._blockchainType = BlockchainType.Ethereum;
console.log('EthCandyShop constructor: init CandyShop=', this);
}

Expand Down
6 changes: 5 additions & 1 deletion core/sdk/src/shop/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export * from './base/BaseShop';
export * from './base/BaseShopModel';

export * from './eth/EthCandyShop';
export * from './eth/EthShopModel';

export * from './sol/CandyShop';
export * from './base/BaseShopModel';
export * from './sol/CandyShopModel';
37 changes: 16 additions & 21 deletions core/sdk/src/shop/sol/CandyShop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import {
} from '../../vendor';
import { configBaseUrl } from '../../vendor/config';
import { supply } from '../../vendor/shipping';
import { BlockchainType, CandyShopVersion, ExplorerLinkBase, ShopSettings } from '../base/BaseShopModel';
import { CandyShopVersion, ExplorerLinkBase, ShopSettings } from '../base/BaseShopModel';

const Logger = 'CandyShop';

Expand Down Expand Up @@ -102,7 +102,6 @@ export class CandyShop extends BaseShop {
private _isEnterprise: boolean;
private _version: CandyShopVersion;
private _program: Program | undefined;
private _blockchainType: BlockchainType;

get currencyDecimals(): number {
return this._settings.currencyDecimals;
Expand All @@ -112,10 +111,6 @@ export class CandyShop extends BaseShop {
return this._settings;
}

get blockchain(): BlockchainType {
return this._blockchainType;
}

get env(): Blockchain {
return this._env;
}
Expand Down Expand Up @@ -176,6 +171,21 @@ export class CandyShop extends BaseShop {
return this._settings.explorerLink;
}

/**
* Get JSON rpc connection
*/
get connection(): web3.Connection {
const options = Provider.defaultOptions();
if (this._env === 'devnet') {
return new web3.Connection(web3.clusterApiUrl('devnet'));
}

return new web3.Connection(
this._settings.mainnetConnectionUrl,
this._settings.connectionConfig || options.commitment
);
}

/**
* Instantiate a CandyShop object
*
Expand Down Expand Up @@ -212,7 +222,6 @@ export class CandyShop extends BaseShop {
explorerLink: settings?.explorerLink ?? ExplorerLinkBase.SolanaFM
};
this._baseUnitsPerCurrency = Math.pow(10, this._settings.currencyDecimals);
this._blockchainType = BlockchainType.Solana;
console.log('CandyShop constructor: init CandyShop=', this);
const url = this._env === Blockchain.SolMainnetBeta ? SOL_BACKEND_PROD_URL : SOL_BACKEND_STAGING_URL;

Expand All @@ -230,20 +239,6 @@ export class CandyShop extends BaseShop {
}
}

/**
* Get JSON rpc connection
*/
get connection(): web3.Connection {
const options = Provider.defaultOptions();
if (this._env === 'devnet') {
return new web3.Connection(web3.clusterApiUrl('devnet'));
}

return new web3.Connection(
this._settings.mainnetConnectionUrl,
this._settings.connectionConfig || options.commitment
);
}
/**
* Gets anchor Program object for Candy Shop program
*
Expand Down
4 changes: 1 addition & 3 deletions core/ui/src/components/CancelModal/CancelModalDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export const CancelModalDetail: React.FC<CancelModalDetailProps> = ({
)}
<div className="candy-stat-horizontal">
<div>
<div className="candy-label">
{blockchain === BlockchainType.Ethereum ? 'CONTRACT ADDRESS' : 'MINT ADDRESS'}
</div>
<div className="candy-label">{blockchain === BlockchainType.EVM ? 'CONTRACT ADDRESS' : 'MINT ADDRESS'}</div>
<div className="candy-value">
<div className="candy-value">
{
Expand Down
2 changes: 1 addition & 1 deletion core/ui/src/components/ExplorerLink/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const ExplorerLink: React.FC<ExplorerLinkProps> = ({

let aLink = '';

if (blockchain === BlockchainType.Solana) {
if (blockchain === BlockchainType.SOL) {
aLink = `${BaseUrlType[explorerLink || ExplorerLinkBase.Explorer]}/${type}/${address}${getClusterQuery(
candyShopEnv,
explorerLink || ExplorerLinkBase.Explorer
Expand Down
30 changes: 10 additions & 20 deletions core/ui/src/components/InfiniteOrderList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import React, { useMemo, useState } from 'react';
import { Order as OrderComponent } from 'components/Order';
import { Order } from '@liqnft/candy-shop-types';
import InfiniteScroll from 'react-infinite-scroll-component';
import { BlockchainType, CandyShop, getCandyShopSync } from '@liqnft/candy-shop-sdk';
import { CandyShop, getCandyShopSync } from '@liqnft/candy-shop-sdk';
import { Order as OrderSchema } from '@liqnft/candy-shop-types';
import { web3 } from '@project-serum/anchor';
import { getDefaultExchange, getExchangeInfo } from 'utils/getExchangeInfo';
import { BuyModal } from 'components/BuyModal';
import { CancelModal } from 'components/CancelModal';
import { LoadingSkeleton } from 'components/LoadingSkeleton';
import { Order as OrderComponent } from 'components/Order';
import { ShopProps } from 'model';
import { CancelerFactory } from 'services/canceler';
import { BuyerFactory } from 'services/buyer';
import { web3 } from '@project-serum/anchor';
import { StoreProvider } from 'market';

interface InfiniteOrderListProps extends ShopProps {
orders: Order[];
Expand All @@ -29,32 +28,24 @@ export const InfiniteOrderList: React.FC<InfiniteOrderListProps> = ({
hasNextPage,
loadNextPage,
sellerUrl,
blockchain,
candyShop,
wallet
}) => {
const { canceler, buyer } = useMemo(() => {
return {
canceler: CancelerFactory({ candyShop, wallet, blockchain }),
buyer: BuyerFactory({ candyShop, wallet, blockchain })
};
}, [candyShop, blockchain, wallet]);
const store = useMemo(() => StoreProvider({ candyShop, wallet }), [candyShop, wallet]);

const cancelOrder = (order: Order) => {
return canceler.cancel(order);
return store.cancel(order);
};

const buyNft = (order: Order) => {
return buyer.buy(order);
return store.buy(order);
};

const [selectedOrder, setSelectedOrder] = useState<OrderSchema>();
const onSelectOrder = (order?: OrderSchema) => setSelectedOrder(order);

const exchangeInfo =
blockchain === BlockchainType.Solana
? getExchangeInfo(selectedOrder, candyShop as CandyShop)
: getDefaultExchange(candyShop);
candyShop instanceof CandyShop ? getExchangeInfo(selectedOrder, candyShop) : getDefaultExchange(candyShop);

const isUserListing =
selectedOrder &&
Expand All @@ -64,7 +55,7 @@ export const InfiniteOrderList: React.FC<InfiniteOrderListProps> = ({
const shopAddress = useMemo(() => {
if (!selectedOrder) return '';

if (blockchain === BlockchainType.Solana) {
if (candyShop instanceof CandyShop) {
return getCandyShopSync(
new web3.PublicKey(selectedOrder.candyShopCreatorAddress),
new web3.PublicKey(selectedOrder.treasuryMint),
Expand All @@ -73,7 +64,7 @@ export const InfiniteOrderList: React.FC<InfiniteOrderListProps> = ({
} else {
return candyShop.candyShopAddress;
}
}, [blockchain, candyShop.candyShopAddress, selectedOrder]);
}, [candyShop, selectedOrder]);

return (
<>
Expand All @@ -87,7 +78,6 @@ export const InfiniteOrderList: React.FC<InfiniteOrderListProps> = ({
url={url}
sellerUrl={sellerUrl}
onOrderSelection={onSelectOrder}
blockchain={blockchain}
candyShop={candyShop}
wallet={wallet}
/>
Expand Down
4 changes: 1 addition & 3 deletions core/ui/src/components/NftStat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export const NftStat: React.FC<NftStatProps> = ({
return (
<div className="candy-stat-horizontal">
<div>
<div className="candy-label">
{blockchain === BlockchainType.Ethereum ? 'CONTRACT ADDRESS' : 'MINT ADDRESS'}
</div>
<div className="candy-label">{blockchain === BlockchainType.EVM ? 'CONTRACT ADDRESS' : 'MINT ADDRESS'}</div>
<div className="candy-value">
<ExplorerLink
type="address"
Expand Down
6 changes: 3 additions & 3 deletions core/ui/src/components/SellModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import './style.less';
export type SellModalProps = {
nft: SingleTokenInfo;
wallet: Wallet;
shop: CandyShopResponse;
shopResponse: CandyShopResponse;
currencySymbol: string;
candyShopEnv: Blockchain;
explorerLink: ExplorerLinkBase;
Expand All @@ -31,7 +31,7 @@ export const SellModal: React.FC<SellModalProps> = ({
onCancel: onUnSelectItem,
nft,
wallet,
shop,
shopResponse,
currencySymbol,
candyShopEnv,
explorerLink,
Expand Down Expand Up @@ -108,7 +108,7 @@ export const SellModal: React.FC<SellModalProps> = ({
};

const disableListedBtn = formState.price === undefined || loading;
const transactionFee = shop ? shop.feeRate / 100 : 0;
const transactionFee = shopResponse ? shopResponse.feeRate / 100 : 0;
const isEth = candyShopEnv !== Blockchain.SolDevnet && candyShopEnv !== Blockchain.SolMainnetBeta;

return (
Expand Down
Loading