/
eth_format.go
83 lines (68 loc) · 2.75 KB
/
eth_format.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
package adapters
import (
"github.com/GoPlugin/Plugin/core/services/keystore"
"github.com/GoPlugin/Plugin/core/store"
"github.com/GoPlugin/Plugin/core/store/models"
"github.com/GoPlugin/Plugin/core/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
// EthBytes32 holds no fields.
type EthBytes32 struct{}
// TaskType returns the type of Adapter.
func (e *EthBytes32) TaskType() models.TaskType {
return TaskTypeEthBytes32
}
// Perform returns the hex value of the first 32 bytes of a string
// so that it is in the proper format to be written to the blockchain.
//
// For example, after converting the string "16800.01" to hex encoded Ethereum
// ABI, it would be:
// "0x31363830302e3031000000000000000000000000000000000000000000000000"
func (*EthBytes32) Perform(input models.RunInput, _ *store.Store, _ *keystore.Master) models.RunOutput {
result := input.Result()
value := common.RightPadBytes([]byte(result.String()), utils.EVMWordByteLen)
hex := utils.RemoveHexPrefix(hexutil.Encode(value))
if len(hex) > utils.EVMWordHexLen {
hex = hex[:utils.EVMWordHexLen]
}
return models.NewRunOutputCompleteWithResult(utils.AddHexPrefix(hex), input.ResultCollection())
}
// EthInt256 holds no fields
type EthInt256 struct{}
// TaskType returns the type of Adapter.
func (e *EthInt256) TaskType() models.TaskType {
return TaskTypeEthInt256
}
// Perform returns the hex value of a given string so that it
// is in the proper format to be written to the blockchain.
//
// For example, after converting the string "-123.99" to hex encoded Ethereum
// ABI, it would be:
// "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85"
func (*EthInt256) Perform(input models.RunInput, _ *store.Store, _ *keystore.Master) models.RunOutput {
value, err := utils.EVMTranscodeInt256(input.Result())
if err != nil {
return models.NewRunOutputError(err)
}
return models.NewRunOutputCompleteWithResult(hexutil.Encode(value), input.ResultCollection())
}
// EthUint256 holds no fields.
type EthUint256 struct{}
// TaskType returns the type of Adapter.
func (e *EthUint256) TaskType() models.TaskType {
return TaskTypeEthUint256
}
// Perform returns the hex value of a given string so that it
// is in the proper format to be written to the blockchain.
//
// For example, after converting the string "123.99" to hex encoded Ethereum
// ABI, it would be:
// "0x000000000000000000000000000000000000000000000000000000000000007b"
func (*EthUint256) Perform(input models.RunInput, _ *store.Store, _ *keystore.Master) models.RunOutput {
value, err := utils.EVMTranscodeUint256(input.Result())
if err != nil {
return models.NewRunOutputError(err)
}
return models.NewRunOutputCompleteWithResult(hexutil.Encode(value), input.ResultCollection())
}