-
Notifications
You must be signed in to change notification settings - Fork 300
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
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
33abaa1
Blockchain.com uses batch request for balances
justinkaseman 13c912d
fix: blockchain.com batch requests use comma, not pipes
justinkaseman bb209fb
Blockcypher uses batch request
justinkaseman 9655bdb
BTC.com uses API key & API secret, balance ea-factory takes config as…
justinkaseman bc22cf7
Remove async promise
justinkaseman 7d528d0
Move blocktrail-sdk typings
justinkaseman 6ecf2e2
Adds throttle env variable for blockcypher
justinkaseman 13c21cf
Linter doesn't like Function? Fine, take any. TYP SAYF 👷
justinkaseman 5d8c8ef
Cleanup & jsdoc
justinkaseman 79d3448
Rename ImplConfig --> Config
justinkaseman b4786d8
Btc.com API_KEY and API_SECRET are required
justinkaseman 94a4d79
Rename throttle --> rate limit
justinkaseman 4f70c11
Rename throttle env var to API_RATE_LIMIT & move throttle util to boo…
justinkaseman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 }) |
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
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 |
---|---|---|
@@ -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 | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
"typescript": "^3.9.7" | ||
}, | ||
"dependencies": { | ||
"blocktrail-sdk": "^3.7.22", | ||
"object-path": "^0.11.4" | ||
} | ||
} |
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
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 | ||
} |
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 |
---|---|---|
@@ -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' | ||
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 }) |
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 |
---|---|---|
@@ -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']) |
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 @@ | ||
declare module 'blocktrail-sdk' |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Blocktrail is now btc.com?
If this is true could we document it somehow, as it's confusing reading it as is.
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.
What would be the benefit of accessing the API through this SDK?
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.
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.
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.