Skip to content

Commit 6d00970

Browse files
authored
Merge pull request #6747 from BitGo/WIN-6658
feat(sdk-coin-polyx): add support for token enablement
2 parents a75999a + 894459a commit 6d00970

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

examples/js/.DS_Store

6 KB
Binary file not shown.

modules/sdk-coin-polyx/src/polyx.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Environments,
1313
MPCSweepRecoveryOptions,
1414
MPCTxs,
15+
TokenEnablementConfig,
1516
} from '@bitgo/sdk-core';
1617
import { ApiPromise, WsProvider } from '@polkadot/api';
1718
import { BaseCoin as StaticsBaseCoin, coins, SubstrateSpecNameType } from '@bitgo/statics';
@@ -343,4 +344,23 @@ export class Polyx extends SubstrateCoin {
343344
}
344345
return { transactions: broadcastableTransactions, lastScanIndex };
345346
}
347+
348+
/**
349+
* Gets config for how token enablements work for this coin
350+
* @returns
351+
* requiresTokenEnablement: True if tokens need to be enabled for this coin
352+
* supportsMultipleTokenEnablements: True if multiple tokens can be enabled in one transaction
353+
* validateWallet: Function to validate wallet type for token enablement
354+
*/
355+
getTokenEnablementConfig(): TokenEnablementConfig {
356+
return {
357+
requiresTokenEnablement: true,
358+
supportsMultipleTokenEnablements: false,
359+
validateWallet: (walletType: string) => {
360+
if (walletType !== 'custodial') {
361+
throw new Error('Token enablement for Polymesh (polyx) is only supported for custodial wallets');
362+
}
363+
},
364+
};
365+
}
346366
}

modules/sdk-coin-polyx/test/unit/polyx.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,24 @@ describe('Polyx:', function () {
179179
should.deepEqual(txJson.eraPeriod, baseCoin.SWEEP_TXN_DURATION);
180180
});
181181
});
182+
183+
describe('Token Enablement:', function () {
184+
it('should have correct token enablement config', function () {
185+
const config = baseCoin.getTokenEnablementConfig();
186+
should.exist(config);
187+
config.requiresTokenEnablement.should.equal(true);
188+
config.supportsMultipleTokenEnablements.should.equal(false);
189+
});
190+
191+
it('should validate wallet type for token enablement', function () {
192+
const config = baseCoin.getTokenEnablementConfig();
193+
(() => config.validateWallet('custodial')).should.not.throw();
194+
(() => config.validateWallet('hot')).should.throw(
195+
/Token enablement for Polymesh \(polyx\) is only supported for custodial wallets/
196+
);
197+
(() => config.validateWallet('cold')).should.throw(
198+
/Token enablement for Polymesh \(polyx\) is only supported for custodial wallets/
199+
);
200+
});
201+
});
182202
});

modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ export interface RecoverTokenTransaction {
455455
export interface TokenEnablementConfig {
456456
requiresTokenEnablement: boolean;
457457
supportsMultipleTokenEnablements: boolean;
458+
validateWallet?: (walletType: string) => void;
458459
}
459460

460461
export interface MessagePrep {

modules/sdk-core/src/bitgo/wallet/wallet.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,12 @@ export class Wallet implements IWallet {
31183118
if (!teConfig.requiresTokenEnablement) {
31193119
throw new Error(`${this.baseCoin.getFullName()} does not require token enablements`);
31203120
}
3121+
3122+
// Validate wallet type if coin requires it
3123+
if (typeof teConfig.validateWallet === 'function' && this._wallet.type) {
3124+
teConfig.validateWallet(this._wallet.type);
3125+
}
3126+
31213127
if (params.enableTokens.length === 0) {
31223128
throw new Error('No tokens are being specified');
31233129
}
@@ -3185,6 +3191,11 @@ export class Wallet implements IWallet {
31853191
throw new Error(`${this.baseCoin.getFullName()} does not require token enablement transactions`);
31863192
}
31873193

3194+
// Validate wallet type if coin requires it
3195+
if (teConfig.validateWallet && this._wallet.type) {
3196+
teConfig.validateWallet(this._wallet.type);
3197+
}
3198+
31883199
if (typeof params.prebuildTx === 'string' || params.prebuildTx?.buildParams?.type !== 'enabletoken') {
31893200
throw new Error('Invalid build of token enablement.');
31903201
}

0 commit comments

Comments
 (0)