form.service.ts 5.46 KB
Newer Older
XFT-dev's avatar
XFT-dev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';
import {AbstractControl, FormBuilder, FormGroup, Validators} from '@angular/forms';
import {observable, action} from 'mobx-angular';
import {StakeSlpService} from '../core/services/services/stake-slp.service';
import {UtilsService} from '../core/services/services/utils.service';
import {AuthService} from '../core/services/auth/auth.service';
import {takeUntil} from 'rxjs/operators';
import {StatusModalService} from '../shared/services/status-modal.service';



@Injectable({
  providedIn: 'root'
})
export class FormService {


  public formStake: FormGroup;
  public formUnstake: FormGroup;
  private unsubscribe$ = new Subject();

  @observable serverErrorMessage: string;
  @observable isSubmittingApprove = false;
  @observable isSubmittingClaimRewards = false;
  @observable isSubmittingStake = false;
  @observable isSubmittingUnstake = false;
  @observable slpBalance: string;
  @observable staked: string;
  @observable slpAllowance: string;
  @observable rewardsXFT: number;
  @observable rewardsSushi: number;
  @observable apySushi: number;
  @observable apyXFT: number;


  constructor(
    private fb: FormBuilder,
    private stakeSlpService: StakeSlpService,
    private utilsService: UtilsService,
    private authService: AuthService,
    private statusModalService: StatusModalService,
  ) { }

  @action init() {
    this.createFormStake();
    this.createFormUnstake();
    this.getDataFromContract();
  }
  private getDataFromContract() {
    this.stakeSlpService.slpBalanceChange.pipe(takeUntil(this.unsubscribe$)).subscribe((res: string) => {
      this.slpBalance = res;
    });
    this.stakeSlpService.getSLPBalance(this.authService.user.address);

    this.stakeSlpService.stakedChange.pipe(takeUntil(this.unsubscribe$)).subscribe((res: string) => {
      this.staked = res;

    });
    this.stakeSlpService.getStaked(this.authService.user.address);

    this.stakeSlpService.slpAllowanceChange.pipe(takeUntil(this.unsubscribe$)).subscribe((res: string) => {
      this.slpAllowance = res;
    });
    this.stakeSlpService.getSLPAllowance(this.authService.user.address);

    this.stakeSlpService.rewardsXFTChange.pipe(takeUntil(this.unsubscribe$)).subscribe((res: number) => {
      this.rewardsXFT = res;
    });
    this.stakeSlpService.getRewardsXFT(this.authService.user.address);

    this.stakeSlpService.rewardsSushiChange.pipe(takeUntil(this.unsubscribe$)).subscribe((res: number) => {
      this.rewardsSushi = res;
    });
    this.stakeSlpService.getRewardsSushi(this.authService.user.address);

    this.stakeSlpService.apySushiChange.pipe(takeUntil(this.unsubscribe$)).subscribe((res: number) => {
      this.apySushi = res;
    });
    this.stakeSlpService.getAPYSushi(this.authService.user.address);

    this.stakeSlpService.apyXFTChange.pipe(takeUntil(this.unsubscribe$)).subscribe((res: number) => {
      this.apyXFT = res;
    });
    this.stakeSlpService.getAPYXft(this.authService.user.address);


  }

  @action createFormStake() {
    this.formStake = this.fb.group({
      amount: this.fb.control(null, Validators.required),
    });
  }
  @action createFormUnstake() {
    this.formUnstake = this.fb.group({
      amount: this.fb.control(null, Validators.required),
    });
  }

  @action   submitFormStake() {



    if (this.isBlockedFormStake ) {
      return;
    }
    if (this.utilsService.greaterThen(this.stakeAmount.value , this.slpAllowance)) {
      this.isSubmittingApprove = true;
      this.stakeSlpService.approveNewAllowance(
          this.utilsService.convertAmount(this.stakeAmount.value),
          this.authService.user.address
      ).then(() => {
        this.isSubmittingApprove = false;
      }, () => {
        this.isSubmittingApprove = false;
      });
    } else {
      this.isSubmittingStake = true;
      this.stakeSlpService.stake(this.utilsService.convertAmount(this.stakeAmount.value), this.authService.user.address).then((res) => {
        this.isSubmittingStake = false;
        this.formStake.reset();
        this.statusModalService.openStatusModal({hash: res});
      }, () => {
        this.isSubmittingStake = false;
      });
    }
  }
  @action submitFormUnstake() {
    if (this.isBlockedFormUnstake ) {
      return;
    }
    if (this.utilsService.lessOrEqual(this.unstakeAmount.value , this.staked)) {
      this.isSubmittingUnstake = true;
      this.stakeSlpService.unStake(this.utilsService.convertAmount(this.unstakeAmount.value), this.authService.user.address).then((res) => {
        this.isSubmittingUnstake = false;
        this.formUnstake.reset();
        this.statusModalService.openStatusModal({hash: res});
      }, () => {
        this.isSubmittingUnstake = false;
      });
    } else {
      this.isSubmittingUnstake = false;
    }
  }
  @action submitClaimRewards() {
      this.isSubmittingClaimRewards = true;
      this.stakeSlpService.claimReward(this.authService.user.address).then((res) => {
        this.isSubmittingClaimRewards = false;
        this.statusModalService.openStatusModal({hash: res});
      }, () => {
      this.isSubmittingClaimRewards = false;
    });
  }


  get stakeAmount(): AbstractControl {
    return this.formStake.get('amount');
  }
  get unstakeAmount(): AbstractControl {
    return this.formUnstake.get('amount');
  }

  get isBlockedFormStake() {
    return this.formStake.invalid;
  }
  get isBlockedFormUnstake() {
    return this.formUnstake.invalid;
  }


  @action unsubscribe() {
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }
}