Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

auto-swap ETH deposits #24

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 80 additions & 8 deletions browser-sdk/src/controllers/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { BigNumber } from "ethers";
import { EventNames } from "@connext/types";
import { addressBook } from "@connext/contracts";

import * as constants from "../constants";
import {
Expand Down Expand Up @@ -34,9 +36,9 @@ class DepositController {
if (typeof this.sdk.channel === "undefined") {
throw new Error(this.sdk.text.error.not_logged_in);
}
// await this.sdk.channel.requestDepositRights({
// assetId: constants.ETH_ASSET_ID,
// });
await this.sdk.channel.requestDepositRights({
assetId: constants.ETH_ASSET_ID,
});
await this.sdk.channel.requestDepositRights({
assetId: this.sdk.tokenAddress,
});
Expand All @@ -46,9 +48,9 @@ class DepositController {
if (typeof this.sdk.channel === "undefined") {
throw new Error(this.sdk.text.error.not_logged_in);
}
// await this.sdk.channel.rescindDepositRights({
// assetId: constants.ETH_ASSET_ID,
// });
await this.sdk.channel.rescindDepositRights({
assetId: constants.ETH_ASSET_ID,
});
await this.sdk.channel.rescindDepositRights({
assetId: this.sdk.tokenAddress,
});
Expand Down Expand Up @@ -85,6 +87,10 @@ class DepositController {
try {
await this.unsubscribeToDeposit();
await this.rescindDepositRights();
const freeBalanceEth = await this.getEthFreeBalance();
if (freeBalanceEth.gt(BigNumber.from(0))) {
await this.swapEthForToken(freeBalanceEth);
}
this.sdk.events.emit(constants.DEPOSIT_SUCCESS);
this.sdk.modal.setDepositStage(constants.DEPOSIT_SUCCESS);
} catch (e) {
Expand All @@ -94,6 +100,16 @@ class DepositController {
}
}

private async getEthFreeBalance() {
if (typeof this.sdk.channel === "undefined") {
throw new Error(this.sdk.text.error.not_logged_in);
}
const result = await this.sdk.channel.getFreeBalance(
constants.ETH_ASSET_ID
);
return BigNumber.from(result[this.sdk.channel.signerAddress]);
}

private async getOnChainTokenBalance() {
if (typeof this.sdk.channel === "undefined") {
throw new Error(this.sdk.text.error.not_logged_in);
Expand All @@ -117,6 +133,56 @@ class DepositController {
return ethBalance;
}

private async swapEthForToken(amount: BigNumber) {
return new Promise(async (resolve, reject) => {
if (typeof this.sdk.channel === "undefined") {
throw new Error(this.sdk.text.error.missing_channel);
}
this.sdk.channel.on(EventNames.UNINSTALL_EVENT, async (e) => {
if (typeof this.sdk.channel === "undefined") {
throw new Error(this.sdk.text.error.missing_channel);
}
const network = await this.sdk.channel.ethProvider.getNetwork();
if (
e.uninstalledApp.appDefinition !==
addressBook[network.chainId].DepositApp.address &&
e.uninstalledApp.latestState.assetId === constants.ETH_ASSET_ID
) {
return;
}
const swapRate = await this.sdk.channel.getLatestSwapRate(
constants.ETH_ASSET_ID,
this.sdk.tokenAddress
);
await this.sdk.channel.swap({
amount,
swapRate,
fromAssetId: constants.ETH_ASSET_ID,
toAssetId: this.sdk.tokenAddress,
});
resolve();
});
this.sdk.channel.on(EventNames.UNINSTALL_FAILED_EVENT, async (e) => {
if (typeof this.sdk.channel === "undefined") {
throw new Error(this.sdk.text.error.missing_channel);
}
const res = await this.sdk.channel.getAppInstance(
e.params.appIdentityHash
);
if (typeof res === "undefined") return;
const network = await this.sdk.channel.ethProvider.getNetwork();
if (
res.appInstance.appDefinition !==
addressBook[network.chainId].DepositApp.address &&
res.appInstance.latestState.assetId === constants.ETH_ASSET_ID
) {
return;
}
reject(e.error);
});
});
}

private async onNewBlock() {
if (typeof this.sdk.channel === "undefined") {
throw new Error(this.sdk.text.error.missing_channel);
Expand All @@ -131,8 +197,14 @@ class DepositController {
}
private async assertBalanceIncrease(preDepositBalance: PreDepositBalance) {
const tokenBalance = await this.getOnChainTokenBalance();
return BigNumber.from(tokenBalance).gt(
BigNumber.from(preDepositBalance.tokenBalance)
const ethBalance = await this.getOnChainEthBalance();
return (
BigNumber.from(tokenBalance).gt(
BigNumber.from(preDepositBalance.tokenBalance)
) ||
BigNumber.from(ethBalance).gt(
BigNumber.from(preDepositBalance.ethBalance)
)
);
}

Expand Down