Module: Sdp::Resources::Wallets

Included in:
Client
Defined in:
lib/sdp/resources/wallets.rb

Overview

Wallet endpoints: custody initialize, create, list, plus the balances read (which lives under /v1/payments but is wallet-shaped).

Instance Method Summary collapse

Instance Method Details

#create_wallet(label:, provider: nil) ⇒ Object

POST /v1/wallets → Sdp::Wallet. Wallet#id is SDP’s walletId.



59
60
61
62
63
64
# File 'lib/sdp/resources/wallets.rb', line 59

def create_wallet(label:, provider: nil)
  response = post("/v1/wallets", { label: label, provider: provider }.compact)
  data = response.data
  src = data.is_a?(Hash) ? (data[:wallet] || data) : data
  Wallet.from_hash(src)
end

#initialize_custody(provider: nil, wallet_label: nil) ⇒ Object

POST /v1/wallets/initialize — one-time project custody setup. Both fields are optional; the request is sent without a body when neither is given. Returns the snake_cased data hash exactly as SDP sent it (custody config + root wallet fields incl. :public_key) —kept tolerant because the shape varies by custody provider.



53
54
55
56
# File 'lib/sdp/resources/wallets.rb', line 53

def initialize_custody(provider: nil, wallet_label: nil)
  payload = { provider: provider, walletLabel: wallet_label }.compact
  post("/v1/wallets/initialize", payload.empty? ? nil : payload).data
end

#list_wallets(provider: nil, project_id: nil, include_balances: nil) ⇒ Object

GET /v1/wallets → [Sdp::Wallet, …] Filters (camelCased on the wire): provider:, project_id:, include_balances:. Not paginated at v0.28 — the whole list comes back in one response — but routed through Pagination.enumerate so a paginated upstream upgrade is a one-line change here.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sdp/resources/wallets.rb', line 71

def list_wallets(provider: nil, project_id: nil, include_balances: nil)
  query = { provider: provider, projectId: project_id, includeBalances: include_balances }.compact
  Pagination.enumerate(self, "/v1/wallets", query) do |response|
    rows =
      case response.data
      when Array then response.data
      when Hash  then response.data[:wallets] || []
      else []
      end
    rows.map { |wallet| Wallet.from_hash(wallet) }
  end.to_a
end

#wallet_balances(wallet_id) ⇒ Object

GET /v1/payments/wallets/:id/balances → [Sdp::Balance, …] Upstream swallows RPC failures, so a token row (even SOL) may be MISSING entirely — a missing row means “unavailable”, never zero.



87
88
89
90
91
# File 'lib/sdp/resources/wallets.rb', line 87

def wallet_balances(wallet_id)
  data = get("/v1/payments/wallets/#{encode_path_segment(wallet_id)}/balances").data
  container = data.is_a?(Hash) ? (data[:wallet_balances] || data) : {}
  (container[:balances] || []).map { |balance| Balance.from_hash(balance) }
end