Skip to content

Commit 8cbd6c1

Browse files
committed
fix: tx builder acc to gateway & vault changes
1 parent 04cacdb commit 8cbd6c1

File tree

4 files changed

+917
-938
lines changed

4 files changed

+917
-938
lines changed

universalClient/chains/evm/tx_builder.go

Lines changed: 31 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@ import (
1818
uetypes "github.com/pushchain/push-chain-node/x/uexecutor/types"
1919
)
2020

21-
2221
// RevertInstructions represents the struct for revert instruction in contracts
2322
// Matches: struct RevertInstructions { address revertRecipient; bytes revertMsg; }
2423
type RevertInstructions struct {
2524
RevertRecipient ethcommon.Address
2625
RevertMsg []byte
2726
}
2827

29-
// TxBuilder implements OutboundTxBuilder for EVM chains using Vault + Gateway contracts.
30-
//
31-
// Routing:
32-
// - FUNDS, FUNDS_AND_PAYLOAD, PAYLOAD → Vault.finalizeUniversalTx
33-
// - INBOUND_REVERT (native) → Gateway.revertUniversalTx
34-
// - INBOUND_REVERT (ERC20) → Vault.revertUniversalTxToken
28+
// TxBuilder implements OutboundTxBuilder for EVM chains using the Vault contract.
3529
type TxBuilder struct {
3630
rpcClient *RPCClient
3731
chainID string
@@ -83,7 +77,6 @@ func NewTxBuilder(
8377
return tb, nil
8478
}
8579

86-
8780
// GetOutboundSigningRequest creates a signing request from outbound event data
8881
func (tb *TxBuilder) GetOutboundSigningRequest(
8982
ctx context.Context,
@@ -135,14 +128,14 @@ func (tb *TxBuilder) GetOutboundSigningRequest(
135128
return nil, fmt.Errorf("failed to encode function call: %w", err)
136129
}
137130

138-
txValue, toAddress, err := tb.resolveTxParams(funcName, assetAddr, amount)
139-
if err != nil {
140-
return nil, fmt.Errorf("failed to resolve tx params: %w", err)
131+
txValue := big.NewInt(0)
132+
if assetAddr == (ethcommon.Address{}) {
133+
txValue = amount
141134
}
142135

143136
tx := types.NewTransaction(
144137
nonce,
145-
toAddress,
138+
tb.vaultAddress,
146139
txValue,
147140
gasLimit.Uint64(),
148141
gasPrice,
@@ -207,9 +200,9 @@ func (tb *TxBuilder) BroadcastOutboundSigningRequest(
207200
return "", fmt.Errorf("failed to encode function call: %w", err)
208201
}
209202

210-
txValue, toAddress, err := tb.resolveTxParams(funcName, assetAddr, amount)
211-
if err != nil {
212-
return "", fmt.Errorf("failed to resolve tx params: %w", err)
203+
txValue := big.NewInt(0)
204+
if assetAddr == (ethcommon.Address{}) {
205+
txValue = amount
213206
}
214207

215208
gasLimitForTx, err := parseGasLimit(data.GasLimit)
@@ -224,7 +217,7 @@ func (tb *TxBuilder) BroadcastOutboundSigningRequest(
224217

225218
tx := types.NewTransaction(
226219
req.Nonce,
227-
toAddress,
220+
tb.vaultAddress,
228221
txValue,
229222
gasLimitForTx.Uint64(),
230223
gasPrice,
@@ -270,52 +263,22 @@ func (tb *TxBuilder) VerifyBroadcastedTx(ctx context.Context, txHash string) (fo
270263
return true, receiptBlock, confs, uint8(receipt.Status), nil
271264
}
272265

273-
// resolveTxParams determines the transaction value and destination address.
266+
// determineFunctionName determines the Vault function name based on TxType.
274267
//
275-
// Routing:
276-
// - finalizeUniversalTx → vault (value = amount for native)
277-
// - revertUniversalTxToken → vault (value = 0, ERC20 only)
278-
// - revertUniversalTx → gateway (value = amount, native only)
279-
func (tb *TxBuilder) resolveTxParams(funcName string, assetAddr ethcommon.Address, amount *big.Int) (txValue *big.Int, toAddress ethcommon.Address, err error) {
280-
isNative := assetAddr == (ethcommon.Address{})
281-
282-
switch funcName {
283-
case "revertUniversalTx":
284-
// Native revert goes to gateway — no vault needed
285-
return amount, tb.gatewayAddress, nil
286-
287-
case "finalizeUniversalTx":
288-
if isNative {
289-
return amount, tb.vaultAddress, nil
290-
}
291-
return big.NewInt(0), tb.vaultAddress, nil
292-
293-
case "revertUniversalTxToken":
294-
return big.NewInt(0), tb.vaultAddress, nil
295-
296-
default:
297-
return big.NewInt(0), tb.vaultAddress, nil
298-
}
299-
}
300-
301-
// determineFunctionName determines the function name based on TxType and asset type.
302-
//
303-
// Routing:
268+
// Routing (all on Vault):
304269
// - FUNDS, FUNDS_AND_PAYLOAD, PAYLOAD → Vault.finalizeUniversalTx
305-
// - INBOUND_REVERT (native) → Gateway.revertUniversalTx
306-
// - INBOUND_REVERT (ERC20) → Vault.revertUniversalTxToken
270+
// - INBOUND_REVERT → Vault.revertUniversalTx
271+
// - RESCUE_FUNDS → Vault.rescueFunds
307272
func (tb *TxBuilder) determineFunctionName(txType uetypes.TxType, assetAddr ethcommon.Address) string {
308-
isNative := assetAddr == (ethcommon.Address{})
309-
310273
switch txType {
311274
case uetypes.TxType_FUNDS, uetypes.TxType_FUNDS_AND_PAYLOAD, uetypes.TxType_PAYLOAD:
312275
return "finalizeUniversalTx"
313276

314277
case uetypes.TxType_INBOUND_REVERT:
315-
if isNative {
316-
return "revertUniversalTx"
317-
}
318-
return "revertUniversalTxToken"
278+
return "revertUniversalTx"
279+
280+
case uetypes.TxType_RESCUE_FUNDS:
281+
return "rescueFunds"
319282

320283
default:
321284
return "finalizeUniversalTx"
@@ -371,20 +334,21 @@ func (tb *TxBuilder) encodeFunctionCall(
371334

372335
switch funcName {
373336
case "finalizeUniversalTx":
374-
// finalizeUniversalTx(bytes32 txId, bytes32 universalTxId, address pushAccount, address token, address target, uint256 amount, bytes data)
337+
// Vault: finalizeUniversalTx(bytes32 subTxId, bytes32 universalTxId, address pushAccount, address recipient, address token, uint256 amount, bytes data)
375338
arguments = abi.Arguments{
376-
{Type: bytes32Type}, // txId
339+
{Type: bytes32Type}, // subTxId
377340
{Type: bytes32Type}, // universalTxId
378341
{Type: addressType}, // pushAccount
342+
{Type: addressType}, // recipient
379343
{Type: addressType}, // token
380-
{Type: addressType}, // target
381344
{Type: uint256Type}, // amount
382345
{Type: bytesType}, // data
383346
}
384-
values = []interface{}{txID32, universalTxID, pushAccount, assetAddr, target, amount, payloadBytes}
347+
values = []interface{}{txID32, universalTxID, pushAccount, target, assetAddr, amount, payloadBytes}
385348

386-
case "revertUniversalTx":
387-
// Gateway: revertUniversalTx(bytes32 txId, bytes32 universalTxId, uint256 amount, RevertInstructions revertInstruction)
349+
case "revertUniversalTx", "rescueFunds":
350+
// Vault: revertUniversalTx(bytes32 subTxId, bytes32 universalTxId, address token, uint256 amount, RevertInstructions revertInstruction)
351+
// Vault: rescueFunds(bytes32 subTxId, bytes32 universalTxId, address token, uint256 amount, RevertInstructions revertInstruction)
388352
revertMsgBytes, err := hex.DecodeString(removeHexPrefix(data.RevertMsg))
389353
if err != nil {
390354
revertMsgBytes = []byte{}
@@ -394,36 +358,15 @@ func (tb *TxBuilder) encodeFunctionCall(
394358
{Name: "revertMsg", Type: "bytes"},
395359
})
396360
arguments = abi.Arguments{
397-
{Type: bytes32Type}, // txId
398-
{Type: bytes32Type}, // universalTxId
399-
{Type: uint256Type}, // amount
400-
{Type: revertInstructionType}, // revertInstruction
401-
}
402-
values = []interface{}{txID32, universalTxID, amount, RevertInstructions{
403-
RevertRecipient: target,
404-
RevertMsg: revertMsgBytes,
405-
}}
406-
407-
case "revertUniversalTxToken":
408-
// Vault: revertUniversalTxToken(bytes32 txId, bytes32 universalTxId, address token, uint256 amount, RevertInstructions revertInstruction)
409-
revertMsgBytesToken, err := hex.DecodeString(removeHexPrefix(data.RevertMsg))
410-
if err != nil {
411-
revertMsgBytesToken = []byte{}
412-
}
413-
revertInstructionType, _ := abi.NewType("tuple", "", []abi.ArgumentMarshaling{
414-
{Name: "revertRecipient", Type: "address"},
415-
{Name: "revertMsg", Type: "bytes"},
416-
})
417-
arguments = abi.Arguments{
418-
{Type: bytes32Type}, // txId
361+
{Type: bytes32Type}, // subTxId
419362
{Type: bytes32Type}, // universalTxId
420363
{Type: addressType}, // token
421364
{Type: uint256Type}, // amount
422365
{Type: revertInstructionType}, // revertInstruction
423366
}
424367
values = []interface{}{txID32, universalTxID, assetAddr, amount, RevertInstructions{
425368
RevertRecipient: target,
426-
RevertMsg: revertMsgBytesToken,
369+
RevertMsg: revertMsgBytes,
427370
}}
428371

429372
default:
@@ -448,12 +391,12 @@ func (tb *TxBuilder) getFunctionSignature(funcName string, _ bool) string {
448391
return "finalizeUniversalTx(bytes32,bytes32,address,address,address,uint256,bytes)"
449392

450393
case "revertUniversalTx":
451-
// Gateway: revertUniversalTx(bytes32,bytes32,uint256,(address,bytes))
452-
return "revertUniversalTx(bytes32,bytes32,uint256,(address,bytes))"
394+
// Vault: revertUniversalTx(bytes32,bytes32,address,uint256,(address,bytes))
395+
return "revertUniversalTx(bytes32,bytes32,address,uint256,(address,bytes))"
453396

454-
case "revertUniversalTxToken":
455-
// Vault: revertUniversalTxToken(bytes32,bytes32,address,uint256,(address,bytes))
456-
return "revertUniversalTxToken(bytes32,bytes32,address,uint256,(address,bytes))"
397+
case "rescueFunds":
398+
// Vault: rescueFunds(bytes32,bytes32,address,uint256,(address,bytes))
399+
return "rescueFunds(bytes32,bytes32,address,uint256,(address,bytes))"
457400

458401
default:
459402
return ""

0 commit comments

Comments
 (0)