Skip to content

Commit

Permalink
Merge pull request #152 from galacticcouncil/dca-slider-fix
Browse files Browse the repository at this point in the history
Step down DCA frequency unit when interval multiplier is lowered
  • Loading branch information
nohaapav authored Dec 4, 2024
2 parents 8060729 + 9b5f968 commit 3865e7f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-turtles-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@galacticcouncil/apps': patch
---

Step down DCA frequency unit when interval multiplier is lowered
46 changes: 23 additions & 23 deletions packages/apps/src/app/dca/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,17 @@ export class DcaForm extends BaseElement {
this.dispatchEvent(new CustomEvent('interval-mul-change', options));

setTimeout(() => {
if (this.frequencyRanges[this.frequencyUnit] <= 1) {
this.setFrequencyUnit(this.maxFrequency, 'min');
if (multipliplier && this.frequencyRanges[this.frequencyUnit] <= 1) {
const units = Object.keys(this.frequencyRanges);
const unit =
(units[units.indexOf(this.frequencyUnit) - 1] as FrequencyUnit) ||
'min';
const values = {
min: this.maxFrequency,
hour: Math.floor(this.maxFrequency / HOUR_MIN),
day: Math.floor(this.maxFrequency / DAY_MIN),
};
this.setFrequencyUnit(values[unit], unit);
}
}, 0);
}
Expand Down Expand Up @@ -365,21 +374,6 @@ export class DcaForm extends BaseElement {
const max = this.maxFrequency;
const value = this.frequency ?? max;

const valueMsec = value * 60 * 1000;
const blockTime = 12_000;
const blockCount = Math.floor(valueMsec / blockTime);
const blockHint =
blockCount > 0
? i18n.t('form.advanced.intervalBlocks', {
minutes: value,
blocks: blockCount,
})
: undefined;

const range = max - min;
const rangeInHours = Math.floor(range / HOUR_MIN);
const rangeInDays = Math.floor(range / DAY_MIN);

const minValues: Record<FrequencyUnit, number> = {
min: min,
hour: Math.ceil(min / HOUR_MIN),
Expand All @@ -398,18 +392,24 @@ export class DcaForm extends BaseElement {
day: Math.floor(value / DAY_MIN),
};

const frequencyRanges = {
min: range,
hour: rangeInHours,
day: rangeInDays,
};

const units = [
'min',
this.frequencyRanges.hour > 0 && 'hour',
this.frequencyRanges.day > 0 && 'day',
].filter((u): u is FrequencyUnit => !!u);

const valueMsec = value * 60 * 1000;
const blockTime = 12_000;
const blockCount = Math.floor(valueMsec / blockTime);
const blockHint =
blockCount > 0
? i18n.t('form.advanced.intervalBlocks', {
unit: i18n.t(`form.frequency.${this.frequencyUnit}`),
value: values[this.frequencyUnit],
blocks: blockCount,
})
: undefined;

return html`
<uigc-slider
label=${i18n.t('form.advanced.interval')}
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/src/app/dca/translation.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"form.advanced": "Advanced settings",
"form.advanced.desc": "Customize your trades to an even greater extent.",
"form.advanced.interval": "Custom interval",
"form.advanced.intervalBlocks": "{{minutes}} minutes = {{blocks}} blocks",
"form.advanced.intervalBlocks": "{{value}} {{unit}} = ~{{blocks}} blocks",

"form.summary": "Summary",
"form.summary.message": "Swap <1>{{amountIn}} {{assetIn}}</1> for <1>{{assetOut}}</1> every <1>~{{frequency}}</1> with a total budget of <1>{{amountInBudget}} {{assetIn}}</1> over the period of <1>~{{time}}</1>",
Expand Down

0 comments on commit 3865e7f

Please sign in to comment.