Skip to content

Commit

Permalink
Merge pull request #49 from astriaorg/bharath/prevent-child-wallet-fu…
Browse files Browse the repository at this point in the history
…nding-for-univ3tx

send univ3swaptxs only via the root wallet
  • Loading branch information
bharath-123 authored Jan 9, 2025
2 parents 5bbad98 + 728124a commit b9772e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 160 deletions.
4 changes: 2 additions & 2 deletions cmd/spamooor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func main() {
RpcHosts: rpcHosts,
WalletPrivkey: cliArgs.privkey,
WalletCount: 100,
WalletPrefund: utils.EtherToWei(uint256.NewInt(3)),
WalletMinfund: utils.EtherToWei(uint256.NewInt(3)),
WalletPrefund: utils.WeiToEther(uint256.NewInt(10000000000000000)),
WalletMinfund: utils.WeiToEther(uint256.NewInt(10000000000000000)),
Scenario: scenarioName,
}
err := scenario.Init(testerConfig)
Expand Down
158 changes: 4 additions & 154 deletions scenarios/univ3swaptx/univ3swaptx.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,12 @@ func (s *Scenario) Setup(testerCfg *tester.Tester) error {

s.logger.Infof("starting scenario: univ3tx")

// now we need to mint DAI and WETH for all child wallets
s.logger.Infof("minting Erc20 and WETH for child wallets...")
errorMap, err := s.MintTokenAndWethForChildWallets()
if err != nil {
s.logger.Errorf("could not mint Erc20 and WETH for child wallets: %v", err)
return err
}

if len(errorMap) > 0 {
// print errors
for addr, errs := range errorMap {
for _, e := range errs {
s.logger.Errorf("error for wallet: %v: %v", addr.String(), e)
}
}
}

return nil
}

