-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* GasHawk integration Fixes #2205 * change link url
- Loading branch information
Showing
13 changed files
with
193 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,25 @@ | ||
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; | ||
}> = (() => { | ||
if (getEnvValue('NEXT_PUBLIC_SAVE_ON_GAS_ENABLED') === 'true' && marketplace.isEnabled) { | ||
return Object.freeze({ | ||
title, | ||
isEnabled: true, | ||
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,88 @@ | ||
import { Image, Skeleton } from '@chakra-ui/react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import React from 'react'; | ||
import * as v from 'valibot'; | ||
|
||
import config from 'configs/app'; | ||
import LinkExternal from 'ui/shared/links/LinkExternal'; | ||
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"/> | ||
<LinkExternal href="https://www.gashawk.io" fontSize="sm"> | ||
Save { percent.toLocaleString(undefined, { maximumFractionDigits: 0 }) }% with GasHawk | ||
</LinkExternal> | ||
</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