-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package hedera | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashgraph/hedera-sdk-go/proto" | ||
) | ||
|
||
// ContractID is the ID for a Hedera smart contract | ||
type ContractID struct { | ||
Shard uint64 | ||
Realm uint64 | ||
Contract uint64 | ||
} | ||
|
||
// ContractIDFromString constructs a ContractID from a string formatted as `Shard.Realm.Contract` (for example "0.0.3") | ||
func ContractIDFromString(s string) (ContractID, error) { | ||
shard, realm, num, err := idFromString(s) | ||
if err != nil { | ||
return ContractID{}, err | ||
} | ||
|
||
return ContractID{ | ||
Shard: uint64(shard), | ||
Realm: uint64(realm), | ||
Contract: uint64(num), | ||
}, nil | ||
} | ||
|
||
// ContractIDFromSolidityAddress constructs a ContractID from a string representation of a solidity address | ||
func ContractIDFromSolidityAddress(s string) (ContractID, error) { | ||
shard, realm, contract, err := idFromSolidityAddress(s) | ||
if err != nil { | ||
return ContractID{}, err | ||
} | ||
|
||
return ContractID{ | ||
Shard: shard, | ||
Realm: realm, | ||
Contract: contract, | ||
}, nil | ||
} | ||
|
||
// String returns the string representation of a ContractID formatted as `Shard.Realm.Contract` (for example "0.0.3") | ||
func (id ContractID) String() string { | ||
return fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.Contract) | ||
} | ||
|
||
// ToSolidityAddress returns the string representation of the ContractID as a solidity address. | ||
func (id ContractID) ToSolidityAddress() string { | ||
return idToSolidityAddress(id.Shard, id.Realm, id.Contract) | ||
} | ||
|
||
func (id ContractID) toProto() *proto.ContractID { | ||
return &proto.ContractID{ | ||
ShardNum: int64(id.Shard), | ||
RealmNum: int64(id.Realm), | ||
ContractNum: int64(id.Contract), | ||
} | ||
} | ||
|
||
func contractIDFromProto(pb *proto.ContractID) ContractID { | ||
return ContractID{ | ||
Shard: uint64(pb.ShardNum), | ||
Realm: uint64(pb.RealmNum), | ||
Contract: uint64(pb.ContractNum), | ||
} | ||
} |