-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add btc provider skeleton * add btc data provider for txs and balance * add btc to token list in common * add btc transaction offset and pagination
- Loading branch information
Showing
12 changed files
with
413 additions
and
42 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
backend/native/backpack-api/src/routes/graphql/clients/blockchainInfo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { RESTDataSource } from "@apollo/datasource-rest"; | ||
|
||
type BlockchainInfoOptions = {}; | ||
|
||
/** | ||
* Custom GraphQL REST data source class abstraction for Blockchain Info API. | ||
* @export | ||
* @class BlockchainInfo | ||
* @extends {RESTDataSource} | ||
*/ | ||
export class BlockchainInfo extends RESTDataSource { | ||
override baseURL = "https://blockchain.info"; | ||
|
||
constructor(_opts: BlockchainInfoOptions) { | ||
super(); | ||
} | ||
|
||
/** | ||
* Get the Bitcoin balance data for the argued wallet address. | ||
* @param {string} address | ||
* @returns {Promise<BlockchainInfoBalanceResponse>} | ||
* @memberof BlockchainInfo | ||
*/ | ||
async getBalance(address: string): Promise<BlockchainInfoBalanceResponse> { | ||
const resp: Record<string, BlockchainInfoBalanceResponse> = await this.get( | ||
`/balance?active=${address}` | ||
); | ||
|
||
if (!resp[address]) { | ||
throw new Error(`no balance data found for ${address}`); | ||
} | ||
|
||
return resp[address]; | ||
} | ||
|
||
/** | ||
* Return the recent transactions for a Bitcoin wallet address. | ||
* @param {string} address | ||
* @param {number} [transactionOffset] | ||
* @returns {Promise<BlockchainInfoTransactionsResponse>} | ||
* @memberof BlockchainInfo | ||
*/ | ||
async getRawAddressData( | ||
address: string, | ||
transactionOffset?: number | ||
): Promise<BlockchainInfoTransactionsResponse> { | ||
return this.get(`/rawaddr/${address}`, { | ||
params: transactionOffset | ||
? { | ||
offset: transactionOffset.toString(), | ||
} | ||
: undefined, | ||
}); | ||
} | ||
} | ||
|
||
//////////////////////////////////////////// | ||
// Types // | ||
//////////////////////////////////////////// | ||
|
||
type BlockchainInfoBalanceResponse = { | ||
final_balance: number; | ||
n_tx: number; | ||
total_received: number; | ||
}; | ||
|
||
type BlockchainInfoTransactionsResponse = { | ||
hash160: string; | ||
address: string; | ||
n_tx: number; | ||
n_unredeemed: number; | ||
total_received: number; | ||
total_sent: number; | ||
final_balance: number; | ||
txs: Array<{ | ||
hash: string; | ||
ver: number; | ||
vin_sz: number; | ||
vout_sz: number; | ||
size: number; | ||
weight: number; | ||
fee: number; | ||
relayed_by: string; | ||
lock_time: number; | ||
tx_index: number; | ||
double_spend: boolean; | ||
time: number; | ||
block_index: number; | ||
block_height: number; | ||
result: number; | ||
balance: number; | ||
inputs: Array<{ | ||
sequence: number; | ||
witness: string; | ||
script: string; | ||
index: number; | ||
prev_out: { | ||
addr: string; | ||
n: number; | ||
script: string; | ||
spending_outpoints: Array<{ | ||
n: number; | ||
tx_index: number; | ||
}>; | ||
spent: boolean; | ||
tx_index: number; | ||
type: number; | ||
value: number; | ||
}; | ||
}>; | ||
out: Array<{ | ||
type: number; | ||
spent: boolean; | ||
value: number; | ||
spending_outpoints: any[]; | ||
n: number; | ||
tx_index: number; | ||
script: string; | ||
addr: string; | ||
}>; | ||
}>; | ||
}; |
1 change: 1 addition & 0 deletions
1
backend/native/backpack-api/src/routes/graphql/clients/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
backend/native/backpack-api/src/routes/graphql/providers/bitcoin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
import { BitcoinToken } from "@coral-xyz/common"; | ||
import { ethers } from "ethers"; | ||
|
||
import type { ApiContext } from "../context"; | ||
import { NodeBuilder } from "../nodes"; | ||
import { | ||
type BalanceFiltersInput, | ||
type Balances, | ||
type NftConnection, | ||
type NftFiltersInput, | ||
ProviderId, | ||
type TokenBalance, | ||
type Transaction, | ||
type TransactionConnection, | ||
type TransactionFiltersInput, | ||
} from "../types"; | ||
import { calculateBalanceAggregate, createConnection } from "../utils"; | ||
|
||
import type { BlockchainDataProvider } from "."; | ||
|
||
/** | ||
* Bitcoin blockchain implementation for the common API. | ||
* @export | ||
* @class Bitcoin | ||
* @implements {BlockchainDataProvider} | ||
*/ | ||
export class Bitcoin implements BlockchainDataProvider { | ||
readonly #ctx?: ApiContext; | ||
|
||
constructor(ctx?: ApiContext) { | ||
this.#ctx = ctx; | ||
} | ||
|
||
/** | ||
* Chain ID enum variant. | ||
* @returns {ProviderId} | ||
* @memberof Bitcoin | ||
*/ | ||
id(): ProviderId { | ||
return ProviderId.Bitcoin; | ||
} | ||
|
||
/** | ||
* Native coin decimals. | ||
* @returns {number} | ||
* @memberof Bitcoin | ||
*/ | ||
decimals(): number { | ||
return 8; | ||
} | ||
|
||
/** | ||
* Default native address. | ||
* @returns {string} | ||
* @memberof Bitcoin | ||
*/ | ||
defaultAddress(): string { | ||
return BitcoinToken.address; | ||
} | ||
|
||
/** | ||
* Logo of the native coin. | ||
* @returns {string} | ||
* @memberof Bitcoin | ||
*/ | ||
logo(): string { | ||
return BitcoinToken.logo!; | ||
} | ||
|
||
/** | ||
* The display name of the data provider. | ||
* @returns {string} | ||
* @memberof Bitcoin | ||
*/ | ||
name(): string { | ||
return BitcoinToken.name; | ||
} | ||
|
||
/** | ||
* Symbol of the native coin. | ||
* @returns {string} | ||
* @memberof Bitcoin | ||
*/ | ||
symbol(): string { | ||
return BitcoinToken.symbol; | ||
} | ||
|
||
/** | ||
* Fetch and aggregate the native and prices for the argued wallet address. | ||
* @param {string} address | ||
* @param {BalanceFiltersInput} [_filters] | ||
* @returns {Promise<Balances>} | ||
* @memberof Bitcoin | ||
*/ | ||
async getBalancesForAddress( | ||
address: string, | ||
_filters?: BalanceFiltersInput | ||
): Promise<Balances> { | ||
if (!this.#ctx) { | ||
throw new Error("API context object not available"); | ||
} | ||
|
||
const balance = await this.#ctx.dataSources.blockchainInfo.getBalance( | ||
address | ||
); | ||
|
||
const prices = await this.#ctx.dataSources.coinGecko.getPrices(["bitcoin"]); | ||
const displayAmount = ethers.utils.formatUnits( | ||
balance.final_balance, | ||
this.decimals() | ||
); | ||
|
||
const nodes: TokenBalance[] = [ | ||
NodeBuilder.tokenBalance( | ||
this.id(), | ||
{ | ||
address, | ||
amount: balance.final_balance.toString(), | ||
decimals: this.decimals(), | ||
displayAmount, | ||
marketData: prices?.bitcoin | ||
? NodeBuilder.marketData("bitcoin", { | ||
lastUpdatedAt: prices.bitcoin.last_updated, | ||
percentChange: prices.bitcoin.price_change_percentage_24h, | ||
price: prices.bitcoin.current_price, | ||
sparkline: prices.bitcoin.sparkline_in_7d.price, | ||
usdChange: prices.bitcoin.price_change_24h, | ||
value: parseFloat(displayAmount) * prices.bitcoin.current_price, | ||
valueChange: | ||
parseFloat(displayAmount) * prices.bitcoin.price_change_24h, | ||
}) | ||
: undefined, | ||
token: this.defaultAddress(), | ||
tokenListEntry: NodeBuilder.tokenListEntry({ | ||
address: this.defaultAddress(), | ||
coingeckoId: "bitcoin", | ||
logo: this.logo(), | ||
name: this.name(), | ||
symbol: this.symbol(), | ||
}), | ||
}, | ||
true | ||
), | ||
]; | ||
|
||
return NodeBuilder.balances(address, this.id(), { | ||
aggregate: calculateBalanceAggregate(address, nodes), | ||
tokens: createConnection(nodes, false, false), | ||
}); | ||
} | ||
|
||
/** | ||
* Get a list of NFT data for tokens owned by the argued address. | ||
* @param {string} _address | ||
* @param {NftFiltersInput} [_filters] | ||
* @returns {Promise<NftConnection>} | ||
* @memberof Bitcoin | ||
*/ | ||
async getNftsForAddress( | ||
_address: string, | ||
_filters?: NftFiltersInput | undefined | ||
): Promise<NftConnection> { | ||
return createConnection([], false, false); | ||
} | ||
|
||
/** | ||
* Get the transaction history with parameters for the argued address. | ||
* @param {string} address | ||
* @param {TransactionFiltersInput} [filters] | ||
* @returns {Promise<TransactionConnection>} | ||
* @memberof Bitcoin | ||
*/ | ||
async getTransactionsForAddress( | ||
address: string, | ||
filters?: TransactionFiltersInput | ||
): Promise<TransactionConnection> { | ||
if (!this.#ctx) { | ||
throw new Error("API context object not available"); | ||
} | ||
|
||
const resp = await this.#ctx.dataSources.blockchainInfo.getRawAddressData( | ||
address, | ||
filters?.offset ?? undefined | ||
); | ||
|
||
const nodes: Transaction[] = resp.txs.map((t) => | ||
NodeBuilder.transaction(this.id(), { | ||
block: t.block_index, | ||
fee: t.fee.toString(), | ||
hash: t.hash, | ||
raw: t, | ||
timestamp: new Date(t.time).toISOString(), | ||
type: "standard", | ||
}) | ||
); | ||
|
||
const hasNext = resp.n_tx > (filters?.offset ?? 0) + 50; | ||
const hasPrevious = filters?.offset ? filters.offset > 0 : false; | ||
return createConnection(nodes, hasNext, hasPrevious); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
5d8531f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
backpack – ./
backpack-200ms.vercel.app
www.backpack.app
backpack-git-master-200ms.vercel.app
backpack.app
devnet.backpack.app