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

ERC20Transfers sort #16

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
tokenBalance, err := client.TokenBalance("contractAddress", "holderAddress")

// check ERC20 transactions from/to a specified address
transfers, err := client.ERC20Transfers("contractAddress", "address", startBlock, endBlock, page, offset)
transfers, err := client.ERC20Transfers("contractAddress", "address","asc", startBlock, endBlock, page, offset)
}
```

Expand All @@ -53,7 +53,7 @@ You may find full method list at [GoDoc](https://godoc.org/github.com/nanmu42/et

You may apply for an API key on [etherscan](https://etherscan.io/apis).

> The Etherscan Ethereum Developer APIs are provided as a community service and without warranty, so please just use what you need and no more. They support both GET/POST requests and a rate limit of 5 requests/sec (exceed and you will be blocked).
> The Etherscan Ethereum Developer APIs are provided as a community service and without warranty, so please just use what you need and no more. They support both GET/POST requests and a rate limit of 5 requests/sec (exceed and you will be blocked).

# Paperwork Things

Expand Down
4 changes: 2 additions & 2 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
tokenBalance, err := client.TokenBalance("contractAddress", "holderAddress")

// 查询出入指定地址的ERC20转账列表
transfers, err := client.ERC20Transfers("contractAddress", "address", startBlock, endBlock, page, offset)
transfers, err := client.ERC20Transfers("contractAddress", "address","asc", startBlock, endBlock, page, offset)
}
```

Expand All @@ -66,4 +66,4 @@ API的调用速率不能高于5次/秒,否则会遭到封禁。

MIT

请自由享受开源,欢迎贡献开源。
请自由享受开源,欢迎贡献开源。
3 changes: 2 additions & 1 deletion account.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *Client) InternalTxByAddress(address string, startBlock *int, endBlock *
//
// More information can be found at:
// https://github.com/nanmu42/etherscan-api/issues/8
func (c *Client) ERC20Transfers(contractAddress, address *string, startBlock *int, endBlock *int, page int, offset int) (txs []ERC20Transfer, err error) {
func (c *Client) ERC20Transfers(contractAddress, address,sort *string, startBlock *int, endBlock *int, page int, offset int) (txs []ERC20Transfer, err error) {
param := M{
"page": page,
"offset": offset,
Expand All @@ -96,6 +96,7 @@ func (c *Client) ERC20Transfers(contractAddress, address *string, startBlock *in
compose(param, "address", address)
compose(param, "startblock", startBlock)
compose(param, "endblock", endBlock)
compose(param, "sort", sort) // asc - 升序,desc - 降序
Copy link
Owner

@nanmu42 nanmu42 Jul 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请使用和这里相同的方式来引入排序方式,从而保持这个库的API风格一致:

etherscan-api/account.go

Lines 35 to 49 in 28455ff

//
// if desc is true, result will be sorted in blockNum descendant order.
func (c *Client) NormalTxByAddress(address string, startBlock *int, endBlock *int, page int, offset int, desc bool) (txs []NormalTx, err error) {
param := M{
"address": address,
"page": page,
"offset": offset,
}
compose(param, "startblock", startBlock)
compose(param, "endblock", endBlock)
if desc {
param["sort"] = "desc"
} else {
param["sort"] = "asc"
}

修改后的该函数形如:

func (c *Client) ERC20Transfers(contractAddress, address, startBlock *int, endBlock *int, page int, offset int, desc bool) (txs []ERC20Transfer, err error) 

代码中请使用英文注释,注释请放在函数级别,这句话也许有帮助:

if desc is true, result will be sorted in blockNum descendant order. 


err = c.call("account", "tokentx", param, &txs)
return
Expand Down
8 changes: 4 additions & 4 deletions account_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func TestClient_ERC20Transfers(t *testing.T) {
)

var a, b = 3273004, 3328071
var contract, address = "0xe0b7927c4af23765cb51314a0e0521a9645f0e2a", "0x4e83362442b8d1bec281594cea3050c8eb01311c"
txs, err := api.ERC20Transfers(&contract, &address, &a, &b, 1, 500)
var contract, address,sort = "0xe0b7927c4af23765cb51314a0e0521a9645f0e2a", "0x4e83362442b8d1bec281594cea3050c8eb01311c","asc"
txs, err := api.ERC20Transfers(&contract, &address,&sort, &a, &b, 1, 500)
noError(t, err, "api.ERC20Transfers 1")

//j, _ := json.MarshalIndent(txs, "", " ")
Expand All @@ -88,7 +88,7 @@ func TestClient_ERC20Transfers(t *testing.T) {
t.Errorf("got txs length %v, want %v", len(txs), wantLen1)
}

txs, err = api.ERC20Transfers(nil, &address, nil, &b, 1, 500)
txs, err = api.ERC20Transfers(nil, &address,&sort, nil, &b, 1, 500)
noError(t, err, "api.ERC20Transfers 2")
if len(txs) != wantLen2 {
t.Errorf("got txs length %v, want %v", len(txs), wantLen2)
Expand All @@ -99,7 +99,7 @@ func TestClient_ERC20Transfers(t *testing.T) {
var specialContract = "0x5eac95ad5b287cf44e058dcf694419333b796123"
var specialStartHeight = 6024142
var specialEndHeight = 6485274
txs, err = api.ERC20Transfers(&specialContract, nil, &specialStartHeight, &specialEndHeight, 1, 500)
txs, err = api.ERC20Transfers(&specialContract, nil,&sort, &specialStartHeight, &specialEndHeight, 1, 500)
noError(t, err, "api.ERC20Transfers 2")
if len(txs) != wantLen3 {
t.Errorf("got txs length %v, want %v", len(txs), wantLen3)
Expand Down