forked from iotexproject/iotex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erc721_token.go
83 lines (72 loc) · 2.39 KB
/
erc721_token.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2019 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package blockchain
import (
"math/big"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-address/address"
)
type (
// Erc721Token erc721 token interface
Erc721Token interface {
Contract
CreateToken(string, string) (string, error)
Transfer(string, string, string, string, string) (string, error)
}
erc721Token struct {
Contract
}
)
// NewErc721Token creates a new Erc721Token
func NewErc721Token(exp string) Erc721Token {
return &erc721Token{Contract: NewContract(exp)}
}
func (f *erc721Token) CreateToken(tokenid, creditor string) (string, error) {
TokenID, ok := big.NewInt(0).SetString(tokenid, 10)
if !ok {
return "", errors.Errorf("invalid tokenid = %s", tokenid)
}
addrCreditor, err := address.FromString(creditor)
if err != nil {
return "", errors.Errorf("invalid creditor address = %s", creditor)
}
owner := f.RunAsOwner()
h, err := owner.Call(CreateTo, addrCreditor.Bytes(), TokenID.Bytes())
if err != nil {
return h, errors.Wrapf(err, "call failed to create")
}
if _, err := f.CheckCallResult(h); err != nil {
return h, errors.Wrapf(err, "check failed to create")
}
return h, nil
}
//
func (f *erc721Token) Transfer(token, sender, prvkey, receiver string, tokenid string) (string, error) {
TokenID, ok := big.NewInt(0).SetString(tokenid, 10)
if !ok {
return "", errors.Errorf("invalid tokenid = %s", tokenid)
}
from, err := address.FromString(sender)
if err != nil {
return "", errors.Errorf("invalid account address = %s", sender)
}
addrReceiver, err := address.FromString(receiver)
if err != nil {
return "", errors.Errorf("invalid account address = %s", receiver)
}
// transfer to receiver
h, err := f.SetAddress(token).
SetExecutor(sender).
SetPrvKey(prvkey).
Call(TransferFrom, from.Bytes(), addrReceiver.Bytes(), TokenID.Bytes())
if err != nil {
return h, errors.Wrap(err, "call transfer failed")
}
if _, err := f.CheckCallResult(h); err != nil {
return h, errors.Wrap(err, "check transfer failed")
}
return h, nil
}