Home > Blockchain >  Go panic interface conversion with a compatible interface
Go panic interface conversion with a compatible interface

Time:09-22

I'm facing an unsupportable problem for hours, I have to decode data from an Ethereum smart contract call (but it's not the topic because this part work) but I recover theses data into an interface{}.

And when I want to cast this empty interface to a struct that I can use, I got this message : panic: interface conversion: interface {} is struct { SrcToken common.Address "json:\"srcToken\""; DstToken common.Address "json:\"dstToken\""; SrcReceiver common.Address "json:\"srcReceiver\""; DstReceiver common.Address "json:\"dstReceiver\""; Amount *big.Int "json:\"amount\""; MinReturnAmount *big.Int "json:\"minReturnAmount\""; Flags *big.Int "json:\"flags\""; Permit []uint8 "json:\"permit\"" }, not oneinch.OneInchSwapDataDesc

The struct is :

type OneInchSwapDataDesc struct {
    SrcToken        common.Address
    DstToken        common.Address
    SrcReceiver     common.Address
    DstReceiver     common.Address
    Amount          *big.Int
    MinReturnAmount *big.Int
    Flags           *big.Int
    Permit          []uint8
}

When I look for the type and the data value I got :

fmt.Println("Type: ", reflect.TypeOf(data))
fmt.Printf("Data: % v\n", data)

// Logs:
// Type:  struct { SrcToken common.Address "json:\"srcToken\""; DstToken common.Address "json:\"dstToken\""; SrcReceiver common.Address "json:\"srcReceiver\""; DstReceiver common.Address "json:\"dstReceiver\""; Amount *big.Int "json:\"amount\""; MinReturnAmount *big.Int "json:\"minReturnAmount\""; Flags *big.Int "json:\"flags\""; Permit []uint8 "json:\"permit\"" }
// Data: {SrcToken:0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 DstToken:0xc2132D05D31c914a87C6611C10748AEb04B58e8F SrcReceiver:0x11431a89893025D2a48dCA4EddC396f8C8117187 DstReceiver:0x2C2d6E5E16FE924cE31784cdAd27C01D1BC62873 Amount: 10000000 MinReturnAmount: 9949450 Flags: 4 Permit:[]}

I think this problem come from those "json" that comes out from the TypeOf but I don;t find any tricks to clear it (in fact, I'm not really sure if the problem really come from that).

If someone have an idea, you'll save my brain from burning in hell. Thanks.

CodePudding user response:

The interface has a value of type struct { SrcToken ...}. It is an unnamed type. Type assertion will only work if the types are identical, or if the asserted type is an interface and the underlying interface type implements that interface. The type you are trying to convert to is not an interface, but a struct. So you can do this:

value:=data.(struct { 
SrcToken common.Address `json:"srcToken"`
 DstToken common.Address `json:"dstToken"`
 SrcReceiver common.Address `json:"srcReceiver"`
 DstReceiver common.Address `json:"dstReceiver"`
 Amount *big.Int `json:"amount"`
 MinReturnAmount *big.Int `json:"minReturnAmount"`
 Flags *big.Int `json:"flags"`
 Permit []uint8 `json:"permit"` })

Once you have this, you can convert value to the type you want using:

targetValue:=oneinch.OneInchSwapDataDesc(value)
  • Related