Skip to content

Commit

Permalink
feat: replace transfer with mint for localnet chain (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
gacevicljubisa authored Sep 28, 2023
1 parent 4e613b4 commit 904df4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pkg/wallet/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/ethereum/go-ethereum/common"
)

const SwarmTokenDecimals = 16
const (
SwarmTokenDecimals = 16
LocalnetChainID = 12345
)

type Token struct {
Contract common.Address
Expand All @@ -34,7 +37,7 @@ var chainToSwarmTokenMap = map[int64]Token{
},

// Localnet
12345: {
LocalnetChainID: {
Contract: common.HexToAddress("0x6aab14fe9cccd64a502d23842d916eb5321c26e7"),
Symbol: "tBZZ",
Decimals: SwarmTokenDecimals,
Expand All @@ -55,7 +58,7 @@ var chainToNativeCoinMap = map[int64]Token{
},

// Localnet
12345: {
LocalnetChainID: {
Symbol: "tETH",
Decimals: 18,
},
Expand Down
16 changes: 16 additions & 0 deletions pkg/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package wallet

import (
"context"
"encoding/hex"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -191,11 +192,26 @@ func (w *erc20Wallet) Transfer(
amount *big.Int,
token Token,
) error {
chainID, err := w.client.ChainID(ctx)
if err != nil {
return fmt.Errorf("failed to get network id, %w", err)
}

callData, err := erc20ABI.Pack("transfer", toAddr, amount)
if err != nil {
return fmt.Errorf("failed to pack abi, %w", err)
}

// Custom handling for LocalnetChainID.
if chainID.Int64() == LocalnetChainID {
localnetMintFunction, decodeErr := hex.DecodeString("40c10f19") // mint(address,uint256)
if decodeErr != nil {
return fmt.Errorf("failed decode string %w", err)
}
// Replace the first 4 bytes of the call data (transfer) with the localnet mint function.
copy(callData[:4], localnetMintFunction)
}

err = w.trxSender.Send(ctx, token.Contract, nil, callData)
if err != nil {
return fmt.Errorf("failed to make ERC20 token transfer, %w", err)
Expand Down

0 comments on commit 904df4c

Please sign in to comment.