Skip to content

Commit 460e725

Browse files
[chore] CreateTransferOptions type for createtransfer (#77)
1 parent 97f0fcd commit 460e725

File tree

9 files changed

+148
-129
lines changed

9 files changed

+148
-129
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Coinbase Node.js SDK Changelog
22

3+
## Pending
4+
5+
### Added
6+
7+
- `CreateTransferOptions` type
8+
39
## [0.0.8] - 2024-06-18
410

511
### Added

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ console.log(`Faucet transaction: ${faucetTransaction}`);
145145
// Create a new Wallet to transfer funds to.
146146
// Then, we can transfer 0.00001 ETH out of the Wallet to another Wallet.
147147
const anotherWallet = await user.createWallet();
148-
const transfer = await wallet.createTransfer(0.00001, Coinbase.assets.Eth, anotherWallet);
148+
const transfer = await wallet.createTransfer({ amount: 0.00001, assetId: Coinbase.assets.Eth, destination: anotherWallet });
149149
```
150150

151151

src/coinbase/address/wallet_address.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ArgumentError, InternalError } from "../errors";
1010
import { FaucetTransaction } from "../faucet_transaction";
1111
import { Trade } from "../trade";
1212
import { Transfer } from "../transfer";
13-
import { Amount, Destination, TransferStatus } from "../types";
13+
import { Amount, Destination, TransferStatus, CreateTransferOptions } from "../types";
1414
import { delay } from "../utils";
1515
import { Wallet as WalletClass } from "../wallet";
1616

@@ -154,23 +154,24 @@ export class WalletAddress extends Address {
154154
/**
155155
* Transfers the given amount of the given Asset to the given address. Only same-Network Transfers are supported.
156156
*
157-
* @param amount - The amount of the Asset to send.
158-
* @param assetId - The ID of the Asset to send. For Ether, Coinbase.assets.Eth, Coinbase.assets.Gwei, and Coinbase.assets.Wei supported.
159-
* @param destination - The destination of the transfer. If a Wallet, sends to the Wallet's default address. If a String, interprets it as the address ID.
160-
* @param intervalSeconds - The interval at which to poll the Network for Transfer status, in seconds.
161-
* @param timeoutSeconds - The maximum amount of time to wait for the Transfer to complete, in seconds.
157+
* @param options - The options to create the Transfer.
158+
* @param options.amount - The amount of the Asset to send.
159+
* @param options.assetId - The ID of the Asset to send. For Ether, Coinbase.assets.Eth, Coinbase.assets.Gwei, and Coinbase.assets.Wei supported.
160+
* @param options.destination - The destination of the transfer. If a Wallet, sends to the Wallet's default address. If a String, interprets it as the address ID.
161+
* @param options.timeoutSeconds - The maximum amount of time to wait for the Transfer to complete, in seconds.
162+
* @param options.intervalSeconds - The interval at which to poll the Network for Transfer status, in seconds.
162163
* @returns The transfer object.
163164
* @throws {APIError} if the API request to create a Transfer fails.
164165
* @throws {APIError} if the API request to broadcast a Transfer fails.
165166
* @throws {Error} if the Transfer times out.
166167
*/
167-
public async createTransfer(
168-
amount: Amount,
169-
assetId: string,
170-
destination: Destination,
171-
intervalSeconds = 0.2,
168+
public async createTransfer({
169+
amount,
170+
assetId,
171+
destination,
172172
timeoutSeconds = 10,
173-
): Promise<Transfer> {
173+
intervalSeconds = 0.2,
174+
}: CreateTransferOptions): Promise<Transfer> {
174175
if (!Coinbase.useServerSigner && !this.key) {
175176
throw new InternalError("Cannot transfer from address without private key loaded");
176177
}

src/coinbase/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ export enum ServerSignerStatus {
561561
*/
562562
export type WalletCreateOptions = {
563563
networkId?: string;
564-
intervalSeconds?: number;
565564
timeoutSeconds?: number;
565+
intervalSeconds?: number;
566566
};
567567

568568
/**
@@ -649,3 +649,14 @@ export enum StakeOptionsMode {
649649
*/
650650
PARTIAL = "partial",
651651
}
652+
653+
/**
654+
* Options for creating a Transfer.
655+
*/
656+
export type CreateTransferOptions = {
657+
amount: Amount;
658+
assetId: string;
659+
destination: Destination;
660+
timeoutSeconds?: number;
661+
intervalSeconds?: number;
662+
};

src/coinbase/wallet.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { ArgumentError, InternalError } from "./errors";
1111
import { Transfer } from "./transfer";
1212
import {
1313
Amount,
14-
Destination,
1514
SeedData,
1615
WalletData,
1716
ServerSignerStatus,
1817
WalletCreateOptions,
18+
CreateTransferOptions,
1919
} from "./types";
2020
import { convertStringToHex, delay } from "./utils";
2121
import { FaucetTransaction } from "./faucet_transaction";
@@ -81,8 +81,8 @@ export class Wallet {
8181
*/
8282
public static async create({
8383
networkId = Coinbase.networks.BaseSepolia,
84-
intervalSeconds = 0.2,
8584
timeoutSeconds = 20,
85+
intervalSeconds = 0.2,
8686
}: WalletCreateOptions = {}): Promise<Wallet> {
8787
const result = await Coinbase.apiClients.wallet!.createWallet({
8888
wallet: {
@@ -582,33 +582,34 @@ export class Wallet {
582582
* Transfers the given amount of the given Asset to the given address. Only same-Network Transfers are supported.
583583
* Currently only the default_address is used to source the Transfer.
584584
*
585-
* @param amount - The amount of the Asset to send.
586-
* @param assetId - The ID of the Asset to send.
587-
* @param destination - The destination of the transfer. If a Wallet, sends to the Wallet's default address. If a String, interprets it as the address ID.
588-
* @param intervalSeconds - The interval at which to poll the Network for Transfer status, in seconds.
589-
* @param timeoutSeconds - The maximum amount of time to wait for the Transfer to complete, in seconds.
585+
* @param options - The options to create the Transfer.
586+
* @param options.amount - The amount of the Asset to send.
587+
* @param options.assetId - The ID of the Asset to send.
588+
* @param options.destination - The destination of the transfer. If a Wallet, sends to the Wallet's default address. If a String, interprets it as the address ID.
589+
* @param options.timeoutSeconds - The maximum amount of time to wait for the Transfer to complete, in seconds.
590+
* @param options.intervalSeconds - The interval at which to poll the Network for Transfer status, in seconds.
590591
* @returns The hash of the Transfer transaction.
591592
* @throws {APIError} if the API request to create a Transfer fails.
592593
* @throws {APIError} if the API request to broadcast a Transfer fails.
593594
* @throws {Error} if the Transfer times out.
594595
*/
595-
public async createTransfer(
596-
amount: Amount,
597-
assetId: string,
598-
destination: Destination,
599-
intervalSeconds = 0.2,
596+
public async createTransfer({
597+
amount,
598+
assetId,
599+
destination,
600600
timeoutSeconds = 10,
601-
): Promise<Transfer> {
601+
intervalSeconds = 0.2,
602+
}: CreateTransferOptions): Promise<Transfer> {
602603
if (!this.getDefaultAddress()) {
603604
throw new InternalError("Default address not found");
604605
}
605-
return await this.getDefaultAddress()!.createTransfer(
606+
return await this.getDefaultAddress()!.createTransfer({
606607
amount,
607608
assetId,
608609
destination,
609-
intervalSeconds,
610610
timeoutSeconds,
611-
);
611+
intervalSeconds,
612+
});
612613
}
613614

614615
/**

src/tests/e2e.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ describe("Coinbase SDK E2E Test", () => {
8686
expect(unhydratedWallet.getId()).toBe(walletId);
8787

8888
console.log("Transfering 0.000000001 ETH from default address to second address...");
89-
const transfer = await unhydratedWallet.createTransfer(
90-
0.000000001,
91-
Coinbase.assets.Eth,
92-
wallet,
93-
);
89+
const transfer = await unhydratedWallet.createTransfer({
90+
amount: 0.000000001,
91+
assetId: Coinbase.assets.Eth,
92+
destination: wallet,
93+
});
9494
expect(await transfer.getStatus()).toBe(TransferStatus.COMPLETE);
9595
console.log(`Transferred 1 Gwei from ${unhydratedWallet} to ${wallet}`);
9696

src/tests/user_test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ describe("User Class", () => {
232232
);
233233
await expect(unhydratedWallet.createAddress()).rejects.toThrow(InternalError);
234234
await expect(
235-
unhydratedWallet.createTransfer(
236-
new Decimal("500000000000000000"),
237-
Coinbase.assets.Eth,
238-
address1,
239-
),
235+
unhydratedWallet.createTransfer({
236+
amount: new Decimal("500000000000000000"),
237+
assetId: Coinbase.assets.Eth,
238+
destination: address1,
239+
}),
240240
).rejects.toThrow(InternalError);
241241
expect(unhydratedWallet.canSign()).toBe(false);
242242
});

src/tests/wallet_address_test.ts

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ describe("WalletAddress", () => {
202202
);
203203
});
204204

205-
describe(".createTransfer", () => {
205+
describe("#createTransfer", () => {
206206
let weiAmount, destination, intervalSeconds, timeoutSeconds;
207207
let walletId, id;
208208

@@ -243,13 +243,13 @@ describe("WalletAddress", () => {
243243
status: TransferStatus.COMPLETE,
244244
});
245245

246-
await address.createTransfer(
247-
weiAmount,
248-
Coinbase.assets.Wei,
246+
await address.createTransfer({
247+
amount: weiAmount,
248+
assetId: Coinbase.assets.Wei,
249249
destination,
250-
intervalSeconds,
251250
timeoutSeconds,
252-
);
251+
intervalSeconds,
252+
});
253253

254254
expect(Coinbase.apiClients.transfer!.createTransfer).toHaveBeenCalledTimes(1);
255255
expect(Coinbase.apiClients.transfer!.broadcastTransfer).toHaveBeenCalledTimes(1);
@@ -261,26 +261,26 @@ describe("WalletAddress", () => {
261261
new APIError("Failed to create transfer"),
262262
);
263263
await expect(
264-
address.createTransfer(
265-
weiAmount,
266-
Coinbase.assets.Wei,
264+
address.createTransfer({
265+
amount: weiAmount,
266+
assetId: Coinbase.assets.Wei,
267267
destination,
268-
intervalSeconds,
269268
timeoutSeconds,
270-
),
269+
intervalSeconds,
270+
}),
271271
).rejects.toThrow(APIError);
272272
});
273273

274274
it("should throw an InternalError if the address key is not provided", async () => {
275275
const addressWithoutKey = new WalletAddress(VALID_ADDRESS_MODEL, null!);
276276
await expect(
277-
addressWithoutKey.createTransfer(
278-
weiAmount,
279-
Coinbase.assets.Wei,
277+
addressWithoutKey.createTransfer({
278+
amount: weiAmount,
279+
assetId: Coinbase.assets.Wei,
280280
destination,
281-
intervalSeconds,
282281
timeoutSeconds,
283-
),
282+
intervalSeconds,
283+
}),
284284
).rejects.toThrow(InternalError);
285285
});
286286

@@ -302,13 +302,13 @@ describe("WalletAddress", () => {
302302
networkId: Coinbase.networks.BaseMainnet,
303303
});
304304
await expect(
305-
address.createTransfer(
306-
weiAmount,
307-
Coinbase.assets.Wei,
308-
invalidDestination,
309-
intervalSeconds,
305+
address.createTransfer({
306+
amount: weiAmount,
307+
assetId: Coinbase.assets.Wei,
308+
destination: invalidDestination,
310309
timeoutSeconds,
311-
),
310+
intervalSeconds,
311+
}),
312312
).rejects.toThrow(ArgumentError);
313313
});
314314

@@ -318,13 +318,13 @@ describe("WalletAddress", () => {
318318
null!,
319319
);
320320
await expect(
321-
address.createTransfer(
322-
weiAmount,
323-
Coinbase.assets.Wei,
324-
invalidDestination,
325-
intervalSeconds,
321+
address.createTransfer({
322+
amount: weiAmount,
323+
assetId: Coinbase.assets.Wei,
324+
destination: invalidDestination,
326325
timeoutSeconds,
327-
),
326+
intervalSeconds,
327+
}),
328328
).rejects.toThrow(ArgumentError);
329329
});
330330

@@ -334,13 +334,13 @@ describe("WalletAddress", () => {
334334
new APIError("Failed to broadcast transfer"),
335335
);
336336
await expect(
337-
address.createTransfer(
338-
weiAmount,
339-
Coinbase.assets.Wei,
337+
address.createTransfer({
338+
amount: weiAmount,
339+
assetId: Coinbase.assets.Wei,
340340
destination,
341-
intervalSeconds,
342341
timeoutSeconds,
343-
),
342+
intervalSeconds,
343+
}),
344344
).rejects.toThrow(APIError);
345345
});
346346

@@ -358,26 +358,26 @@ describe("WalletAddress", () => {
358358
timeoutSeconds = 0.000002;
359359

360360
await expect(
361-
address.createTransfer(
362-
weiAmount,
363-
Coinbase.assets.Wei,
361+
address.createTransfer({
362+
amount: weiAmount,
363+
assetId: Coinbase.assets.Wei,
364364
destination,
365-
intervalSeconds,
366365
timeoutSeconds,
367-
),
366+
intervalSeconds,
367+
}),
368368
).rejects.toThrow("Transfer timed out");
369369
});
370370

371371
it("should throw an ArgumentError if there are insufficient funds", async () => {
372372
const insufficientAmount = new Decimal("10000000000000000000");
373373
await expect(
374-
address.createTransfer(
375-
insufficientAmount,
376-
Coinbase.assets.Wei,
374+
address.createTransfer({
375+
amount: insufficientAmount,
376+
assetId: Coinbase.assets.Wei,
377377
destination,
378-
intervalSeconds,
379378
timeoutSeconds,
380-
),
379+
intervalSeconds,
380+
}),
381381
).rejects.toThrow(ArgumentError);
382382
});
383383

@@ -389,13 +389,13 @@ describe("WalletAddress", () => {
389389
status: TransferStatus.COMPLETE,
390390
});
391391

392-
await address.createTransfer(
393-
weiAmount,
394-
Coinbase.assets.Wei,
392+
await address.createTransfer({
393+
amount: weiAmount,
394+
assetId: Coinbase.assets.Wei,
395395
destination,
396-
intervalSeconds,
397396
timeoutSeconds,
398-
);
397+
intervalSeconds,
398+
});
399399

400400
expect(Coinbase.apiClients.transfer!.createTransfer).toHaveBeenCalledTimes(1);
401401
expect(Coinbase.apiClients.transfer!.getTransfer).toHaveBeenCalledTimes(1);
@@ -406,7 +406,7 @@ describe("WalletAddress", () => {
406406
});
407407
});
408408

409-
describe(".getTransfers", () => {
409+
describe("#getTransfers", () => {
410410
beforeEach(() => {
411411
jest.clearAllMocks();
412412
const pages = ["abc", "def"];

0 commit comments

Comments
 (0)