Skip to content

Commit

Permalink
gql: rpc fallback for solana data provider (#4208)
Browse files Browse the repository at this point in the history
* add solana rpc fallback provider with balances impl

* additional fallback rpc logic and optional provider id override for wallet

* add primary wallets to friends

* add solana rpc fallback for transactions
  • Loading branch information
callensm authored Jun 22, 2023
1 parent 586af83 commit a39d57c
Show file tree
Hide file tree
Showing 12 changed files with 939 additions and 483 deletions.
29 changes: 27 additions & 2 deletions backend/native/backpack-api/src/routes/graphql/clients/hasura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type Notification,
type NotificationConnection,
type NotificationFiltersInput,
type ProviderId,
type User,
type Wallet,
type WalletConnection,
Expand Down Expand Up @@ -159,6 +160,10 @@ export class Hasura {
{
id: true,
username: true,
public_keys: [
{ where: { is_primary: { _eq: true } } },
{ blockchain: true, public_key: true },
],
},
],
},
Expand All @@ -168,6 +173,19 @@ export class Hasura {
return detailsResp.auth_users.map((u) =>
NodeBuilder.friend(u.id, {
avatar: `https://swr.xnfts.dev/avatars/${u.username}`,
primaryWallets: u.public_keys.map((pk) => {
const provider = getProviderForId(
inferProviderIdFromString(pk.blockchain)
);
return NodeBuilder.friendPrimaryWallet(u.id as string, {
address: pk.public_key,
provider: NodeBuilder.provider({
logo: provider.logo(),
name: provider.name(),
providerId: provider.id(),
}),
});
}),
username: u.username as string,
})
);
Expand Down Expand Up @@ -339,10 +357,15 @@ export class Hasura {
* by the user ID in the database.
* @param {string} id
* @param {string} address
* @param {ProviderId} [providerId]
* @returns {Promise<Wallet | null>}
* @memberof Hasura
*/
async getWallet(id: string, address: string): Promise<Wallet | null> {
async getWallet(
id: string,
address: string,
providerId?: ProviderId
): Promise<Wallet | null> {
// Query Hasura for a single public key owned by the argued user ID
// and matches the argued public key address
const resp = await this.#chain("query")(
Expand Down Expand Up @@ -370,7 +393,9 @@ export class Hasura {
}

const { blockchain, created_at, is_primary } = resp.auth_public_keys[0];
const provider = getProviderForId(inferProviderIdFromString(blockchain));
const provider = getProviderForId(
providerId ?? inferProviderIdFromString(blockchain)
);

return NodeBuilder.wallet(provider.id(), {
address,
Expand Down
11 changes: 11 additions & 0 deletions backend/native/backpack-api/src/routes/graphql/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
Balances,
Collection,
Friend,
FriendPrimaryWallet,
FriendRequest,
Listing,
MarketData,
Expand Down Expand Up @@ -31,6 +32,16 @@ export abstract class NodeBuilder {
return this._createNode(`friend:${dbId}`, data);
}

static friendPrimaryWallet(
userId: string,
data: Omit<FriendPrimaryWallet, "id">
): FriendPrimaryWallet {
return this._createNode(
`friend_primary_wallet:${userId}:${data.address}`,
data
);
}

static friendRequest(
dbId: unknown,
data: Omit<FriendRequest, "id">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class Bitcoin implements BlockchainDataProvider {
const nodes: Transaction[] = resp.txs.map((t) =>
NodeBuilder.transaction(this.id(), {
block: t.block_index,
fee: t.fee.toString(),
fee: `${ethers.utils.formatUnits(t.fee, this.decimals())} BTC`,
hash: t.hash,
raw: t,
timestamp: new Date(t.time).toISOString(),
Expand Down
Loading

1 comment on commit a39d57c

@vercel
Copy link

@vercel vercel bot commented on a39d57c Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.