Skip to content

Commit a152f42

Browse files
authored
Merge pull request #6409 from BitGo/WIN-5750-1
fix(sdk-coin-polyx): add mainnet address format
2 parents fffccf8 + 4d02932 commit a152f42

18 files changed

+491
-24
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { TransactionBuilder } from '@bitgo/abstract-substrate';
2+
import { Transaction } from './transaction';
3+
import { BaseCoin as CoinConfig } from '@bitgo/statics';
4+
import { TransactionType } from '@bitgo/sdk-core';
5+
import { DecodedSignedTx, DecodedSigningPayload, UnsignedTransaction } from '@substrate/txwrapper-core';
6+
import { TxMethod } from './iface';
7+
import utils from './utils';
8+
9+
/**
10+
* Base builder class for Polyx transactions
11+
* Extends the abstract-substrate TransactionBuilder with Polyx-specific functionality
12+
*/
13+
export abstract class PolyxBaseBuilder<M = TxMethod, T extends Transaction = Transaction> extends TransactionBuilder<
14+
M,
15+
T
16+
> {
17+
constructor(_coinConfig: Readonly<CoinConfig>) {
18+
super(_coinConfig);
19+
// Override the transaction instance with our PolyX-specific Transaction
20+
this._transaction = new Transaction(_coinConfig) as T;
21+
}
22+
23+
/**
24+
* Override the getAddressFormat method to return different values based on network type
25+
* Returns 12 for mainnet and 42 for testnet
26+
*
27+
* @returns {number} The address format to use
28+
*/
29+
protected getAddressFormat(): number {
30+
return utils.getAddressFormat(this._coinConfig.name);
31+
}
32+
33+
// These methods are abstract in the parent class and must be implemented by concrete subclasses
34+
protected abstract buildTransaction(): UnsignedTransaction;
35+
protected abstract get transactionType(): TransactionType;
36+
abstract validateDecodedTransaction(
37+
decodedTxn: DecodedSigningPayload | DecodedSignedTx,
38+
rawTransaction?: string
39+
): void;
40+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { TransactionBuilder, Transaction } from '@bitgo/abstract-substrate';
1+
import { Transaction } from './transaction';
2+
import { PolyxBaseBuilder } from './baseBuilder';
23
import { DecodedSignedTx, DecodedSigningPayload, UnsignedTransaction } from '@substrate/txwrapper-core';
34
import { methods } from '@substrate/txwrapper-polkadot';
45
import { BaseCoin as CoinConfig } from '@bitgo/statics';
@@ -24,7 +25,7 @@ type ControllerValue = string | DecodedController;
2425
type PayeeValue = string | DecodedPayee;
2526
type AmountValue = string | number;
2627

27-
export class BatchStakingBuilder extends TransactionBuilder {
28+
export class BatchStakingBuilder extends PolyxBaseBuilder {
2829
// For bond operation
2930
protected _amount: string;
3031
protected _controller: string;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { methods } from '@substrate/txwrapper-polkadot';
33
import { UnsignedTransaction, DecodedSigningPayload, DecodedSignedTx } from '@substrate/txwrapper-core';
44
import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
55
import BigNumber from 'bignumber.js';
6-
import { TransactionBuilder, Transaction } from '@bitgo/abstract-substrate';
6+
import { Transaction } from './transaction';
7+
import { PolyxBaseBuilder } from './baseBuilder';
78
import { BatchArgs } from './iface';
89
import { BatchUnstakingTransactionSchema } from './txnSchema';
910
import utils from './utils';
1011

11-
export class BatchUnstakingBuilder extends TransactionBuilder {
12+
export class BatchUnstakingBuilder extends PolyxBaseBuilder {
1213
protected _amount: string;
1314

1415
constructor(_coinConfig: Readonly<CoinConfig>) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { TransactionBuilder, Transaction } from '@bitgo/abstract-substrate';
1+
import { Transaction } from './transaction';
2+
import { PolyxBaseBuilder } from './baseBuilder';
23
import { DecodedSignedTx, DecodedSigningPayload, UnsignedTransaction } from '@substrate/txwrapper-core';
34
import { methods } from '@substrate/txwrapper-polkadot';
45
import { BaseCoin as CoinConfig } from '@bitgo/statics';
@@ -8,7 +9,7 @@ import utils from './utils';
89
import { BondExtraArgs } from './iface';
910
import BigNumber from 'bignumber.js';
1011

11-
export class BondExtraBuilder extends TransactionBuilder {
12+
export class BondExtraBuilder extends PolyxBaseBuilder {
1213
protected _amount: string;
1314

1415
constructor(_coinConfig: Readonly<CoinConfig>) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export {
99
} from '@bitgo/abstract-substrate';
1010

1111
export { TransactionBuilderFactory } from './transactionBuilderFactory';
12+
export { PolyxBaseBuilder } from './baseBuilder';
1213
export { TransferBuilder } from './transferBuilder';
1314
export { RegisterDidWithCDDBuilder } from './registerDidWithCDDBuilder';
1415
export { Transaction as PolyxTransaction } from './transaction';

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { TransactionBuilder, Interface, utils } from '@bitgo/abstract-substrate';
1+
import { Interface, utils } from '@bitgo/abstract-substrate';
2+
import { PolyxBaseBuilder } from './baseBuilder';
23
import { DecodedSignedTx, DecodedSigningPayload, defineMethod, UnsignedTransaction } from '@substrate/txwrapper-core';
34
import { BaseCoin as CoinConfig } from '@bitgo/statics';
45
import { TransactionType, BaseAddress, InvalidTransactionError } from '@bitgo/sdk-core';
56
import { RegisterDidWithCDDArgs, TxMethod } from './iface';
67
import { RegisterDidWithCDDTransactionSchema } from './txnSchema';
78
import { Transaction } from './transaction';
89

9-
export class RegisterDidWithCDDBuilder extends TransactionBuilder<TxMethod, Transaction> {
10+
export class RegisterDidWithCDDBuilder extends PolyxBaseBuilder<TxMethod, Transaction> {
1011
protected _to: string;
1112
protected _method: TxMethod;
1213

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@ import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
33
import { construct, decode } from '@substrate/txwrapper-polkadot';
44
import { decodeAddress } from '@polkadot/keyring';
55
import { DecodedTx, RegisterDidWithCDDArgs } from './iface';
6+
import polyxUtils from './utils';
67

78
export class Transaction extends SubstrateTransaction {
9+
/**
10+
* Override the getAddressFormat method to return different values based on network type
11+
* Returns 12 for mainnet and 42 for testnet
12+
*
13+
* @returns {number} The address format to use
14+
* @override
15+
*/
16+
protected getAddressFormat(): number {
17+
return polyxUtils.getAddressFormat(this._coinConfig.name);
18+
}
19+
820
/** @inheritdoc */
921
toJson(): Interface.TxData {
1022
if (!this._substrateTransaction) {
@@ -40,7 +52,7 @@ export class Transaction extends SubstrateTransaction {
4052
result.to = keypairDest.getAddress(this.getAddressFormat());
4153
result.amount = '0'; // RegisterDidWithCDD does not transfer any value
4254
} else {
43-
super.toJson();
55+
return super.toJson();
4456
}
4557

4658
return result;

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import BigNumber from 'bignumber.js';
22

3-
import { TransactionBuilder, Transaction, Interface, Schema } from '@bitgo/abstract-substrate';
3+
import { Interface, Schema } from '@bitgo/abstract-substrate';
4+
import { Transaction } from './transaction';
5+
import { TxMethod } from './iface';
6+
import { PolyxBaseBuilder } from './baseBuilder';
47
import { DecodedSignedTx, DecodedSigningPayload, defineMethod, UnsignedTransaction } from '@substrate/txwrapper-core';
58
import { BaseCoin as CoinConfig } from '@bitgo/statics';
69
import { BaseAddress, InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
710

811
import utils from './utils';
912

10-
export class TransferBuilder extends TransactionBuilder {
13+
export class TransferBuilder extends PolyxBaseBuilder<TxMethod, Transaction> {
1114
protected _amount: string;
1215
protected _to: string;
1316
protected _memo: string;
@@ -98,7 +101,9 @@ export class TransferBuilder extends TransactionBuilder {
98101
if (this._method?.name === Interface.MethodNames.TransferWithMemo) {
99102
const txMethod = this._method.args as Interface.TransferWithMemoArgs;
100103
this.amount(txMethod.value);
101-
this.to({ address: utils.decodeSubstrateAddress(txMethod.dest.id, this.getAddressFormat()) });
104+
this.to({
105+
address: utils.decodeSubstrateAddress(txMethod.dest.id, utils.getAddressFormat(this._coinConfig.name)),
106+
});
102107
this.memo(txMethod.memo);
103108
} else {
104109
throw new InvalidTransactionError(`Invalid Transaction Type: ${this._method?.name}. Expected transferWithMemo`);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { TransactionBuilder, Transaction } from '@bitgo/abstract-substrate';
1+
import { Transaction } from './transaction';
2+
import { PolyxBaseBuilder } from './baseBuilder';
23
import { DecodedSignedTx, DecodedSigningPayload, UnsignedTransaction } from '@substrate/txwrapper-core';
34
import { methods } from '@substrate/txwrapper-polkadot';
45
import { BaseCoin as CoinConfig } from '@bitgo/statics';
@@ -8,7 +9,7 @@ import utils from './utils';
89
import { UnbondArgs } from './iface';
910
import BigNumber from 'bignumber.js';
1011

11-
export class UnbondBuilder extends TransactionBuilder {
12+
export class UnbondBuilder extends PolyxBaseBuilder {
1213
protected _amount: string;
1314

1415
constructor(_coinConfig: Readonly<CoinConfig>) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import { mainnetMaterial, testnetMaterial } from '../resources';
55
import { BatchCallObject } from './iface';
66

77
export class Utils extends SubstrateUtils {
8+
/**
9+
* Get the appropriate address format based on network type
10+
* Returns 12 for mainnet and 42 for testnet
11+
*
12+
* @param coinName The name of the coin
13+
* @returns The address format to use
14+
*/
15+
getAddressFormat(coinName: string): number {
16+
const isMainnet = coinName.toLowerCase() === 'polyx';
17+
return isMainnet ? 12 : 42;
18+
}
19+
820
getMaterial(networkType: NetworkType): Interface.Material {
921
return (networkType === NetworkType.MAINNET ? mainnetMaterial : testnetMaterial) as unknown as Interface.Material;
1022
}

0 commit comments

Comments
 (0)