gotron-sdk icon indicating copy to clipboard operation
gotron-sdk copied to clipboard

Add ParseTopicsIntoMap method for abi utilities

Open ciricc opened this issue 8 months ago • 1 comments

Now we have only one way to parse event log inputs - through ethereum's MapTopicsIntoMap function with type convertations after. What about this?

func convertEthereumAddressToTronAddress(
	addr eCommon.Address,
) address.Address {
	addrBytes := make([]byte, 1+len(addr.Bytes()))
	copy(addrBytes[:1], []byte{address.TronBytePrefix})
	copy(addrBytes[1:], addr.Bytes())
	return addrBytes
}

func ParseTopicsIntoMap(out map[string]interface{}, values eABI.Arguments, topics [][]byte) error {
	if out == nil {
		return fmt.Errorf("out is nil")
	}

	ethTopics := make([]eCommon.Hash, len(topics))
	for i, v := range topics {
		ethTopics[i] = eCommon.Hash(v)
	}

	err := eABI.ParseTopicsIntoMap(out, values, ethTopics)
	if err != nil {
		return err
	}

	for k, v := range out {
		switch tV := v.(type) {
		case eCommon.Address:
			out[k] = convertEthereumAddressToTronAddress(tV)
		}
	}

	return nil
}

ciricc avatar Oct 25 '23 13:10 ciricc