Skip to content

Commit 2f30d95

Browse files
committed
feat: borrow action
1 parent 4ee72ba commit 2f30d95

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './markets';
22
export * from './misc';
33
export * from './reserve';
4+
export * from './transactions';
45
export * from './user';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
BorrowQuery,
3+
type BorrowRequest,
4+
type Transaction,
5+
} from '@aave/graphql';
6+
import type { ResultAsync } from '@aave/types';
7+
import type { AaveClient } from '../client';
8+
import type { UnexpectedError } from '../errors';
9+
10+
/**
11+
* Creates a transaction to borrow from a market.
12+
*
13+
* ```ts
14+
* const result = await borrow(client, {
15+
* market: evmAddress('0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2'),
16+
* amount: {
17+
* erc20: {
18+
* currency: evmAddress('0xa0b86a33e6441c8c5f0bb9b7e5e1f8bbf5b78b5c'),
19+
* value: '1000'
20+
* }
21+
* },
22+
* borrower: evmAddress('0x742d35cc6e5c4ce3b69a2a8c7c8e5f7e9a0b1234'),
23+
* chainId: chainId(1)
24+
* });
25+
* ```
26+
*
27+
* @param client - Aave client.
28+
* @param request - The borrow request parameters.
29+
* @returns The transaction data, approval requirements, or insufficient balance error.
30+
*/
31+
export function borrow(
32+
client: AaveClient,
33+
request: BorrowRequest,
34+
): ResultAsync<Transaction, UnexpectedError> {
35+
return client.query(BorrowQuery, { request });
36+
}

packages/graphql/src/fragments/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './common';
33
export * from './market';
44
export * from './positions';
55
export * from './reserve';
6+
export * from './transaction';
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import type { FragmentOf } from 'gql.tada';
2+
import { graphql } from '../graphql';
3+
import { DecimalValueFragment } from './common';
4+
5+
export const TransactionRequestFragment = graphql(
6+
`fragment TransactionRequest on TransactionRequest {
7+
__typename
8+
to
9+
from
10+
data
11+
value
12+
chainId
13+
}`,
14+
);
15+
export type TransactionRequest = FragmentOf<typeof TransactionRequestFragment>;
16+
17+
export const ApprovalRequiredFragment = graphql(
18+
`fragment ApprovalRequired on ApprovalRequired {
19+
__typename
20+
approval {
21+
...TransactionRequest
22+
}
23+
reason
24+
requiredAmount {
25+
...DecimalValue
26+
}
27+
currentAllowance {
28+
...DecimalValue
29+
}
30+
originalTransaction {
31+
...TransactionRequest
32+
}
33+
}`,
34+
[TransactionRequestFragment, DecimalValueFragment],
35+
);
36+
export type ApprovalRequired = FragmentOf<typeof ApprovalRequiredFragment>;
37+
38+
export const InsufficientBalanceErrorFragment = graphql(
39+
`fragment InsufficientBalanceError on InsufficientBalanceError {
40+
__typename
41+
required {
42+
...DecimalValue
43+
}
44+
available {
45+
...DecimalValue
46+
}
47+
}`,
48+
[DecimalValueFragment],
49+
);
50+
export type InsufficientBalanceError = FragmentOf<typeof InsufficientBalanceErrorFragment>;
51+
52+
export const TransactionFragment = graphql(
53+
`fragment Transaction on Transaction {
54+
__typename
55+
... on TransactionRequest {
56+
...TransactionRequest
57+
}
58+
... on ApprovalRequired {
59+
...ApprovalRequired
60+
}
61+
... on InsufficientBalanceError {
62+
...InsufficientBalanceError
63+
}
64+
}`,
65+
[TransactionRequestFragment, ApprovalRequiredFragment, InsufficientBalanceErrorFragment],
66+
);
67+
export type Transaction = FragmentOf<typeof TransactionFragment>;

packages/graphql/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export * from './graphql';
55
export * from './markets';
66
export * from './misc';
77
export * from './reserves';
8+
export * from './transactions';
89
export * from './user';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TransactionFragment } from './fragments';
2+
import { graphql, type RequestOf } from './graphql';
3+
4+
/**
5+
* @internal
6+
*/
7+
export const BorrowQuery = graphql(
8+
`query Borrow($request: BorrowRequest!) {
9+
value: borrow(request: $request) {
10+
...Transaction
11+
}
12+
}`,
13+
[TransactionFragment],
14+
);
15+
export type BorrowRequest = RequestOf<typeof BorrowQuery>;

0 commit comments

Comments
 (0)