forked from harmony-one/multichain
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultichain.go
More file actions
229 lines (199 loc) · 6.53 KB
/
multichain.go
File metadata and controls
229 lines (199 loc) · 6.53 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Package multichain defines all supported assets and chains. It also
// re-exports the individual multichain APIs.
package multichain
import (
"github.com/renproject/multichain/api/account"
"github.com/renproject/multichain/api/address"
"github.com/renproject/multichain/api/contract"
"github.com/renproject/multichain/api/gas"
"github.com/renproject/multichain/api/utxo"
"github.com/renproject/multichain/chain/ethereum"
"github.com/renproject/surge"
)
type (
Address = address.Address
AddressEncodeDecoder = address.EncodeDecoder
EthereumCompatAddress = ethereum.Address
RawAddress = address.RawAddress
)
type (
AccountTx = account.Tx
AccountTxBuilder = account.TxBuilder
AccountClient = account.Client
)
type (
UTXOutpoint = utxo.Outpoint
UTXOutput = utxo.Output
UTXOInput = utxo.Input
UTXORecipient = utxo.Recipient
UTXOTx = utxo.Tx
UTXOTxBuilder = utxo.TxBuilder
UTXOClient = utxo.Client
)
type (
ContractCallData = contract.CallData
ContractCaller = contract.Caller
)
type (
GasEstimator = gas.Estimator
)
// An Asset uniquely identifies assets using human-readable strings.
type Asset string
// Enumeration of supported assets. When introducing a new chain, or new asset
// from an existing chain, you must add a human-readable string to this set of
// enumerated values. Assets must be listed in alphabetical order.
const (
BCH = Asset("BCH") // Bitcoin Cash
BNB = Asset("BNB") // Binance Coin
BTC = Asset("BTC") // Bitcoin
CELO = Asset("CELO") // Celo
DGB = Asset("DGB") // DigiByte
DOGE = Asset("DOGE") // Dogecoin
ETH = Asset("ETH") // Ether
FIL = Asset("FIL") // Filecoin
FTM = Asset("FTM") // Fantom
SOL = Asset("SOL") // Solana
LUNA = Asset("LUNA") // Luna
ONE = Asset("ONE") // Harmony
ZEC = Asset("ZEC") // Zcash
)
// OriginChain returns the chain upon which the asset originates. For example,
// the origin chain of BTC is Bitcoin.
func (asset Asset) OriginChain() Chain {
switch asset {
case BCH:
return BitcoinCash
case BNB:
return BinanceSmartChain
case BTC:
return Bitcoin
case CELO:
return Celo
case DGB:
return DigiByte
case DOGE:
return Dogecoin
case ETH:
return Ethereum
case FIL:
return Filecoin
case FTM:
return Fantom
case LUNA:
return Terra
case SOL:
return Solana
case ONE:
return Harmony
case ZEC:
return Zcash
default:
return Chain("")
}
}
// SizeHint returns the number of bytes required to represent the asset in
// binary.
func (asset Asset) SizeHint() int {
return surge.SizeHintString(string(asset))
}
// Marshal the asset to binary.
func (asset Asset) Marshal(buf []byte, rem int) ([]byte, int, error) {
return surge.MarshalString(string(asset), buf, rem)
}
// Unmarshal the asset from binary.
func (asset *Asset) Unmarshal(buf []byte, rem int) ([]byte, int, error) {
return surge.UnmarshalString((*string)(asset), buf, rem)
}
// A Chain uniquely identifies a blockchain using a human-readable string.
type Chain string
// Enumeration of supported chains. When introducing a new chain, you must add a
// human-readable string to this set of enumerated values. Chains must be listed
// in alphabetical order.
const (
Acala = Chain("Acala")
BinanceSmartChain = Chain("BinanceSmartChain")
Bitcoin = Chain("Bitcoin")
BitcoinCash = Chain("BitcoinCash")
Celo = Chain("Celo")
DigiByte = Chain("DigiByte")
Dogecoin = Chain("Dogecoin")
Ethereum = Chain("Ethereum")
Fantom = Chain("Fantom")
Filecoin = Chain("Filecoin")
Harmony = Chain("Harmony")
Solana = Chain("Solana")
Terra = Chain("Terra")
Zcash = Chain("Zcash")
)
// SizeHint returns the number of bytes required to represent the chain in
// binary.
func (chain Chain) SizeHint() int {
return surge.SizeHintString(string(chain))
}
// Marshal the chain to binary. You should not call this function directly,
// unless you are implementing marshalling for a container type.
func (chain Chain) Marshal(buf []byte, rem int) ([]byte, int, error) {
return surge.MarshalString(string(chain), buf, rem)
}
// Unmarshal the chain from binary. You should not call this function directly,
// unless you are implementing unmarshalling for a container type.
func (chain *Chain) Unmarshal(buf []byte, rem int) ([]byte, int, error) {
return surge.UnmarshalString((*string)(chain), buf, rem)
}
func (chain Chain) ChainType() ChainType {
switch chain {
case Bitcoin, BitcoinCash, DigiByte, Dogecoin, Zcash:
return ChainTypeUTXOBased
case BinanceSmartChain, Ethereum:
return ChainTypeAccountBased
default:
return ChainType("")
}
}
func (chain Chain) IsAccountBased() bool {
return chain.ChainType() == ChainTypeAccountBased
}
func (chain Chain) IsUTXOBased() bool {
return chain.ChainType() == ChainTypeUTXOBased
}
type ChainType string
const (
ChainTypeAccountBased = ChainType("Account")
ChainTypeUTXOBased = ChainType("UTXO")
)
// SizeHint returns the number of bytes required to represent the chain type in
// binary.
func (chainType ChainType) SizeHint() int {
return surge.SizeHintString(string(chainType))
}
// Marshal the chain type to binary. You should not call this function directly,
// unless you are implementing marshalling for a container type.
func (chainType ChainType) Marshal(buf []byte, rem int) ([]byte, int, error) {
return surge.MarshalString(string(chainType), buf, rem)
}
// Unmarshal the chain type from binary. You should not call this function
// directly, unless you are implementing unmarshalling for a container type.
func (chainType *ChainType) Unmarshal(buf []byte, rem int) ([]byte, int, error) {
return surge.UnmarshalString((*string)(chainType), buf, rem)
}
type Network string
const (
NetworkLocalnet = Network("localnet")
NetworkTestnet = Network("testnet")
NetworkMainnet = Network("mainnet")
)
// SizeHint returns the number of bytes required to represent the network in
// binary.
func (net Network) SizeHint() int {
return surge.SizeHintString(string(net))
}
// Marshal the network to binary. You should not call this function directly,
// unless you are implementing marshalling for a container type.
func (net Network) Marshal(buf []byte, rem int) ([]byte, int, error) {
return surge.MarshalString(string(net), buf, rem)
}
// Unmarshal the network from binary. You should not call this function
// directly, unless you are implementing unmarshalling for a container type.
func (net *Network) Unmarshal(buf []byte, rem int) ([]byte, int, error) {
return surge.UnmarshalString((*string)(net), buf, rem)
}