|
| 1 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 2 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 3 | +import assert from 'assert'; |
| 4 | +import { InstructionBuilderTypes } from './constants'; |
| 5 | +import { AtaRecoverNested } from './iface'; |
| 6 | +import { Transaction } from './transaction'; |
| 7 | +import { TransactionBuilder } from './transactionBuilder'; |
| 8 | +import { validateAddress } from './utils'; |
| 9 | + |
| 10 | +export class RecoverNestedAtaBuilder extends TransactionBuilder { |
| 11 | + protected _nestedAccountAddress: string; |
| 12 | + protected _nestedMintAddress: string; |
| 13 | + protected _destinationAccountAddress: string; |
| 14 | + protected _ownerAccountAddress: string; |
| 15 | + protected _ownerMintAddress: string; |
| 16 | + protected _walletAddress: string; |
| 17 | + |
| 18 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 19 | + super(_coinConfig); |
| 20 | + this._transaction = new Transaction(_coinConfig); |
| 21 | + } |
| 22 | + |
| 23 | + protected get transactionType(): TransactionType { |
| 24 | + return TransactionType.CloseAssociatedTokenAccount; |
| 25 | + } |
| 26 | + |
| 27 | + nestedAccountAddress(nestedAccountAddress: string): this { |
| 28 | + validateAddress(nestedAccountAddress, 'nestedAccountAddress'); |
| 29 | + this._nestedAccountAddress = nestedAccountAddress; |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + nestedMintAddress(nestedMintAddress: string): this { |
| 34 | + validateAddress(nestedMintAddress, 'nestedMintAddress'); |
| 35 | + this._nestedMintAddress = nestedMintAddress; |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + destinationAccountAddress(destinationAccountAddress: string): this { |
| 40 | + validateAddress(destinationAccountAddress, 'destinationAccountAddress'); |
| 41 | + this._destinationAccountAddress = destinationAccountAddress; |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + ownerAccountAddress(ownerAccountAddress: string): this { |
| 46 | + validateAddress(ownerAccountAddress, 'ownerAccountAddress'); |
| 47 | + this._ownerAccountAddress = ownerAccountAddress; |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + ownerMintAddress(ownerMintAddress: string): this { |
| 52 | + validateAddress(ownerMintAddress, 'ownerMintAddress'); |
| 53 | + this._ownerMintAddress = ownerMintAddress; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + walletAddress(walletAddress: string): this { |
| 58 | + validateAddress(walletAddress, 'walletAddress'); |
| 59 | + this._walletAddress = walletAddress; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /** @inheritDoc */ |
| 64 | + initBuilder(tx: Transaction): void { |
| 65 | + super.initBuilder(tx); |
| 66 | + for (const instruction of this._instructionsData) { |
| 67 | + if (instruction.type === InstructionBuilderTypes.RecoverNestedAssociatedTokenAccount) { |
| 68 | + const recoverNestedInstruction: AtaRecoverNested = instruction; |
| 69 | + this.nestedAccountAddress(recoverNestedInstruction.params.nestedAccountAddress); |
| 70 | + this.nestedMintAddress(recoverNestedInstruction.params.nestedMintAddress); |
| 71 | + this.destinationAccountAddress(recoverNestedInstruction.params.destinationAccountAddress); |
| 72 | + this.ownerAccountAddress(recoverNestedInstruction.params.ownerAccountAddress); |
| 73 | + this.ownerMintAddress(recoverNestedInstruction.params.ownerMintAddress); |
| 74 | + this.walletAddress(recoverNestedInstruction.params.walletAddress); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** @inheritdoc */ |
| 80 | + protected async buildImplementation(): Promise<Transaction> { |
| 81 | + assert(this._nestedAccountAddress, 'nestedAccountAddress must be set before building the transaction'); |
| 82 | + assert(this._nestedMintAddress, 'nestedMintAddress must be set before building the transaction'); |
| 83 | + assert(this._destinationAccountAddress, 'destinationAccountAddress must be set before building the transaction'); |
| 84 | + assert(this._ownerAccountAddress, 'ownerAccountAddress must be set before building the transaction'); |
| 85 | + assert(this._ownerMintAddress, 'ownerMintAddress must be set before building the transaction'); |
| 86 | + assert(this._walletAddress, 'walletAddress must be set before building the transaction'); |
| 87 | + |
| 88 | + const recoverNestedData: AtaRecoverNested = { |
| 89 | + type: InstructionBuilderTypes.RecoverNestedAssociatedTokenAccount, |
| 90 | + params: { |
| 91 | + nestedAccountAddress: this._nestedAccountAddress, |
| 92 | + nestedMintAddress: this._nestedMintAddress, |
| 93 | + destinationAccountAddress: this._destinationAccountAddress, |
| 94 | + ownerAccountAddress: this._ownerAccountAddress, |
| 95 | + ownerMintAddress: this._ownerMintAddress, |
| 96 | + walletAddress: this._walletAddress, |
| 97 | + }, |
| 98 | + }; |
| 99 | + |
| 100 | + this._instructionsData = [recoverNestedData]; |
| 101 | + |
| 102 | + return await super.buildImplementation(); |
| 103 | + } |
| 104 | +} |
0 commit comments