Skip to content

Commit ae2118d

Browse files
committed
feat: mint to multicall3 then transfer to wallets
Signed-off-by: Ji Hwan <jkim@polygon.technology>
1 parent df64673 commit ae2118d

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

cmd/fund/fund.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,15 @@ func fundWalletsWithMulticall3(ctx context.Context, c *ethclient.Client, tops *b
418418
var tx *types.Transaction
419419
if params.TokenAddress != "" {
420420
tokenAddress := common.HexToAddress(params.TokenAddress)
421-
var txApprove *types.Transaction
422-
txApprove, tx, iErr = util.Multicall3FundAccountsWithERC20Token(ctx, c, tops, accs, tokenAddress, params.TokenAmount, multicall3Addr)
423-
if txApprove != nil {
421+
var txMint *types.Transaction
422+
txMint, tx, iErr = util.Multicall3FundAccountsWithERC20Token(ctx, c, tops, accs, tokenAddress, params.TokenAmount, multicall3Addr)
423+
if txMint != nil {
424424
log.Info().
425-
Stringer("txHash", txApprove.Hash()).
425+
Stringer("txHash", txMint.Hash()).
426426
Int("done", i+1).
427427
Uint64("of", uint64(len(wallets))).
428-
Msg("transaction to approve ERC20 token spending by multicall3 sent")
429-
txsCh <- txApprove
428+
Msg("transaction to mint ERC20 tokens to multicall3 sent")
429+
txsCh <- txMint
430430
}
431431
} else {
432432
tx, iErr = util.Multicall3FundAccountsWithNativeToken(c, tops, accs, params.FundingAmountInWei, multicall3Addr)

util/multicall3.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package util
33
import (
44
"context"
55
"math/big"
6+
"strings"
67

78
"github.com/0xPolygon/polygon-cli/bindings/multicall3"
89
"github.com/0xPolygon/polygon-cli/bindings/tokens"
10+
"github.com/ethereum/go-ethereum/accounts/abi"
911
"github.com/ethereum/go-ethereum/accounts/abi/bind/v2"
1012
"github.com/ethereum/go-ethereum/common"
1113
"github.com/ethereum/go-ethereum/core/types"
@@ -145,16 +147,20 @@ func Multicall3FundAccountsWithERC20Token(ctx context.Context, c *ethclient.Clie
145147
return nil, nil, err
146148
}
147149

148-
erc20, err := tokens.NewERC20(tokenAddress, c)
150+
// Calculate total amount needed
151+
totalAmount := big.NewInt(0).Mul(amount, big.NewInt(int64(len(accounts))))
152+
153+
// Create ABI for mint(address, uint256) function to mint tokens to multicall3
154+
mintABI, err := abi.JSON(strings.NewReader(`[{"type":"function","name":"mint","inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"outputs":[],"stateMutability":"nonpayable"}]`))
149155
if err != nil {
150156
return nil, nil, err
151157
}
152158

153-
// Calculate total amount to approve
154-
totalAmount := big.NewInt(0).Mul(amount, big.NewInt(int64(len(accounts))))
159+
// Create bound contract for minting
160+
mintContract := bind.NewBoundContract(tokenAddress, mintABI, c, c, c)
155161

156-
// Prepare approve calldata for the Multicall3 contract to spend tokens
157-
approveTx, err = erc20.Approve(tops, scAddr, totalAmount)
162+
// Mint tokens directly to multicall3 contract so it can distribute them
163+
approveTx, err = mintContract.Transact(tops, "mint", scAddr, totalAmount)
158164
if err != nil {
159165
return nil, nil, err
160166
}
@@ -169,9 +175,11 @@ func Multicall3FundAccountsWithERC20Token(ctx context.Context, c *ethclient.Clie
169175
return approveTx, nil, err
170176
}
171177

178+
// Now prepare multicall3 calls to transfer tokens from multicall3 to each account
179+
// Using transfer instead of transferFrom since multicall3 now owns the tokens
172180
calls := make([]multicall3.Multicall3Call3, 0, len(accounts))
173181
for _, account := range accounts {
174-
callData, iErr := erc20ABI.Pack("transferFrom", tops.From, account, amount)
182+
callData, iErr := erc20ABI.Pack("transfer", account, amount)
175183
if iErr != nil {
176184
return approveTx, nil, iErr
177185
}

0 commit comments

Comments
 (0)