From 6e06d5a38f29ca060782f18df2562611369b4100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vitor=20de=20Lima=20Matos?= Date: Thu, 25 Mar 2021 12:28:28 -0300 Subject: [PATCH] sdk: rename Udc to UDC and address PR comments - Raiden.udcWithdraw -> Raiden.withdrawFromUDC - Raiden.planUdcWithdraw -> Raiden.planUDCWithdraw - config.autoUdcWithdraw -> config.autoUDCWithdraw - 100 -> constants.UDC_WITHDRAW_TIMEOUT --- .../dialogs/UdcWithdrawalDialog.vue | 2 +- raiden-dapp/src/services/raiden-service.ts | 4 ++-- .../dialogs/udc-withdrawal-dialog.spec.ts | 2 +- raiden-ts/CHANGELOG.md | 5 ++++- raiden-ts/src/config.ts | 6 ++--- raiden-ts/src/constants.ts | 1 + raiden-ts/src/errors.json | 4 ++-- raiden-ts/src/raiden.ts | 22 +++++++++---------- raiden-ts/src/services/epics/udc.ts | 16 +++++--------- raiden-ts/tests/unit/epics/udc.spec.ts | 4 ++-- 10 files changed, 32 insertions(+), 34 deletions(-) diff --git a/raiden-dapp/src/components/dialogs/UdcWithdrawalDialog.vue b/raiden-dapp/src/components/dialogs/UdcWithdrawalDialog.vue index d0d2fae7a6..3f2cd8ad37 100644 --- a/raiden-dapp/src/components/dialogs/UdcWithdrawalDialog.vue +++ b/raiden-dapp/src/components/dialogs/UdcWithdrawalDialog.vue @@ -153,7 +153,7 @@ export default class UdcWithdrawalDialog extends Vue { async planWithdraw() { this.inProgress = true; try { - await this.$raiden.planUdcWithdraw(this.withdrawAmount); + await this.$raiden.planUDCWithdraw(this.withdrawAmount); this.done(); } catch (e) { this.error = e; diff --git a/raiden-dapp/src/services/raiden-service.ts b/raiden-dapp/src/services/raiden-service.ts index 8889e8508a..a70b52b6be 100644 --- a/raiden-dapp/src/services/raiden-service.ts +++ b/raiden-dapp/src/services/raiden-service.ts @@ -645,8 +645,8 @@ export default class RaidenService { } /* istanbul ignore next */ - async planUdcWithdraw(amount: BigNumber): Promise { - return this.raiden.planUdcWithdraw(amount); + async planUDCWithdraw(amount: BigNumber): Promise { + return this.raiden.planUDCWithdraw(amount); } /* istanbul ignore next */ diff --git a/raiden-dapp/tests/unit/components/dialogs/udc-withdrawal-dialog.spec.ts b/raiden-dapp/tests/unit/components/dialogs/udc-withdrawal-dialog.spec.ts index 5dc8b0ec38..221e4a251a 100644 --- a/raiden-dapp/tests/unit/components/dialogs/udc-withdrawal-dialog.spec.ts +++ b/raiden-dapp/tests/unit/components/dialogs/udc-withdrawal-dialog.spec.ts @@ -64,7 +64,7 @@ describe('UdcWithdrawalDialog.vue', function () { expect.assertions(1); await (wrapper.vm as any).planWithdraw(); await flushPromises(); - expect($raiden.planUdcWithdraw).toBeCalledTimes(1); + expect($raiden.planUDCWithdraw).toBeCalledTimes(1); }); test('invalid withdraw amount is zero', async () => { diff --git a/raiden-ts/CHANGELOG.md b/raiden-ts/CHANGELOG.md index 6a18de7432..09841eb4ba 100644 --- a/raiden-ts/CHANGELOG.md +++ b/raiden-ts/CHANGELOG.md @@ -4,12 +4,13 @@ ### Added - [#1342] Flat (fixed) mediation fees for mediator nodes - [#2581] `config.pfsSafetyMargin` now also accepts a `[f, a]` pair, which will add `f*fee + a*amount` on top of PFS's estimated fee, if one wants finer-grain control on safety margin which is added on the transfer to be initiated. -- [#2629] `config.autoUdcWithdraw` (default=true) to allow disabling automatically completing a planned UDC withdraw, and new `Raiden.getUdcWithdrawPlan` and `Raiden.udcWithdraw` to check and perform UDC withdraw when not in auto mode. +- [#2629] `config.autoUDCWithdraw` (default=true) to allow disabling automatically completing a planned UDC withdraw, and new `Raiden.getUDCWithdrawPlan` and `Raiden.withdrawFromUDC` to check and perform UDC withdraw when not in auto mode. ### Changed - [#2536] Wait for global messages before resolving deposits and channel open request - [#2566] Optimize initial sync and resume previous sync filters scans - [#2570] Support multiple custom services in config.pfs +- [#2635] **BREAKING** Renamed `Raiden.planUdcWithdraw` to `Raiden.planUDCWithdraw` for consistency ### Removed - [#2550] **BREAKING** Remove migration of legacy state at localStorage during creation @@ -28,6 +29,8 @@ [#2581]: https://github.com/raiden-network/light-client/pull/2581 [#2596]: https://github.com/raiden-network/light-client/issues/2596 [#2600]: https://github.com/raiden-network/light-client/issues/2600 +[#2629]: https://github.com/raiden-network/light-client/issues/2629 +[#2635]: https://github.com/raiden-network/light-client/pull/2635 ## [0.15.0] - 2021-01-26 ### Added diff --git a/raiden-ts/src/config.ts b/raiden-ts/src/config.ts index a9d3518688..e962d940ce 100644 --- a/raiden-ts/src/config.ts +++ b/raiden-ts/src/config.ts @@ -58,7 +58,7 @@ const RTCIceServer = t.type({ urls: t.union([t.string, t.array(t.string)]) }); * approving tokens should be needed only once, trusting TokenNetwork's & UDC contracts; * Set to Zero to fallback to approving the strictly needed deposit amounts * - autoSettle - Whether to channelSettle.request settleable channels automatically - * - autoUdcWithdraw - Whether to udcWithdraw.request planned withdraws automatically + * - autoUDCWithdraw - Whether to udcWithdraw.request planned withdraws automatically * - mediationFees - deps.mediationFeeCalculator config. It's typed as unknown because it'll be * validated and decoded by [[FeeModel.decodeConfig]]. * - matrixServer? - Specify a matrix server to use. @@ -98,7 +98,7 @@ export const RaidenConfig = t.readonly( pollingInterval: t.number, minimumAllowance: UInt(32), autoSettle: t.boolean, - autoUdcWithdraw: t.boolean, + autoUDCWithdraw: t.boolean, mediationFees: t.unknown, }), t.partial({ @@ -168,7 +168,7 @@ export function makeDefaultConfig( pollingInterval: 5000, minimumAllowance: MaxUint256 as UInt<32>, autoSettle: false, - autoUdcWithdraw: true, + autoUDCWithdraw: true, mediationFees: {}, ...overwrites, caps, // merged caps overwrites 'overwrites.caps' diff --git a/raiden-ts/src/constants.ts b/raiden-ts/src/constants.ts index 6648ca91b1..4d047e4ee4 100644 --- a/raiden-ts/src/constants.ts +++ b/raiden-ts/src/constants.ts @@ -33,3 +33,4 @@ export const CapsFallback = { export const RAIDEN_DEVICE_ID = 'RAIDEN'; export const DEFAULT_CONFIRMATIONS = 5; +export const UDC_WITHDRAW_TIMEOUT = 100; diff --git a/raiden-ts/src/errors.json b/raiden-ts/src/errors.json index fb7608ace4..c4b3f9643b 100644 --- a/raiden-ts/src/errors.json +++ b/raiden-ts/src/errors.json @@ -92,8 +92,8 @@ "UDC_PLAN_WITHDRAW_EXCEEDS_AVAILABLE" : "The planned withdraw amount exceeds the total amount available for withdrawing.", "UDC_WITHDRAW_NO_BALANCE" : "There is no balances left to withdraw from UDC.", "UDC_WITHDRAW_FAILED" : "An error occurred while withdrawing from the UDC.", - "UDC_WITHDRAW_AUTO_ENABLED": "Interactive udcWithdraw disabled when config.autoUdcWithdraw is enabled; request will be made automatically", - "UDC_WITHDRAW_NO_PLAN": "No plan currently registered; you must call Raiden.planUdcWithdraw first", + "UDC_WITHDRAW_AUTO_ENABLED": "Interactive UDC withdraw disabled when config.autoUDCWithdraw is enabled; request will be made automatically", + "UDC_WITHDRAW_NO_PLAN": "No plan currently registered; you must call Raiden.planUDCWithdraw first", "UDC_WITHDRAW_TOO_EARLY": "The current UDC withdraw plan is not yet ready to be withdrawn", "UDC_WITHDRAW_TOO_LARGE": "Your current plan is not enough to withdraw the requested amount" } diff --git a/raiden-ts/src/raiden.ts b/raiden-ts/src/raiden.ts index c7245df26c..b2280f2f68 100644 --- a/raiden-ts/src/raiden.ts +++ b/raiden-ts/src/raiden.ts @@ -1393,9 +1393,9 @@ export class Raiden { * * @returns Promise to object containing maximum 'amount' planned for withdraw and 'block' at * which withdraw will become available, and 'ready' after it can be withdrawn with - * [[udcWithdraw]]; resolves to undefined if there's no current plan + * [[withdrawFromUDC]]; resolves to undefined if there's no current plan */ - public async getUdcWithdrawPlan(): Promise< + public async getUDCWithdrawPlan(): Promise< { amount: UInt<32>; block: number; ready: boolean } | undefined > { const plan = await this.deps.userDepositContract.withdraw_plans(this.address); @@ -1410,15 +1410,14 @@ export class Raiden { /** * Records a UDC withdraw plan for our UDC deposit * - * The plan will be ready for withdraw after 100 blocks. * Maximum 'value' which can be planned is current [[getUDCCapacity]] plus current - * [[getUdcWithdrawPlan]].amount, since new plan overwrites previous. + * [[getUDCWithdrawPlan]].amount, since new plan overwrites previous. * * @param value - Maximum value which we may try to withdraw. An error will be thrown if this - * value is larger than [[getUDCCapacity]]+[[getUdcWithdrawPlan]].amount + * value is larger than [[getUDCCapacity]]+[[getUDCWithdrawPlan]].amount * @returns Promise to hash of plan transaction, if it succeeds. */ - public async planUdcWithdraw(value: BigNumberish): Promise { + public async planUDCWithdraw(value: BigNumberish): Promise { const meta = { amount: decode(UInt(32), value, ErrorCodes.DTA_INVALID_AMOUNT, this.log.error), }; @@ -1433,16 +1432,15 @@ export class Raiden { * Complete a planned UDC withdraw and get the deposit to account. * * Maximum 'value' is the one from current plan, attempting to withdraw a larger value will throw - * an error, but a smaller value is valid. This method may only be called after plan is 'ready', - * i.e. at least 100 blocks have passed after it was planned. + * an error, but a smaller value is valid. This method may only be called after plan is 'ready' * * @param value - Maximum value which we may try to withdraw. An error will be thrown if this - * value is larger than [[getUDCCapacity]]+[[getUdcWithdrawPlan]].amount + * value is larger than [[getUDCCapacity]]+[[getUDCWithdrawPlan]].amount * @returns Promise to hash of plan transaction, if it succeeds. */ - public async udcWithdraw(value?: BigNumberish): Promise { - assert(!this.config.autoUdcWithdraw, ErrorCodes.UDC_WITHDRAW_AUTO_ENABLED, this.log.warn); - const plan = await this.getUdcWithdrawPlan(); + public async withdrawFromUDC(value?: BigNumberish): Promise { + assert(!this.config.autoUDCWithdraw, ErrorCodes.UDC_WITHDRAW_AUTO_ENABLED, this.log.warn); + const plan = await this.getUDCWithdrawPlan(); assert(plan, ErrorCodes.UDC_WITHDRAW_NO_PLAN, this.log.warn); assert(plan.ready, ErrorCodes.UDC_WITHDRAW_TOO_EARLY, this.log.warn); if (!value) { diff --git a/raiden-ts/src/services/epics/udc.ts b/raiden-ts/src/services/epics/udc.ts index d7d170c13d..c39b8f2d2f 100644 --- a/raiden-ts/src/services/epics/udc.ts +++ b/raiden-ts/src/services/epics/udc.ts @@ -21,6 +21,7 @@ import type { RaidenAction } from '../../actions'; import { newBlock } from '../../channels/actions'; import { approveIfNeeded$, assertTx } from '../../channels/utils'; import { intervalFromConfig } from '../../config'; +import { UDC_WITHDRAW_TIMEOUT } from '../../constants'; import type { HumanStandardToken, UserDeposit } from '../../contracts'; import { chooseOnchainAccount, getContractWithSigner } from '../../helpers'; import type { RaidenState } from '../../state'; @@ -272,7 +273,7 @@ export function udcWithdrawPlanRequestEpic( } /** - * If config.autoUdcWithdraw is enabled, monitors planned withdraws and udcWithdraw.request when + * If config.autoUDCWithdraw is enabled, monitors planned withdraws and udcWithdraw.request when * ready * * @param action$ - Observable of RaidenActions @@ -289,7 +290,6 @@ export function udcAutoWithdrawEpic( {}: Observable, { userDepositContract, address, config$, log }: RaidenEpicDeps, ): Observable { - const WITHDRAW_TIMEOUT = 100; let nextCheckBlock = 0; return action$.pipe( filter(newBlock.is), @@ -300,7 +300,7 @@ export function udcAutoWithdrawEpic( filter(({ withdraw_block: withdrawBlock }) => { const currentBlock = action.payload.blockNumber; if (withdrawBlock.isZero()) { - nextCheckBlock = currentBlock + WITHDRAW_TIMEOUT; + nextCheckBlock = currentBlock + UDC_WITHDRAW_TIMEOUT; return false; } else if (withdrawBlock.gt(currentBlock)) { nextCheckBlock = withdrawBlock.toNumber(); @@ -318,7 +318,7 @@ export function udcAutoWithdrawEpic( }), ), ), - takeIf(config$.pipe(pluck('autoUdcWithdraw'))), + takeIf(config$.pipe(pluck('autoUDCWithdraw'))), ); } @@ -346,11 +346,7 @@ export function udcWithdrawEpic( concatMap((action) => { const contract = getContractWithSigner(userDepositContract, signer); let balance: UInt<32>; - return retryAsync$( - async () => userDepositContract.callStatic.balances(address), - intervalFromConfig(config$), - { onErrors: networkErrors, log: log.info }, - ).pipe( + return defer(async () => userDepositContract.callStatic.balances(address)).pipe( mergeMap(async (balance_) => { assert(balance_.gt(Zero), [ ErrorCodes.UDC_WITHDRAW_NO_BALANCE, @@ -360,7 +356,7 @@ export function udcWithdrawEpic( return contract.withdraw(action.meta.amount); }), assertTx('withdraw', ErrorCodes.UDC_WITHDRAW_FAILED, { log, provider }), - retryWhile(intervalFromConfig(config$), { onErrors: commonTxErrors, log: log.debug }), + retryWhile(intervalFromConfig(config$), { onErrors: commonTxErrors, log: log.info }), mergeMap(([, { transactionHash, blockNumber }]) => action$.pipe( filter(newBlock.is), diff --git a/raiden-ts/tests/unit/epics/udc.spec.ts b/raiden-ts/tests/unit/epics/udc.spec.ts index da020a6ad6..87bcdb8aaa 100644 --- a/raiden-ts/tests/unit/epics/udc.spec.ts +++ b/raiden-ts/tests/unit/epics/udc.spec.ts @@ -303,7 +303,7 @@ describe('udcWithdraw', () => { const withdrawTx = makeTransaction(1); const raiden = await makeRaiden(undefined, false); - raiden.store.dispatch(raidenConfigUpdate({ autoUdcWithdraw: false })); + raiden.store.dispatch(raidenConfigUpdate({ autoUDCWithdraw: false })); raiden.deps.userDepositContract.withdraw.mockResolvedValue(withdrawTx); raiden.deps.userDepositContract.balances .mockClear() @@ -336,7 +336,7 @@ describe('udcWithdraw', () => { const withdrawTx = makeTransaction(0); // failed tx const raiden = await makeRaiden(undefined, false); - raiden.store.dispatch(raidenConfigUpdate({ autoUdcWithdraw: false })); + raiden.store.dispatch(raidenConfigUpdate({ autoUDCWithdraw: false })); raiden.deps.userDepositContract.withdraw.mockResolvedValue(withdrawTx); raiden.deps.userDepositContract.balances .mockClear()