Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of Reserve batch requests #276

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blockchain.com/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const execute: ExecuteWithConfig<Config> = async (request, config) => {
const endpoint = validator.validated.data.endpoint || DEFAULT_ENDPOINT

switch (endpoint) {
case balance.Name: {
case balance.NAME: {
return balance.makeExecute(config)(request)
}
default: {
Expand Down
31 changes: 20 additions & 11 deletions blockchain.com/src/endpoint/balance.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import { balance } from '@chainlink/ea-factories'
import { Requester } from '@chainlink/external-adapter'
import { Config } from '@chainlink/types'
import { Config, Account } from '@chainlink/types'
import { getBaseURL } from '../config'
import { ChainType, isCoinType, isChainType } from '.'

export const Name = 'balance'
export const NAME = 'balance'

const getBalanceURI = (address: string, confirmations: number) =>
`/q/addressbalance/${address}?confirmations=${confirmations}`
const getBalanceURI = (addresses: string[]) => `balance?active=${addresses.join(',')}`

const getBalance: balance.GetBalance = async (account, config) => {
const reqConfig = {
const getBalances: balance.GetBalances = async (accounts, config) => {
const addresses = accounts.map((a) => a.address)
const { chain } = accounts[0]

const options: any = {
...config.api,
baseURL: config.api.baseURL || getBaseURL(account.chain as ChainType),
url: getBalanceURI(account.address, config.confirmations as number),
baseURL: config.api.baseURL || getBaseURL(chain as ChainType),
url: getBalanceURI(addresses),
}

const response = await Requester.request(reqConfig)
const response = await Requester.request(options)

const toResultWithBalance = (acc: Account) => ({
...acc,
balance: String(response.data[acc.address].final_balance),
})

const resultWithBalance = accounts.map(toResultWithBalance)

return {
payload: response.data,
result: [{ ...account, balance: String(response.data) }],
result: resultWithBalance,
}
}

const isSupported: balance.IsSupported = (coin, chain) => isChainType(chain) && isCoinType(coin)

export const makeExecute = (config: Config) => balance.make({ ...config, getBalance, isSupported })
export const makeExecute = (config: Config) => balance.make({ ...config, getBalances, isSupported })
1 change: 1 addition & 0 deletions blockcypher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The adapter takes the following environment variables:

- `API_KEY`: Optional blockcypher.com API key to use
- `API_RATE_LIMIT`: Optional amount to throttle the number of requests that are sent per second. This is useful for using a lower tier subscription plan.

## Input Params

Expand Down
4 changes: 2 additions & 2 deletions blockcypher/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Requester, Validator, AdapterError } from '@chainlink/external-adapter'
import { ExecuteWithConfig, ExecuteFactory, Config } from '@chainlink/types'
import { makeConfig, DEFAULT_ENDPOINT } from './config'
import { ExecuteWithConfig, ExecuteFactory } from '@chainlink/types'
import { makeConfig, DEFAULT_ENDPOINT, Config } from './config'
import { balance } from './endpoint'

const inputParams = {
Expand Down
16 changes: 14 additions & 2 deletions blockcypher/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { Requester } from '@chainlink/external-adapter'
import { ConfigFactory } from '@chainlink/types'
import types from '@chainlink/types'
import { util } from '@chainlink/ea-bootstrap'

export const DEFAULT_ENDPOINT = 'balance'

export const makeConfig: ConfigFactory = (prefix?) => Requester.getDefaultConfig(prefix)
export const ENV_RATE_LIMIT = 'API_RATE_LIMIT'

export type Config = types.Config & {
ratelimit: number
}

export const makeConfig = (prefix = ''): Config => {
const config = Requester.getDefaultConfig(prefix)
const ratelimit = util.getEnv(ENV_RATE_LIMIT, prefix)
if (ratelimit) config.ratelimit = Number(ratelimit)
return config
}
44 changes: 33 additions & 11 deletions blockcypher/src/endpoint/balance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import bcypher from 'blockcypher'
import { balance } from '@chainlink/ea-factories'
import { Config } from '@chainlink/types'
import { Account } from '@chainlink/types'
import { Config } from '../config'
import { CoinType, ChainType, isCoinType, isChainType } from '.'
import { util } from '@chainlink/ea-bootstrap'

export const Name = 'balance'

Expand All @@ -28,25 +30,45 @@ const getChainId = (coin: CoinType, chain: ChainType): string => {
}
}

const getBalance: balance.GetBalance = async (account, config) => {
const chainId = getChainId(account.coin as CoinType, account.chain as ChainType)
const api = new bcypher(account.coin, chainId, config.apiKey)
const getBalances: balance.GetBalances<Config> = async (accounts, config) => {
const addresses = accounts.map((a) => a.address)
const { coin, chain } = accounts[0]
const chainId = getChainId(coin as CoinType, chain as ChainType)
const api = new bcypher(coin, chainId, config.apiKey)
const params = { confirmations: config.confirmations }
const _getAddrBal = (): Promise<AddressBalance> =>
new Promise((resolve, reject) => {
api.getAddrBal(account.address, params, (error: Error, body: AddressBalance) =>
error ? reject(error) : resolve(body),

const _getAddrBal = (addrs: string[]) =>
new Promise<AddressBalance[]>((resolve, reject) => {
api.getAddrBal(
addrs.join(';'),
params,
(error: Error, body: AddressBalance | AddressBalance[]) => {
const data = addrs.length > 1 ? (body as AddressBalance[]) : [body as AddressBalance]
error ? reject(error) : resolve(data)
},
)
})

const response = await _getAddrBal()
const response = config.ratelimit
? await util.throttle(config.ratelimit, addresses, _getAddrBal)
: await _getAddrBal(addresses)

const addrLookup: { [key: string]: AddressBalance } = {}
response.forEach((r: any) => (addrLookup[r.address] = r))

const addBalance = (acc: Account) => ({
...acc,
balance: String(addrLookup[acc.address].final_balance),
})
const resultWithBalance = accounts.map(addBalance)

return {
payload: response,
result: [{ ...account, balance: String(response.balance) }],
result: resultWithBalance,
}
}

const isSupported: balance.IsSupported = (coin, chain) => isChainType(chain) && isCoinType(coin)

export const makeExecute = (config: Config) => balance.make({ ...config, getBalance, isSupported })
export const makeExecute = (config: Config) =>
balance.make<Config>({ ...config, getBalances, isSupported })
41 changes: 41 additions & 0 deletions bootstrap/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,44 @@ export const toFixedMax = (num: number | string | Decimal, decimals: number): st
.replace(/(\.\d*?[1-9])0+$/g, '$1')
// remove decimal part if all zeros (or only decimal point)
.replace(/\.0*$/g, '')

/**
* Chunk an array into a nested array
*
* @param amount number the max size of the chunks
* @param data array of arbitrary data
*
* @returns 2d array
*/
export const chunk = (amount: number, data: any[]) => {
const output: any[][] = []
const length = data.length
if (amount < 1 || length < 1) return output
const chunks = Math.ceil(data.length / amount)
for (let i = 0; i < chunks; i++) {
const offset = amount * i
const slice = data.slice(offset, offset + amount)
output.push(slice)
}
return output
}

/**
* Delays the amount of requests sent per second
*
* @param amount maximum number of requests per second
* @param data array of arbitrary data
* @param exec function handler of a chunk
*
* @returns array of response data
*/
export const throttle = async (amount: number, data: any[], exec: any) => {
const chunks = chunk(amount, data)
const delay = 1000
const withDelay = async (c: any, i: number) => {
await new Promise((resolve) => setTimeout(resolve, i * delay))
return await exec(c)
}
const output = await Promise.all(chunks.map(withDelay))
return output.flat()
}
2 changes: 2 additions & 0 deletions btc.com/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

The adapter takes the following environment variables:

- `API_KEY`
- `API_SECRET`
- `API_TIMEOUT`: Optional timeout param, defaults to `30000`

## Input Params
Expand Down
1 change: 1 addition & 0 deletions btc.com/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"typescript": "^3.9.7"
},
"dependencies": {
"blocktrail-sdk": "^3.7.22",
"object-path": "^0.11.4"
}
}
6 changes: 3 additions & 3 deletions btc.com/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Requester, Validator, AdapterError } from '@chainlink/external-adapter'
import { ExecuteWithConfig, ExecuteFactory, Config } from '@chainlink/types'
import { makeConfig, DEFAULT_ENDPOINT } from './config'
import { ExecuteWithConfig, ExecuteFactory } from '@chainlink/types'
import { makeConfig, Config, DEFAULT_ENDPOINT } from './config'
import { balance } from './endpoint'

const inputParams = {
Expand All @@ -18,7 +18,7 @@ export const execute: ExecuteWithConfig<Config> = async (request, config) => {
const endpoint = validator.validated.data.endpoint || DEFAULT_ENDPOINT

switch (endpoint) {
case balance.Name: {
case balance.NAME: {
return balance.makeExecute(config)(request)
}
default: {
Expand Down
9 changes: 8 additions & 1 deletion btc.com/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Requester } from '@chainlink/external-adapter'
import { Config } from '@chainlink/types'
import types from '@chainlink/types'
import { util } from '@chainlink/ea-bootstrap'

export const DEFAULT_API_ENDPOINT = 'https://chain.api.btc.com'

export const DEFAULT_ENDPOINT = 'balance'

export type Config = types.Config & {
apiSecret: string
}

export const makeConfig = (prefix = ''): Config => {
const config = Requester.getDefaultConfig(prefix)
config.api.baseURL = config.api.baseURL || DEFAULT_API_ENDPOINT
config.apiSecret = util.getRequiredEnv('API_SECRET', prefix)
config.apiKey = util.getRequiredEnv('API_KEY', prefix)
return config
}
33 changes: 19 additions & 14 deletions btc.com/src/endpoint/balance.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { balance } from '@chainlink/ea-factories'
import { Requester } from '@chainlink/external-adapter'
import { Config } from '@chainlink/types'
import { isChainType, isCoinType } from '.'
import * as blocktrail from 'blocktrail-sdk'
Copy link
Contributor

Choose a reason for hiding this comment

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

Blocktrail is now btc.com?

If this is true could we document it somehow, as it's confusing reading it as is.

Copy link
Contributor

Choose a reason for hiding this comment

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

What would be the benefit of accessing the API through this SDK?

Copy link
Collaborator Author

@justinkaseman justinkaseman Feb 1, 2021

Choose a reason for hiding this comment

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

Blocktrail is now btc.com?

If this is true could we document it somehow, as it's confusing reading it as is.

I'm not sure how this is all settling on their side.
Blocktrail's website now links to BTC.com and BTC.com's new docs say to use the SDK.
Their support email shows their name as bitmain.

What would be the benefit of accessing the API through this SDK?

Originally I did it because of https://dev.btc.com/docs/js#api-authentication (they haven't documented how to authenticate so I just went through the SDK).
I could go back now and try sending the API_KEY and API_SECRET as a param.

import { Config } from '../config'

export const Name = 'balance'
export const NAME = 'balance'

const getBalanceURI = (address: string) => `/v3/address/${address}`
const getBalance: balance.GetBalance<Config> = async (account, config) => {
const client = blocktrail.BlocktrailSDK({
apiKey: config.apiKey,
apiSecret: config.apiSecret,
network: account.coin?.toUpperCase(),
testnet: account.chain === 'testnet',
})

const getBalance: balance.GetBalance = async (account, config) => {
const reqConfig = {
...config.api,
url: getBalanceURI(account.address),
}

const response = await Requester.request(reqConfig)
const response: any = await new Promise((resolve, reject) =>
client.address(account.address, (error: any, address: any) =>
error ? reject(error) : resolve(address),
),
)

return {
payload: response.data,
result: [{ ...account, balance: String(response.data.data.balance) }],
payload: response,
result: [{ ...account, balance: String(response.balance) }],
}
}

const isSupported: balance.IsSupported = (coin, chain) => isChainType(chain) && isCoinType(coin)

export const makeExecute = (config: Config) => balance.make({ ...config, getBalance, isSupported })
export const makeExecute = (config: Config) =>
balance.make<Config>({ ...config, getBalance, isSupported })
9 changes: 8 additions & 1 deletion btc.com/test/balance.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { makeExecute } from '../src/adapter'
import { Requester } from '@chainlink/external-adapter'
import { DEFAULT_API_ENDPOINT } from '../src/config'
import { shouldBehaveLikeBalanceAdapter } from '@chainlink/adapter-test-helpers'

shouldBehaveLikeBalanceAdapter(makeExecute(), ['bitcoin_mainnet'])
const config = Requester.getDefaultConfig()
config.api.baseURL = config.api.baseURL || DEFAULT_API_ENDPOINT
config.apiSecret = process.env.API_SECRET || ''
config.apiKey = process.env.API_KEY || ''

shouldBehaveLikeBalanceAdapter(makeExecute(config), ['bitcoin_mainnet'])
1 change: 1 addition & 0 deletions btc.com/typings/blocktrail-sdk/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'blocktrail-sdk'
15 changes: 15 additions & 0 deletions composite/proof-of-reserves/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The feed takes the following environment variables:
| ✅ | `PROTOCOL_ADAPTER` | The protocol type | `renvm`, `wbtc` | |
| ✅ | `BTC_INDEXER_ADAPTER` | BTC indexer adapter type | `amberdata`, `blockchain_com`, `blockcypher`. `blockchair`, `btc_com`,`cryptoapis`, `sochain` | |
| 🟡 | `*_API_KEY` (where \* is the capitalized `BTC_INDEXER_ADAPTER`) | The API key for an indexer adapter | (e.g. BLOCKCYPHER_API_KEY="34234dmmd313" ) | |
| |

Each protocol may need additional configuration:

Expand All @@ -26,6 +27,20 @@ Each protocol may need additional configuration:
| :-------: | :-----------------: | :---------------------------: | :----------------------: | :---------: |
| ✅ | `WBTC_API_ENDPOINT` | The endpoint to query WBTC at | (e.g. "https://api..." ) | |

Each indexer may take additional configuration:

### Blockcypher

| Required? | Name | Description | Options | Defaults to |
| :-------: | :--------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------: | :-----: | :---------: |
| | `BLOCKCYPHER_API_RATE_LIMIT` | Provide the plan rate limit to throttle the amount of requests that are sent per second. This is useful for using a lower tier subscription plan. | | |

### BTC_COM

| Required? | Name | Description | Options | Defaults to |
| :-------: | :------------------------------------------------------------------: | :----------------------------------------------: | :-----: | :---------: |
| 🟡 | `BTC_COM_API_SECRET` (only when using `BTC_INDEXER_ADAPTER=btc_com`) | An API secret set up through BTC.com's dashboard |

## Running this adapter

### Local
Expand Down
Loading