-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherc721_uristorage.go
More file actions
41 lines (34 loc) · 1.05 KB
/
Copy patherc721_uristorage.go
File metadata and controls
41 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package ethcli
import (
"context"
"math/big"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
var (
openzeppelinERC721URIStorageAbi = `[{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"tokenURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]`
)
// ERC721TokenURI
// for ERC721Metadata && ERC721URIStorage
func (cli *EvmClient) ERC721TokenURI(ctx context.Context, token string, tokenId *big.Int, blockNumber *big.Int) (string, error) {
ins, err := abi.JSON(strings.NewReader(openzeppelinERC721URIStorageAbi))
if err != nil {
return "", err
}
data, _ := ins.Pack("tokenURI", tokenId)
contract := common.HexToAddress(token)
bz, err := cli.CallContract(ctx, ethereum.CallMsg{
To: &contract,
Data: data,
}, blockNumber)
if err != nil {
return "", err
}
results, err := ins.Unpack("tokenURI", bz)
if err != nil {
return "", err
}
return results[0].(string), nil
}