Beispiel #1
0
// bindType converts a Solidity type to a Go one. Since there is no clear mapping
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
// mapped will use an upscaled type (e.g. *big.Int).
func bindType(kind abi.Type) string {
	stringKind := kind.String()

	switch {
	case stringKind == "address":
		return "common.Address"

	case stringKind == "hash":
		return "common.Hash"

	case strings.HasPrefix(stringKind, "bytes"):
		if stringKind == "bytes" {
			return "[]byte"
		}
		return fmt.Sprintf("[%s]byte", stringKind[5:])

	case strings.HasPrefix(stringKind, "int"):
		switch stringKind[:3] {
		case "8", "16", "32", "64":
			return stringKind
		}
		return "*big.Int"

	case strings.HasPrefix(stringKind, "uint"):
		switch stringKind[:4] {
		case "8", "16", "32", "64":
			return stringKind
		}
		return "*big.Int"

	default:
		return stringKind
	}
}
Beispiel #2
0
// bindType converts a Solidity type to a Go one. Since there is no clear mapping
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
// mapped will use an upscaled type (e.g. *big.Int).
func bindType(kind abi.Type) string {
	stringKind := kind.String()

	switch {
	case strings.HasPrefix(stringKind, "address"):
		parts := regexp.MustCompile("address(\\[[0-9]*\\])?").FindStringSubmatch(stringKind)
		if len(parts) != 2 {
			return stringKind
		}
		return fmt.Sprintf("%scommon.Address", parts[1])

	case strings.HasPrefix(stringKind, "bytes"):
		parts := regexp.MustCompile("bytes([0-9]*)(\\[[0-9]*\\])?").FindStringSubmatch(stringKind)
		if len(parts) != 3 {
			return stringKind
		}
		return fmt.Sprintf("%s[%s]byte", parts[2], parts[1])

	case strings.HasPrefix(stringKind, "int") || strings.HasPrefix(stringKind, "uint"):
		parts := regexp.MustCompile("(u)?int([0-9]*)(\\[[0-9]*\\])?").FindStringSubmatch(stringKind)
		if len(parts) != 4 {
			return stringKind
		}
		switch parts[2] {
		case "8", "16", "32", "64":
			return fmt.Sprintf("%s%sint%s", parts[3], parts[1], parts[2])
		}
		return fmt.Sprintf("%s*big.Int", parts[3])

	case strings.HasPrefix(stringKind, "bool") || strings.HasPrefix(stringKind, "string"):
		parts := regexp.MustCompile("([a-z]+)(\\[[0-9]*\\])?").FindStringSubmatch(stringKind)
		if len(parts) != 3 {
			return stringKind
		}
		return fmt.Sprintf("%s%s", parts[2], parts[1])

	default:
		return stringKind
	}
}