-
Notifications
You must be signed in to change notification settings - Fork 40
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
[Balance] fetch historical balance #140
Closed
Closed
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1bc6462
[chore] Update package-lock.json (#132)
John-peterson-coinbase eda4835
Merge branch 'v0.0.15' of https://github.com/coinbase/coinbase-sdk-no…
xinyu-li-cb c5a4463
[Balance] fetch historical balance
xinyu-li-cb 0a94ddd
formatting
xinyu-li-cb 8188553
fix lint
xinyu-li-cb 4c06f29
add max size for fetching all history
xinyu-li-cb f0b07f2
rebase error
xinyu-li-cb 7fc209f
tweak naming
xinyu-li-cb ef18a0d
address comments
xinyu-li-cb 5edb455
move list historical balance test under address
xinyu-li-cb c113325
address comment
xinyu-li-cb 992e831
fix lint
xinyu-li-cb 026b0ca
fix IDE change
xinyu-li-cb 4bedc8a
update changelog
xinyu-li-cb 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Decimal from "decimal.js"; | ||
import { HistoricalBalance as HistoricalBalanceModel } from "../client"; | ||
import { Asset } from "./asset"; | ||
|
||
/** A representation of historical balance. */ | ||
export class HistoricalBalance { | ||
public readonly amount: Decimal; | ||
public readonly blockHash: string; | ||
public readonly blockHeight: Decimal; | ||
public readonly asset: Asset; | ||
|
||
/** | ||
* Private constructor to prevent direct instantiation outside of the factory methods. | ||
* | ||
* @ignore | ||
* @param {Decimal} amount - The amount of the balance. | ||
* @param {Decimal} blockHeight - The block height at which the balance was recorded. | ||
* @param {string} blockHash - The block hash at which the balance was recorded | ||
* @param {string} asset - The asset we want to fetch. | ||
* @hideconstructor | ||
*/ | ||
private constructor(amount: Decimal, blockHeight: Decimal, blockHash: string, asset: Asset) { | ||
this.amount = amount; | ||
this.blockHeight = blockHeight; | ||
this.blockHash = blockHash; | ||
this.asset = asset; | ||
} | ||
|
||
/** | ||
* Converts a BalanceModel into a Balance object. | ||
* | ||
* @param {HistoricalBalanceModel} model - The historical balance model object. | ||
* @returns {HistoricalBalance} The HistoricalBalance object. | ||
*/ | ||
public static fromModel(model: HistoricalBalanceModel): HistoricalBalance { | ||
const asset = Asset.fromModel(model.asset); | ||
return new HistoricalBalance( | ||
asset.fromAtomicAmount(new Decimal(model.amount)), | ||
new Decimal(model.block_height), | ||
model.block_hash, | ||
asset, | ||
); | ||
} | ||
} |
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
xinyu-li-cb marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { HistoricalBalance } from "../coinbase/historical_balance"; | ||
import { HistoricalBalance as HistoricalBalanceModel } from "../client"; | ||
import { Decimal } from "decimal.js"; | ||
import { Coinbase } from "../coinbase/coinbase"; | ||
|
||
describe("HistoricalBalance", () => { | ||
describe("#fromModel", () => { | ||
const amount = new Decimal(1); | ||
const historyModel: HistoricalBalanceModel = { | ||
amount: "1000000000000000000", | ||
block_hash: "0x0dadd465fb063ceb78babbb30abbc6bfc0730d0c57a53e8f6dc778dafcea568f", | ||
block_height: "11349306", | ||
asset: { | ||
asset_id: Coinbase.assets.Eth, | ||
network_id: Coinbase.networks.BaseSepolia, | ||
decimals: 18, | ||
contract_address: "0x", | ||
}, | ||
}; | ||
|
||
const historicalBalance = HistoricalBalance.fromModel(historyModel); | ||
|
||
it("returns a new Balance object with the correct amount", () => { | ||
expect(historicalBalance.amount).toEqual(amount); | ||
}); | ||
|
||
it("returns a new Balance object with the correct asset_id", () => { | ||
expect(historicalBalance.asset.assetId).toBe(Coinbase.assets.Eth); | ||
}); | ||
}); | ||
}); |
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.
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.
Pick a magic number for fetching all historical balance (fetch without page parameters).