Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetStorage RPC call #18

Merged
merged 6 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this
project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## v1.7.0 - 2017-10-27

### Added

- New function to fetch a storage value of a given smart contract.

```golang
storage, err := client.GetStorage(
"0xecc6b20d3ccac1ee9ef109af5a7cdb85706b1df9", // RPX smart contract
"totalSupply",
)
```

## v1.6.1 - 2017-10-13

### Changed
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.6.1
1.7.0
17 changes: 17 additions & 0 deletions neo/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package neo

import (
"encoding/hex"
"net"
"net/url"

Expand Down Expand Up @@ -106,6 +107,22 @@ func (c Client) GetConnectionCount() (int64, error) {
return response.Result, nil
}

// GetStorage takes a smart contract hash and a storage key, and returns the storage value
// if available.
func (c Client) GetStorage(scriptHash string, storageKey string) (string, error) {
requestBodyParams := []interface{}{
scriptHash, hex.EncodeToString([]byte(storageKey)),
}
var response response.String

err := executeRequest("getstorage", requestBodyParams, c.NodeURI, &response)
if err != nil {
return "", err
}

return response.Result, nil
}

// GetTransaction returns the corresponding transaction information based on the
// specified hash value.
func (c Client) GetTransaction(hash string) (*models.Transaction, error) {
Expand Down
14 changes: 14 additions & 0 deletions neo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ func TestClient(t *testing.T) {
})
})

t.Run(".GetStorage()", func(t *testing.T) {
t.Run("HappyCase", func(t *testing.T) {
client := neo.NewClient(nodeURI)

storage, err := client.GetStorage(
"0xecc6b20d3ccac1ee9ef109af5a7cdb85706b1df9",
"totalSupply",
)

assert.NoError(t, err)
assert.Equal(t, "0072ef3e2597e201", storage)
})
})

t.Run(".GetTransaction()", func(t *testing.T) {
t.Run("HappyCase", func(t *testing.T) {
client := neo.NewClient(nodeURI)
Expand Down
22 changes: 11 additions & 11 deletions neo/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ var (
numberOfTransactions int
}{
{
hash: "0x3f0b498c0d57f73c674a1e28045f5e9a0991f9dac214076fadb5e6bafd546170",
hash: "0x9ceb257832478ef778c1f26ad916ef8cbf116c71fe3cd6c5a8f672c1663539ae",
id: "1",
index: 316675,
merkleRoot: "0xd51ef6237173eee1d422811c2e79d2e30928ed7c487ff9be60c493a9901b03b8",
numberOfTransactions: 2,
index: 1511369,
merkleRoot: "0x04bb7e7c56711b3387f1593c36dcdc36516b6ccd06d0e0c15adeba3c33643ebe",
numberOfTransactions: 17,
},
}

Expand All @@ -57,9 +57,9 @@ var (
size int64
}{
{
hash: "0x8aaf766179c07941f24a08157d7e6796e6d4aa999d3eaf83e74024c28d081af0",
hash: "0xc515c4d2db27e06fd2305a5c5378f820d2c4cc04477ebe40ffa40b956eb4f8b5",
id: "1",
size: 262,
size: 202,
},
}

Expand All @@ -71,11 +71,11 @@ var (
value string
}{
{
asset: "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7",
hash: "0x8aaf766179c07941f24a08157d7e6796e6d4aa999d3eaf83e74024c28d081af0",
asset: "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
hash: "0xc515c4d2db27e06fd2305a5c5378f820d2c4cc04477ebe40ffa40b956eb4f8b5",
id: "1",
index: 0,
value: "50",
value: "96",
},
}

Expand All @@ -86,8 +86,8 @@ var (
}{
{
id: "1",
index: 316675,
hash: "0x3f0b498c0d57f73c674a1e28045f5e9a0991f9dac214076fadb5e6bafd546170",
index: 1511369,
hash: "0x9ceb257832478ef778c1f26ad916ef8cbf116c71fe3cd6c5a8f672c1663539ae",
},
}

Expand Down
4 changes: 2 additions & 2 deletions neo/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

const (
cozNodeURI = "http://test%d.cityofzion.io:8880"
neoNodeURI = "http://seed%d.neo.org:10332"
)

func selectTestNode() string {
var nodeURI string

for i := 1; i <= 5; i++ {
uri := fmt.Sprintf(cozNodeURI, i)
uri := fmt.Sprintf(neoNodeURI, i)
client := neo.NewClient(uri)

ok := client.Ping()
Expand Down