Skip to content

Commit f9c23bc

Browse files
committed
fix(sdk-coin-polyx): all fixed
TICKET: WIN-5425
1 parent 2461fbf commit f9c23bc

File tree

5 files changed

+43
-33
lines changed

5 files changed

+43
-33
lines changed

modules/abstract-substrate/src/lib/transactionBuilder.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ import { Transaction } from './transaction';
2727
import { BaseTransactionSchema, SignedTransactionSchema, SigningPayloadTransactionSchema } from './txnSchema';
2828
import utils from './utils';
2929

30-
export abstract class TransactionBuilder extends BaseTransactionBuilder {
31-
protected _transaction: Transaction;
30+
export abstract class TransactionBuilder<
31+
TMethod = TxMethod,
32+
TTransaction extends Transaction = Transaction
33+
> extends BaseTransactionBuilder {
34+
protected _transaction: TTransaction;
3235
protected _keyPair: KeyPair;
3336
protected _signature?: string;
3437
protected _sender: string;
@@ -39,7 +42,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
3942
protected _tip?: number;
4043
protected _eraPeriod?: number;
4144
protected _registry: TypeRegistry;
42-
protected _method?: TxMethod;
45+
protected _method?: TMethod;
4346
protected _material: Material;
4447
// signatures that will be used to sign a transaction when building
4548
// not the same as the _signatures in transaction which is the signature in
@@ -48,7 +51,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
4851

4952
constructor(_coinConfig: Readonly<CoinConfig>) {
5053
super(_coinConfig);
51-
this._transaction = new Transaction(_coinConfig);
54+
this._transaction = new Transaction(_coinConfig) as TTransaction;
5255
}
5356

5457
/**
@@ -134,7 +137,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
134137
return this;
135138
}
136139

137-
private method(method: TxMethod): this {
140+
private method(method: TMethod): this {
138141
this._method = method;
139142
return this;
140143
}
@@ -154,17 +157,17 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
154157
}
155158

156159
/** @inheritdoc */
157-
protected get transaction(): Transaction {
160+
protected get transaction(): TTransaction {
158161
return this._transaction;
159162
}
160163

161164
/** @inheritdoc */
162-
protected set transaction(transaction: Transaction) {
165+
protected set transaction(transaction: TTransaction) {
163166
this._transaction = transaction;
164167
}
165168

166169
/** @inheritdoc */
167-
protected fromImplementation(rawTransaction: string): Transaction {
170+
protected fromImplementation(rawTransaction: string): TTransaction {
168171
const decodedTxn = decode(rawTransaction, {
169172
metadataRpc: this._material.metadata,
170173
registry: this._registry,
@@ -186,7 +189,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
186189
if (decodedTxn.tip) {
187190
this.fee({ amount: `${decodedTxn.tip}`, type: 'tip' });
188191
}
189-
this.method(decodedTxn.method as unknown as TxMethod);
192+
this.method(decodedTxn.method as unknown as TMethod);
190193
return this._transaction;
191194
}
192195

@@ -197,7 +200,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
197200
}
198201

199202
/** @inheritdoc */
200-
protected async buildImplementation(): Promise<Transaction> {
203+
protected async buildImplementation(): Promise<TTransaction> {
201204
this.transaction.setTransaction(this.buildTransaction());
202205
this.transaction.transactionType(this.transactionType);
203206
this.transaction.registry(this._registry);
@@ -320,7 +323,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
320323
}
321324

322325
/** @inheritdoc */
323-
validateTransaction(_: Transaction): void {
326+
validateTransaction(_: TTransaction): void {
324327
this.validateBaseFields(
325328
this._sender,
326329
this._blockNumber,
@@ -382,7 +385,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
382385
}
383386

384387
/** @inheritdoc */
385-
protected signImplementation({ key }: BaseKey): Transaction {
388+
protected signImplementation({ key }: BaseKey): TTransaction {
386389
this._keyPair = new KeyPair({ prv: key });
387390
return this._transaction;
388391
}

modules/sdk-coin-polyx/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@bitgo/abstract-substrate": "^1.4.9",
4444
"@substrate/txwrapper-core": "7.5.2",
4545
"@substrate/txwrapper-polkadot": "7.5.2",
46+
"@polkadot/keyring": "13.3.1",
4647
"@bitgo/sdk-core": "^33.2.0",
4748
"@bitgo/statics": "^52.2.0",
4849
"bignumber.js": "^9.1.2",

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { TransactionBuilder, Interface } from '@bitgo/abstract-substrate';
1+
import { TransactionBuilder, Interface, utils } from '@bitgo/abstract-substrate';
22
import { DecodedSignedTx, DecodedSigningPayload, defineMethod, UnsignedTransaction } from '@substrate/txwrapper-core';
33
import { BaseCoin as CoinConfig } from '@bitgo/statics';
44
import { TransactionType, BaseAddress, InvalidTransactionError } from '@bitgo/sdk-core';
5-
import { RegisterDidWithCDDArgs } from './iface';
5+
import { RegisterDidWithCDDArgs, TxMethod } from './iface';
66
import { RegisterDidWithCDDTransactionSchema } from './txnSchema';
77
import { Transaction } from './transaction';
88

9-
export class RegisterDidWithCDDBuilder extends TransactionBuilder {
9+
export class RegisterDidWithCDDBuilder extends TransactionBuilder<TxMethod, Transaction> {
1010
protected _to: string;
11+
protected _method: TxMethod;
1112

1213
constructor(_coinConfig: Readonly<CoinConfig>) {
1314
super(_coinConfig);
@@ -44,15 +45,16 @@ export class RegisterDidWithCDDBuilder extends TransactionBuilder {
4445
}
4546

4647
/** @inheritdoc */
47-
// protected fromImplementation(rawTransaction: string): Transaction {
48-
// if (this._method?.name === Interface.MethodNames.RegisterDidWithCDD) {
49-
// const txMethod = this._method.args as RegisterDidWithCDDArgs;
50-
// this.to({ address: utils.decodeSubstrateAddress(txMethod.targetAccount, this.getAddressFormat()) });
51-
// } else {
52-
// throw new InvalidTransactionError(`Invalid Transaction Type: ${this._method?.name}. Expected transferWithMemo`);
53-
// }
54-
// return this._transaction;
55-
// }
48+
protected fromImplementation(rawTransaction: string): Transaction {
49+
const tx = super.fromImplementation(rawTransaction);
50+
if (this._method?.name === Interface.MethodNames.RegisterDidWithCDD) {
51+
const txMethod = this._method.args as RegisterDidWithCDDArgs;
52+
this.to({ address: utils.decodeSubstrateAddress(txMethod.targetAccount, this.getAddressFormat()) });
53+
} else {
54+
throw new InvalidTransactionError(`Invalid Transaction Type: ${this._method?.name}. Expected transferWithMemo`);
55+
}
56+
return tx;
57+
}
5658

5759
/** @inheritdoc */
5860
validateDecodedTransaction(decodedTxn: DecodedSigningPayload | DecodedSignedTx, rawTransaction?: string): void {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { TransferBuilder } from './transferBuilder';
55
import { RegisterDidWithCDDBuilder } from './registerDidWithCDDBuilder';
66
import utils from './utils';
77
import { Interface, SingletonRegistry, TransactionBuilder } from './';
8+
import { TxMethod } from './iface';
9+
import { Transaction as BaseTransaction } from '@bitgo/abstract-substrate';
10+
import { Transaction as PolyxTransaction } from './transaction';
11+
12+
export type SupportedTransaction = BaseTransaction | PolyxTransaction;
813

914
export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
1015
protected _material: Interface.Material;
@@ -26,7 +31,7 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
2631
throw new NotImplementedError(`walletInitialization for ${this._coinConfig.name} not implemented`);
2732
}
2833

29-
from(rawTxn: string): TransactionBuilder {
34+
from(rawTxn: string): TransactionBuilder<TxMethod, SupportedTransaction> {
3035
const builder = this.getBuilder(rawTxn);
3136
builder.from(rawTxn);
3237
return builder;
@@ -37,7 +42,7 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
3742
return this;
3843
}
3944

40-
private getBuilder(rawTxn: string): TransactionBuilder {
45+
private getBuilder(rawTxn: string): TransactionBuilder<TxMethod, SupportedTransaction> {
4146
const registry = SingletonRegistry.getInstance(this._material);
4247
const decodedTxn = decode(rawTxn, {
4348
metadataRpc: this._material.metadata,

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,21 @@ describe('Polyx Register DID with CDD builder Builder', () => {
126126
.referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d');
127127
const tx = await builder.build();
128128
const txJson = tx.toJson();
129-
should.deepEqual(txJson.to, '5F8jxKE81GhFrphyfMFr5UjeAz5wS4AaZFmeFPnf8wTetD72');
130-
should.deepEqual(txJson.sender, '5CLYvxwx4PUS678MNuhNJ9EfpUU9utrYCz9WVxovac4u9AYD');
129+
should.deepEqual(txJson.to, '5EFWg5wKTgkFE9XCxigBYPYKQg173djwSmRbkALCdL1jFVUU');
130+
should.deepEqual(txJson.sender, '5E7XWJRysj27EzibT4duRxrBQT9Qfa7Z5nAAvJmvd32nhkjH');
131131
should.deepEqual(txJson.blockNumber, 3933);
132132
should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d');
133133
should.deepEqual(txJson.genesisHash, genesisHash);
134134
should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion));
135-
should.deepEqual(txJson.nonce, 34);
135+
should.deepEqual(txJson.nonce, 1);
136136
should.deepEqual(txJson.tip, 0);
137137
should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion));
138138
should.deepEqual(txJson.chainName, chainName);
139139
should.deepEqual(txJson.eraPeriod, 64);
140140
});
141141

142142
it('should build from raw unsigned tx', async () => {
143-
builder.from(rawTx.transfer.unsigned);
143+
builder.from(rawTx.cddTransaction.unsigned);
144144
builder
145145
.validity({ firstValid: 3933, maxDuration: 64 })
146146
.referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d')
@@ -149,14 +149,13 @@ describe('Polyx Register DID with CDD builder Builder', () => {
149149

150150
const tx = await builder.build();
151151
const txJson = tx.toJson();
152-
should.deepEqual(txJson.amount, '2000000000');
153-
should.deepEqual(txJson.to, '5F8jxKE81GhFrphyfMFr5UjeAz5wS4AaZFmeFPnf8wTetD72');
152+
should.deepEqual(txJson.to, '5EFWg5wKTgkFE9XCxigBYPYKQg173djwSmRbkALCdL1jFVUU');
154153
should.deepEqual(txJson.sender, sender.address);
155154
should.deepEqual(txJson.blockNumber, 3933);
156155
should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d');
157156
should.deepEqual(txJson.genesisHash, genesisHash);
158157
should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion));
159-
should.deepEqual(txJson.nonce, 36);
158+
should.deepEqual(txJson.nonce, 1);
160159
should.deepEqual(txJson.eraPeriod, 64);
161160
should.deepEqual(txJson.tip, 0);
162161
should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion));

0 commit comments

Comments
 (0)