Skip to content

Commit

Permalink
feat(PSDK-782): allow users to send sponsored transactions immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
0xRAG committed Jan 9, 2025
1 parent 343e123 commit acd1c55
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/coinbase/address/wallet_address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class WalletAddress extends Address {
* @param options.assetId - The ID of the Asset to send. For Ether, Coinbase.assets.Eth, Coinbase.assets.Gwei, and Coinbase.assets.Wei supported.
* @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.
* @param options.gasless - Whether the Transfer should be gasless. Defaults to false.
* @param options.skipBatching - When true, the Transfer will be submitted immediately. Otherwise, the Transfer will be batched. Defaults to false.
* @returns The transfer object.
* @throws {APIError} if the API request to create a Transfer fails.
* @throws {APIError} if the API request to broadcast a Transfer fails.
Expand All @@ -212,6 +213,7 @@ export class WalletAddress extends Address {
assetId,
destination,
gasless = false,
skipBatching = false,
}: CreateTransferOptions): Promise<Transfer> {
if (!Coinbase.useServerSigner && !this.key) {
throw new Error("Cannot transfer from address without private key loaded");
Expand All @@ -234,6 +236,7 @@ export class WalletAddress extends Address {
asset_id: asset.primaryDenomination(),
destination: destinationAddress,
gasless: gasless,
skip_batching: skipBatching,
};

const response = await Coinbase.apiClients.transfer!.createTransfer(
Expand Down
1 change: 1 addition & 0 deletions src/coinbase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ export type CreateTransferOptions = {
assetId: string;
destination: Destination;
gasless?: boolean;
skipBatching?: boolean;
};

/**
Expand Down
45 changes: 45 additions & 0 deletions src/tests/wallet_address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,51 @@ describe("WalletAddress", () => {
expect(transfer.getId()).toBe(VALID_TRANSFER_MODEL.transfer_id);
});

it("should default skipBatching to false", async () => {
Coinbase.apiClients.transfer!.createTransfer = mockReturnValue(VALID_TRANSFER_MODEL);
Coinbase.apiClients.transfer!.broadcastTransfer = mockReturnValue({
transaction_hash: "0x6c087c1676e8269dd81e0777244584d0cbfd39b6997b3477242a008fa9349e11",
...VALID_TRANSFER_MODEL,
});

await address.createTransfer({
amount: weiAmount,
assetId: Coinbase.assets.Wei,
destination,
});

expect(Coinbase.apiClients.transfer!.createTransfer).toHaveBeenCalledWith(
address.getWalletId(),
address.getId(),
expect.objectContaining({
skip_batching: false,
}),
);
});

it("should allow skipBatching to be set to true", async () => {
Coinbase.apiClients.transfer!.createTransfer = mockReturnValue(VALID_TRANSFER_MODEL);
Coinbase.apiClients.transfer!.broadcastTransfer = mockReturnValue({
transaction_hash: "0x6c087c1676e8269dd81e0777244584d0cbfd39b6997b3477242a008fa9349e11",
...VALID_TRANSFER_MODEL,
});

await address.createTransfer({
amount: weiAmount,
assetId: Coinbase.assets.Wei,
destination,
skipBatching: true,
});

expect(Coinbase.apiClients.transfer!.createTransfer).toHaveBeenCalledWith(
address.getWalletId(),
address.getId(),
expect.objectContaining({
skip_batching: true,
}),
);
});

it("should successfully construct createTransfer request when using a large number that causes scientific notation", async () => {
Coinbase.apiClients.transfer!.createTransfer = mockReturnValue(VALID_TRANSFER_MODEL);
Coinbase.apiClients.transfer!.broadcastTransfer = mockReturnValue({
Expand Down

0 comments on commit acd1c55

Please sign in to comment.