-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2205
- Loading branch information
Showing
13 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Feature } from './types'; | ||
|
||
import { getEnvValue } from '../utils'; | ||
import marketplace from './marketplace'; | ||
|
||
const title = 'Save on gas with GasHawk'; | ||
|
||
const config: Feature<{ | ||
apiUrlTemplate: string; | ||
dappId: string; | ||
}> = (() => { | ||
if (getEnvValue('NEXT_PUBLIC_SAVE_ON_GAS_ENABLED') === 'true' && marketplace.isEnabled) { | ||
return Object.freeze({ | ||
title, | ||
isEnabled: true, | ||
dappId: 'gas-hawk', | ||
apiUrlTemplate: 'https://core.gashawk.io/apiv2/stats/address/<address>/savingsPotential/0x1', | ||
}); | ||
} | ||
|
||
return Object.freeze({ | ||
title, | ||
isEnabled: false, | ||
}); | ||
})(); | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type CspDev from 'csp-dev'; | ||
|
||
import config from 'configs/app'; | ||
|
||
const feature = config.features.saveOnGas; | ||
|
||
export function gasHawk(): CspDev.DirectiveDescriptor { | ||
if (!feature.isEnabled) { | ||
return {}; | ||
} | ||
|
||
const apiOrigin = (() => { | ||
try { | ||
const url = new URL(feature.apiUrlTemplate); | ||
return url.origin; | ||
} catch (error) { | ||
return ''; | ||
} | ||
})(); | ||
|
||
if (!apiOrigin) { | ||
return {}; | ||
} | ||
|
||
return { | ||
'connect-src': [ | ||
apiOrigin, | ||
], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Image, Skeleton } from '@chakra-ui/react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import React from 'react'; | ||
import * as v from 'valibot'; | ||
|
||
import { route } from 'nextjs-routes'; | ||
|
||
import config from 'configs/app'; | ||
import LinkInternal from 'ui/shared/links/LinkInternal'; | ||
import TextSeparator from 'ui/shared/TextSeparator'; | ||
|
||
const feature = config.features.saveOnGas; | ||
|
||
const responseSchema = v.object({ | ||
percent: v.number(), | ||
}); | ||
|
||
const ERROR_NAME = 'Invalid response schema'; | ||
|
||
interface Props { | ||
gasUsed: string; | ||
address: string; | ||
} | ||
|
||
const AddressSaveOnGas = ({ gasUsed, address }: Props) => { | ||
|
||
const gasUsedNumber = Number(gasUsed); | ||
|
||
const query = useQuery({ | ||
queryKey: [ 'gas_hawk_saving_potential', { address } ], | ||
queryFn: async() => { | ||
if (!feature.isEnabled) { | ||
return; | ||
} | ||
|
||
const response = await fetch(feature.apiUrlTemplate.replace('<address>', address)); | ||
const data = await response.json(); | ||
return data; | ||
}, | ||
select: (response) => { | ||
const parsedResponse = v.safeParse(responseSchema, response); | ||
|
||
if (!parsedResponse.success) { | ||
throw Error('Invalid response schema'); | ||
} | ||
|
||
return parsedResponse.output; | ||
}, | ||
placeholderData: { percent: 42 }, | ||
enabled: feature.isEnabled && gasUsedNumber > 0, | ||
}); | ||
|
||
const errorMessage = query.error && 'message' in query.error ? query.error.message : undefined; | ||
|
||
React.useEffect(() => { | ||
if (errorMessage === ERROR_NAME) { | ||
fetch('/node-api/monitoring/invalid-api-schema', { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
resource: 'gas_hawk_saving_potential', | ||
url: feature.isEnabled ? feature.apiUrlTemplate.replace('<address>', address) : undefined, | ||
}), | ||
}); | ||
} | ||
}, [ address, errorMessage ]); | ||
|
||
if (gasUsedNumber <= 0 || !feature.isEnabled || query.isError || !query.data?.percent) { | ||
return null; | ||
} | ||
|
||
const percent = Math.round(query.data.percent); | ||
|
||
if (percent < 1) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<TextSeparator color="divider"/> | ||
<Skeleton isLoaded={ !query.isPlaceholderData } display="flex" alignItems="center" columnGap={ 2 }> | ||
<Image src="/static/gas_hawk_logo.svg" w="15px" h="20px" alt="GasHawk logo"/> | ||
<LinkInternal href={ route({ pathname: '/apps/[id]', query: { id: feature.dappId } }) } fontSize="sm"> | ||
Save { percent.toLocaleString(undefined, { maximumFractionDigits: 0 }) }% with GasHawk | ||
</LinkInternal> | ||
</Skeleton> | ||
</> | ||
); | ||
}; | ||
|
||
export default React.memo(AddressSaveOnGas); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters