Skip to content
Closed
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
2 changes: 1 addition & 1 deletion modules/sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
]
},
"dependencies": {
"@bitgo/public-types": "6.22.0",
"@bitgo/public-types": "^6.39.0",
"@bitgo/sdk-lib-mpc": "^10.15.0",
"@bitgo/secp256k1": "^1.11.0",
"@bitgo/sjcl": "^1.1.0",
Expand Down
75 changes: 63 additions & 12 deletions modules/sdk-core/src/bitgo/defi/defiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
* @prettier
*/
import {
ConcreteDepositResult,
DefiOperation,
DefiOperationListResult,
DepositResult,
DepositToVaultOptions,
GetOperationOptions,
GetVaultConfigOptions,
IDefiVault,
ListOperationsOptions,
ResumeDepositOptions,
VaultConfig,
} from './iDefiVault';
import { IWallet } from '../wallet';
import { BitGoBase } from '../bitgoBase';
Expand Down Expand Up @@ -47,13 +50,25 @@ 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<VaultConfig> {
if (!params.vaultId) {
throw new Error('vaultId is required');
}
return await this.bitgo
.get(this.bitgo.microservicesUrl(`/api/defi-service/v1/vaults/${params.vaultId}`))
.result();
}

/**
* 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,16 +83,52 @@ export class DefiVault implements IDefiVault {
throw new Error('amount is required');
}

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

if (config.provider === 'concrete_btccx') {
return this.depositToConcreteVault(params);
}

return this.depositToMorphoVault(params);
}

/**
* 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',
defiParams: {
vaultId: params.vaultId,
amount: params.amount,
actionType: 'defi-deposit',
},
...(params.walletPassphrase ? { walletPassphrase: params.walletPassphrase } : {}),
});

return this.extractConcreteDepositResult(sendManyResult);
}

private extractConcreteDepositResult(sendManyResult: unknown): ConcreteDepositResult {
const pendingApproval = (sendManyResult as { pendingApproval?: { id?: string; state?: string } })?.pendingApproval;
if (!pendingApproval?.id) {
throw new Error('Unexpected sendMany response for defi-deposit: no pendingApproval.id');
}
return { pendingApprovalId: pendingApproval.id, state: pendingApproval.state ?? 'awaitingSignature' };
}

/**
* Morpho vault deposit path. Issues two sendMany calls (approve + deposit)
* and returns the operationId that links them.
*/
private async depositToMorphoVault(params: DepositToVaultOptions): Promise<{
operationId: string;
txRequestIds: { approve: string; deposit: string };
}> {
// 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
// .get(this.bitgo.microservicesUrl(this.operationsUrl()))
// .query({ vaultId: params.vaultId, state: 'active' })
// .result();
//
// if (activeOps.items && activeOps.items.length > 0) {
// throw new ActiveOperationExistsError(activeOps.items[0].operationId);
// }

// Step 1: Approve txRequest via sendMany
const approveResult = await this.wallet.sendMany({
Expand Down
42 changes: 36 additions & 6 deletions modules/sdk-core/src/bitgo/defi/iDefiVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface ListOperationsOptions {
cursor?: string;
}

export interface GetVaultConfigOptions {
vaultId: string;
}

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

export interface DepositResult {
operationId: string;
txRequestIds: {
approve: string;
deposit: string;
};
export type VaultProvider = 'morpho' | 'concrete_btccx';

export interface ConcreteVaultConfig {
sourceWalletId: string;
escrowWalletId: string;
escrowDepositAddress: string;
positionWalletId: string;
positionBaseAddress: string;
}

export interface VaultConfig {
id: string;
name: string;
provider: VaultProvider;
status: string;
coin: string;
assetToken: string;
shareToken: string;
riskManager: string;
custodyType: string;
vaultContractAddress?: string;
concreteConfig?: ConcreteVaultConfig;
}

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

export type DepositResult =
| { pendingApprovalId: string; state: string } // concrete_btccx
| { operationId: string; txRequestIds: { approve: string; deposit: string } }; // morpho

export interface DefiOperationListResult {
items: DefiOperation[];
nextCursor?: string;
Expand All @@ -63,4 +92,5 @@ export interface IDefiVault {
resumeDeposit(params: ResumeDepositOptions): Promise<DepositResult>;
getOperation(params: GetOperationOptions): Promise<DefiOperation>;
listOperations(params: ListOperationsOptions): Promise<DefiOperationListResult>;
getVaultConfig(params: GetVaultConfigOptions): Promise<VaultConfig>;
}
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 @@ -135,6 +135,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
1 change: 1 addition & 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,7 @@ 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