code.vegaprotocol.io/vega@v0.79.0/core/bridges/erc20_asset_pool.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package bridges 17 18 import ( 19 "fmt" 20 21 "code.vegaprotocol.io/vega/libs/num" 22 23 "github.com/ethereum/go-ethereum/accounts/abi" 24 ethcmn "github.com/ethereum/go-ethereum/common" 25 ) 26 27 type ERC20AssetPool struct { 28 signer Signer 29 poolAddr string 30 chainID string 31 v1 bool 32 } 33 34 func NewERC20AssetPool(signer Signer, poolAddr, chainID string, v1 bool) *ERC20AssetPool { 35 return &ERC20AssetPool{ 36 signer: signer, 37 poolAddr: poolAddr, 38 chainID: chainID, 39 v1: v1, 40 } 41 } 42 43 func (e ERC20AssetPool) SetBridgeAddress( 44 newAddress string, 45 nonce *num.Uint, 46 ) (*SignaturePayload, error) { 47 typAddr, err := abi.NewType("address", "", nil) 48 if err != nil { 49 return nil, err 50 } 51 typString, err := abi.NewType("string", "", nil) 52 if err != nil { 53 return nil, err 54 } 55 typU256, err := abi.NewType("uint256", "", nil) 56 if err != nil { 57 return nil, err 58 } 59 60 args := abi.Arguments([]abi.Argument{ 61 { 62 Name: "address", 63 Type: typAddr, 64 }, 65 { 66 Name: "nonce", 67 Type: typU256, 68 }, 69 { 70 Name: "func_name", 71 Type: typString, 72 }, 73 }) 74 75 newAddressEth := ethcmn.HexToAddress(newAddress) 76 buf, err := args.Pack([]interface{}{ 77 newAddressEth, nonce.BigInt(), "setBridgeAddress", 78 }...) 79 if err != nil { 80 return nil, fmt.Errorf("couldn't pack abi message: %w", err) 81 } 82 83 msg, err := packScheme(buf, e.poolAddr, e.chainID, e.v1) 84 if err != nil { 85 return nil, fmt.Errorf("couldn't pack abi message: %w", err) 86 } 87 88 return sign(e.signer, msg) 89 } 90 91 func (e ERC20AssetPool) SetMultiSigControl( 92 newAddress string, 93 nonce *num.Uint, 94 ) (*SignaturePayload, error) { 95 typAddr, err := abi.NewType("address", "", nil) 96 if err != nil { 97 return nil, err 98 } 99 typString, err := abi.NewType("string", "", nil) 100 if err != nil { 101 return nil, err 102 } 103 typU256, err := abi.NewType("uint256", "", nil) 104 if err != nil { 105 return nil, err 106 } 107 108 args := abi.Arguments([]abi.Argument{ 109 { 110 Name: "address", 111 Type: typAddr, 112 }, 113 { 114 Name: "nonce", 115 Type: typU256, 116 }, 117 { 118 Name: "func_name", 119 Type: typString, 120 }, 121 }) 122 123 newAddressEth := ethcmn.HexToAddress(newAddress) 124 buf, err := args.Pack([]interface{}{ 125 newAddressEth, nonce.BigInt(), "setMultisigControl", 126 }...) 127 if err != nil { 128 return nil, fmt.Errorf("couldn't pack abi message: %w", err) 129 } 130 131 msg, err := packScheme(buf, e.poolAddr, e.chainID, e.v1) 132 if err != nil { 133 return nil, fmt.Errorf("couldn't pack abi message: %w", err) 134 } 135 136 return sign(e.signer, msg) 137 }