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. provider falls back to the client’s configured custody_provider. A managed provider (e.g. privy) is required for Wallet-per-User — local custody holds a single root wallet and raises Sdp::ProviderCapabilityError.



64
65
66
67
68
69
# File 'lib/sdp/resources/wallets.rb', line 64

def create_wallet(label:, provider: nil)
  response = post("/v1/wallets", { label: label, provider: provider || custody_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. provider falls back to the client’s configured custody_provider (SDP_CUSTODY_PROVIDER) when not passed. 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.



55
56
57
58
# File 'lib/sdp/resources/wallets.rb', line 55

def initialize_custody(provider: nil, wallet_label: nil)
  payload = { provider: provider || custody_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.31 — the whole list comes back in one response — but routed through Pagination.enumerate so a paginated upstream upgrade is a one-line change here.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sdp/resources/wallets.rb', line 76

def list_wallets(provider: nil, project_id: nil, include_balances: nil)
  query = { provider: provider || custody_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.



93
94
95
96
97
# File 'lib/sdp/resources/wallets.rb', line 93

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