Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable wallet activity events support for webhooks at wallet level #245

Merged
merged 45 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c1dd4a4
add eventTypeFilter
howard-at-cb Sep 17, 2024
a8cd844
Update types.ts
howard-at-cb Sep 17, 2024
51dff2a
Update webhook_test.ts
howard-at-cb Sep 17, 2024
539a84f
Update webhook.ts
howard-at-cb Sep 17, 2024
4b049c0
Update webhook_test.ts
howard-at-cb Sep 17, 2024
d154e96
Update webhook.ts
howard-at-cb Sep 17, 2024
f78922d
Update webhook_test.ts
howard-at-cb Sep 17, 2024
d97b7f1
Update webhook_test.ts
howard-at-cb Sep 17, 2024
18c8526
Update webhook_test.ts
howard-at-cb Sep 17, 2024
da1ca8b
Update webhook_test.ts
howard-at-cb Sep 17, 2024
068f805
Update webhook_test.ts
howard-at-cb Sep 17, 2024
c7df99b
wallet.create_webhook
howard-at-cb Sep 18, 2024
6c57b85
Changelog
rohan-agarwal-coinbase Sep 17, 2024
6905296
Update wallet.ts
howard-at-cb Sep 18, 2024
97e2f8b
Update wallet.ts
howard-at-cb Sep 18, 2024
1ce5232
Update wallet_test.ts
howard-at-cb Sep 18, 2024
024ae12
Update wallet_test.ts
howard-at-cb Sep 18, 2024
e631fe4
Update wallet_test.ts
howard-at-cb Sep 18, 2024
f1dca6f
Update wallet_test.ts
howard-at-cb Sep 18, 2024
03a6252
Update wallet_test.ts
howard-at-cb Sep 18, 2024
c16fa08
fmt
howard-at-cb Sep 18, 2024
aad6998
Update wallet_test.ts
howard-at-cb Sep 18, 2024
d283acb
Update wallet_test.ts
howard-at-cb Sep 18, 2024
4dee149
Update webhook.ts
howard-at-cb Sep 18, 2024
4dc95e8
Update wallet_test.ts
howard-at-cb Sep 18, 2024
8644ed3
Update wallet_test.ts
howard-at-cb Sep 18, 2024
9e86ce9
address comments
howard-at-cb Sep 18, 2024
aecc257
fix test
howard-at-cb Sep 18, 2024
a7fbf00
Update wallet_test.ts
howard-at-cb Sep 18, 2024
5576e44
Update wallet_test.ts
howard-at-cb Sep 18, 2024
1c46dd8
Update wallet_test.ts
howard-at-cb Sep 18, 2024
b14688c
Update wallet_test.ts
howard-at-cb Sep 18, 2024
9d1274b
Update jest.config.js
howard-at-cb Sep 18, 2024
15fa739
add sol, lamport asset suppport
drohit-cb Sep 18, 2024
14eb52f
Fix Asset toAtomicAmount to not return scientific notation (#250)
rohan-agarwal-coinbase Sep 18, 2024
43a0c36
Fix merge conflicts between master and v0.6.0 (#255)
rohan-agarwal-coinbase Sep 18, 2024
8cdfa92
address comments
howard-at-cb Sep 19, 2024
522e499
Update api.ts
howard-at-cb Sep 24, 2024
ec8a3a1
Update CHANGELOG.md
howard-at-cb Sep 24, 2024
b7ac866
apply new changes
howard-at-cb Sep 24, 2024
a6885ff
fix tests
howard-at-cb Sep 24, 2024
1910a8e
Update wallet.ts
howard-at-cb Sep 24, 2024
d9e9c77
Update CHANGELOG.md
howard-at-cb Sep 24, 2024
393e648
Update CHANGELOG.md
howard-at-cb Sep 25, 2024
b3fe7a1
Update jest.config.js
howard-at-cb Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## Unreleased

## [0.7.0] - 2024-09-24
### Added
- Add `deployNFT` method to `WalletAddress` and `Wallet` to deploy an ERC721, updated `SmartContract` class to support deployment and fetching contract details
- Add `deployMultiToken` method to `WalletAddress` and `Wallet` to deploy an ERC1155, updated `SmartContract` class to support deployment and fetching contract details
- Add `createWebhook` method to `Wallet` to deploy a wallet activity webhook, updated `Webhook` class to disallow users from specifying webhook signature. Webhook signature is now generated by the API.

## [0.6.1] - 2024-09-23

Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module.exports = {
coverageThreshold: {
"./src/coinbase/**": {
branches: 77,
functions: 90,
functions: 85,
statements: 85,
lines: 87,
lines: 85,
},
},
};
106 changes: 106 additions & 0 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,25 @@ export interface CreateWalletRequestWallet {
*/
'use_server_signer'?: boolean;
}
/**
*
* @export
* @interface CreateWalletWebhookRequest
*/
export interface CreateWalletWebhookRequest {
/**
* The URL to which the notifications will be sent
* @type {string}
* @memberof CreateWalletWebhookRequest
*/
'notification_uri': string;
/**
* The custom header to be used for x-webhook-signature header on callbacks, so developers can verify the requests are coming from Coinbase.
* @type {string}
* @memberof CreateWalletWebhookRequest
*/
'signature_header'?: string;
}
/**
*
* @export
Expand Down Expand Up @@ -9288,6 +9307,44 @@ export class WalletsApi extends BaseAPI implements WalletsApiInterface {
*/
export const WebhooksApiAxiosParamCreator = function (configuration?: Configuration) {
return {
/**
* Create a new webhook scoped to a wallet
* @summary Create a new webhook scoped to a wallet
* @param {string} walletId The ID of the wallet to create the webhook for.
* @param {CreateWalletWebhookRequest} [createWalletWebhookRequest]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
createWalletWebhook: async (walletId: string, createWalletWebhookRequest?: CreateWalletWebhookRequest, options: RawAxiosRequestConfig = {}): Promise<RequestArgs> => {
// verify required parameter 'walletId' is not null or undefined
assertParamExists('createWalletWebhook', 'walletId', walletId)
const localVarPath = `/v1/wallets/{wallet_id}/webhooks`
.replace(`{${"wallet_id"}}`, encodeURIComponent(String(walletId)));
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;



localVarHeaderParameter['Content-Type'] = 'application/json';

setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
localVarRequestOptions.data = serializeDataIfNeeded(createWalletWebhookRequest, localVarRequestOptions, configuration)

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
* Create a new webhook
* @summary Create a new webhook
Expand Down Expand Up @@ -9444,6 +9501,20 @@ export const WebhooksApiAxiosParamCreator = function (configuration?: Configurat
export const WebhooksApiFp = function(configuration?: Configuration) {
const localVarAxiosParamCreator = WebhooksApiAxiosParamCreator(configuration)
return {
/**
* Create a new webhook scoped to a wallet
* @summary Create a new webhook scoped to a wallet
* @param {string} walletId The ID of the wallet to create the webhook for.
* @param {CreateWalletWebhookRequest} [createWalletWebhookRequest]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async createWalletWebhook(walletId: string, createWalletWebhookRequest?: CreateWalletWebhookRequest, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<Webhook>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.createWalletWebhook(walletId, createWalletWebhookRequest, options);
const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
const localVarOperationServerBasePath = operationServerMap['WebhooksApi.createWalletWebhook']?.[localVarOperationServerIndex]?.url;
return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, localVarOperationServerBasePath || basePath);
},
/**
* Create a new webhook
* @summary Create a new webhook
Expand Down Expand Up @@ -9508,6 +9579,17 @@ export const WebhooksApiFp = function(configuration?: Configuration) {
export const WebhooksApiFactory = function (configuration?: Configuration, basePath?: string, axios?: AxiosInstance) {
const localVarFp = WebhooksApiFp(configuration)
return {
/**
* Create a new webhook scoped to a wallet
* @summary Create a new webhook scoped to a wallet
* @param {string} walletId The ID of the wallet to create the webhook for.
* @param {CreateWalletWebhookRequest} [createWalletWebhookRequest]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
createWalletWebhook(walletId: string, createWalletWebhookRequest?: CreateWalletWebhookRequest, options?: RawAxiosRequestConfig): AxiosPromise<Webhook> {
return localVarFp.createWalletWebhook(walletId, createWalletWebhookRequest, options).then((request) => request(axios, basePath));
},
/**
* Create a new webhook
* @summary Create a new webhook
Expand Down Expand Up @@ -9559,6 +9641,17 @@ export const WebhooksApiFactory = function (configuration?: Configuration, baseP
* @interface WebhooksApi
*/
export interface WebhooksApiInterface {
/**
* Create a new webhook scoped to a wallet
* @summary Create a new webhook scoped to a wallet
* @param {string} walletId The ID of the wallet to create the webhook for.
* @param {CreateWalletWebhookRequest} [createWalletWebhookRequest]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof WebhooksApiInterface
*/
createWalletWebhook(walletId: string, createWalletWebhookRequest?: CreateWalletWebhookRequest, options?: RawAxiosRequestConfig): AxiosPromise<Webhook>;

/**
* Create a new webhook
* @summary Create a new webhook
Expand Down Expand Up @@ -9610,6 +9703,19 @@ export interface WebhooksApiInterface {
* @extends {BaseAPI}
*/
export class WebhooksApi extends BaseAPI implements WebhooksApiInterface {
/**
* Create a new webhook scoped to a wallet
* @summary Create a new webhook scoped to a wallet
* @param {string} walletId The ID of the wallet to create the webhook for.
* @param {CreateWalletWebhookRequest} [createWalletWebhookRequest]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof WebhooksApi
*/
public createWalletWebhook(walletId: string, createWalletWebhookRequest?: CreateWalletWebhookRequest, options?: RawAxiosRequestConfig) {
return WebhooksApiFp(this.configuration).createWalletWebhook(walletId, createWalletWebhookRequest, options).then((request) => request(this.axios, this.basePath));
}

/**
* Create a new webhook
* @summary Create a new webhook
Expand Down
18 changes: 18 additions & 0 deletions src/coinbase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import {
CreateSmartContractRequest,
SmartContract as SmartContractModel,
DeploySmartContractRequest,
WebhookEventTypeFilter,
CreateWalletWebhookRequest,
} from "./../client/api";
import { Address } from "./address";
import { Wallet } from "./wallet";
Expand Down Expand Up @@ -1038,6 +1040,21 @@ export type ListHistoricalBalancesResult = {
};

export interface WebhookApiClient {
/**
* Create a new webhook for a wallet
*
* @summary Create a new webhook for a wallet
* @param {string} [walletId]
* @param {CreateWalletWebhookRequest} [createWalletWebhookRequest]
* @param {*} [options] - Override http request option.
* @throws {RequiredError}
*/
createWalletWebhook(
walletId?: string,
createWalletWebhookRequest?: CreateWalletWebhookRequest,
options?: RawAxiosRequestConfig,
): AxiosPromise<WebhookModel>;

/**
* Create a new webhook
*
Expand Down Expand Up @@ -1187,6 +1204,7 @@ export type CreateWebhookOptions = {
networkId: string;
notificationUri: string;
eventType: WebhookEventType;
eventTypeFilter?: WebhookEventTypeFilter;
eventFilters?: Array<WebhookEventFilter>;
signatureHeader?: string;
};
Expand Down
16 changes: 16 additions & 0 deletions src/coinbase/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { StakingBalance } from "./staking_balance";
import { PayloadSignature } from "./payload_signature";
import { ContractInvocation } from "../coinbase/contract_invocation";
import { SmartContract } from "./smart_contract";
import { Webhook } from "./webhook";

/**
* A representation of a Wallet. Wallets come with a single default Address, but can expand to have a set of Addresses,
Expand Down Expand Up @@ -750,6 +751,21 @@ export class Wallet {
return (await this.getDefaultAddress()).createPayloadSignature(unsignedPayload);
}

/**
* Creates a Webhook for a wallet, monitors all wallet addresses for onchain events.
*
* @param notificationUri - The URI to which the webhook notifications will be sent.
*
* @returns The newly created webhook instance.
*/
public async createWebhook(notificationUri: string): Promise<Webhook> {
const result = await Coinbase.apiClients.webhook!.createWalletWebhook(this.getId(), {
notification_uri: notificationUri,
});

return Webhook.init(result.data);
}

/**
* Invokes a contract with the given data.
*
Expand Down
24 changes: 19 additions & 5 deletions src/coinbase/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Webhook as WebhookModel, WebhookEventType, WebhookEventFilter } from "../client/api";
import {
Webhook as WebhookModel,
WebhookEventType,
WebhookEventFilter,
WebhookEventTypeFilter,
} from "../client/api";
import { Coinbase } from "./coinbase";
import { CreateWebhookOptions } from "./types";

Expand Down Expand Up @@ -40,24 +45,23 @@ export class Webhook {
* @param options.networkId - The network ID for which the webhook is created.
* @param options.notificationUri - The URI where notifications should be sent.
* @param options.eventType - The type of event for the webhook.
* @param options.eventTypeFilter - Filter for wallet activity event type.
* @param options.eventFilters - Filters applied to the events that determine which specific events trigger the webhook.
* @param options.signatureHeader - The custom header to be used for x-webhook-signature header on callbacks,
* so developers can verify the requests are coming from Coinbase.
* @returns A promise that resolves to a new instance of Webhook.
*/
public static async create({
networkId,
notificationUri,
eventType,
eventTypeFilter,
eventFilters = [],
signatureHeader = "",
}: CreateWebhookOptions): Promise<Webhook> {
const result = await Coinbase.apiClients.webhook!.createWebhook({
network_id: networkId,
notification_uri: notificationUri,
event_type: eventType,
event_type_filter: eventTypeFilter,
event_filters: eventFilters,
howard-at-cb marked this conversation as resolved.
Show resolved Hide resolved
signature_header: signatureHeader,
});

return new Webhook(result.data);
Expand Down Expand Up @@ -131,6 +135,15 @@ export class Webhook {
return this.model?.event_type;
}

/**
* Returns the event type filter of the webhook.
*
* @returns The filter which will be used to filter for events of a certain event type
*/
public getEventTypeFilter(): WebhookEventTypeFilter | undefined {
return this.model?.event_type_filter;
}

/**
* Returns the event filters applied to the webhook.
*
Expand Down Expand Up @@ -186,6 +199,7 @@ export class Webhook {
return (
`Webhook { id: '${this.getId()}', networkId: '${this.getNetworkId()}', ` +
`eventType: '${this.getEventType()}', eventFilter: ${JSON.stringify(this.getEventFilters())}, ` +
`eventTypeFilter: ${JSON.stringify(this.getEventTypeFilter())}, ` +
`notificationUri: '${this.getNotificationURI()}', signatureHeader: '${this.getSignatureHeader()}' }`
);
}
Expand Down
Loading
Loading