|
| 1 | +import { BuildTransactionError } from '@bitgo/sdk-core'; |
| 2 | +import { isValidEthAddress } from './utils'; |
| 3 | +import { buildApproveCalldata, buildDepositCalldata, approveMethodId, depositMethodId } from './zamaStakingUtils'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Distinguishes between the two staking operations in the delegate flow. |
| 7 | + */ |
| 8 | +export enum ZamaStakingOperationType { |
| 9 | + /** ERC20 approve — grant OperatorStaking spending allowance. */ |
| 10 | + APPROVE = 'approve', |
| 11 | + /** ERC4626 deposit — deposit ZAMA tokens into the OperatorStaking vault. */ |
| 12 | + DEPOSIT = 'deposit', |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * The output of ZamaStakingBuilder.build(). |
| 17 | + * |
| 18 | + * Contains everything the TransactionBuilder needs to construct a signed Ethereum |
| 19 | + * transaction: the target contract address, ABI-encoded calldata, and ETH value. |
| 20 | + */ |
| 21 | +export interface ZamaStakingBuildResult { |
| 22 | + /** Target contract address (token for approve, operator for deposit). */ |
| 23 | + address: string; |
| 24 | + /** ABI-encoded calldata (approve or deposit). */ |
| 25 | + data: string; |
| 26 | + /** Always '0' — staking transactions carry no ETH value. */ |
| 27 | + value: string; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Fluent builder for ZAMA ERC-4626 staking delegate flow transactions. |
| 32 | + * |
| 33 | + * Used as a helper owned by the abstract-eth TransactionBuilder. The TransactionBuilder |
| 34 | + * exposes `stakingApprove()` and `stakingDeposit()` methods that create and return this |
| 35 | + * builder for fluent configuration. |
| 36 | + * |
| 37 | + * The delegate flow consists of two transactions: |
| 38 | + * |
| 39 | + * 1. **Approve (TX1):** ERC20 `approve(address,uint256)` on the ZAMA token contract, |
| 40 | + * granting the OperatorStaking contract permission to transfer tokens. |
| 41 | + * |
| 42 | + * 2. **Deposit (TX2):** ERC4626 `deposit(uint256,address)` on the OperatorStaking |
| 43 | + * contract, depositing ZAMA tokens and receiving stZAMA shares. |
| 44 | + * |
| 45 | + * Usage via TransactionBuilder: |
| 46 | + * txBuilder.type(TransactionType.ContractCall); |
| 47 | + * txBuilder.stakingApprove() |
| 48 | + * .tokenContractAddress('0xZamaToken...') |
| 49 | + * .spenderAddress('0xOperatorStaking...') |
| 50 | + * .amount('1000000000000000000'); |
| 51 | + * const tx = await txBuilder.build(); |
| 52 | + */ |
| 53 | +export class ZamaStakingBuilder { |
| 54 | + private _type?: ZamaStakingOperationType; |
| 55 | + private _amount?: string; |
| 56 | + private _tokenContractAddress?: string; |
| 57 | + private _spenderAddress?: string; |
| 58 | + private _operatorAddress?: string; |
| 59 | + private _receiverAddress?: string; |
| 60 | + |
| 61 | + /** |
| 62 | + * Set the staking operation type. |
| 63 | + * |
| 64 | + * @param type APPROVE or DEPOSIT |
| 65 | + */ |
| 66 | + type(type: ZamaStakingOperationType): this { |
| 67 | + this._type = type; |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Set the amount of ZAMA tokens (18 decimals, as a decimal string). |
| 73 | + * |
| 74 | + * @param value Token amount |
| 75 | + */ |
| 76 | + amount(value: string): this { |
| 77 | + if (!value || value === '0') { |
| 78 | + throw new BuildTransactionError('Invalid amount for staking transaction'); |
| 79 | + } |
| 80 | + this._amount = value; |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Set the ZAMA ERC-20 token contract address (used for APPROVE). |
| 86 | + * |
| 87 | + * @param address Token contract address |
| 88 | + */ |
| 89 | + tokenContractAddress(address: string): this { |
| 90 | + if (!isValidEthAddress(address)) { |
| 91 | + throw new BuildTransactionError('Invalid token contract address: ' + address); |
| 92 | + } |
| 93 | + this._tokenContractAddress = address; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Set the OperatorStaking contract address — the approved spender (used for APPROVE). |
| 99 | + * |
| 100 | + * @param address Spender address |
| 101 | + */ |
| 102 | + spenderAddress(address: string): this { |
| 103 | + if (!isValidEthAddress(address)) { |
| 104 | + throw new BuildTransactionError('Invalid spender address: ' + address); |
| 105 | + } |
| 106 | + this._spenderAddress = address; |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Set the OperatorStaking contract address to deposit into (used for DEPOSIT). |
| 112 | + * |
| 113 | + * @param address Operator contract address |
| 114 | + */ |
| 115 | + operatorAddress(address: string): this { |
| 116 | + if (!isValidEthAddress(address)) { |
| 117 | + throw new BuildTransactionError('Invalid operator address: ' + address); |
| 118 | + } |
| 119 | + this._operatorAddress = address; |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Set the address that will receive the minted stZAMA shares (used for DEPOSIT). |
| 125 | + * |
| 126 | + * @param address Receiver address |
| 127 | + */ |
| 128 | + receiverAddress(address: string): this { |
| 129 | + if (!isValidEthAddress(address)) { |
| 130 | + throw new BuildTransactionError('Invalid receiver address: ' + address); |
| 131 | + } |
| 132 | + this._receiverAddress = address; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Build the staking transaction. |
| 138 | + * |
| 139 | + * Validates required fields and produces a ZamaStakingBuildResult with the target |
| 140 | + * contract address and ABI-encoded calldata. |
| 141 | + * |
| 142 | + * @returns ZamaStakingBuildResult containing {address, data, value} |
| 143 | + * @throws BuildTransactionError if required fields are missing |
| 144 | + */ |
| 145 | + build(): ZamaStakingBuildResult { |
| 146 | + if (this._type === undefined) { |
| 147 | + throw new BuildTransactionError('Missing staking operation type'); |
| 148 | + } |
| 149 | + if (this._amount === undefined) { |
| 150 | + throw new BuildTransactionError('Missing amount for staking transaction'); |
| 151 | + } |
| 152 | + |
| 153 | + switch (this._type) { |
| 154 | + case ZamaStakingOperationType.APPROVE: |
| 155 | + return this.buildApprove(); |
| 156 | + case ZamaStakingOperationType.DEPOSIT: |
| 157 | + return this.buildDeposit(); |
| 158 | + default: |
| 159 | + throw new BuildTransactionError('Invalid staking operation type: ' + this._type); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private buildApprove(): ZamaStakingBuildResult { |
| 164 | + if (!this._tokenContractAddress) { |
| 165 | + throw new BuildTransactionError('Missing token contract address for approve'); |
| 166 | + } |
| 167 | + if (!this._spenderAddress) { |
| 168 | + throw new BuildTransactionError('Missing spender address for approve'); |
| 169 | + } |
| 170 | + |
| 171 | + return { |
| 172 | + address: this._tokenContractAddress, |
| 173 | + data: buildApproveCalldata(this._spenderAddress, this._amount!), |
| 174 | + value: '0', |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + private buildDeposit(): ZamaStakingBuildResult { |
| 179 | + if (!this._operatorAddress) { |
| 180 | + throw new BuildTransactionError('Missing operator address for deposit'); |
| 181 | + } |
| 182 | + if (!this._receiverAddress) { |
| 183 | + throw new BuildTransactionError('Missing receiver address for deposit'); |
| 184 | + } |
| 185 | + |
| 186 | + return { |
| 187 | + address: this._operatorAddress, |
| 188 | + data: buildDepositCalldata(this._amount!, this._receiverAddress), |
| 189 | + value: '0', |
| 190 | + }; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Classify staking operation type from serialized calldata. |
| 195 | + * |
| 196 | + * @param data ABI-encoded calldata hex string |
| 197 | + * @returns true if the data matches a known staking selector |
| 198 | + */ |
| 199 | + static isStakingData(data: string): boolean { |
| 200 | + if (!data || data.length < 10) { |
| 201 | + return false; |
| 202 | + } |
| 203 | + const selector = data.slice(0, 10).toLowerCase(); |
| 204 | + return selector === approveMethodId.toLowerCase() || selector === depositMethodId.toLowerCase(); |
| 205 | + } |
| 206 | +} |
0 commit comments