@@ -3,10 +3,9 @@ package util
33import (
44 "context"
55 "math/big"
6- "strings"
76
87 "github.com/0xPolygon/polygon-cli/bindings/multicall3"
9- "github.com/ethereum/go-ethereum/accounts/abi "
8+ "github.com/0xPolygon/polygon-cli/bindings/tokens "
109 "github.com/ethereum/go-ethereum/accounts/abi/bind/v2"
1110 "github.com/ethereum/go-ethereum/common"
1211 "github.com/ethereum/go-ethereum/core/types"
@@ -140,23 +139,41 @@ func Multicall3FundAccountsWithNativeToken(c *ethclient.Client, tops *bind.Trans
140139 return sc .Aggregate3Value (tops , calls )
141140}
142141
143- func Multicall3MintERC20ToAccounts (ctx context.Context , c * ethclient.Client , tops * bind.TransactOpts , accounts []common.Address , tokenAddress common.Address , amount * big.Int , customAddr * common.Address ) (* types.Transaction , error ) {
144- _ , sc , err := Multicall3New (c , customAddr )
142+ func Multicall3FundAccountsWithERC20Token (ctx context.Context , c * ethclient.Client , tops * bind.TransactOpts , accounts []common.Address , tokenAddress common.Address , amount * big.Int , customAddr * common.Address ) (approveTx , transfersTx * types.Transaction , err error ) {
143+ scAddr , sc , err := Multicall3New (c , customAddr )
145144 if err != nil {
146- return nil , err
145+ return nil , nil , err
147146 }
148147
149- // Create ABI for mint(address, uint256) function
150- mintABI , err := abi .JSON (strings .NewReader (`[{"type":"function","name":"mint","inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"outputs":[],"stateMutability":"nonpayable"}]` ))
148+ erc20 , err := tokens .NewERC20 (tokenAddress , c )
151149 if err != nil {
152- return nil , err
150+ return nil , nil , err
151+ }
152+
153+ // Calculate total amount to approve
154+ totalAmount := big .NewInt (0 ).Mul (amount , big .NewInt (int64 (len (accounts ))))
155+
156+ // Prepare approve calldata for the Multicall3 contract to spend tokens
157+ approveTx , err = erc20 .Approve (tops , scAddr , totalAmount )
158+ if err != nil {
159+ return nil , nil , err
160+ }
161+
162+ receipt , err := bind .WaitMined (ctx , c , approveTx .Hash ())
163+ if err != nil || receipt == nil || receipt .Status != 1 {
164+ return approveTx , nil , err
165+ }
166+
167+ erc20ABI , err := tokens .ERC20MetaData .GetAbi ()
168+ if err != nil {
169+ return approveTx , nil , err
153170 }
154171
155172 calls := make ([]multicall3.Multicall3Call3 , 0 , len (accounts ))
156173 for _ , account := range accounts {
157- callData , iErr := mintABI .Pack ("mint" , account , amount )
174+ callData , iErr := erc20ABI .Pack ("transferFrom" , tops . From , account , amount )
158175 if iErr != nil {
159- return nil , iErr
176+ return approveTx , nil , iErr
160177 }
161178
162179 calls = append (calls , multicall3.Multicall3Call3 {
@@ -166,5 +183,10 @@ func Multicall3MintERC20ToAccounts(ctx context.Context, c *ethclient.Client, top
166183 })
167184 }
168185
169- return sc .Aggregate3 (tops , calls )
186+ transfersTx , err = sc .Aggregate3 (tops , calls )
187+ if err != nil {
188+ return approveTx , nil , err
189+ }
190+
191+ return approveTx , transfersTx , nil
170192}
0 commit comments