-
Notifications
You must be signed in to change notification settings - Fork 302
feat(sdk-api): add registerWithBaseCoin for dynamic token registration #8589
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
Open
manas-at-bitgo
wants to merge
1
commit into
master
Choose a base branch
from
feat/CSHLD-24-register-token
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| import sinon from 'sinon'; | ||
| import assert from 'assert'; | ||
| import { BitGoAPI } from '@bitgo/sdk-api'; | ||
| import { coins, Erc20Coin } from '@bitgo/statics'; | ||
| import { register, registerWithCoinMap } from '../../src/register'; | ||
| import { Erc20Token } from '../../src/erc20Token'; | ||
| import { Erc721Token } from '../../src/erc721Token'; | ||
|
|
||
| describe('ETH Register', function () { | ||
| let bitgo: BitGoAPI; | ||
| let registerSpy: sinon.SinonSpy; | ||
| let registerWithBaseCoinSpy: sinon.SinonSpy; | ||
|
|
||
| beforeEach(function () { | ||
| bitgo = new BitGoAPI({ env: 'test' }); | ||
| registerSpy = sinon.spy(bitgo, 'register'); | ||
| registerWithBaseCoinSpy = sinon.spy(bitgo, 'registerWithBaseCoin'); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| registerSpy.restore(); | ||
| registerWithBaseCoinSpy.restore(); | ||
| }); | ||
|
|
||
| describe('register', function () { | ||
| it('should register base coins and token constructors', function () { | ||
| register(bitgo); | ||
|
|
||
| const registeredNames = registerSpy.getCalls().map((call) => call.args[0]); | ||
|
|
||
| // Base coins should be registered | ||
| assert.ok(registeredNames.includes('eth')); | ||
| assert.ok(registeredNames.includes('gteth')); | ||
| assert.ok(registeredNames.includes('teth')); | ||
| assert.ok(registeredNames.includes('hteth')); | ||
|
|
||
| // ERC20 and ERC721 tokens should be registered | ||
| const erc20Count = Erc20Token.createTokenConstructors().length; | ||
| const erc721Count = Erc721Token.createTokenConstructors().length; | ||
| assert.strictEqual(registerSpy.callCount, 4 + erc20Count + erc721Count); | ||
| }); | ||
| }); | ||
|
|
||
| describe('registerWithCoinMap', function () { | ||
| it('should call register internally for base coins and tokens', function () { | ||
| registerWithCoinMap(bitgo, coins); | ||
|
|
||
| const registeredNames = registerSpy.getCalls().map((call) => call.args[0]); | ||
|
|
||
| // Base coins should be registered via register() | ||
| assert.ok(registeredNames.includes('eth')); | ||
| assert.ok(registeredNames.includes('gteth')); | ||
| assert.ok(registeredNames.includes('teth')); | ||
| assert.ok(registeredNames.includes('hteth')); | ||
| }); | ||
|
|
||
| it('should register dynamic ERC20 tokens via registerWithBaseCoin', function () { | ||
| registerWithCoinMap(bitgo, coins); | ||
|
|
||
| // registerWithBaseCoin should have been called for dynamic tokens | ||
| assert.ok(registerWithBaseCoinSpy.callCount > 0); | ||
|
|
||
| // Each call should pass a valid baseCoin from the coinMap | ||
| for (let i = 0; i < registerWithBaseCoinSpy.callCount; i++) { | ||
| const call = registerWithBaseCoinSpy.getCall(i); | ||
| const baseCoin = call.args[1]; | ||
| assert.ok(coins.has(baseCoin.name), `${baseCoin.name} should exist in the coin map`); | ||
| } | ||
| }); | ||
|
|
||
| it('should not call registerWithBaseCoin when coin map has no ERC20 tokens', function () { | ||
| // Create a coin map with only base coins (no ERC20 tokens) | ||
| const limitedCoinMap = coins.filter((coin) => !(coin instanceof Erc20Coin)); | ||
|
|
||
| registerWithCoinMap(bitgo, limitedCoinMap); | ||
|
|
||
| // registerWithBaseCoin should not be called since no ERC20 tokens are in the map | ||
| assert.strictEqual(registerWithBaseCoinSpy.callCount, 0); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { | |
| GetSharingKeyOptions, | ||
| IRequestTracer, | ||
| } from '../api'; | ||
| import { BaseCoin as StaticsBaseCoin } from '@bitgo/statics'; | ||
| import { IBaseCoin } from './baseCoin'; | ||
| import { CoinConstructor } from './coinFactory'; | ||
| import { EnvironmentName } from './environments'; | ||
|
|
@@ -35,4 +36,5 @@ export interface BitGoBase { | |
| setRequestTracer(reqTracer: IRequestTracer): void; | ||
| url(path: string, version?: number): string; | ||
| register(name: string, coin: CoinConstructor): void; | ||
| registerWithBaseCoin(coin: CoinConstructor, baseCoin: Readonly<StaticsBaseCoin>): void; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Base Coin can get confusing, there are two types of base coin, one used for coin map and one used for coin factory, we can rename it to |
||
| } | ||
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.
Will this not throw an error if coin is not present in the coin map maintained by the coin factory?