Skip to content

Commit fda70e7

Browse files
authored
feat: fetchOfframpLiquidity method and add orderOwner argument in ABI (#587)
* feat: `fetchOfframpLiquidity` method * feat: add `orderOwner` as argument * chore: update version * fix use `isAddress` * fix: change to minor version release
1 parent effb335 commit fda70e7

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gobob/bob-sdk",
3-
"version": "3.3.0",
3+
"version": "3.3.1",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"scripts": {

sdk/src/gateway/abi.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export const offrampCreateOrderCaller = [
7373
type: 'address',
7474
internalType: 'address',
7575
},
76+
{
77+
name: 'orderOwner',
78+
type: 'address',
79+
internalType: 'address',
80+
},
7681
],
7782
},
7883
],

sdk/src/gateway/client.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
OfframpRawOrder,
2727
OfframpOrderStatus,
2828
EnrichedToken,
29+
OfframpLiquidity,
2930
} from './types';
3031
import { createBitcoinPsbt, getAddressInfo } from '../wallet';
3132
import { SYMBOL_LOOKUP, ADDRESS_LOOKUP, getTokenDecimals } from './tokens';
@@ -200,6 +201,47 @@ export class GatewayApiClient {
200201
return JSON.parse(await response.text()) as Address;
201202
}
202203

204+
/**
205+
* Fetches an offramp liquidity for the given token.
206+
*
207+
* @param token ERC20 token address.
208+
* @returns Offramp liquidity details.
209+
*/
210+
async fetchOfframpLiquidity(token: string): Promise<OfframpLiquidity> {
211+
let outputTokenAddress = '';
212+
const toToken = token.toLowerCase();
213+
214+
if (isAddress(toToken)) {
215+
outputTokenAddress = toToken;
216+
} else if (SYMBOL_LOOKUP[this.chainId][toToken]) {
217+
outputTokenAddress = SYMBOL_LOOKUP[this.chainId][toToken].address;
218+
} else {
219+
throw new Error('Unknown output token');
220+
}
221+
222+
const response = await fetch(`${this.baseUrl}/offramp-liquidity/${outputTokenAddress}`, {
223+
method: 'GET',
224+
headers: {
225+
'Content-Type': 'application/json',
226+
Accept: 'application/json',
227+
},
228+
});
229+
230+
if (!response.ok) {
231+
const errorData = await response.json().catch(() => null);
232+
const errorMessage = errorData?.message || 'Failed to get offramp liquidity';
233+
throw new Error(`Offramp liquidity API Error: ${errorMessage}`);
234+
}
235+
236+
const rawLiquidity = await response.json();
237+
238+
return {
239+
token: rawLiquidity.tokenAddress as Address,
240+
maxOrderAmount: BigInt(rawLiquidity.maxOrderAmount),
241+
totalOfframpLiquidity: BigInt(rawLiquidity.totalOfframpLiquidity),
242+
};
243+
}
244+
203245
/**
204246
* Fetches an offramp quote for the given token and amount.
205247
*
@@ -312,6 +354,7 @@ export class GatewayApiClient {
312354
orderCreationDeadline: offrampQuote.deadline,
313355
outputScript: receiverAddress,
314356
token: offrampQuote.token,
357+
orderOwner: params.fromUserAddress as Address,
315358
},
316359
],
317360
};

sdk/src/gateway/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,16 @@ export interface OfframpQuote {
373373
token: Address;
374374
}
375375

376+
/** @dev Offramp Available Liquidity */
377+
export interface OfframpLiquidity {
378+
/** @dev Token address used for payment */
379+
token: Address;
380+
/** @dev Max token amount a *single* order can be served with (in token decimals) */
381+
maxOrderAmount: bigint;
382+
/** @dev Total liquidity across all solver addresses (in token decimals) */
383+
totalOfframpLiquidity: bigint;
384+
}
385+
376386
/** @dev Params used for createOrder call on the off-ramp contract */
377387
export type OfframpCreateOrderParams = {
378388
quote: OfframpQuote;
@@ -390,6 +400,8 @@ export type OfframpCreateOrderParams = {
390400
outputScript: string;
391401
/** @dev Token to use for payment */
392402
token: Address;
403+
/** @dev EVM address of the user who can unlock the order or bump its fee */
404+
orderOwner: Address;
393405
},
394406
];
395407
};

sdk/test/gateway.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ describe('Gateway Tests', () => {
565565
orderCreationDeadline: result.offrampArgs[0].orderCreationDeadline,
566566
outputScript: '0x1600149d5e60f3b5cc2d246f990692ee4b267d1cd58477',
567567
token: '0xda472456b1a6a2fc9ae7edb0e007064224d4284c',
568+
orderOwner: '0xFAEe001465dE6D7E8414aCDD9eF4aC5A35B2B808',
568569
});
569570
});
570571

@@ -676,4 +677,30 @@ describe('Gateway Tests', () => {
676677
// Assert the result is true (claim delay has passed)
677678
expect(result).toBe(true);
678679
});
680+
681+
it('fetches the correct offramp liquidity', async () => {
682+
const gatewaySDK = new GatewaySDK('signet');
683+
const tokenAddress = '0x4496ebE7C8666a8103713EE6e0c08cA0cD25b888'.toLowerCase();
684+
nock(SIGNET_GATEWAY_BASE_URL).persist().get(`/offramp-liquidity/${tokenAddress}`).reply(200, {
685+
tokenAddress,
686+
maxOrderAmount: '861588',
687+
totalOfframpLiquidity: '861588',
688+
});
689+
690+
const offrampLiquidityTokenAddressAsParam = await gatewaySDK.fetchOfframpLiquidity(tokenAddress);
691+
692+
expect(offrampLiquidityTokenAddressAsParam).toEqual({
693+
token: tokenAddress,
694+
maxOrderAmount: BigInt('861588'),
695+
totalOfframpLiquidity: BigInt('861588'),
696+
});
697+
698+
const offrampLiquidityTokenSymbolAsParam = await gatewaySDK.fetchOfframpLiquidity('bobBTC');
699+
700+
expect(offrampLiquidityTokenSymbolAsParam).toEqual({
701+
token: tokenAddress,
702+
maxOrderAmount: BigInt('861588'),
703+
totalOfframpLiquidity: BigInt('861588'),
704+
});
705+
});
679706
});

0 commit comments

Comments
 (0)