Skip to content

Commit f1ae73f

Browse files
chore: improve offramp types (#598)
* chore: improve gateway class typings * chore: merge abi parts
1 parent 49e1a42 commit f1ae73f

File tree

4 files changed

+24
-39
lines changed

4 files changed

+24
-39
lines changed

sdk/src/gateway/abi.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export const strategyCaller = [
3838
},
3939
] as const;
4040

41-
export const offrampCreateOrderCaller = [
42-
{
41+
export const offrampCaller = {
42+
createOrder: {
4343
type: 'function',
4444
name: 'createOrder',
4545
inputs: [
@@ -84,10 +84,7 @@ export const offrampCreateOrderCaller = [
8484
outputs: [],
8585
stateMutability: 'nonpayable',
8686
},
87-
] as const;
88-
89-
export const offrampBumpFeeCaller = [
90-
{
87+
bumpFeeOfExistingOrder: {
9188
type: 'function',
9289
name: 'bumpFeeOfExistingOrder',
9390
inputs: [
@@ -105,10 +102,7 @@ export const offrampBumpFeeCaller = [
105102
outputs: [],
106103
stateMutability: 'nonpayable',
107104
},
108-
] as const;
109-
110-
export const offrampUnlockFundsCaller = [
111-
{
105+
unlockFunds: {
112106
type: 'function',
113107
name: 'unlockFunds',
114108
inputs: [
@@ -126,17 +120,7 @@ export const offrampUnlockFundsCaller = [
126120
outputs: [],
127121
stateMutability: 'nonpayable',
128122
},
129-
] as const;
130-
131-
export const compoundV2CTokenAbi = parseAbi([
132-
'function exchangeRateCurrent() external returns (uint256)',
133-
'function underlying() external view returns (address)',
134-
]);
135-
136-
export const aaveV2AtokenAbi = parseAbi(['function UNDERLYING_ASSET_ADDRESS() external view returns (address)']);
137-
138-
export const offrampGetOrderCaller = [
139-
{
123+
getOfframpOrder: {
140124
type: 'function',
141125
name: 'getOfframpOrder',
142126
inputs: [
@@ -166,6 +150,13 @@ export const offrampGetOrderCaller = [
166150
],
167151
stateMutability: 'view',
168152
},
169-
] as const;
153+
} as const;
154+
155+
export const compoundV2CTokenAbi = parseAbi([
156+
'function exchangeRateCurrent() external returns (uint256)',
157+
'function underlying() external view returns (address)',
158+
]);
159+
160+
export const aaveV2AtokenAbi = parseAbi(['function UNDERLYING_ASSET_ADDRESS() external view returns (address)']);
170161

171162
export const claimDelayAbi = parseAbi(['function CLAIM_DELAY() view returns (uint64)']);

sdk/src/gateway/client.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@ import { createBitcoinPsbt, getAddressInfo } from '../wallet';
3232
import { SYMBOL_LOOKUP, ADDRESS_LOOKUP, getTokenDecimals } from './tokens';
3333
import { AddressType, Network } from 'bitcoin-address-validation';
3434
import { EsploraClient } from '../esplora';
35-
import {
36-
claimDelayAbi,
37-
offrampBumpFeeCaller,
38-
offrampCreateOrderCaller,
39-
offrampGetOrderCaller,
40-
offrampUnlockFundsCaller,
41-
strategyCaller,
42-
} from './abi';
35+
import { claimDelayAbi, offrampCaller, strategyCaller } from './abi';
4336
import { isAddress, Address, isAddressEqual, createPublicClient, http, PublicClient } from 'viem';
4437
import * as bitcoin from 'bitcoinjs-lib';
4538
import { bob, bobSepolia } from 'viem/chains';
@@ -85,7 +78,7 @@ export class GatewayApiClient {
8578
* @constructor
8679
* @param chainName The chain name.
8780
*/
88-
constructor(chainName: string, options?: { rpcUrl?: string }) {
81+
constructor(chainName: 'mainnet' | 'testnet' | 'signet' | 'bob', options?: { rpcUrl?: string }) {
8982
switch (chainName) {
9083
case 'mainnet':
9184
case Chain.BOB:
@@ -345,7 +338,7 @@ export class GatewayApiClient {
345338

346339
return {
347340
quote: offrampQuote,
348-
offrampABI: offrampCreateOrderCaller,
341+
offrampABI: offrampCaller['createOrder'],
349342
offrampFunctionName: 'createOrder' as const,
350343
offrampArgs: [
351344
{
@@ -393,7 +386,7 @@ export class GatewayApiClient {
393386
const offrampRegistryAddress: Address = await this.fetchOfframpRegistryAddress();
394387

395388
return {
396-
offrampABI: offrampBumpFeeCaller,
389+
offrampABI: offrampCaller['bumpFeeOfExistingOrder'],
397390
offrampRegistryAddress: offrampRegistryAddress,
398391
offrampFunctionName: 'bumpFeeOfExistingOrder' as const,
399392
offrampArgs: [orderId, newFeeSat],
@@ -426,7 +419,7 @@ export class GatewayApiClient {
426419
}
427420

428421
return {
429-
offrampABI: offrampUnlockFundsCaller,
422+
offrampABI: offrampCaller['unlockFunds'],
430423
offrampRegistryAddress: offrampRegistryAddress,
431424
offrampFunctionName: 'unlockFunds',
432425
offrampArgs: [orderId, receiver],
@@ -531,7 +524,7 @@ export class GatewayApiClient {
531524

532525
const order = await publicClient.readContract({
533526
address: offrampRegistryAddress,
534-
abi: offrampGetOrderCaller,
527+
abi: [offrampCaller['getOfframpOrder']],
535528
functionName: 'getOfframpOrder',
536529
args: [orderId],
537530
});

sdk/src/gateway/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EsploraClient } from '../esplora';
22
import { Address } from 'viem';
3-
import { offrampBumpFeeCaller, offrampCreateOrderCaller, offrampUnlockFundsCaller, strategyCaller } from './abi';
3+
import { offrampCaller, strategyCaller } from './abi';
44

55
type ChainSlug = string | number;
66
type TokenSymbol = string;
@@ -386,7 +386,7 @@ export interface OfframpLiquidity {
386386
/** @dev Params used for createOrder call on the off-ramp contract */
387387
export type OfframpCreateOrderParams = {
388388
quote: OfframpQuote;
389-
offrampABI: typeof offrampCreateOrderCaller;
389+
offrampABI: (typeof offrampCaller)['createOrder'];
390390
offrampFunctionName: 'createOrder';
391391
offrampArgs: [
392392
{
@@ -408,7 +408,7 @@ export type OfframpCreateOrderParams = {
408408

409409
/** @dev Params used to bump fee for an existing order */
410410
export type OfframpBumpFeeParams = {
411-
offrampABI: typeof offrampBumpFeeCaller;
411+
offrampABI: (typeof offrampCaller)['bumpFeeOfExistingOrder'];
412412
offrampRegistryAddress: Address;
413413
offrampFunctionName: 'bumpFeeOfExistingOrder';
414414
/**
@@ -420,7 +420,7 @@ export type OfframpBumpFeeParams = {
420420

421421
/** @dev Params used to unlock funds after order completion or refund */
422422
export type OfframpUnlockFundsParams = {
423-
offrampABI: typeof offrampUnlockFundsCaller;
423+
offrampABI: (typeof offrampCaller)['unlockFunds'];
424424
offrampRegistryAddress: Address;
425425
offrampFunctionName: 'unlockFunds';
426426
/**

sdk/test/gateway.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('Gateway Tests', () => {
3434

3535
it('should reject invalid chain', async () => {
3636
expect(() => {
37+
// @ts-expect-error invalid chain id
3738
new GatewaySDK('bob-testnet');
3839
}).toThrowError('Invalid chain');
3940
});

0 commit comments

Comments
 (0)