forked from hyperledger/firefly-evmconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of AddressBalance() FFCAPI function
Signed-off-by: Matthew Whitehead <[email protected]>
- Loading branch information
1 parent
87a5291
commit 33846ee
Showing
2 changed files
with
120 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,43 @@ | ||
// Copyright © 2022 Kaleido, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ethereum | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hyperledger/firefly-common/pkg/fftypes" | ||
"github.com/hyperledger/firefly-signer/pkg/ethtypes" | ||
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" | ||
) | ||
|
||
func (c *ethConnector) AddressBalance(ctx context.Context, req *ffcapi.AddressBalanceRequest) (*ffcapi.AddressBalanceResponse, ffcapi.ErrorReason, error) { | ||
|
||
var addressBalance ethtypes.HexInteger | ||
var blockTag = req.BlockTag | ||
if blockTag == "" { | ||
blockTag = "latest" | ||
} | ||
err := c.backend.CallRPC(ctx, &addressBalance, "eth_getBalance", req.Address, blockTag) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return &ffcapi.AddressBalanceResponse{ | ||
Balance: (*fftypes.FFBigInt)(&addressBalance), | ||
}, "", nil | ||
|
||
} |
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,77 @@ | ||
// Copyright © 2022 Kaleido, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ethereum | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hyperledger/firefly-signer/pkg/ethtypes" | ||
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
const sampleGetBalance = `{ | ||
"ffcapi": { | ||
"version": "v1.0.0", | ||
"id": "904F177C-C790-4B01-BDF4-F2B4E52E607E", | ||
"type": "get_address_balance" | ||
}, | ||
"address": "0x4a8c8f1717570f9774652075e249ded38124d708" | ||
}` | ||
|
||
func TestGetAddressBalanceOK(t *testing.T) { | ||
|
||
ctx, c, mRPC, done := newTestConnector(t) | ||
defer done() | ||
|
||
mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBalance", "0x4a8c8f1717570f9774652075e249ded38124d708", "latest"). | ||
Return(nil). | ||
Run(func(args mock.Arguments) { | ||
args[1].(*ethtypes.HexInteger).BigInt().SetString("999", 10) | ||
}) | ||
|
||
var req ffcapi.AddressBalanceRequest | ||
err := json.Unmarshal([]byte(sampleGetBalance), &req) | ||
assert.NoError(t, err) | ||
res, reason, err := c.AddressBalance(ctx, &req) | ||
assert.NoError(t, err) | ||
assert.Empty(t, reason) | ||
|
||
assert.Equal(t, int64(999), res.Nonce.Int64()) | ||
|
||
} | ||
|
||
func TestGetAddressBalanceFail(t *testing.T) { | ||
|
||
ctx, c, mRPC, done := newTestConnector(t) | ||
defer done() | ||
|
||
mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBalance", "0x4a8c8f1717570f9774652075e249ded38124d708", "latest"). | ||
Return(fmt.Errorf("pop")) | ||
|
||
var req ffcapi.AddressBalanceRequest | ||
err := json.Unmarshal([]byte(sampleGetBalance), &req) | ||
assert.NoError(t, err) | ||
res, reason, err := c.AddressBalance(ctx, &req) | ||
assert.Regexp(t, "pop", err) | ||
assert.Empty(t, reason) | ||
assert.Nil(t, res) | ||
|
||
} |