func (s *Scenario) Run() error {
s.logger.Info("running scenario: univ3tx")

txIdxCounter := uint64(0)
counterMutex := sync.Mutex{}
waitGroup := sync.WaitGroup{}
Expand Down Expand Up @@ -218,7 +203,8 @@ func (s *Scenario) Run() error {

func (s *Scenario) sendTx(txIdx uint64) (*types.Transaction, *txbuilder.Client, error) {
client := s.tester.GetClient(tester.SelectByIndex, int(txIdx))
wallet := s.tester.GetWallet(tester.SelectByIndex, int(txIdx))
//wallet := s.tester.GetWallet(tester.SelectByIndex, int(txIdx))
wallet := s.tester.GetRootWallet()

var feeCap *big.Int
var tipCap *big.Int
Expand Down Expand Up @@ -385,142 +371,6 @@ func (s *Scenario) sendTx(txIdx uint64) (*types.Transaction, *txbuilder.Client,
return tx, client, nil
}

func (s *Scenario) MintTokenAndWethForChildWallets() (map[common.Address][]error, error) {
if s.options.MaxWallets == 0 {
return nil, fmt.Errorf("max wallets not set")
}

client := s.tester.GetClient(tester.SelectByIndex, 0)

rootWallet := s.tester.GetRootWallet()
rootWalletTransactor, err := rootWallet.GetTransactor(true, big.NewInt(0))
if err != nil {
s.logger.Errorf("could not get transactor for root wallet: %v", err)
return nil, err
}

tokenMintAmount := s.tokenMintAmount.Mul(s.tokenMintAmount, big.NewInt(1000000000))
batchSize := uint64(100)
batchIndex := uint64(0)

errorMapLock := sync.Mutex{}
errorMap := make(map[common.Address][]error)

wg := sync.WaitGroup{}
// batch up the mints and deposits in order to not overwhelm the rpc
for {
wg.Add(1)
go func(batchIndex uint64, batchSize uint64, errorMap *map[common.Address][]error, errorMapLock *sync.Mutex) {
defer wg.Done()
finalBatchIndex := batchIndex + batchSize

s.logger.Infof("funding child wallets: %v/%v", batchIndex, s.tester.GetTotalChildWallets())

wg1 := sync.WaitGroup{}

for {
if batchIndex > uint64(s.tester.GetTotalChildWallets()) || batchIndex > finalBatchIndex {
break
}

childWallet := s.tester.GetWallet(tester.SelectByIndex, int(batchIndex))

wg1.Add(1)
go func(errorMap *map[common.Address][]error, errorMapLock *sync.Mutex) {
defer wg1.Done()

tokenContract, err := s.GetTokenContract()
if err != nil {
s.logger.Errorf("could not create Dai contract: %v", err)
errorMapLock.Lock()
(*errorMap)[childWallet.GetAddress()] = append((*errorMap)[childWallet.GetAddress()], err)
errorMapLock.Unlock()
return
}

wethContract, err := s.GetWethContract()
if err != nil {
s.logger.Errorf("could not create WETH contract: %v", err)
errorMapLock.Lock()
(*errorMap)[childWallet.GetAddress()] = append((*errorMap)[childWallet.GetAddress()], err)
errorMapLock.Unlock()
return
}

// transfer erc20 for child wallet
transferTx, err := tokenContract.Transfer(rootWalletTransactor, childWallet.GetAddress(), tokenMintAmount)
if err != nil {
s.logger.Errorf("could not transfer token for child wallet: %v", err)
errorMapLock.Lock()
(*errorMap)[childWallet.GetAddress()] = append((*errorMap)[childWallet.GetAddress()], err)
errorMapLock.Unlock()
return
}

// transfer weth
wethTransferTx, err := wethContract.Transfer(rootWalletTransactor, childWallet.GetAddress(), tokenMintAmount)
if err != nil {
s.logger.Errorf("could not transfer WETH for child wallet: %v", err)
errorMapLock.Lock()
(*errorMap)[childWallet.GetAddress()] = append((*errorMap)[childWallet.GetAddress()], err)
errorMapLock.Unlock()
return
}

_, _, err = txbuilder.SendAndAwaitTx(txbuilder.SendTxOpts{
Gas: 0,
Wallet: rootWallet,
Tx: transferTx,
Client: client,
BaseFee: int64(s.options.BaseFee),
TipFee: int64(s.options.TipFee),
})
if err != nil {
s.logger.Errorf("could not mint DAI for child wallet: %v", err)
errorMapLock.Lock()
(*errorMap)[childWallet.GetAddress()] = append((*errorMap)[childWallet.GetAddress()], err)
errorMapLock.Unlock()
return
}

_, _, err = txbuilder.SendAndAwaitTx(txbuilder.SendTxOpts{
Gas: 0,
Wallet: rootWallet,
Tx: wethTransferTx,
Client: client,
BaseFee: int64(s.options.BaseFee),
TipFee: int64(s.options.TipFee),
})
if err != nil {
s.logger.Errorf("could not transfer WETH for child wallet: %v", err)
errorMapLock.Lock()
(*errorMap)[childWallet.GetAddress()] = append((*errorMap)[childWallet.GetAddress()], err)
errorMapLock.Unlock()
return
}

}(errorMap, errorMapLock)
batchIndex += 1
}
wg1.Wait()

}(batchIndex, batchSize, &errorMap, &errorMapLock)

batchIndex += batchSize

// we are done if this is true
if batchIndex >= uint64(s.tester.GetTotalChildWallets()) {
break
}
}

wg.Wait()

s.logger.Infof("minted Token for child wallets")

return errorMap, nil
}

func (s *Scenario) awaitTx(txIdx uint64, tx *types.Transaction, client *txbuilder.Client, wallet *txbuilder.Wallet) {
var awaitConfirmation bool = true
defer func() {
Expand Down
12 changes: 8 additions & 4 deletions tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ func (tester *Tester) Start(seed string) error {
}

// prepare wallets with Eth
err = tester.PrepareWallets(seed)
if err != nil {
return err
if tester.config.Scenario != "univ3swaptx" {
err = tester.PrepareWallets(seed)
if err != nil {
return err
}
go tester.watchWalletBalancesLoop()
} else {
tester.logger.Infof("univ3tx scenario does not require child wallet funding")
}
go tester.watchWalletBalancesLoop()
}

return nil
Expand Down

0 comments on commit b9772e6

Please sign in to comment.