Skip to content

Commit

Permalink
sdk: adds Raiden.getUdcWithdrawPlan,udcWithdraw methods
Browse files Browse the repository at this point in the history
The former queries the UDC for currently planned withdraws, returning
amount, block in which it becomes available and ready state; the later
is to be called when config.autoUdcWithdraw is false and after plan is
'ready', and interactively performs the last UDC withdraw phase.
  • Loading branch information
andrevmatos committed Mar 24, 2021
1 parent 470256c commit a4fae97
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
6 changes: 5 additions & 1 deletion raiden-ts/src/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
"UDC_PLAN_WITHDRAW_GT_ZERO" : "The planned withdraw amount has to be greater than zero.",
"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_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_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"
}

63 changes: 62 additions & 1 deletion raiden-ts/src/raiden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import {
} from './helpers';
import { createPersisterMiddleware } from './persister';
import { raidenReducer } from './reducer';
import { pathFind, udcDeposit, udcWithdrawPlan } from './services/actions';
import { pathFind, udcDeposit, udcWithdraw, udcWithdrawPlan } from './services/actions';
import type { IOU, RaidenPaths, RaidenPFS, SuggestedPartner } from './services/types';
import { Paths, PFS, PfsMode, SuggestedPartners } from './services/types';
import { pfsListInfo } from './services/utils';
Expand Down Expand Up @@ -1388,6 +1388,36 @@ export class Raiden {
return receipt.transactionHash as Hash;
}

/**
* Fetches our current UDC withdraw plan
*
* @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
*/
public async getUdcWithdrawPlan(): Promise<
{ amount: UInt<32>; block: number; ready: boolean } | undefined
> {
const plan = await this.deps.userDepositContract.withdraw_plans(this.address);
if (plan.withdraw_block.isZero()) return;
return {
amount: plan.amount as UInt<32>,
block: plan.withdraw_block.toNumber(),
ready: plan.withdraw_block.lte(this.state.blockNumber),
};
}

/**
* 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.
*
* @param value - Maximum value which we may try to withdraw. An error will be thrown if this
* value is larger than [[getUDCCapacity]]+[[getUdcWithdrawPlan]].amount
* @returns Promise to hash of plan transaction, if it succeeds.
*/
public async planUdcWithdraw(value: BigNumberish): Promise<Hash> {
const meta = {
amount: decode(UInt(32), value, ErrorCodes.DTA_INVALID_AMOUNT, this.log.error),
Expand All @@ -1399,6 +1429,37 @@ export class Raiden {
return promise;
}

/**
* 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.
*
* @param value - Maximum value which we may try to withdraw. An error will be thrown if this
* value is larger than [[getUDCCapacity]]+[[getUdcWithdrawPlan]].amount
* @returns Promise to hash of plan transaction, if it succeeds.
*/
public async udcWithdraw(value?: BigNumberish): Promise<Hash> {
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) {
value = plan.amount;
} else {
assert(plan.amount.gte(value), ErrorCodes.UDC_WITHDRAW_TOO_LARGE, this.log.warn);
}
const meta = {
amount: decode(UInt(32), value, ErrorCodes.DTA_INVALID_AMOUNT, this.log.error),
};
const promise = asyncActionToPromise(udcWithdraw, meta, this.action$, true).then(
({ txHash }) => txHash,
);
this.store.dispatch(udcWithdraw.request(undefined, meta));
return promise;
}

/**
* Requests to withdraw from channel
*
Expand Down

0 comments on commit a4fae97

Please sign in to comment.