Parsing of ERC20 name, symbol and decimals in different format #400

This commit is contained in:
Martin Boehm 2020-05-02 17:24:56 +02:00
parent 124dee84fa
commit 828e10b629
2 changed files with 24 additions and 12 deletions

View File

@ -145,24 +145,26 @@ func parseErc20StringProperty(contractDesc bchain.AddressDescriptor, data string
n := parseErc20NumericProperty(contractDesc, data[64:128]) n := parseErc20NumericProperty(contractDesc, data[64:128])
if n != nil { if n != nil {
l := n.Uint64() l := n.Uint64()
if 2*int(l) <= len(data)-128 { if l > 0 && 2*int(l) <= len(data)-128 {
b, err := hex.DecodeString(data[128 : 128+2*l]) b, err := hex.DecodeString(data[128 : 128+2*l])
if err == nil { if err == nil {
return string(b) return string(b)
} }
} }
} }
} else if len(data) == 64 { }
// allow string properties as 32 bytes of UTF-8 data // allow string properties as UTF-8 data
b, err := hex.DecodeString(data) b, err := hex.DecodeString(data)
if err == nil { if err == nil {
i := bytes.Index(b, []byte{0}) i := bytes.Index(b, []byte{0})
if i > 0 { if i > 32 {
b = b[:i] i = 32
} }
if utf8.Valid(b) { if i > 0 {
return string(b) b = b[:i]
} }
if utf8.Valid(b) {
return string(b)
} }
} }
if glog.V(1) { if glog.V(1) {

View File

@ -138,6 +138,16 @@ func TestErc20_parseErc20StringProperty(t *testing.T) {
args: "0x44616920537461626c65636f696e2076312e3020444444444444444444444444", args: "0x44616920537461626c65636f696e2076312e3020444444444444444444444444",
want: "Dai Stablecoin v1.0 DDDDDDDDDDDD", want: "Dai Stablecoin v1.0 DDDDDDDDDDDD",
}, },
{
name: "long",
args: "0x556e6973776170205631000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
want: "Uniswap V1",
},
{
name: "garbage",
args: "0x2234880850896048596206002535425366538144616734015984380565810000",
want: "",
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {