From 19769c48972a7e7b8273be8b6c48bed1a30f7ef1 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Fri, 4 Sep 2020 17:53:58 +0200 Subject: [PATCH 1/4] uncomment eth auto-swap --- browser-sdk/src/controllers/deposit.ts | 64 +++++++++++++------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/browser-sdk/src/controllers/deposit.ts b/browser-sdk/src/controllers/deposit.ts index ed090cc..9a16c43 100644 --- a/browser-sdk/src/controllers/deposit.ts +++ b/browser-sdk/src/controllers/deposit.ts @@ -34,9 +34,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, }); @@ -46,9 +46,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, }); @@ -85,18 +85,18 @@ class DepositController { try { await this.unsubscribeToDeposit(); await this.rescindDepositRights(); - // const freeBalanceEth = await this.getEthFreeBalance(); - // if (freeBalanceEth.gt(BigNumber.from(0))) { - // await this.sdk.channel.swap({ - // fromAssetId: constants.ETH_ASSET_ID, - // toAssetId: this.sdk.tokenAddress, - // amount: freeBalanceEth, - // swapRate: await this.sdk.channel.getLatestSwapRate( - // constants.ETH_ASSET_ID, - // this.sdk.tokenAddress - // ), - // }); - // } + const freeBalanceEth = await this.getEthFreeBalance(); + if (freeBalanceEth.gt(BigNumber.from(0))) { + await this.sdk.channel.swap({ + fromAssetId: constants.ETH_ASSET_ID, + toAssetId: this.sdk.tokenAddress, + amount: freeBalanceEth, + swapRate: await this.sdk.channel.getLatestSwapRate( + constants.ETH_ASSET_ID, + this.sdk.tokenAddress + ), + }); + } this.sdk.emit(constants.DEPOSIT_SUCCESS); this.sdk.modal.setDepositStage(constants.DEPOSIT_SUCCESS); } catch (e) { @@ -106,15 +106,15 @@ 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 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") { @@ -154,13 +154,13 @@ class DepositController { } private async assertBalanceIncrease(preDepositBalance: PreDepositBalance) { const tokenBalance = await this.getOnChainTokenBalance(); - // const ethBalance = await this.getOnChainEthBalance(); + const ethBalance = await this.getOnChainEthBalance(); return BigNumber.from(tokenBalance).gt( BigNumber.from(preDepositBalance.tokenBalance) - ); - // BigNumber.from(ethBalance).gt( - // BigNumber.from(preDepositBalance.ethBalance) - // ) + || + BigNumber.from(ethBalance).gt( + BigNumber.from(preDepositBalance.ethBalance) + ) } private setPreDepositBalance(preDepositBalance: PreDepositBalance): void { From 307098274d48eba89fea2587cdfc384114e2030e Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Fri, 4 Sep 2020 18:11:30 +0200 Subject: [PATCH 2/4] await deposit confirmed --- browser-sdk/package.json | 6 ++-- browser-sdk/src/controllers/deposit.ts | 48 ++++++++++++++++++-------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/browser-sdk/package.json b/browser-sdk/package.json index fa2d74e..4acd99e 100644 --- a/browser-sdk/package.json +++ b/browser-sdk/package.json @@ -32,9 +32,9 @@ "typescript": "^3.9.7" }, "dependencies": { - "@connext/channel-provider": "^7.3.2", - "@connext/client": "^7.3.2", - "@connext/types": "^7.3.2", + "@connext/channel-provider": "^7.3.8", + "@connext/client": "^7.3.8", + "@connext/types": "^7.3.8", "eventemitter3": "4.0.4", "magic-sdk": "^2.4.7", "react": "^16.13.1", diff --git a/browser-sdk/src/controllers/deposit.ts b/browser-sdk/src/controllers/deposit.ts index 9a16c43..e25442e 100644 --- a/browser-sdk/src/controllers/deposit.ts +++ b/browser-sdk/src/controllers/deposit.ts @@ -9,6 +9,7 @@ import { } from "../helpers"; import { PreDepositBalance } from "../typings"; import ConnextSDK from ".."; +import { EventNames } from "@connext/types"; class DepositController { constructor(private sdk: ConnextSDK) { @@ -87,15 +88,7 @@ class DepositController { await this.rescindDepositRights(); const freeBalanceEth = await this.getEthFreeBalance(); if (freeBalanceEth.gt(BigNumber.from(0))) { - await this.sdk.channel.swap({ - fromAssetId: constants.ETH_ASSET_ID, - toAssetId: this.sdk.tokenAddress, - amount: freeBalanceEth, - swapRate: await this.sdk.channel.getLatestSwapRate( - constants.ETH_ASSET_ID, - this.sdk.tokenAddress - ), - }); + await this.swapEthForToken(freeBalanceEth); } this.sdk.emit(constants.DEPOSIT_SUCCESS); this.sdk.modal.setDepositStage(constants.DEPOSIT_SUCCESS); @@ -139,6 +132,33 @@ 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.once(EventNames.DEPOSIT_CONFIRMED_EVENT, async () => { + if (typeof this.sdk.channel === "undefined") { + throw new Error(this.sdk.text.error.missing_channel); + } + 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.once(EventNames.DEPOSIT_FAILED_EVENT, (e) => { + reject(e); + }); + }); + } + private async onNewBlock() { if (typeof this.sdk.channel === "undefined") { throw new Error(this.sdk.text.error.missing_channel); @@ -156,11 +176,11 @@ class DepositController { const tokenBalance = await this.getOnChainTokenBalance(); const ethBalance = await this.getOnChainEthBalance(); return BigNumber.from(tokenBalance).gt( - BigNumber.from(preDepositBalance.tokenBalance) - || - BigNumber.from(ethBalance).gt( - BigNumber.from(preDepositBalance.ethBalance) - ) + BigNumber.from(preDepositBalance.tokenBalance) || + BigNumber.from(ethBalance).gt( + BigNumber.from(preDepositBalance.ethBalance) + ) + ); } private setPreDepositBalance(preDepositBalance: PreDepositBalance): void { From e9e35675ec8580324978aa06ee2a05c0a69735ea Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Fri, 4 Sep 2020 18:28:28 +0200 Subject: [PATCH 3/4] typo --- browser-sdk/src/controllers/deposit.ts | 13 +++++++------ browser-sdk/src/index.tsx | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/browser-sdk/src/controllers/deposit.ts b/browser-sdk/src/controllers/deposit.ts index e25442e..765e6a8 100644 --- a/browser-sdk/src/controllers/deposit.ts +++ b/browser-sdk/src/controllers/deposit.ts @@ -163,7 +163,6 @@ class DepositController { if (typeof this.sdk.channel === "undefined") { throw new Error(this.sdk.text.error.missing_channel); } - this.sdk.channel.ethProvider.off("block", this.onNewBlock.bind(this)); const preDepositBalance = this.getPreDepositBalance(); if (preDepositBalance === null) { return this.unsubscribeToDeposit(); @@ -175,11 +174,13 @@ class DepositController { private async assertBalanceIncrease(preDepositBalance: PreDepositBalance) { const tokenBalance = await this.getOnChainTokenBalance(); const ethBalance = await this.getOnChainEthBalance(); - return BigNumber.from(tokenBalance).gt( - BigNumber.from(preDepositBalance.tokenBalance) || - BigNumber.from(ethBalance).gt( - BigNumber.from(preDepositBalance.ethBalance) - ) + return ( + BigNumber.from(tokenBalance).gt( + BigNumber.from(preDepositBalance.tokenBalance) + ) || + BigNumber.from(ethBalance).gt( + BigNumber.from(preDepositBalance.ethBalance) + ) ); } diff --git a/browser-sdk/src/index.tsx b/browser-sdk/src/index.tsx index 36e3d80..2eb0fbb 100644 --- a/browser-sdk/src/index.tsx +++ b/browser-sdk/src/index.tsx @@ -128,8 +128,8 @@ class ConnextSDK extends EventEmitter { this.modal.displayDeposit(); this.modal.setDepositStage(constants.DEPOSIT_PENDING); await this.depositController.requestDepositRights(); - this.modal.setDepositStage(constants.DEPOSIT_SHOW_QR); await this.depositController.subscribeToDeposit(); + this.modal.setDepositStage(constants.DEPOSIT_SHOW_QR); return new Promise((resolve, reject) => { this.on(constants.DEPOSIT_SUCCESS, () => resolve()); this.on(constants.DEPOSIT_FAILURE, () => reject()); From ae9bf9ff9e7c741e9c16b9bc1709daa61b76d58b Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Tue, 8 Sep 2020 16:38:10 +0200 Subject: [PATCH 4/4] eth swap --- browser-sdk/src/controllers/deposit.ts | 88 +++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/browser-sdk/src/controllers/deposit.ts b/browser-sdk/src/controllers/deposit.ts index 83b5ce5..f329f34 100644 --- a/browser-sdk/src/controllers/deposit.ts +++ b/browser-sdk/src/controllers/deposit.ts @@ -1,4 +1,6 @@ import { BigNumber } from "ethers"; +import { EventNames } from "@connext/types"; +import { addressBook } from "@connext/contracts"; import * as constants from "../constants"; import { @@ -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, }); @@ -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, }); @@ -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) { @@ -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); @@ -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); @@ -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) + ) ); }