|
| 1 | +import { |
| 2 | + CosmosTransaction, |
| 3 | + CosmosTransactionBuilder, |
| 4 | + CosmosTransferBuilder, |
| 5 | + StakingActivateBuilder, |
| 6 | + StakingDeactivateBuilder, |
| 7 | + StakingRedelegateBuilder, |
| 8 | + StakingWithdrawRewardsBuilder, |
| 9 | + ContractCallBuilder, |
| 10 | +} from '@bitgo/abstract-cosmos'; |
| 11 | +import { BaseCoin as CoinConfig, CosmosNetwork } from '@bitgo/statics'; |
| 12 | +import { BaseTransactionBuilderFactory, InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 13 | +import { Utils } from './utils'; |
| 14 | + |
| 15 | +export class TransactionBuilderFactory extends BaseTransactionBuilderFactory { |
| 16 | + private readonly _utils: Utils; |
| 17 | + |
| 18 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 19 | + super(_coinConfig); |
| 20 | + this._utils = new Utils(_coinConfig.network as CosmosNetwork); |
| 21 | + } |
| 22 | + |
| 23 | + /** @inheritdoc */ |
| 24 | + from(raw: string): CosmosTransactionBuilder { |
| 25 | + const tx = new CosmosTransaction(this._coinConfig, this._utils); |
| 26 | + tx.enrichTransactionDetailsFromRawTransaction(raw); |
| 27 | + try { |
| 28 | + switch (tx.type) { |
| 29 | + case TransactionType.Send: |
| 30 | + return this.getTransferBuilder(tx); |
| 31 | + case TransactionType.StakingActivate: |
| 32 | + return this.getStakingActivateBuilder(tx); |
| 33 | + case TransactionType.StakingDeactivate: |
| 34 | + return this.getStakingDeactivateBuilder(tx); |
| 35 | + case TransactionType.StakingWithdraw: |
| 36 | + return this.getStakingWithdrawRewardsBuilder(tx); |
| 37 | + case TransactionType.ContractCall: |
| 38 | + return this.getContractCallBuilder(tx); |
| 39 | + case TransactionType.StakingRedelegate: |
| 40 | + return this.getStakingRedelegateBuilder(tx); |
| 41 | + default: |
| 42 | + throw new InvalidTransactionError('Invalid transaction'); |
| 43 | + } |
| 44 | + } catch (e) { |
| 45 | + throw new InvalidTransactionError('Invalid transaction: ' + e.message); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** @inheritdoc */ |
| 50 | + getTransferBuilder(tx?: CosmosTransaction): CosmosTransferBuilder { |
| 51 | + return this.initializeBuilder(tx, new CosmosTransferBuilder(this._coinConfig, this._utils)); |
| 52 | + } |
| 53 | + |
| 54 | + /** @inheritdoc */ |
| 55 | + getStakingActivateBuilder(tx?: CosmosTransaction): StakingActivateBuilder { |
| 56 | + return this.initializeBuilder(tx, new StakingActivateBuilder(this._coinConfig, this._utils)); |
| 57 | + } |
| 58 | + |
| 59 | + /** @inheritdoc */ |
| 60 | + getStakingDeactivateBuilder(tx?: CosmosTransaction): StakingDeactivateBuilder { |
| 61 | + return this.initializeBuilder(tx, new StakingDeactivateBuilder(this._coinConfig, this._utils)); |
| 62 | + } |
| 63 | + |
| 64 | + /** @inheritdoc */ |
| 65 | + getStakingWithdrawRewardsBuilder(tx?: CosmosTransaction): StakingWithdrawRewardsBuilder { |
| 66 | + return this.initializeBuilder(tx, new StakingWithdrawRewardsBuilder(this._coinConfig, this._utils)); |
| 67 | + } |
| 68 | + |
| 69 | + /** @inheritdoc */ |
| 70 | + getStakingRedelegateBuilder(tx?: CosmosTransaction): StakingRedelegateBuilder { |
| 71 | + return this.initializeBuilder(tx, new StakingRedelegateBuilder(this._coinConfig, this._utils)); |
| 72 | + } |
| 73 | + |
| 74 | + /** @inheritdoc */ |
| 75 | + getContractCallBuilder(tx?: CosmosTransaction): ContractCallBuilder { |
| 76 | + return this.initializeBuilder(tx, new ContractCallBuilder(this._coinConfig, this._utils)); |
| 77 | + } |
| 78 | + |
| 79 | + /** @inheritdoc */ |
| 80 | + getWalletInitializationBuilder(): void { |
| 81 | + throw new Error('Method not implemented.'); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Initialize the builder with the given transaction |
| 86 | + * |
| 87 | + * @param {CosmosTransaction | undefined} tx - the transaction used to initialize the builder |
| 88 | + * @param {CosmosTransactionBuilder} builder - the builder to be initialized |
| 89 | + * @returns {CosmosTransactionBuilder} the builder initialized |
| 90 | + */ |
| 91 | + private initializeBuilder<T extends CosmosTransactionBuilder>(tx: CosmosTransaction | undefined, builder: T): T { |
| 92 | + if (tx) { |
| 93 | + builder.initBuilder(tx); |
| 94 | + } |
| 95 | + return builder; |
| 96 | + } |
| 97 | +} |
0 commit comments