Skip to content

Commit

Permalink
feat: add APIs to withdraw all collateral
Browse files Browse the repository at this point in the history
  • Loading branch information
bvotteler committed Feb 23, 2024
1 parent f26028f commit b73ba26
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/parachain/nomination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ export class DefaultNominationAPI implements NominationAPI {
const parsedNonce = api.createType("Index", definedNonce);
return api.tx.nomination.withdrawCollateral(vaultId, amountAsPlanck, parsedNonce);
}

static async buildWithdrawAllCollateralExtrinsic(
api: ApiPromise,
rewardsAPI: RewardsAPI,
vaultAccountId: AccountId,
collateralCurrency: Currency,
wrappedCurrency: Currency,
nonce?: number
): Promise<SubmittableExtrinsic<"promise", ISubmittableResult>> {
const vaultId = newVaultId(api, vaultAccountId.toString(), collateralCurrency, wrappedCurrency);
const definedNonce = nonce ? nonce : await rewardsAPI.getStakingPoolNonce(collateralCurrency, vaultAccountId);
const parsedNonce = api.createType("Index", definedNonce);
return api.tx.nomination.withdrawCollateral(vaultId, null, parsedNonce);
}

async withdrawCollateral(
vaultAccountId: AccountId,
Expand Down
41 changes: 40 additions & 1 deletion src/parachain/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ export interface VaultsAPI {
*/
withdrawCollateral(amount: MonetaryAmount<CollateralCurrencyExt>): Promise<ExtrinsicData>;

/**
* Build withdraw collateral extrinsic (transaction) without sending it.
*
* @param collateralCurrency The collateral currency for which to withdraw all
* @returns A withdraw collateral submittable extrinsic as promise.
*/
buildWithdrawAllCollateralExtrinsic(
collateralCurrency: CollateralCurrencyExt
): Promise<SubmittableExtrinsic<"promise", ISubmittableResult>>;

/**
* @param collateralCurrency The collateral currency for which to withdraw all
* @returns {Promise<ExtrinsicData>} A submittable extrinsic and an event that is emitted when extrinsic is submitted.
*/
withdrawAllCollateral(collateralCurrency: CollateralCurrencyExt): Promise<ExtrinsicData>;

/**
* Build deposit collateral extrinsic (transaction) without sending it.
*
Expand Down Expand Up @@ -462,7 +478,7 @@ export class DefaultVaultsAPI implements VaultsAPI {
this.rewardsAPI,
vaultAccountId,
amount,
this.wrappedCurrency
this.wrappedCurrency,
);
}

Expand All @@ -471,6 +487,29 @@ export class DefaultVaultsAPI implements VaultsAPI {
return { extrinsic: tx, event: this.api.events.vaultRegistry.WithdrawCollateral };
}

async buildWithdrawAllCollateralExtrinsic(
collateralCurrency: CollateralCurrencyExt
): Promise<SubmittableExtrinsic<"promise", ISubmittableResult>> {
const account = this.transactionAPI.getAccount();
if (account == undefined) {
throw new Error("Account must be connected to create a collateral withdrawal request.");
}
const vaultAccountId = addressOrPairAsAccountId(this.api, account);

return await DefaultNominationAPI.buildWithdrawAllCollateralExtrinsic(
this.api,
this.rewardsAPI,
vaultAccountId,
collateralCurrency,
this.wrappedCurrency
);
}

async withdrawAllCollateral(collateralCurrency: CollateralCurrencyExt): Promise<ExtrinsicData> {
const tx = await this.buildWithdrawAllCollateralExtrinsic(collateralCurrency);
return { extrinsic: tx, event: this.api.events.vaultRegistry.WithdrawCollateral };
}

buildDepositCollateralExtrinsic(
amount: MonetaryAmount<CollateralCurrencyExt>
): SubmittableExtrinsic<"promise", ISubmittableResult> {
Expand Down

0 comments on commit b73ba26

Please sign in to comment.