From 2261d8443163d98ead136ef614d7cb34b432d6df Mon Sep 17 00:00:00 2001 From: Chaoya Ji Date: Mon, 16 Sep 2024 12:20:04 -0700 Subject: [PATCH 1/8] port gen --- lib/coinbase/client/models/webhook.rb | 11 +- .../client/models/webhook_event_type.rb | 3 +- .../models/webhook_event_type_filter.rb | 105 ++++++++ .../models/webhook_wallet_activity_filter.rb | 228 ++++++++++++++++++ 4 files changed, 345 insertions(+), 2 deletions(-) create mode 100644 lib/coinbase/client/models/webhook_event_type_filter.rb create mode 100644 lib/coinbase/client/models/webhook_wallet_activity_filter.rb diff --git a/lib/coinbase/client/models/webhook.rb b/lib/coinbase/client/models/webhook.rb index 061928d4..d7e47de9 100644 --- a/lib/coinbase/client/models/webhook.rb +++ b/lib/coinbase/client/models/webhook.rb @@ -24,6 +24,8 @@ class Webhook attr_accessor :event_type + attr_accessor :event_type_filter + # Webhook will monitor all events that matches any one of the event filters. attr_accessor :event_filters @@ -67,6 +69,7 @@ def self.attribute_map :'id' => :'id', :'network_id' => :'network_id', :'event_type' => :'event_type', + :'event_type_filter' => :'event_type_filter', :'event_filters' => :'event_filters', :'notification_uri' => :'notification_uri', :'created_at' => :'created_at', @@ -86,6 +89,7 @@ def self.openapi_types :'id' => :'String', :'network_id' => :'String', :'event_type' => :'WebhookEventType', + :'event_type_filter' => :'WebhookEventTypeFilter', :'event_filters' => :'Array', :'notification_uri' => :'String', :'created_at' => :'Time', @@ -127,6 +131,10 @@ def initialize(attributes = {}) self.event_type = attributes[:'event_type'] end + if attributes.key?(:'event_type_filter') + self.event_type_filter = attributes[:'event_type_filter'] + end + if attributes.key?(:'event_filters') if (value = attributes[:'event_filters']).is_a?(Array) self.event_filters = value @@ -173,6 +181,7 @@ def ==(o) id == o.id && network_id == o.network_id && event_type == o.event_type && + event_type_filter == o.event_type_filter && event_filters == o.event_filters && notification_uri == o.notification_uri && created_at == o.created_at && @@ -189,7 +198,7 @@ def eql?(o) # Calculates hash code according to all attributes. # @return [Integer] Hash code def hash - [id, network_id, event_type, event_filters, notification_uri, created_at, updated_at, signature_header].hash + [id, network_id, event_type, event_type_filter, event_filters, notification_uri, created_at, updated_at, signature_header].hash end # Builds the object from hash diff --git a/lib/coinbase/client/models/webhook_event_type.rb b/lib/coinbase/client/models/webhook_event_type.rb index 0ca06bb5..f3a6e1e2 100644 --- a/lib/coinbase/client/models/webhook_event_type.rb +++ b/lib/coinbase/client/models/webhook_event_type.rb @@ -18,10 +18,11 @@ class WebhookEventType UNSPECIFIED = "unspecified".freeze ERC20_TRANSFER = "erc20_transfer".freeze ERC721_TRANSFER = "erc721_transfer".freeze + WALLET_ACTIVITY = "wallet_activity".freeze UNKNOWN_DEFAULT_OPEN_API = "unknown_default_open_api".freeze def self.all_vars - @all_vars ||= [UNSPECIFIED, ERC20_TRANSFER, ERC721_TRANSFER, UNKNOWN_DEFAULT_OPEN_API].freeze + @all_vars ||= [UNSPECIFIED, ERC20_TRANSFER, ERC721_TRANSFER, WALLET_ACTIVITY, UNKNOWN_DEFAULT_OPEN_API].freeze end # Builds the enum from string diff --git a/lib/coinbase/client/models/webhook_event_type_filter.rb b/lib/coinbase/client/models/webhook_event_type_filter.rb new file mode 100644 index 00000000..b4990f60 --- /dev/null +++ b/lib/coinbase/client/models/webhook_event_type_filter.rb @@ -0,0 +1,105 @@ +=begin +#Coinbase Platform API + +#This is the OpenAPI 3.0 specification for the Coinbase Platform APIs, used in conjunction with the Coinbase Platform SDKs. + +The version of the OpenAPI document: 0.0.1-alpha + +Generated by: https://openapi-generator.tech +Generator version: 7.8.0 + +=end + +require 'date' +require 'time' + +module Coinbase::Client + # The event_type_filter parameter specifies the criteria to filter events based on event type. + module WebhookEventTypeFilter + class << self + # List of class defined in oneOf (OpenAPI v3) + def openapi_one_of + [ + :'WebhookWalletActivityFilter' + ] + end + + # Builds the object + # @param [Mixed] Data to be matched against the list of oneOf items + # @return [Object] Returns the model or the data itself + def build(data) + # Go through the list of oneOf items and attempt to identify the appropriate one. + # Note: + # - We do not attempt to check whether exactly one item matches. + # - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 }) + # due to the way the deserialization is made in the base_object template (it just casts without verifying). + # - TODO: scalar values are de facto behaving as if they were nullable. + # - TODO: logging when debugging is set. + openapi_one_of.each do |klass| + begin + next if klass == :AnyType # "nullable: true" + typed_data = find_and_cast_into_type(klass, data) + return typed_data if typed_data + rescue # rescue all errors so we keep iterating even if the current item lookup raises + end + end + + openapi_one_of.include?(:AnyType) ? data : nil + end + + private + + SchemaMismatchError = Class.new(StandardError) + + # Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse. + def find_and_cast_into_type(klass, data) + return if data.nil? + + case klass.to_s + when 'Boolean' + return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass) + when 'Float' + return data if data.instance_of?(Float) + when 'Integer' + return data if data.instance_of?(Integer) + when 'Time' + return Time.parse(data) + when 'Date' + return Date.parse(data) + when 'String' + return data if data.instance_of?(String) + when 'Object' # "type: object" + return data if data.instance_of?(Hash) + when /\AArray<(?.+)>\z/ # "type: array" + if data.instance_of?(Array) + sub_type = Regexp.last_match[:sub_type] + return data.map { |item| find_and_cast_into_type(sub_type, item) } + end + when /\AHash.+)>\z/ # "type: object" with "additionalProperties: { ... }" + if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) } + sub_type = Regexp.last_match[:sub_type] + return data.each_with_object({}) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) } + end + else # model + const = Coinbase::Client.const_get(klass) + if const + if const.respond_to?(:openapi_one_of) # nested oneOf model + model = const.build(data) + return model if model + else + # raise if data contains keys that are not known to the model + raise if const.respond_to?(:acceptable_attributes) && !(data.keys - const.acceptable_attributes).empty? + model = const.build_from_hash(data) + return model if model + end + end + end + + raise # if no match by now, raise + rescue + raise SchemaMismatchError, "#{data} doesn't match the #{klass} type" + end + end + end + +end diff --git a/lib/coinbase/client/models/webhook_wallet_activity_filter.rb b/lib/coinbase/client/models/webhook_wallet_activity_filter.rb new file mode 100644 index 00000000..9448e09b --- /dev/null +++ b/lib/coinbase/client/models/webhook_wallet_activity_filter.rb @@ -0,0 +1,228 @@ +=begin +#Coinbase Platform API + +#This is the OpenAPI 3.0 specification for the Coinbase Platform APIs, used in conjunction with the Coinbase Platform SDKs. + +The version of the OpenAPI document: 0.0.1-alpha + +Generated by: https://openapi-generator.tech +Generator version: 7.8.0 + +=end + +require 'date' +require 'time' + +module Coinbase::Client + # Filter for wallet activity events. This filter allows the client to specify one or more wallet addresses to monitor for activities such as transactions, transfers, or other types of events that are associated with the specified addresses. + class WebhookWalletActivityFilter + # A list of wallet addresses to filter on. + attr_accessor :addresses + + # The ID of the wallet that owns the webhook. + attr_accessor :wallet_id + + # Attribute mapping from ruby-style variable name to JSON key. + def self.attribute_map + { + :'addresses' => :'addresses', + :'wallet_id' => :'wallet_id' + } + end + + # Returns all the JSON keys this model knows about + def self.acceptable_attributes + attribute_map.values + end + + # Attribute type mapping. + def self.openapi_types + { + :'addresses' => :'Array', + :'wallet_id' => :'String' + } + end + + # List of attributes with nullable: true + def self.openapi_nullable + Set.new([ + ]) + end + + # Initializes the object + # @param [Hash] attributes Model attributes in the form of hash + def initialize(attributes = {}) + if (!attributes.is_a?(Hash)) + fail ArgumentError, "The input argument (attributes) must be a hash in `Coinbase::Client::WebhookWalletActivityFilter` initialize method" + end + + # check to see if the attribute exists and convert string to symbol for hash key + attributes = attributes.each_with_object({}) { |(k, v), h| + if (!self.class.attribute_map.key?(k.to_sym)) + fail ArgumentError, "`#{k}` is not a valid attribute in `Coinbase::Client::WebhookWalletActivityFilter`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect + end + h[k.to_sym] = v + } + + if attributes.key?(:'addresses') + if (value = attributes[:'addresses']).is_a?(Array) + self.addresses = value + end + end + + if attributes.key?(:'wallet_id') + self.wallet_id = attributes[:'wallet_id'] + end + end + + # Show invalid properties with the reasons. Usually used together with valid? + # @return Array for valid properties with the reasons + def list_invalid_properties + warn '[DEPRECATED] the `list_invalid_properties` method is obsolete' + invalid_properties = Array.new + invalid_properties + end + + # Check to see if the all the properties in the model are valid + # @return true if the model is valid + def valid? + warn '[DEPRECATED] the `valid?` method is obsolete' + true + end + + # Checks equality by comparing each attribute. + # @param [Object] Object to be compared + def ==(o) + return true if self.equal?(o) + self.class == o.class && + addresses == o.addresses && + wallet_id == o.wallet_id + end + + # @see the `==` method + # @param [Object] Object to be compared + def eql?(o) + self == o + end + + # Calculates hash code according to all attributes. + # @return [Integer] Hash code + def hash + [addresses, wallet_id].hash + end + + # Builds the object from hash + # @param [Hash] attributes Model attributes in the form of hash + # @return [Object] Returns the model itself + def self.build_from_hash(attributes) + return nil unless attributes.is_a?(Hash) + attributes = attributes.transform_keys(&:to_sym) + transformed_hash = {} + openapi_types.each_pair do |key, type| + if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil? + transformed_hash["#{key}"] = nil + elsif type =~ /\AArray<(.*)>/i + # check to ensure the input is an array given that the attribute + # is documented as an array but the input is not + if attributes[attribute_map[key]].is_a?(Array) + transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) } + end + elsif !attributes[attribute_map[key]].nil? + transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]]) + end + end + new(transformed_hash) + end + + # Deserializes the data based on type + # @param string type Data type + # @param string value Value to be deserialized + # @return [Object] Deserialized data + def self._deserialize(type, value) + case type.to_sym + when :Time + Time.parse(value) + when :Date + Date.parse(value) + when :String + value.to_s + when :Integer + value.to_i + when :Float + value.to_f + when :Boolean + if value.to_s =~ /\A(true|t|yes|y|1)\z/i + true + else + false + end + when :Object + # generic object (usually a Hash), return directly + value + when /\AArray<(?.+)>\z/ + inner_type = Regexp.last_match[:inner_type] + value.map { |v| _deserialize(inner_type, v) } + when /\AHash<(?.+?), (?.+)>\z/ + k_type = Regexp.last_match[:k_type] + v_type = Regexp.last_match[:v_type] + {}.tap do |hash| + value.each do |k, v| + hash[_deserialize(k_type, k)] = _deserialize(v_type, v) + end + end + else # model + # models (e.g. Pet) or oneOf + klass = Coinbase::Client.const_get(type) + klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value) + end + end + + # Returns the string representation of the object + # @return [String] String presentation of the object + def to_s + to_hash.to_s + end + + # to_body is an alias to to_hash (backward compatibility) + # @return [Hash] Returns the object in the form of hash + def to_body + to_hash + end + + # Returns the object in the form of hash + # @return [Hash] Returns the object in the form of hash + def to_hash + hash = {} + self.class.attribute_map.each_pair do |attr, param| + value = self.send(attr) + if value.nil? + is_nullable = self.class.openapi_nullable.include?(attr) + next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}")) + end + + hash[param] = _to_hash(value) + end + hash + end + + # Outputs non-array value in the form of hash + # For object, use to_hash. Otherwise, just return the value + # @param [Object] value Any valid value + # @return [Hash] Returns the value in the form of hash + def _to_hash(value) + if value.is_a?(Array) + value.compact.map { |v| _to_hash(v) } + elsif value.is_a?(Hash) + {}.tap do |hash| + value.each { |k, v| hash[k] = _to_hash(v) } + end + elsif value.respond_to? :to_hash + value.to_hash + else + value + end + end + + end + +end From aec0c242d7de5ffde495262306351d921c8d85c8 Mon Sep 17 00:00:00 2001 From: Chaoya Ji Date: Mon, 16 Sep 2024 18:02:25 -0700 Subject: [PATCH 2/8] wallet implementation --- lib/coinbase/client.rb | 2 ++ lib/coinbase/wallet.rb | 13 +++++++++++++ lib/coinbase/webhook.rb | 16 ++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/coinbase/client.rb b/lib/coinbase/client.rb index 71ed2ec8..63766511 100644 --- a/lib/coinbase/client.rb +++ b/lib/coinbase/client.rb @@ -99,6 +99,8 @@ Coinbase::Client.autoload :WebhookEventFilter, 'coinbase/client/models/webhook_event_filter' Coinbase::Client.autoload :WebhookEventType, 'coinbase/client/models/webhook_event_type' Coinbase::Client.autoload :WebhookList, 'coinbase/client/models/webhook_list' +Coinbase::Client.autoload :WebhookEventTypeFilter, 'coinbase/client/models/webhook_event_type_filter' +Coinbase::Client.autoload :WebhookWalletActivityFilter, 'coinbase/client/models/webhook_wallet_activity_filter' # APIs Coinbase::Client.autoload :AddressesApi, 'coinbase/client/api/addresses_api' diff --git a/lib/coinbase/wallet.rb b/lib/coinbase/wallet.rb index 0f178994..1da59c10 100644 --- a/lib/coinbase/wallet.rb +++ b/lib/coinbase/wallet.rb @@ -592,5 +592,18 @@ def set_addresses build_wallet_address(address_model, index) end end + + def create_webhook(notification_uri:, addresses:, signature_header: '') + Coinbase::Webhook.create( + network_id: network, + notification_uri: notification_uri, + event_type: Coinbase::Webhook::WALLET_ACTIVITY_EVENT, + event_type_filter: { + addresses: addresses, + wallet_id: id, + }, + signature_header: signature_header + ) + end end end diff --git a/lib/coinbase/webhook.rb b/lib/coinbase/webhook.rb index 179ae22a..5dbae43b 100644 --- a/lib/coinbase/webhook.rb +++ b/lib/coinbase/webhook.rb @@ -11,6 +11,8 @@ class Webhook # Event type for ERC721 transfer ERC721_TRANSFER_EVENT = 'erc721_transfer' + WALLET_ACTIVITY_EVENT = 'wallet_activity' + class << self # Creates a new webhook for a specified network. # @@ -34,7 +36,7 @@ class << self # event_filters: [{ 'contract_address' => '0x...', 'from_address' => '0x...', 'to_address' => '0x...' }], # signature_header: 'example_header' # ) - def create(network_id:, notification_uri:, event_type:, event_filters:, signature_header: '') + def create(network_id:, notification_uri:, event_type:, event_filters: [], signature_header: '', event_type_filter: nil) model = Coinbase.call_api do webhooks_api.create_webhook( create_webhook_request: { @@ -42,7 +44,8 @@ def create(network_id:, notification_uri:, event_type:, event_filters:, signatur notification_uri: notification_uri, event_type: event_type, event_filters: event_filters, - signature_header: signature_header + signature_header: signature_header, + event_type_filter: event_type_filter } ) end @@ -124,6 +127,10 @@ def signature_header @model.signature_header end + def event_type_filter + @model.event_type_filter + end + # Updates the webhook with a new notification URI. # # @param notification_uri [String] The new URI for webhook notifications. @@ -172,8 +179,9 @@ def to_s network_id: @model.network_id, event_type: @model.event_type, notification_uri: @model.notification_uri, - event_filters: @model.event_filters.map(&:to_hash).to_json, - signature_header: @model.signature_header + event_filters: (@model.event_filters || []).map(&:to_hash).to_json, + signature_header: @model.signature_header, + event_type_filter: @model.event_type_filter ) end From 103fb81054540fed796572430c0107c7534b7da6 Mon Sep 17 00:00:00 2001 From: Chaoya Ji Date: Mon, 16 Sep 2024 21:01:56 -0700 Subject: [PATCH 3/8] wallet imp --- lib/coinbase/wallet.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/coinbase/wallet.rb b/lib/coinbase/wallet.rb index 1da59c10..8f7c096c 100644 --- a/lib/coinbase/wallet.rb +++ b/lib/coinbase/wallet.rb @@ -459,6 +459,19 @@ def load_seed(file_path) "Successfully loaded seed for wallet #{id} from #{file_path}." end + def create_webhook(notification_uri:, addresses: self.addresses.map(&:id), signature_header: '') + Coinbase::Webhook.create( + network_id: Coinbase.normalize_network(network), + notification_uri: notification_uri, + event_type: Coinbase::Webhook::WALLET_ACTIVITY_EVENT, + event_type_filter: { + addresses: addresses, + wallet_id: id, + }, + signature_header: signature_header + ) + end + # Returns a String representation of the Wallet. # @return [String] a String representation of the Wallet def to_s @@ -592,18 +605,5 @@ def set_addresses build_wallet_address(address_model, index) end end - - def create_webhook(notification_uri:, addresses:, signature_header: '') - Coinbase::Webhook.create( - network_id: network, - notification_uri: notification_uri, - event_type: Coinbase::Webhook::WALLET_ACTIVITY_EVENT, - event_type_filter: { - addresses: addresses, - wallet_id: id, - }, - signature_header: signature_header - ) - end end end From f3a601a375fe0e4731db91d54b1cd5caf8a767c5 Mon Sep 17 00:00:00 2001 From: Chaoya Ji Date: Tue, 17 Sep 2024 09:30:48 -0700 Subject: [PATCH 4/8] comment --- lib/coinbase/webhook.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/coinbase/webhook.rb b/lib/coinbase/webhook.rb index 5dbae43b..65c1636f 100644 --- a/lib/coinbase/webhook.rb +++ b/lib/coinbase/webhook.rb @@ -11,6 +11,7 @@ class Webhook # Event type for ERC721 transfer ERC721_TRANSFER_EVENT = 'erc721_transfer' + # Event type for Wallet activity WALLET_ACTIVITY_EVENT = 'wallet_activity' class << self @@ -26,6 +27,7 @@ class << self # can include keys like `contract_address`, `from_address`, or `to_address`. # @param signature_header [String] The custom header to be used for x-webhook-signature header on callbacks, # so developers can verify the requests are coming from Coinbase. + # @param event_type_filter [Hash] Filters applied to wallet activity event type. # @return [Coinbase::Webhook] A new instance of Webhook. # # @example Create a new webhook @@ -34,7 +36,11 @@ class << self # notification_uri: 'https://example.com/callback', # event_type: 'transaction', # event_filters: [{ 'contract_address' => '0x...', 'from_address' => '0x...', 'to_address' => '0x...' }], - # signature_header: 'example_header' + # signature_header: 'example_header', + # event_type_filter: { + # "addresses" => ["0xa3B299855BE3eA231337aC7c40A615e090A3de25"], + # "wallet_id" => "d91d652b-d020-48d4-bf19-5c5eb5e280c7" + # } # ) def create(network_id:, notification_uri:, event_type:, event_filters: [], signature_header: '', event_type_filter: nil) model = Coinbase.call_api do @@ -127,6 +133,9 @@ def signature_header @model.signature_header end + # Returns the event type filters applied to the wallet activity webhook. + # + # @return [Array] A hash of event type filter used by the webhook. def event_type_filter @model.event_type_filter end From 5a4fb80e7a6d4bc9152cf5bc3c3cac2963cf4598 Mon Sep 17 00:00:00 2001 From: Chaoya Ji Date: Tue, 17 Sep 2024 09:34:37 -0700 Subject: [PATCH 5/8] wallet comment --- lib/coinbase/wallet.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/coinbase/wallet.rb b/lib/coinbase/wallet.rb index 8f7c096c..420a6f1a 100644 --- a/lib/coinbase/wallet.rb +++ b/lib/coinbase/wallet.rb @@ -459,6 +459,15 @@ def load_seed(file_path) "Successfully loaded seed for wallet #{id} from #{file_path}." end + # Creates a new webhook on the current wallet for tracking wallet activity events. + # + # @param notification_uri [String] The URI to which the webhook notifications will be sent. + # @param addresses [Array] The list of wallet addresses to include in the event filter, + # defaulting to the addresses of the current wallet. + # @param signature_header [String] (Optional) A header used to sign the webhook request, + # defaulting to an empty string. + # + # @return [Coinbase::Webhook] The newly created webhook instance. def create_webhook(notification_uri:, addresses: self.addresses.map(&:id), signature_header: '') Coinbase::Webhook.create( network_id: Coinbase.normalize_network(network), From ddc52f508698d395821f671ee65b473ded884251 Mon Sep 17 00:00:00 2001 From: Chaoya Ji Date: Tue, 17 Sep 2024 09:44:39 -0700 Subject: [PATCH 6/8] lint --- lib/coinbase/wallet.rb | 2 +- lib/coinbase/webhook.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/coinbase/wallet.rb b/lib/coinbase/wallet.rb index 420a6f1a..9cb4cdf9 100644 --- a/lib/coinbase/wallet.rb +++ b/lib/coinbase/wallet.rb @@ -475,7 +475,7 @@ def create_webhook(notification_uri:, addresses: self.addresses.map(&:id), signa event_type: Coinbase::Webhook::WALLET_ACTIVITY_EVENT, event_type_filter: { addresses: addresses, - wallet_id: id, + wallet_id: id }, signature_header: signature_header ) diff --git a/lib/coinbase/webhook.rb b/lib/coinbase/webhook.rb index 65c1636f..6d2b1e96 100644 --- a/lib/coinbase/webhook.rb +++ b/lib/coinbase/webhook.rb @@ -42,7 +42,14 @@ class << self # "wallet_id" => "d91d652b-d020-48d4-bf19-5c5eb5e280c7" # } # ) - def create(network_id:, notification_uri:, event_type:, event_filters: [], signature_header: '', event_type_filter: nil) + def create( + network_id:, + notification_uri:, + event_type:, + event_filters: [], + signature_header: '', + event_type_filter: nil + ) model = Coinbase.call_api do webhooks_api.create_webhook( create_webhook_request: { From 7b01bddb4532fba4f724db102267064d9cfb73fc Mon Sep 17 00:00:00 2001 From: Chaoya Ji Date: Tue, 17 Sep 2024 14:56:20 -0700 Subject: [PATCH 7/8] tests --- spec/factories/webhook.rb | 18 ++++++++++++ spec/unit/coinbase/wallet_spec.rb | 45 ++++++++++++++++++++++++++++++ spec/unit/coinbase/webhook_spec.rb | 13 ++++++++- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/spec/factories/webhook.rb b/spec/factories/webhook.rb index b70c4526..5ec944b6 100644 --- a/spec/factories/webhook.rb +++ b/spec/factories/webhook.rb @@ -8,10 +8,28 @@ event_filters { [{ 'contract_address' => '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' }] } notification_uri { 'https://example.com/notify' } signature_header { 'example_header' } + event_type_filter do + { + 'addresses' => ['0xa3B299855BE3eA231337aC7c40A615e090A3de25'], + 'wallet_id' => 'd91d652b-d020-48d4-bf19-5c5eb5e280c7' + } + end trait :updated_uri do notification_uri { build(:notification_uri) } end + + trait :wallet_activity do + id { 'wallet_webhook' } + event_type { 'wallet_activity' } + notification_uri { 'https://example.com/notify' } + event_type_filter do + { + 'addresses' => ['0xa3B299855BE3eA231337aC7c40A615e090A3de25'], + 'wallet_id' => 'd91d652b-d020-48d4-bf19-5c5eb5e280c7' + } + end + end end factory :webhook, class: 'Coinbase::Webhook' do diff --git a/spec/unit/coinbase/wallet_spec.rb b/spec/unit/coinbase/wallet_spec.rb index e0f6f56d..97a00bfd 100644 --- a/spec/unit/coinbase/wallet_spec.rb +++ b/spec/unit/coinbase/wallet_spec.rb @@ -21,10 +21,12 @@ let(:second_address_model) do build(:address_model, network_id, :with_seed, seed: seed, wallet_id: wallet_id, index: 1) end + let(:webhook_model) { build(:webhook_model, :wallet_activity) } let(:wallets_api) { instance_double(Coinbase::Client::WalletsApi) } let(:addresses_api) { instance_double(Coinbase::Client::AddressesApi) } let(:external_addresses_api) { instance_double(Coinbase::Client::ExternalAddressesApi) } let(:transfers_api) { instance_double(Coinbase::Client::TransfersApi) } + let(:webhooks_api) { instance_double(Coinbase::Client::WebhooksApi) } let(:use_server_signer) { false } let(:default_network) { build(:network, :base_sepolia) } let(:network) { default_network } @@ -41,6 +43,7 @@ allow(Coinbase::Client::AddressesApi).to receive(:new).and_return(addresses_api) allow(Coinbase::Client::WalletsApi).to receive(:new).and_return(wallets_api) allow(Coinbase::Client::ExternalAddressesApi).to receive(:new).and_return(external_addresses_api) + allow(Coinbase::Client::WebhooksApi).to receive(:new).and_return(webhooks_api) allow(Coinbase).to receive(:configuration).and_return(configuration) @@ -1385,4 +1388,46 @@ def match_create_address_request(req, expected_public_key, expected_address_inde end end end + + describe '#create_webhook' do + subject(:wallet_webhook) { wallet.create_webhook(notification_uri: notification_uri) } + + let(:wallet) { described_class.new(model, seed: seed) } + let(:network_id) { :base_sepolia } + let(:notification_uri) { 'https://example.com/notify' } + let(:event_type) { 'wallet_activity' } + let(:event_type_filter) do + { + 'addresses' => ['0xa3B299855BE3eA231337aC7c40A615e090A3de25'], + 'wallet_id' => 'd91d652b-d020-48d4-bf19-5c5eb5e280c7' + } + end + + before do + allow(addresses_api) + .to receive(:list_addresses) + .and_return(Coinbase::Client::AddressList.new(data: [first_address_model], total_count: 1)) + allow(webhooks_api).to receive(:create_webhook).and_return(webhook_model) + end + + it 'creates a new webhook with wallet activity event type' do + expect(wallet_webhook.event_type).to eq(event_type) + end + + it 'has the correct id' do + expect(wallet_webhook.id).to eq('wallet_webhook') + end + + it 'has the correct network_id' do + expect(wallet_webhook.network_id).to eq(network_id) + end + + it 'has the correct notification_uri' do + expect(wallet_webhook.notification_uri).to eq(notification_uri) + end + + it 'has the correct event_type_filter' do + expect(wallet_webhook.event_type_filter).to eq(event_type_filter) + end + end end diff --git a/spec/unit/coinbase/webhook_spec.rb b/spec/unit/coinbase/webhook_spec.rb index f1dfa01a..a827d411 100644 --- a/spec/unit/coinbase/webhook_spec.rb +++ b/spec/unit/coinbase/webhook_spec.rb @@ -16,7 +16,8 @@ network_id: network_id, notification_uri: notification_uri, event_type: event_type, - event_filters: event_filters + event_filters: event_filters, + event_type_filter: event_type_filter ) end @@ -24,6 +25,12 @@ let(:notification_uri) { 'https://example.com/notify' } let(:event_type) { 'erc20_transfer' } let(:event_filters) { [{ 'contract_address' => '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' }] } + let(:event_type_filter) do + { + 'addresses' => ['0xa3B299855BE3eA231337aC7c40A615e090A3de25'], + 'wallet_id' => 'd91d652b-d020-48d4-bf19-5c5eb5e280c7' + } + end before do allow(webhooks_api).to receive(:create_webhook).and_return(webhook_model) @@ -53,6 +60,10 @@ it 'has the correct event_filters' do expect(webhook.event_filters).to eq(event_filters) end + + it 'has the correct event_type_filter' do + expect(webhook.event_type_filter).to eq(event_type_filter) + end end describe '.list' do From 2150c80e22876dc05d13324a21ae736fe9403cc0 Mon Sep 17 00:00:00 2001 From: Chaoya Ji Date: Wed, 18 Sep 2024 10:50:13 -0700 Subject: [PATCH 8/8] remove optional addresses field --- lib/coinbase/wallet.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/coinbase/wallet.rb b/lib/coinbase/wallet.rb index 9cb4cdf9..5adf31cb 100644 --- a/lib/coinbase/wallet.rb +++ b/lib/coinbase/wallet.rb @@ -462,19 +462,17 @@ def load_seed(file_path) # Creates a new webhook on the current wallet for tracking wallet activity events. # # @param notification_uri [String] The URI to which the webhook notifications will be sent. - # @param addresses [Array] The list of wallet addresses to include in the event filter, - # defaulting to the addresses of the current wallet. # @param signature_header [String] (Optional) A header used to sign the webhook request, # defaulting to an empty string. # # @return [Coinbase::Webhook] The newly created webhook instance. - def create_webhook(notification_uri:, addresses: self.addresses.map(&:id), signature_header: '') + def create_webhook(notification_uri:, signature_header: '') Coinbase::Webhook.create( network_id: Coinbase.normalize_network(network), notification_uri: notification_uri, event_type: Coinbase::Webhook::WALLET_ACTIVITY_EVENT, event_type_filter: { - addresses: addresses, + addresses: addresses.map(&:id), wallet_id: id }, signature_header: signature_header