Skip to content

Commit

Permalink
feat: adding sv and st query parameters for analytics (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother authored Feb 10, 2025
1 parent 26fbdc9 commit 1bfe9c6
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 63 deletions.
59 changes: 22 additions & 37 deletions integration/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ describe('Account balance', () => {

it('fulfilled balance Ethereum address', async () => {
let resp: any = await httpClient.get(
`${baseUrl}/v1/account/${fulfilled_eth_address}/balance?projectId=${projectId}&currency=${currency}`,
{
headers: {
'x-sdk-version': sdk_version,
}
}
`${baseUrl}/v1/account/${fulfilled_eth_address}/balance?projectId=${projectId}&currency=${currency}&sv=${sdk_version}`
)
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
Expand All @@ -40,10 +35,9 @@ describe('Account balance', () => {
}
})

it('fulfilled balance Solana address', async () => {
let chainId = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
it('fulfilled balance Ethereum address (deprecated \'x-sdk-version\')', async () => {
let resp: any = await httpClient.get(
`${baseUrl}/v1/account/${fulfilled_solana_address}/balance?projectId=${projectId}&currency=${currency}&chainId=${chainId}`,
`${baseUrl}/v1/account/${fulfilled_eth_address}/balance?projectId=${projectId}&currency=${currency}`,
{
headers: {
'x-sdk-version': sdk_version,
Expand All @@ -53,6 +47,16 @@ describe('Account balance', () => {
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
expect(resp.data.balances.length).toBeGreaterThan(1)
})

it('fulfilled balance Solana address', async () => {
let chainId = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
let resp: any = await httpClient.get(
`${baseUrl}/v1/account/${fulfilled_solana_address}/balance?projectId=${projectId}&currency=${currency}&chainId=${chainId}&sv=${sdk_version}`
)
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
expect(resp.data.balances.length).toBeGreaterThan(1)

for (const item of resp.data.balances) {
expect(item.chainId).toEqual(chainId)
Expand All @@ -76,12 +80,7 @@ describe('Account balance', () => {

it('empty balance Ethereum address', async () => {
let resp: any = await httpClient.get(
`${baseUrl}/v1/account/${empty_eth_address}/balance?projectId=${projectId}&currency=${currency}`,
{
headers: {
'x-sdk-version': sdk_version,
}
}
`${baseUrl}/v1/account/${empty_eth_address}/balance?projectId=${projectId}&currency=${currency}&sv=${sdk_version}`
)
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
Expand All @@ -91,12 +90,7 @@ describe('Account balance', () => {
it('empty balance Solana address', async () => {
let chainId = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
let resp: any = await httpClient.get(
`${baseUrl}/v1/account/${empty_solana_address}/balance?projectId=${projectId}&currency=${currency}&chainId=${chainId}`,
{
headers: {
'x-sdk-version': sdk_version,
}
}
`${baseUrl}/v1/account/${empty_solana_address}/balance?projectId=${projectId}&currency=${currency}&chainId=${chainId}&sv=${sdk_version}`
)
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
Expand All @@ -107,12 +101,9 @@ describe('Account balance', () => {
// USDC token contract address on Base
const token_contract_address = 'eip155:8453:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913'
const endpoint = `/v1/account/${fulfilled_eth_address}/balance`;
const queryParams = `?projectId=${projectId}&currency=${currency}&forceUpdate=${token_contract_address}`;
const queryParams = `?projectId=${projectId}&currency=${currency}&sv=${sdk_version}&forceUpdate=${token_contract_address}`;
const url = `${baseUrl}${endpoint}${queryParams}`;
const headers = {
'x-sdk-version': sdk_version,
};
let resp = await httpClient.get(url, { headers });
let resp = await httpClient.get(url);
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
expect(resp.data.balances.length).toBeGreaterThan(1)
Expand Down Expand Up @@ -140,20 +131,17 @@ describe('Account balance', () => {
const zero_balance_address = '0x5b6262592954B925B510651462b63ddEbcc22eaD'
const token_contract_address = 'eip155:8453:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913'
const endpoint = `/v1/account/${zero_balance_address}/balance`;
let queryParams = `?projectId=${projectId}&currency=${currency}`;
let queryParams = `?projectId=${projectId}&currency=${currency}&sv=${sdk_version}`;
let url = `${baseUrl}${endpoint}${queryParams}`;
const headers = {
'x-sdk-version': sdk_version,
};
let resp = await httpClient.get(url, { headers });
let resp = await httpClient.get(url);
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
expect(resp.data.balances.length).toBe(0)

// Forcing update and checking injected balance in response
queryParams = `${queryParams}&forceUpdate=${token_contract_address}`;
url = `${baseUrl}${endpoint}${queryParams}`;
resp = await httpClient.get(url, { headers });
resp = await httpClient.get(url);
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
expect(resp.data.balances.length).toBe(1)
Expand All @@ -167,12 +155,9 @@ describe('Account balance', () => {
// We are using `0xe...` as a contract address for native tokens
const token_contract_address = 'eip155:1:0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
const endpoint = `/v1/account/${fulfilled_eth_address}/balance`;
const queryParams = `?projectId=${projectId}&currency=${currency}&forceUpdate=${token_contract_address}`;
const queryParams = `?projectId=${projectId}&currency=${currency}&sv=${sdk_version}&forceUpdate=${token_contract_address}`;
const url = `${baseUrl}${endpoint}${queryParams}`;
const headers = {
'x-sdk-version': sdk_version,
};
let resp = await httpClient.get(url, { headers });
let resp = await httpClient.get(url);
expect(resp.status).toBe(200)
expect(typeof resp.data.balances).toBe('object')
expect(resp.data.balances.length).toBeGreaterThan(1)
Expand Down
8 changes: 8 additions & 0 deletions src/analytics/account_names_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub struct AccountNameRegistration {
pub region: Option<String>,
pub country: Option<Arc<str>>,
pub continent: Option<Arc<str>>,

// Sdk info
pub sv: Option<String>,
pub st: Option<String>,
}

impl AccountNameRegistration {
Expand All @@ -25,6 +29,8 @@ impl AccountNameRegistration {
region: Option<Vec<String>>,
country: Option<Arc<str>>,
continent: Option<Arc<str>>,
sv: Option<String>,
st: Option<String>,
) -> Self {
Self {
timestamp: wc::analytics::time::now(),
Expand All @@ -35,6 +41,8 @@ impl AccountNameRegistration {
region: region.map(|r| r.join(", ")),
country,
continent,
sv,
st,
}
}
}
8 changes: 8 additions & 0 deletions src/analytics/balance_lookup_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub struct BalanceLookupInfo {
pub region: Option<String>,
pub country: Option<Arc<str>>,
pub continent: Option<Arc<str>>,

// Sdk info
pub sv: Option<String>,
pub st: Option<String>,
}

impl BalanceLookupInfo {
Expand All @@ -36,6 +40,8 @@ impl BalanceLookupInfo {
region: Option<Vec<String>>,
country: Option<Arc<str>>,
continent: Option<Arc<str>>,
sv: Option<String>,
st: Option<String>,
) -> Self {
Self {
timestamp: wc::analytics::time::now(),
Expand All @@ -51,6 +57,8 @@ impl BalanceLookupInfo {
region: region.map(|r| r.join(", ")),
country,
continent,
sv,
st,
}
}
}
8 changes: 8 additions & 0 deletions src/analytics/history_lookup_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub struct HistoryLookupInfo {
pub region: Option<String>,
pub country: Option<Arc<str>>,
pub continent: Option<Arc<str>>,

// Sdk info
pub sv: Option<String>,
pub st: Option<String>,
}

impl HistoryLookupInfo {
Expand All @@ -39,6 +43,8 @@ impl HistoryLookupInfo {
region: Option<Vec<String>>,
country: Option<Arc<str>>,
continent: Option<Arc<str>>,
sv: Option<String>,
st: Option<String>,
) -> Self {
HistoryLookupInfo {
timestamp: wc::analytics::time::now(),
Expand All @@ -53,6 +59,8 @@ impl HistoryLookupInfo {
region: region.map(|r| r.join(", ")),
country,
continent,
sv,
st,
}
}
}
9 changes: 9 additions & 0 deletions src/analytics/identity_lookup_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub struct IdentityLookupInfo {

pub client_id: Option<String>,
pub sender: Option<String>,

// Sdk info
pub sv: Option<String>,
pub st: Option<String>,
}

impl IdentityLookupInfo {
Expand All @@ -46,6 +50,8 @@ impl IdentityLookupInfo {
continent: Option<Arc<str>>,
client_id: Option<String>,
sender: Option<String>,
sv: Option<String>,
st: Option<String>,
) -> Self {
Self {
timestamp: wc::analytics::time::now(),
Expand All @@ -68,6 +74,9 @@ impl IdentityLookupInfo {

client_id,
sender,

sv,
st,
}
}
}
9 changes: 9 additions & 0 deletions src/analytics/message_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ pub struct MessageInfo {
pub region: Option<String>,
pub country: Option<Arc<str>>,
pub continent: Option<Arc<str>>,

// Sdk info
pub sv: Option<String>,
pub st: Option<String>,
}

impl MessageInfo {
#[allow(clippy::too_many_arguments)]
pub fn new(
query_params: &RpcQueryParams,
request: &JsonRpcRequest,
Expand All @@ -33,6 +38,8 @@ impl MessageInfo {
continent: Option<Arc<str>>,
provider: &ProviderKind,
origin: Option<String>,
sv: Option<String>,
st: Option<String>,
) -> Self {
Self {
timestamp: wc::analytics::time::now(),
Expand All @@ -52,6 +59,8 @@ impl MessageInfo {
region: region.map(|r| r.join(", ")),
country,
continent,
sv,
st,
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/analytics/onramp_history_lookup_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub struct OnrampHistoryLookupInfo {
pub purchase_currency: String,
pub purchase_network: String,
pub purchase_amount: String,

// Sdk info
pub sv: Option<String>,
pub st: Option<String>,
}

impl OnrampHistoryLookupInfo {
Expand All @@ -40,6 +44,9 @@ impl OnrampHistoryLookupInfo {
purchase_currency: String,
purchase_network: String,
purchase_amount: String,

sv: Option<String>,
st: Option<String>,
) -> Self {
OnrampHistoryLookupInfo {
transaction_id,
Expand All @@ -56,6 +63,9 @@ impl OnrampHistoryLookupInfo {
purchase_currency,
purchase_network,
purchase_amount,

sv,
st,
}
}
}
12 changes: 8 additions & 4 deletions src/handlers/balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
super::{SupportedCurrencies, HANDLER_TASK_METRICS},
super::{SdkInfoParams, SupportedCurrencies, HANDLER_TASK_METRICS},
crate::{
analytics::{BalanceLookupInfo, MessageSource},
error::RpcError,
Expand Down Expand Up @@ -43,6 +43,8 @@ pub struct BalanceQueryParams {
pub chain_id: Option<String>,
/// Comma separated list of CAIP-10 contract addresses to force update the balance
pub force_update: Option<String>,
#[serde(flatten)]
pub sdk_info: SdkInfoParams,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
Expand Down Expand Up @@ -177,10 +179,10 @@ async fn handler_internal(

state.validate_project_access_and_quota(&project_id).await?;

// if headers not contains `x-sdk-version` then respond with an empty balance
// array to fix the issue of redundant calls in sdk versions <= 4.1.8
// if headers not contains `x-sdk-version` and `sv` query parameter then respond
// with an empty balance array to fix the issue of redundant calls in sdk versions <= 4.1.8
// https://github.com/WalletConnect/web3modal/pull/2157
if !headers.contains_key("x-sdk-version") {
if !headers.contains_key("x-sdk-version") && query.sdk_info.sv.is_none() {
return Ok(Json(BalanceResponseBody { balances: vec![] }).into_response());
}

Expand Down Expand Up @@ -264,6 +266,8 @@ async fn handler_internal(
region.clone(),
country.clone(),
continent.clone(),
query.sdk_info.sv.clone(),
query.sdk_info.st.clone(),
));
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/handlers/history.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
super::HANDLER_TASK_METRICS,
super::{SdkInfoParams, HANDLER_TASK_METRICS},
crate::{
analytics::{HistoryLookupInfo, OnrampHistoryLookupInfo},
error::RpcError,
Expand Down Expand Up @@ -28,6 +28,8 @@ pub struct HistoryQueryParams {
pub chain_id: Option<String>,
pub cursor: Option<String>,
pub onramp: Option<String>,
#[serde(flatten)]
pub sdk_info: SdkInfoParams,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
Expand Down Expand Up @@ -252,6 +254,8 @@ async fn handler_internal(
region,
country,
continent,
query.sdk_info.sv.clone(),
query.sdk_info.st.clone(),
));
}
ProviderKind::Coinbase => {
Expand Down Expand Up @@ -286,6 +290,8 @@ async fn handler_internal(
.as_ref()
.map(|v| v[0].quantity.numeric.clone())
.unwrap_or_default(),
query.sdk_info.sv.clone(),
query.sdk_info.st.clone(),
));
}
}
Expand Down
Loading

0 comments on commit 1bfe9c6

Please sign in to comment.