-
Notifications
You must be signed in to change notification settings - Fork 20
feat: donation rate prop #556
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
Changes from 5 commits
f7476fa
50a33f2
93173b4
83c595b
c6ba7d4
eb84790
14a2c91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { Socket } from 'socket.io-client' | |
| import { Theme, ThemeName, ThemeProvider, useTheme } from '../../themes' | ||
| import { Button, animation } from '../Button/Button' | ||
| import BarChart from '../BarChart/BarChart' | ||
| import config from '../../paybutton-config.json' | ||
| import { | ||
| getAddressBalance, | ||
| Currency, | ||
|
|
@@ -33,7 +34,9 @@ import { | |
| setupChronikWebSocket, | ||
| setupAltpaymentSocket, | ||
| CryptoCurrency, | ||
| } from '../../util' | ||
| DEFAULT_DONATE_RATE, | ||
| DEFAULT_MINIMUM_DONATE_AMOUNT | ||
| } from '../../util'; | ||
| import AltpaymentWidget from './AltpaymentWidget' | ||
| import { | ||
| AltpaymentPair, | ||
|
|
@@ -100,6 +103,8 @@ export interface WidgetProps { | |
| setAddressType?: Function | ||
| newTxText?: string | ||
| transactionText?: string | ||
| donationAddress?: string | ||
| donationRate?: number | ||
| } | ||
|
|
||
| interface StyleProps { | ||
|
|
@@ -155,8 +160,9 @@ export const Widget: React.FunctionComponent<WidgetProps> = props => { | |
| altpaymentError, | ||
| setAltpaymentError, | ||
| isChild, | ||
| donationAddress = config.donationAddress, | ||
| donationRate = DEFAULT_DONATE_RATE | ||
| } = props | ||
|
|
||
| const [loading, setLoading] = useState(true) | ||
|
|
||
| // websockets if standalone | ||
|
|
@@ -249,6 +255,12 @@ export const Widget: React.FunctionComponent<WidgetProps> = props => { | |
|
|
||
| const theme = useTheme(props.theme, isValidXecAddress(to)) | ||
|
|
||
| const [thisAmount, setThisAmount] = useState(props.amount) | ||
| const [hasPrice, setHasPrice] = useState(props.price !== undefined && props.price > 0) | ||
| const [thisCurrencyObject, setThisCurrencyObject] = useState(props.currencyObject) | ||
|
|
||
| const blurCSS = isPropsTrue(disabled) ? { filter: 'blur(5px)' } : {} | ||
| const [donationAmount, setDonationAmount] = useState<number | null>(null) | ||
| // inject keyframes once (replacement for @global in makeStyles) | ||
| useEffect(() => { | ||
| const id = 'paybutton-widget-keyframes' | ||
|
|
@@ -388,12 +400,6 @@ export const Widget: React.FunctionComponent<WidgetProps> = props => { | |
| } | ||
| }, [success, loading, theme, recentlyCopied, copied]) | ||
|
|
||
| const [thisAmount, setThisAmount] = useState(props.amount) | ||
| const [hasPrice, setHasPrice] = useState(props.price !== undefined && props.price > 0) | ||
| const [thisCurrencyObject, setThisCurrencyObject] = useState(props.currencyObject) | ||
|
|
||
| const blurCSS = isPropsTrue(disabled) ? { filter: 'blur(5px)' } : {} | ||
|
|
||
| const bchSvg = useMemo((): string => { | ||
| const color = theme.palette.logo ?? theme.palette.primary | ||
| return `data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8'%3F%3E%3Csvg version='1.1' viewBox='0 0 34 34' xmlns='http://www.w3.org/2000/svg'%3E%3Cg transform='translate(1,1)'%3E%3Ccircle cx='16' cy='16' r='17' fill='%23fff' stroke-width='1.0625'/%3E%3C/g%3E%3Cg transform='translate(1,1)' fill-rule='evenodd'%3E%3Ccircle cx='16' cy='16' r='16' fill='${window.encodeURIComponent( | ||
|
|
@@ -565,11 +571,33 @@ export const Widget: React.FunctionComponent<WidgetProps> = props => { | |
| ? getCurrencyObject(convertedAmount, thisAddressType, randomSatoshis) | ||
| : null | ||
| if (convertedObj) { | ||
| let amountToDisplay = thisCurrencyObject.string; | ||
| let convertedAmountToDisplay = convertedObj.string | ||
| if ( donationRate && donationRate >= 5){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No magic number. Make this 5 a constant imported from the constants file.
|
||
| const thisDonationAmount = thisCurrencyObject.float * (donationRate / 100) | ||
| const amountWithDonation = thisCurrencyObject.float + thisDonationAmount | ||
| const amountWithDonationObj = getCurrencyObject( | ||
| amountWithDonation, | ||
| currency, | ||
| false, | ||
| ) | ||
| amountToDisplay = amountWithDonationObj.string | ||
|
|
||
| const convertedDonationAmount = convertedObj.float * (donationRate / 100) | ||
| const convertedAmountWithDonation = convertedObj.float + convertedDonationAmount | ||
| const convertedAmountWithDonationObj = getCurrencyObject( | ||
| convertedAmountWithDonation, | ||
| thisAddressType, | ||
| randomSatoshis, | ||
| ) | ||
| convertedAmountToDisplay = convertedAmountWithDonationObj.string | ||
| setDonationAmount(convertedAmountWithDonationObj.float) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| setText( | ||
| `Send ${thisCurrencyObject.string} ${thisCurrencyObject.currency} = ${convertedObj.string} ${thisAddressType}`, | ||
| `Send ${amountToDisplay} ${thisCurrencyObject.currency} = ${convertedAmountToDisplay} ${thisAddressType}`, | ||
| ) | ||
| nextUrl = resolveUrl(thisAddressType, convertedObj.float) | ||
| setUrl(nextUrl ?? '') | ||
| const url = resolveUrl(thisAddressType, convertedObj.float) | ||
| setUrl(url ?? "") | ||
| } | ||
| } else { | ||
| const notZeroValue = | ||
|
|
@@ -660,20 +688,38 @@ export const Widget: React.FunctionComponent<WidgetProps> = props => { | |
| setRecentlyCopied(true) | ||
| }, [disabled, to, url, setCopied, setRecentlyCopied]) | ||
|
|
||
| const resolveUrl = useCallback( | ||
| (currencyCode: string, amount?: number) => { | ||
| if (disabled || !to) return | ||
| const prefix = CURRENCY_PREFIXES_MAP[currencyCode.toLowerCase() as (typeof CRYPTO_CURRENCIES)[number]] | ||
| if (!prefix) return | ||
| let thisUrl = `${prefix}:${to.replace(/^.*:/, '')}` | ||
| if (amount) { | ||
| const resolveUrl = useCallback((currency: string, amount?: number) => { | ||
| if (disabled || !to) return; | ||
|
|
||
| const prefix = CURRENCY_PREFIXES_MAP[currency.toLowerCase() as typeof CRYPTO_CURRENCIES[number]]; | ||
| if (!prefix) return; | ||
|
|
||
| let thisUrl = `${prefix}:${to.replace(/^.*:/, '')}`; | ||
|
|
||
| if (amount) { | ||
| if (donationAddress && donationRate && Number(donationRate)) { | ||
| const network = Object.entries(CURRENCY_PREFIXES_MAP).find( | ||
| ([, value]) => value === prefix | ||
| )?.[0]; | ||
| const decimals = network ? DECIMALS[network.toUpperCase()] : undefined; | ||
| const donationPercent = donationRate / 100 | ||
| const thisDonationAmount = donationAmount ? donationAmount : amount * donationPercent | ||
|
|
||
| thisUrl+=`?amount=${amount}` | ||
|
chedieck marked this conversation as resolved.
Outdated
|
||
| if(thisDonationAmount > DEFAULT_MINIMUM_DONATE_AMOUNT){ | ||
| thisUrl += `&addr=${donationAddress}&amount=${thisDonationAmount.toFixed(decimals)}`; | ||
| } | ||
| }else{ | ||
| thisUrl += `?amount=${amount}` | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if (opReturn) { | ||
| const separator = thisUrl.includes('?') ? '&' : '?' | ||
| thisUrl += `${separator}op_return_raw=${opReturn}` | ||
| } | ||
| return thisUrl | ||
| } | ||
|
|
||
| if (opReturn) { | ||
| const separator = thisUrl.includes('?') ? '&' : '?'; | ||
| thisUrl += `${separator}op_return_raw=${opReturn}`; | ||
| } | ||
|
|
||
| return thisUrl; | ||
| }, | ||
| [disabled, to, opReturn] | ||
| ) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.