Skip to content

Commit

Permalink
Implementation of AddressBalance() FFCAPI function
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Whitehead <[email protected]>
  • Loading branch information
matthew1001 committed Dec 8, 2022
1 parent 87a5291 commit 33846ee
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
43 changes: 43 additions & 0 deletions internal/ethereum/get_address_balance.go
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

}
77 changes: 77 additions & 0 deletions internal/ethereum/get_address_balance_test.go
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)

}

0 comments on commit 33846ee

Please sign in to comment.