From 4f727d852bb7facc470814b37e26e324d5f80a6e Mon Sep 17 00:00:00 2001 From: infiniteMajor Date: Tue, 21 Dec 2021 03:42:59 +0300 Subject: [PATCH 01/11] affiliate data disply --- src/assets/styles/styled.ts | 2 +- src/custom/pages/Profile/index.tsx | 108 +++++++++++++++++----------- src/custom/pages/Profile/styled.tsx | 61 +++++++--------- 3 files changed, 90 insertions(+), 81 deletions(-) diff --git a/src/assets/styles/styled.ts b/src/assets/styles/styled.ts index 6c9292cc8..86fa70017 100644 --- a/src/assets/styles/styled.ts +++ b/src/assets/styles/styled.ts @@ -2,7 +2,7 @@ import styled from 'styled-components/macro' import * as CSS from 'csstype' // with font size convert to rems -export const Txt = styled.span< +export const Txt = styled.div< Partial< CSS.Properties & { fs?: number diff --git a/src/custom/pages/Profile/index.tsx b/src/custom/pages/Profile/index.tsx index 61cdae6e7..b7b651983 100644 --- a/src/custom/pages/Profile/index.tsx +++ b/src/custom/pages/Profile/index.tsx @@ -1,6 +1,5 @@ import { Txt } from 'assets/styles/styled' import { - FlexCol, FlexWrap, Wrapper, Container, @@ -12,11 +11,12 @@ import { ItemTitle, ChildWrapper, Loader, - ExtLink, + ExtLink as ExternalLink, + FlexRow, } from 'pages/Profile/styled' import { useActiveWeb3React } from 'hooks/web3' import Copy from 'components/Copy/CopyMod' -import { HelpCircle, RefreshCcw } from 'react-feather' +import { ArrowUpRight, HelpCircle, RefreshCcw } from 'react-feather' import Web3Status from 'components/Web3Status' import useReferralLink from 'hooks/useReferralLink' import useFetchProfile from 'hooks/useFetchProfile' @@ -28,6 +28,7 @@ import NotificationBanner from 'components/NotificationBanner' import { SupportedChainId as ChainId } from 'constants/chains' import AffiliateStatusCheck from 'components/AffiliateStatusCheck' import { useHasOrders } from 'api/gnosisProtocol/hooks' +import { shortenAddress } from '@src/utils' export default function Profile() { const referralLink = useReferralLink() @@ -58,36 +59,55 @@ export default function Profile() { - Profile overview - {account && ( - - - - -    - - Last updated - - - - :  + + Profile overview + {account && ( + + + + +    + + Last updated + + + + :  + + {!lastUpdated ? ( + '-' + ) : ( + }> + {lastUpdated} + + )} - {!lastUpdated ? ( - '-' - ) : ( - }> - {lastUpdated} - - )} + + + )} + + + {account && ( + + + Reffered by: {shortenAddress(account)} - {hasOrders && ( - - View all orders ↗ - - )} - - - )} + + + + + )} + + {hasOrders && account && ( + + View all orders + + + + + )} + + {renderNotificationMessages} @@ -118,26 +138,28 @@ export default function Profile() { - - + + 🧑‍🌾 - {formatInt(profileData?.totalTrades)} + + {formatInt(profileData?.totalTrades)} + - + Total trades {isTradesTooltipVisible && ( )} - + - - + + 💰 @@ -147,7 +169,7 @@ export default function Profile() { Total traded volume - + @@ -158,7 +180,7 @@ export default function Profile() { - + 🤝 @@ -168,8 +190,8 @@ export default function Profile() { Total referrals - - + + 💸 @@ -179,7 +201,7 @@ export default function Profile() { Referrals volume - + diff --git a/src/custom/pages/Profile/styled.tsx b/src/custom/pages/Profile/styled.tsx index 27f6ce5f5..5b05b8005 100644 --- a/src/custom/pages/Profile/styled.tsx +++ b/src/custom/pages/Profile/styled.tsx @@ -34,6 +34,8 @@ export const Wrapper = styled(Page)` ` export const ExtLink = styled(ExternalLink)` + color: ${({ theme }) => theme.text1}; + font-weight: 300; &:hover, &:focus { color: ${({ theme }) => theme.text1}; @@ -82,7 +84,11 @@ export const GridWrap = styled.div theme.mediaWidth.upToSmall` + flex-direction: column; + flex-wrap: nowrap; + `} ` export const StyledTitle = styled(Title)` @@ -114,27 +120,30 @@ export const ItemTitle = styled.h3` `} ` -export const FlexWrap = styled.div` - display: flex; - flex-grow: 1; +export const FlexRow = styled.div` + display: inline-flex; align-items: center; - flex-direction: row; justify-content: center; - > div { - width: auto; - } - button { - max-width: 180px; - } +` + +export const FlexWrap = styled.div>` + display: flex; + flex-grow: 1; + flex-direction: ${(props) => (props.col ? 'column' : 'row')}; + flex-wrap: ${(props) => (props.col ? 'nowrap' : 'wrap')}; + ${(props) => props.xAlign && `justify-content: ${props.xAlign};`} + ${(props) => props.yAlign && `align-items: ${props.yAlign};`} ${({ theme }) => theme.mediaWidth.upToSmall` - flex-wrap: wrap; - > div { - width: 50%; - } + flex-direction: column; + flex-wrap: nowrap; + align-items: center; button { max-width: 100%; } `} + button { + max-width: 180px; + } ` export const StyledContainer = styled.div` @@ -149,28 +158,6 @@ export const StyledContainer = styled.div` `} ` -export const FlexCol = styled.div` - display: flex; - flex-grow: 1; - align-items: center; - flex-direction: column; - justify-content: center; - strong { - font-size: 21px; - margin-top: 6px; - ${({ theme }) => theme.mediaWidth.upToSmall` - font-size: 14px; - `} - } - span:not([role='img']) { - font-size: 14px; - color: ${({ theme }) => theme.text6}; - min-height: 32px; - text-align: center; - display: flex; - align-items: center; - } -` export const Loader = styled.div<{ isLoading: boolean }>` display: flex; flex: 1; From 7c3ef78750d517e98cb203c52c4b36c99fc1c31f Mon Sep 17 00:00:00 2001 From: infiniteMajor Date: Tue, 21 Dec 2021 04:31:26 +0300 Subject: [PATCH 02/11] revert a change --- src/assets/styles/styled.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/assets/styles/styled.ts b/src/assets/styles/styled.ts index 86fa70017..6c9292cc8 100644 --- a/src/assets/styles/styled.ts +++ b/src/assets/styles/styled.ts @@ -2,7 +2,7 @@ import styled from 'styled-components/macro' import * as CSS from 'csstype' // with font size convert to rems -export const Txt = styled.div< +export const Txt = styled.span< Partial< CSS.Properties & { fs?: number From ebccc09e9c9ca2c0464368bbc07eed20b82efcaf Mon Sep 17 00:00:00 2001 From: infiniteMajor Date: Tue, 4 Jan 2022 16:04:02 +0300 Subject: [PATCH 03/11] upds --- src/custom/pages/Profile/index.tsx | 12 +++++++----- src/custom/pages/Profile/styled.tsx | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/custom/pages/Profile/index.tsx b/src/custom/pages/Profile/index.tsx index b7b651983..1ce70429c 100644 --- a/src/custom/pages/Profile/index.tsx +++ b/src/custom/pages/Profile/index.tsx @@ -53,6 +53,8 @@ export default function Profile() { ) + console.log('hasOrders', referralLink, account) + return ( {chainId && chainId === ChainId.MAINNET && } @@ -97,8 +99,8 @@ export default function Profile() { )} - - {hasOrders && account && ( + + {referralLink && hasOrders && account && ( View all orders @@ -167,7 +169,7 @@ export default function Profile() { {formatDecimal(profileData?.tradeVolumeUsd)} - Total traded volume + Total traded volume @@ -188,7 +190,7 @@ export default function Profile() { {formatInt(profileData?.totalReferrals)} - Total referrals + Total referrals @@ -199,7 +201,7 @@ export default function Profile() { {formatDecimal(profileData?.referralVolumeUsd)} - Referrals volume + Referrals volume diff --git a/src/custom/pages/Profile/styled.tsx b/src/custom/pages/Profile/styled.tsx index 5b05b8005..1adc7ebed 100644 --- a/src/custom/pages/Profile/styled.tsx +++ b/src/custom/pages/Profile/styled.tsx @@ -60,6 +60,7 @@ export const ChildWrapper = styled.div` `} > .item { width: 100%; + flex-direction: row; } ` From df8e73f40645200fb55d83948c4371a5dc8f4ccc Mon Sep 17 00:00:00 2001 From: infiniteMajor Date: Tue, 4 Jan 2022 17:34:55 +0300 Subject: [PATCH 04/11] upds --- src/custom/pages/Profile/index.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/custom/pages/Profile/index.tsx b/src/custom/pages/Profile/index.tsx index 1ce70429c..3ba2eb364 100644 --- a/src/custom/pages/Profile/index.tsx +++ b/src/custom/pages/Profile/index.tsx @@ -29,9 +29,11 @@ import { SupportedChainId as ChainId } from 'constants/chains' import AffiliateStatusCheck from 'components/AffiliateStatusCheck' import { useHasOrders } from 'api/gnosisProtocol/hooks' import { shortenAddress } from '@src/utils' +import { useReferralAddress } from 'state/affiliate/hooks' export default function Profile() { const referralLink = useReferralLink() + const refAddress = useReferralAddress() const { account, chainId } = useActiveWeb3React() const { profileData, isLoading, error } = useFetchProfile() const lastUpdated = useTimeAgo(profileData?.lastUpdated) @@ -53,8 +55,6 @@ export default function Profile() { ) - console.log('hasOrders', referralLink, account) - return ( {chainId && chainId === ChainId.MAINNET && } @@ -89,13 +89,13 @@ export default function Profile() { )} - {account && ( + {account && refAddress && ( - Reffered by: {shortenAddress(account)} + Referred by: {shortenAddress(refAddress.value)} - + )} From 82fc922743fc07d4316d8b9492de04cee8b7cbcf Mon Sep 17 00:00:00 2001 From: infiniteMajor Date: Wed, 5 Jan 2022 16:02:05 +0300 Subject: [PATCH 05/11] upd import declaration --- src/custom/pages/Profile/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/custom/pages/Profile/index.tsx b/src/custom/pages/Profile/index.tsx index 3ba2eb364..6afc28246 100644 --- a/src/custom/pages/Profile/index.tsx +++ b/src/custom/pages/Profile/index.tsx @@ -28,8 +28,8 @@ import NotificationBanner from 'components/NotificationBanner' import { SupportedChainId as ChainId } from 'constants/chains' import AffiliateStatusCheck from 'components/AffiliateStatusCheck' import { useHasOrders } from 'api/gnosisProtocol/hooks' -import { shortenAddress } from '@src/utils' import { useReferralAddress } from 'state/affiliate/hooks' +import { shortenAddress } from 'utils' export default function Profile() { const referralLink = useReferralLink() From 81c805cd105b03de6477083c5c356fef7c7f9513 Mon Sep 17 00:00:00 2001 From: Mati Dastugue Date: Tue, 15 Feb 2022 10:40:40 -0300 Subject: [PATCH 06/11] Remove one view all orders link --- src/custom/pages/Profile/index.tsx | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/custom/pages/Profile/index.tsx b/src/custom/pages/Profile/index.tsx index ef3848c1b..cc37944ed 100644 --- a/src/custom/pages/Profile/index.tsx +++ b/src/custom/pages/Profile/index.tsx @@ -10,7 +10,6 @@ import { ItemTitle, ChildWrapper, Loader, - ExtLink as ExternalLink, FlexRow, ExtLink, AffiliateTitle, @@ -19,7 +18,7 @@ import { } from 'pages/Profile/styled' import { useActiveWeb3React } from 'hooks/web3' import Copy from 'components/Copy/CopyMod' -import { ArrowUpRight, HelpCircle, RefreshCcw } from 'react-feather' +import { HelpCircle, RefreshCcw } from 'react-feather' import Web3Status from 'components/Web3Status' import useReferralLink from 'hooks/useReferralLink' import useFetchProfile from 'hooks/useFetchProfile' @@ -114,24 +113,12 @@ export default function Profile() { {hasOrders && ( - View all orders ↗ + View all orders ↗ )} )} - - - {referralLink && hasOrders && account && ( - - View all orders - - - - - )} - - {renderNotificationMessages} From 01b1fe4dad48899b42398ced116404ab33b97564 Mon Sep 17 00:00:00 2001 From: Mati Dastugue Date: Tue, 15 Feb 2022 16:40:14 -0300 Subject: [PATCH 07/11] Fix styles for mobile view --- src/custom/pages/Profile/index.tsx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/custom/pages/Profile/index.tsx b/src/custom/pages/Profile/index.tsx index cc37944ed..4d85c9d64 100644 --- a/src/custom/pages/Profile/index.tsx +++ b/src/custom/pages/Profile/index.tsx @@ -10,7 +10,6 @@ import { ItemTitle, ChildWrapper, Loader, - FlexRow, ExtLink, AffiliateTitle, ProfileWrapper, @@ -79,17 +78,20 @@ export default function Profile() { - {account && refAddress.value && ( - - - Referred by: {refAddress.value && shortenAddress(refAddress.value)} - - - - - - )} - Affiliate Program + + Affiliate Program + {account && refAddress.value && ( +
+ + Referred by: {refAddress.value && shortenAddress(refAddress.value)} + + + + +
+ )} +
+ {account && ( From 307710ef41b8d776d9da84b8f817ae3454c98ec2 Mon Sep 17 00:00:00 2001 From: Mati Dastugue Date: Tue, 22 Feb 2022 18:43:10 -0300 Subject: [PATCH 08/11] Create new endpoint for getting all trades for all the environments --- src/custom/api/gnosisProtocol/api.ts | 59 ++++++++++++++++++++++++++++ src/custom/state/affiliate/hooks.ts | 7 ++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/custom/api/gnosisProtocol/api.ts b/src/custom/api/gnosisProtocol/api.ts index 0e0ad84f4..ef45524ca 100644 --- a/src/custom/api/gnosisProtocol/api.ts +++ b/src/custom/api/gnosisProtocol/api.ts @@ -180,6 +180,21 @@ function _fetch(chainId: ChainId, url: string, method: 'GET' | 'POST' | 'DELETE' }) } +function _fetchAllTrades( + chainId: ChainId, + url: string, + method: 'GET' | 'POST' | 'DELETE', + baseUrl?: string, + data?: any +): Promise { + const baseUri = baseUrl ? baseUrl + '/v1' : _getApiBaseUrl(chainId) + return fetch(baseUri + url, { + headers: DEFAULT_HEADERS, + method, + body: data !== undefined ? JSON.stringify(data) : data, + }) +} + function _fetchProfile( chainId: ChainId, url: string, @@ -207,6 +222,10 @@ function _get(chainId: ChainId, url: string): Promise { return _fetch(chainId, url, 'GET') } +function _getAllTrades(chainId: ChainId, url: string, baseurl: string): Promise { + return _fetchAllTrades(chainId, url, 'GET', baseurl) +} + function _getProfile(chainId: ChainId, url: string): Promise { return _fetchProfile(chainId, url, 'GET') } @@ -425,6 +444,45 @@ export async function getTrades(params: GetTradesParams): Promise { + const { chainId, owner, limit, offset } = params + const qsParams = stringify({ owner, limit, offset }) + const stagingUrl = + process.env[`REACT_APP_API_URL_STAGING_${ChainId[chainId]}`] || + `https://barn.api.cow.fi/${ChainId[chainId].toLowerCase()}/api` + const prodUrl = + process.env[`REACT_APP_API_URL_PROD_${ChainId[chainId]}`] || + `https://api.cow.fi/${ChainId[chainId].toLowerCase()}/api` + + console.log('[util:operator] Get all trades for', chainId, owner, { limit, offset }) + try { + const results = await Promise.allSettled([ + await _getAllTrades(chainId, `/trades?${qsParams}`, stagingUrl), + await _getAllTrades(chainId, `/trades?${qsParams}`, prodUrl), + ]) + + const response = results + .filter(({ status }) => status === 'fulfilled') + .filter((res) => (res as PromiseFulfilledResult).value.ok) + .map((p) => (p as PromiseFulfilledResult).value) + + const [stgResponse, prodResponse] = response + + if (!response.length) { + const [stgErrResponse, prodErrResponse] = results as PromiseFulfilledResult[] + const errorResponse = (await stgErrResponse.value.json()) || (await prodErrResponse.value.json()) + throw new Error(errorResponse.description) + } else { + return Promise.allSettled([stgResponse.json(), prodResponse.json()]).then((r) => + r.map((t: any) => t.value).flat() + ) + } + } catch (error) { + console.error('Error getting all trades:', error) + throw new Error('Error getting all trades: ' + error) + } +} + export type ProfileData = { totalTrades: number totalReferrals: number @@ -514,6 +572,7 @@ registerOnWindow({ operator: { getQuote, getTrades, + getAllTrades, getOrder, sendSignedOrder: sendOrder, apiGet: _get, diff --git a/src/custom/state/affiliate/hooks.ts b/src/custom/state/affiliate/hooks.ts index 66822cb1a..12ea53ad0 100644 --- a/src/custom/state/affiliate/hooks.ts +++ b/src/custom/state/affiliate/hooks.ts @@ -3,7 +3,7 @@ import { useActiveWeb3React } from 'hooks/web3' import { useSelector } from 'react-redux' import { AppState } from 'state' import { useAppDispatch } from 'state/hooks' -import { getTrades, getOrder } from 'api/gnosisProtocol/api' +import { getAllTrades, getOrder } from 'api/gnosisProtocol/api' import { updateReferralAddress } from 'state/affiliate/actions' import { decodeAppData } from 'utils/metadata' import { APP_DATA_HASH } from 'constants/index' @@ -47,9 +47,8 @@ export function useReferredByAddress() { const fetchReferredAddress = async () => { if (!chainId || !account) return try { - const trades = await getTrades({ chainId, owner: account, limit: 1 }) - const order = await getOrder(chainId, trades[0]?.orderUid) - + const allTrades = await getAllTrades({ chainId, owner: account, limit: 1 }) + const order = await getOrder(chainId, allTrades[0]?.orderUid) const appDataDecoded = order?.appData && (await decodeAppData(order?.appData.toString())) setReferredAddress(appDataDecoded.metadata.referrer.address) } catch { From 274dfdfa2a8f651f4a98307b58c82792a65cf36e Mon Sep 17 00:00:00 2001 From: Mati Dastugue Date: Wed, 23 Feb 2022 11:10:31 -0300 Subject: [PATCH 09/11] create endpoint for getting order searching in all the environments --- src/custom/api/gnosisProtocol/api.ts | 47 ++++++++++++++++++++++++++-- src/custom/state/affiliate/hooks.ts | 4 +-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/custom/api/gnosisProtocol/api.ts b/src/custom/api/gnosisProtocol/api.ts index ef45524ca..caa6088d0 100644 --- a/src/custom/api/gnosisProtocol/api.ts +++ b/src/custom/api/gnosisProtocol/api.ts @@ -180,7 +180,7 @@ function _fetch(chainId: ChainId, url: string, method: 'GET' | 'POST' | 'DELETE' }) } -function _fetchAllTrades( +function _fetchAll( chainId: ChainId, url: string, method: 'GET' | 'POST' | 'DELETE', @@ -222,8 +222,12 @@ function _get(chainId: ChainId, url: string): Promise { return _fetch(chainId, url, 'GET') } +function _getAllOrder(chainId: ChainId, url: string, baseurl: string): Promise { + return _fetchAll(chainId, url, 'GET', baseurl) +} + function _getAllTrades(chainId: ChainId, url: string, baseurl: string): Promise { - return _fetchAllTrades(chainId, url, 'GET', baseurl) + return _fetchAll(chainId, url, 'GET', baseurl) } function _getProfile(chainId: ChainId, url: string): Promise { @@ -400,6 +404,42 @@ export async function getOrder(chainId: ChainId, orderId: string): Promise { + console.log(`[api:${API_NAME}] Get All order for `, chainId, orderId) + const stagingUrl = + process.env[`REACT_APP_API_URL_STAGING_${ChainId[chainId]}`] || + `https://barn.api.cow.fi/${ChainId[chainId].toLowerCase()}/api` + const prodUrl = + process.env[`REACT_APP_API_URL_PROD_${ChainId[chainId]}`] || + `https://api.cow.fi/${ChainId[chainId].toLowerCase()}/api` + try { + const results = await Promise.allSettled([ + await _getAllOrder(chainId, `/orders/${orderId}`, stagingUrl), + await _getAllOrder(chainId, `/orders/${orderId}`, prodUrl), + ]) + + const response = results + .filter(({ status }) => status === 'fulfilled') + .filter((res) => (res as PromiseFulfilledResult).value.ok) + .map((p) => (p as PromiseFulfilledResult).value) + + const [stgResponse, prodResponse] = response + + if (!response.length) { + const [stgErrResponse, prodErrResponse] = results as PromiseFulfilledResult[] + const errorResponse: ApiErrorObject = (await stgErrResponse.value.json()) || (await prodErrResponse.value.json()) + throw new OperatorError(errorResponse) + } else { + return Promise.allSettled([stgResponse?.json(), prodResponse?.json()]).then( + (r: any) => r.find((t: any) => t.value)?.value as OrderMetaData + ) + } + } catch (error) { + console.error('Error getting all order information:', error) + throw new OperatorError(UNHANDLED_ORDER_ERROR) + } +} + export async function getOrders(chainId: ChainId, owner: string, limit = 1000, offset = 0): Promise { console.log(`[api:${API_NAME}] Get orders for `, chainId, owner, limit, offset) @@ -444,7 +484,7 @@ export async function getTrades(params: GetTradesParams): Promise { +export async function getAllTrades(params: GetTradesParams): Promise { const { chainId, owner, limit, offset } = params const qsParams = stringify({ owner, limit, offset }) const stagingUrl = @@ -574,6 +614,7 @@ registerOnWindow({ getTrades, getAllTrades, getOrder, + getAllOrder, sendSignedOrder: sendOrder, apiGet: _get, apiPost: _post, diff --git a/src/custom/state/affiliate/hooks.ts b/src/custom/state/affiliate/hooks.ts index 12ea53ad0..30bca8c0a 100644 --- a/src/custom/state/affiliate/hooks.ts +++ b/src/custom/state/affiliate/hooks.ts @@ -3,7 +3,7 @@ import { useActiveWeb3React } from 'hooks/web3' import { useSelector } from 'react-redux' import { AppState } from 'state' import { useAppDispatch } from 'state/hooks' -import { getAllTrades, getOrder } from 'api/gnosisProtocol/api' +import { getAllTrades, getAllOrder } from 'api/gnosisProtocol/api' import { updateReferralAddress } from 'state/affiliate/actions' import { decodeAppData } from 'utils/metadata' import { APP_DATA_HASH } from 'constants/index' @@ -48,7 +48,7 @@ export function useReferredByAddress() { if (!chainId || !account) return try { const allTrades = await getAllTrades({ chainId, owner: account, limit: 1 }) - const order = await getOrder(chainId, allTrades[0]?.orderUid) + const order = await getAllOrder(chainId, allTrades[0]?.orderUid) const appDataDecoded = order?.appData && (await decodeAppData(order?.appData.toString())) setReferredAddress(appDataDecoded.metadata.referrer.address) } catch { From b77e1564f84958db04b8fa06720f5d09f90d7083 Mon Sep 17 00:00:00 2001 From: Mati Dastugue Date: Thu, 24 Feb 2022 16:51:46 -0300 Subject: [PATCH 10/11] Refactor api + improve styling --- src/custom/api/gnosisProtocol/api.ts | 80 +++++++----------------- src/custom/constants/chains/chainsMod.ts | 11 ++++ src/custom/pages/Profile/index.tsx | 14 +++-- src/custom/state/affiliate/hooks.ts | 7 ++- 4 files changed, 48 insertions(+), 64 deletions(-) diff --git a/src/custom/api/gnosisProtocol/api.ts b/src/custom/api/gnosisProtocol/api.ts index caa6088d0..965cc1864 100644 --- a/src/custom/api/gnosisProtocol/api.ts +++ b/src/custom/api/gnosisProtocol/api.ts @@ -1,4 +1,9 @@ -import { SupportedChainId as ChainId, SupportedChainId } from 'constants/chains' +import { + SupportedChainId as ChainId, + SupportedChainId, + BARN_API_ENDPOINTS_PER_NETWORK, + PROD_API_ENDPOINTS_PER_NETWORK, +} from 'constants/chains' import { OrderKind, QuoteQuery } from '@gnosis.pm/gp-v2-contracts' import { stringify } from 'qs' import { getSigningSchemeApiValue, OrderCancellation, OrderCreation, SigningSchemeValue } from 'utils/signatures' @@ -26,20 +31,10 @@ import { getAppDataHash } from 'constants/appDataHash' import { GpPriceStrategy } from 'hooks/useGetGpPriceStrategy' function getGnosisProtocolUrl(): Partial> { - if (isLocal || isDev || isPr || isBarn) { - return { - [ChainId.MAINNET]: process.env.REACT_APP_API_URL_STAGING_MAINNET || 'https://barn.api.cow.fi/mainnet/api', - [ChainId.RINKEBY]: process.env.REACT_APP_API_URL_STAGING_RINKEBY || 'https://barn.api.cow.fi/rinkeby/api', - [ChainId.XDAI]: process.env.REACT_APP_API_URL_STAGING_XDAI || 'https://barn.api.cow.fi/xdai/api', - } - } + if (isLocal || isDev || isPr || isBarn) return BARN_API_ENDPOINTS_PER_NETWORK // Production, staging, ens, ... - return { - [ChainId.MAINNET]: process.env.REACT_APP_API_URL_PROD_MAINNET || 'https://api.cow.fi/mainnet/api', - [ChainId.RINKEBY]: process.env.REACT_APP_API_URL_PROD_RINKEBY || 'https://api.cow.fi/rinkeby/api', - [ChainId.XDAI]: process.env.REACT_APP_API_URL_PROD_XDAI || 'https://api.cow.fi/xdai/api', - } + return PROD_API_ENDPOINTS_PER_NETWORK } function getProfileUrl(): Partial> { @@ -171,16 +166,7 @@ export function getOrderLink(chainId: ChainId, orderId: OrderID): string { return baseUrl + `/orders/${orderId}` } -function _fetch(chainId: ChainId, url: string, method: 'GET' | 'POST' | 'DELETE', data?: any): Promise { - const baseUrl = _getApiBaseUrl(chainId) - return fetch(baseUrl + url, { - headers: DEFAULT_HEADERS, - method, - body: data !== undefined ? JSON.stringify(data) : data, - }) -} - -function _fetchAll( +function _fetch( chainId: ChainId, url: string, method: 'GET' | 'POST' | 'DELETE', @@ -218,16 +204,8 @@ function _post(chainId: ChainId, url: string, data: any): Promise { return _fetch(chainId, url, 'POST', data) } -function _get(chainId: ChainId, url: string): Promise { - return _fetch(chainId, url, 'GET') -} - -function _getAllOrder(chainId: ChainId, url: string, baseurl: string): Promise { - return _fetchAll(chainId, url, 'GET', baseurl) -} - -function _getAllTrades(chainId: ChainId, url: string, baseurl: string): Promise { - return _fetchAll(chainId, url, 'GET', baseurl) +function _get(chainId: ChainId, url: string, baseurl?: string): Promise { + return _fetch(chainId, url, 'GET', baseurl) } function _getProfile(chainId: ChainId, url: string): Promise { @@ -404,18 +382,12 @@ export async function getOrder(chainId: ChainId, orderId: string): Promise { - console.log(`[api:${API_NAME}] Get All order for `, chainId, orderId) - const stagingUrl = - process.env[`REACT_APP_API_URL_STAGING_${ChainId[chainId]}`] || - `https://barn.api.cow.fi/${ChainId[chainId].toLowerCase()}/api` - const prodUrl = - process.env[`REACT_APP_API_URL_PROD_${ChainId[chainId]}`] || - `https://api.cow.fi/${ChainId[chainId].toLowerCase()}/api` +export async function getOrderFromAllEnvs(chainId: ChainId, orderId: string): Promise { + console.log(`[api:${API_NAME}] Get order from all envs for `, chainId, orderId) try { const results = await Promise.allSettled([ - await _getAllOrder(chainId, `/orders/${orderId}`, stagingUrl), - await _getAllOrder(chainId, `/orders/${orderId}`, prodUrl), + await _get(chainId, `/orders/${orderId}`, BARN_API_ENDPOINTS_PER_NETWORK[chainId]), + await _get(chainId, `/orders/${orderId}`, PROD_API_ENDPOINTS_PER_NETWORK[chainId]), ]) const response = results @@ -484,21 +456,14 @@ export async function getTrades(params: GetTradesParams): Promise { +export async function getTradesFromAllEnvs(params: GetTradesParams): Promise { const { chainId, owner, limit, offset } = params const qsParams = stringify({ owner, limit, offset }) - const stagingUrl = - process.env[`REACT_APP_API_URL_STAGING_${ChainId[chainId]}`] || - `https://barn.api.cow.fi/${ChainId[chainId].toLowerCase()}/api` - const prodUrl = - process.env[`REACT_APP_API_URL_PROD_${ChainId[chainId]}`] || - `https://api.cow.fi/${ChainId[chainId].toLowerCase()}/api` - console.log('[util:operator] Get all trades for', chainId, owner, { limit, offset }) try { const results = await Promise.allSettled([ - await _getAllTrades(chainId, `/trades?${qsParams}`, stagingUrl), - await _getAllTrades(chainId, `/trades?${qsParams}`, prodUrl), + await _get(chainId, `/trades?${qsParams}`, BARN_API_ENDPOINTS_PER_NETWORK[chainId]), + await _get(chainId, `/trades?${qsParams}`, PROD_API_ENDPOINTS_PER_NETWORK[chainId]), ]) const response = results @@ -514,7 +479,10 @@ export async function getAllTrades(params: GetTradesParams): Promise - r.map((t: any) => t.value).flat() + r + .map((t: any) => t.value) + .flat() + .sort((a, b) => a.blockNumber - b.blockNumber) ) } } catch (error) { @@ -612,9 +580,9 @@ registerOnWindow({ operator: { getQuote, getTrades, - getAllTrades, + getTradesFromAllEnvs, getOrder, - getAllOrder, + getOrderFromAllEnvs, sendSignedOrder: sendOrder, apiGet: _get, apiPost: _post, diff --git a/src/custom/constants/chains/chainsMod.ts b/src/custom/constants/chains/chainsMod.ts index 275e1d393..9d6e75b57 100644 --- a/src/custom/constants/chains/chainsMod.ts +++ b/src/custom/constants/chains/chainsMod.ts @@ -193,3 +193,14 @@ export const NETWORK_LABELS: { [chainId in SupportedChainId | number]: string } // [SupportedChainId.ARBITRUM_KOVAN]: 'kArbitrum', // [SupportedChainId.ARBITRUM_ONE]: 'Arbitrum One', } + +export const BARN_API_ENDPOINTS_PER_NETWORK: Record = { + [SupportedChainId.MAINNET]: process.env.REACT_APP_API_URL_STAGING_MAINNET || 'https://barn.api.cow.fi/mainnet/api', + [SupportedChainId.RINKEBY]: process.env.REACT_APP_API_URL_STAGING_RINKEBY || 'https://barn.api.cow.fi/rinkeby/api', + [SupportedChainId.XDAI]: process.env.REACT_APP_API_URL_STAGING_XDAI || 'https://barn.api.cow.fi/xdai/api', +} +export const PROD_API_ENDPOINTS_PER_NETWORK: Record = { + [SupportedChainId.MAINNET]: process.env.REACT_APP_API_URL_PROD_MAINNET || 'https://api.cow.fi/mainnet/api', + [SupportedChainId.RINKEBY]: process.env.REACT_APP_API_URL_PROD_RINKEBY || 'https://api.cow.fi/rinkeby/api', + [SupportedChainId.XDAI]: process.env.REACT_APP_API_URL_PROD_XDAI || 'https://api.cow.fi/xdai/api', +} diff --git a/src/custom/pages/Profile/index.tsx b/src/custom/pages/Profile/index.tsx index 4d85c9d64..84e38d23e 100644 --- a/src/custom/pages/Profile/index.tsx +++ b/src/custom/pages/Profile/index.tsx @@ -96,26 +96,30 @@ export default function Profile() { - +    - + Last updated - :  + : {!lastUpdated ? ( '-' ) : ( }> - {lastUpdated} + + {lastUpdated} + )} {hasOrders && ( - View all orders ↗ + + View all orders ↗ + )} diff --git a/src/custom/state/affiliate/hooks.ts b/src/custom/state/affiliate/hooks.ts index 30bca8c0a..0139163d4 100644 --- a/src/custom/state/affiliate/hooks.ts +++ b/src/custom/state/affiliate/hooks.ts @@ -3,7 +3,7 @@ import { useActiveWeb3React } from 'hooks/web3' import { useSelector } from 'react-redux' import { AppState } from 'state' import { useAppDispatch } from 'state/hooks' -import { getAllTrades, getAllOrder } from 'api/gnosisProtocol/api' +import { getTradesFromAllEnvs, getOrderFromAllEnvs } from 'api/gnosisProtocol/api' import { updateReferralAddress } from 'state/affiliate/actions' import { decodeAppData } from 'utils/metadata' import { APP_DATA_HASH } from 'constants/index' @@ -47,8 +47,9 @@ export function useReferredByAddress() { const fetchReferredAddress = async () => { if (!chainId || !account) return try { - const allTrades = await getAllTrades({ chainId, owner: account, limit: 1 }) - const order = await getAllOrder(chainId, allTrades[0]?.orderUid) + const allTrades = await getTradesFromAllEnvs({ chainId, owner: account, limit: 1 }) + if (!allTrades[0]?.orderUid) return + const order = await getOrderFromAllEnvs(chainId, allTrades[0].orderUid) const appDataDecoded = order?.appData && (await decodeAppData(order?.appData.toString())) setReferredAddress(appDataDecoded.metadata.referrer.address) } catch { From e5c981d16e8a1e9efd9313bf811c1f648faf3874 Mon Sep 17 00:00:00 2001 From: Mati Dastugue Date: Fri, 25 Feb 2022 15:03:23 -0300 Subject: [PATCH 11/11] Remove referred address if user has no trades --- src/custom/pages/Profile/index.tsx | 6 +++++- src/custom/state/affiliate/hooks.ts | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/custom/pages/Profile/index.tsx b/src/custom/pages/Profile/index.tsx index 84e38d23e..20a698d0f 100644 --- a/src/custom/pages/Profile/index.tsx +++ b/src/custom/pages/Profile/index.tsx @@ -1,4 +1,7 @@ +import { useContext } from 'react' +import { ThemeContext } from 'styled-components/macro' import { Txt } from 'assets/styles/styled' + import { FlexWrap, Wrapper, @@ -46,6 +49,7 @@ export default function Profile() { const lastUpdated = useTimeAgo(profileData?.lastUpdated) const isTradesTooltipVisible = account && chainId == 1 && !!profileData?.totalTrades const hasOrders = useHasOrders(account) + const theme = useContext(ThemeContext) const vCowBalance = useTokenBalance(account || undefined, chainId ? V_COW[chainId] : undefined) @@ -96,7 +100,7 @@ export default function Profile() { - +    Last updated diff --git a/src/custom/state/affiliate/hooks.ts b/src/custom/state/affiliate/hooks.ts index 0139163d4..9915df666 100644 --- a/src/custom/state/affiliate/hooks.ts +++ b/src/custom/state/affiliate/hooks.ts @@ -48,7 +48,10 @@ export function useReferredByAddress() { if (!chainId || !account) return try { const allTrades = await getTradesFromAllEnvs({ chainId, owner: account, limit: 1 }) - if (!allTrades[0]?.orderUid) return + if (!allTrades[0]?.orderUid) { + setReferredAddress('') + return + } const order = await getOrderFromAllEnvs(chainId, allTrades[0].orderUid) const appDataDecoded = order?.appData && (await decodeAppData(order?.appData.toString())) setReferredAddress(appDataDecoded.metadata.referrer.address)