簡體   English   中英

ERC-20 代幣轉移問題:執行恢復:ERC20:從零地址轉移

[英]ERC-20 Token transfer problem: execution reverted: ERC20: transfer from the zero address

我最近一直在努力學習有關智能合約的知識,但是當我試圖了解代幣轉移的工作原理時,我遇到了這個問題。 || 執行恢復:ERC20:從零地址轉移 || (羅普森網絡)

編碼:


import (
    "context"
    "crypto/ecdsa"
    "fmt"
    "github.com/ethereum/go-ethereum"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/hexutil"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/ethclient"
    "golang.org/x/crypto/sha3"
    "log"
    "math/big"
)

func main() {

    client, err := ethclient.Dial("https://ropsten.infura.io/v3")
    if err != nil {
        panic(err)
    }

    chainID, _ := client.NetworkID(context.Background())

    privateKey, err := crypto.HexToECDSA("280a5fb7d2eef8c7956f4e6754c82a46f30496e43f50be4c8a457ef4e45fb1f4")
    if err != nil {
        log.Fatal(err)
    }

    publicKey := privateKey.Public()
    publicKeyECDSA, ok :=publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
    }

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
    nonce, err := client.PendingNonceAt(context.Background(), fromAddress)

    if err != nil {
        log.Fatal(err)
    }

    value := big.NewInt(0)

    toAddress := common.HexToAddress("0x0cFd37C2A1c9d0B8833fFE5a772003a350B5Be3f")

    tokenAddress := common.HexToAddress("0x4f1c3F4D89826f27204769Af1617540c219E3A62")

    transferFnSignature := []byte("transfer(address,uint256)")
    hash := sha3.NewLegacyKeccak256()
    hash.Write(transferFnSignature)

    methodID := hash.Sum(nil)[:4]
    fmt.Println(hexutil.Encode(methodID))

    paddedAddress := common.LeftPadBytes(toAddress.Bytes(),32)
    fmt.Println(hexutil.Encode(paddedAddress))

    amount := new(big.Int)
    amount.SetString("10000000000000000000", 10)
    paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
    fmt.Println(hexutil.Encode(paddedAmount))

    var data []byte
    data = append(data, methodID...)
    data = append(data, paddedAddress...)
    data = append(data, paddedAmount...)

    gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
        To: &tokenAddress,
        Data: data,
    })
    if err != nil {
        log.Fatal(err)
    }
    gasPrice, err := client.SuggestGasPrice(context.Background())
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(gasLimit)

    tx := types.NewTx(&types.LegacyTx{
        Nonce:    nonce,
        GasPrice: gasPrice,
        Gas:      gasLimit,
        To:       &tokenAddress,
        Value:    value,
        Data:     data,
        V:        nil,
        R:        nil,
        S:        nil,
    })

    signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID),privateKey)
    if err != nil {
        log.Fatal(err)
    }

    err = client.SendTransaction(context.Background(), signedTx)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("TOKEN TX SENT! || %s ||", signedTx.Hash().Hex())


}

代幣地址是我的合約地址,fromAddress 是我持有 10000 個代幣的錢包,toAddress 是一個普通錢包。 我在這里錯過了什么?

顯然問題出在gasLimit上。 將 gasLimit 更改為gasLimit := uint64(200000)並且它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM