Skip to content

Commit 0e61c78

Browse files
authored
check if bridge address has code before preparing tx payload (#587)
1 parent b55afd2 commit 0e61c78

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cmd/ulxly/ulxly.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ func bridgeAsset(cmd *cobra.Command) error {
361361
return err
362362
}
363363
defer client.Close()
364+
364365
// Initialize and assign variables required to send transaction payload
365366
bridgeV2, toAddress, auth, err := generateTransactionPayload(cmd.Context(), client, bridgeAddress, privateKey, gasLimit, destinationAddress, chainID)
366367
if err != nil {
@@ -1113,6 +1114,18 @@ func readDeposits(rawDeposits []byte, depositNumber uint32) error {
11131114
return nil
11141115
}
11151116

1117+
func ensureCodePresent(ctx context.Context, client *ethclient.Client, address string) error {
1118+
code, err := client.CodeAt(ctx, common.HexToAddress(address), nil)
1119+
if err != nil {
1120+
log.Error().Err(err).Str("address", address).Msg("error getting code at address")
1121+
return err
1122+
}
1123+
if len(code) == 0 {
1124+
return fmt.Errorf("address %s has no code", address)
1125+
}
1126+
return nil
1127+
}
1128+
11161129
// String will create the json representation of the proof
11171130
func String[T any](p T) string {
11181131
jsonBytes, err := json.Marshal(p)
@@ -1308,6 +1321,13 @@ func generateEmptyHashes(height uint8) []common.Hash {
13081321
}
13091322

13101323
func generateTransactionPayload(ctx context.Context, client *ethclient.Client, ulxlyInputArgBridge string, ulxlyInputArgPvtKey string, ulxlyInputArgGasLimit uint64, ulxlyInputArgDestAddr string, ulxlyInputArgChainID string) (bridgeV2 *ulxly.Ulxly, toAddress common.Address, opts *bind.TransactOpts, err error) {
1324+
// checks if bridge address has code
1325+
err = ensureCodePresent(ctx, client, ulxlyInputArgBridge)
1326+
if err != nil {
1327+
err = fmt.Errorf("bridge code check err: %w", err)
1328+
return
1329+
}
1330+
13111331
ulxlyInputArgPvtKey = strings.TrimPrefix(ulxlyInputArgPvtKey, "0x")
13121332
bridgeV2, err = ulxly.NewUlxly(common.HexToAddress(ulxlyInputArgBridge), client)
13131333
if err != nil {

0 commit comments

Comments
 (0)