Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module.exports = {
'CHALO-',
'CECHO-',
'CSHLD-',
'DEFI-',
'#', // Prefix used by GitHub issues
],
},
Expand Down
78 changes: 74 additions & 4 deletions modules/sdk-core/src/bitgo/defi/defiVault.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
/**
* @prettier
*/
import * as t from 'io-ts';
import { GetVaultResponse, VaultProtocol } from '@bitgo/public-types';
import {
ConcreteDepositResult,
MorphoDepositResult,
DefiOperation,
DefiOperationListResult,
DepositResult,
DepositToVaultOptions,
GetOperationOptions,
GetVaultConfigOptions,
IDefiVault,
ListOperationsOptions,
ResumeDepositOptions,
} from './iDefiVault';
import { IWallet } from '../wallet';
import { BitGoBase } from '../bitgoBase';
import { decodeWithCodec } from '../utils';

/**
* Error thrown when a concurrent active deposit already exists for the (wallet, vault) pair.
Expand Down Expand Up @@ -47,13 +53,26 @@ export class DefiVault implements IDefiVault {
this.bitgo = wallet.bitgo;
}

/**
* Fetch vault config from defi-service. Used internally to determine
* which deposit path to take (Concrete vs Morpho).
*/
async getVaultConfig(params: GetVaultConfigOptions): Promise<GetVaultResponse> {
if (!params.vaultId) {
throw new Error('vaultId is required');
}
const raw = await this.bitgo
.get(this.bitgo.microservicesUrl(`/api/defi-service/v1/vaults/${params.vaultId}`))
.result();
return decodeWithCodec(GetVaultResponse, raw, 'getVaultConfig');
}

/**
* Deposit an amount of underlying asset into a vault.
*
* Internally issues two sendMany calls (approve + deposit) and returns the
* operationId that links them. If the deposit sendMany fails after
* the approve succeeds, the error propagates — the server-side reconciler
* handles orphaned approvals.
* Dispatches to the concrete or morpho path based on vault provider.
* The concrete path returns a pendingApproval (custodial wallet).
* The morpho path issues two sendMany calls (approve + deposit).
*
* @param params.vaultId - DeFi-service vault identifier
* @param params.amount - amount in base units of the underlying asset
Expand All @@ -68,6 +87,41 @@ export class DefiVault implements IDefiVault {
throw new Error('amount is required');
}

const config = await this.getVaultConfig({ vaultId: params.vaultId });

if (config.protocol === VaultProtocol.CONCRETE_BTCCX) {
return this.depositToConcreteVault(params);
} else if (config.protocol === VaultProtocol.MORPHO) {
return this.depositToMorphoVault(params);
} else {
throw new Error(`Unsupported vault protocol: ${config.protocol}`);
}
}

/**
* Concrete BTC vault deposit path. The client BTC wallet is custodial, so
* sendMany returns a pendingApproval rather than a signed transfer.
* No recipients are sent — WP resolves the escrow destination server-side.
*/
private async depositToConcreteVault(params: DepositToVaultOptions): Promise<ConcreteDepositResult> {
const sendManyResult = await this.wallet.sendMany({
type: 'defi-deposit',
Comment thread
venkateshv1266 marked this conversation as resolved.
defiParams: {
vaultId: params.vaultId,
amount: params.amount,
actionType: 'defi-deposit',
},
...(params.walletPassphrase ? { walletPassphrase: params.walletPassphrase } : {}),
});

return this.extractConcreteDepositResult(sendManyResult);
}

/**
* Morpho vault deposit path. Issues two sendMany calls (approve + deposit)
* and returns the operationId that links them.
*/
private async depositToMorphoVault(params: DepositToVaultOptions): Promise<MorphoDepositResult> {
// TODO(CGD-1709): Re-enable active operation pre-flight check once the
// defi-service operations endpoint is deployed and returning active state.
// const activeOps: DefiOperationListResult = await this.bitgo
Expand Down Expand Up @@ -256,6 +310,22 @@ export class DefiVault implements IDefiVault {
return intent?.operationId as string | undefined;
}

/**
* Extracts {@link ConcreteDepositResult} from a custodial sendMany response.
* Concrete BTC deposits return a `pendingApproval` instead of a txRequest —
* throws if `pendingApproval.id` is absent, indicating an unexpected shape.
*/
private extractConcreteDepositResult(sendManyResult: Record<string, unknown>): ConcreteDepositResult {
const SendManyConcreteResponse = t.type({
pendingApproval: t.intersection([t.type({ id: t.string }), t.partial({ state: t.string })]),
});
const decoded = decodeWithCodec(SendManyConcreteResponse, sendManyResult, 'defi-deposit sendMany response');
return {
pendingApprovalId: decoded.pendingApproval.id,
state: decoded.pendingApproval.state ?? 'awaitingSignature',
};
}

private operationsUrl(): string {
return `/api/defi-service/v1/wallets/${this.wallet.id()}/operations`;
}
Expand Down
21 changes: 16 additions & 5 deletions modules/sdk-core/src/bitgo/defi/iDefiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* @prettier
*/

import { GetVaultResponse } from '@bitgo/public-types';

export interface DepositToVaultOptions {
/** DeFi-service vault identifier */
vaultId: string;
Expand Down Expand Up @@ -32,6 +34,10 @@ export interface ListOperationsOptions {
cursor?: string;
}

export interface GetVaultConfigOptions {
vaultId: string;
}

export interface DefiOperation {
operationId: string;
walletId: string;
Expand All @@ -45,14 +51,18 @@ export interface DefiOperation {
updatedAt: string;
}

export interface DepositResult {
export interface ConcreteDepositResult {
pendingApprovalId: string;
state: string;
}

export interface MorphoDepositResult {
operationId: string;
txRequestIds: {
approve: string;
deposit: string;
};
txRequestIds: { approve: string; deposit: string };
}

export type DepositResult = ConcreteDepositResult | MorphoDepositResult;

export interface DefiOperationListResult {
items: DefiOperation[];
nextCursor?: string;
Expand All @@ -63,4 +73,5 @@ export interface IDefiVault {
resumeDeposit(params: ResumeDepositOptions): Promise<DepositResult>;
getOperation(params: GetOperationOptions): Promise<DefiOperation>;
listOperations(params: ListOperationsOptions): Promise<DefiOperationListResult>;
getVaultConfig(params: GetVaultConfigOptions): Promise<GetVaultResponse>;
}
1 change: 1 addition & 0 deletions modules/sdk-core/src/bitgo/wallet/BuildParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const BuildParams = t.exact(
feeToken: t.unknown,
// Bridging parameters for cross-chain operations (e.g., BTC to sBTC)
bridgingParams: t.unknown,
defiParams: t.unknown,
}),
])
);
Expand Down
7 changes: 7 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/iWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,13 @@ export interface SendManyOptions extends PrebuildAndSignTransactionOptions {
eip1559?: EIP1559;
gasLimit?: number;
custodianTransactionId?: string;
defiParams?: {
vaultId?: string;
amount?: string | number;
actionType?: string;
operationId?: string;
clientIdempotencyKey?: string;
};
}

export interface FetchCrossChainUTXOsOptions {
Expand Down
Loading
Loading