Skip to content

Commit

Permalink
Merge pull request #23 from oasisprotocol/pro-wh/feature/cons
Browse files Browse the repository at this point in the history
Construction API work
  • Loading branch information
pro-wh authored Aug 7, 2020
2 parents 883ad28 + f9d7705 commit 248f78d
Show file tree
Hide file tree
Showing 14 changed files with 1,504 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env gmake

OASIS_RELEASE := 20.8.2
ROSETTA_CLI_RELEASE := 0.2.5
ROSETTA_CLI_RELEASE := 0.4.0

OASIS_GO ?= go
GO := env -u GOPATH $(OASIS_GO)
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ go 1.14
replace github.com/tendermint/tendermint => github.com/oasisprotocol/tendermint v0.33.4-oasis2

require (
github.com/coinbase/rosetta-sdk-go v0.2.0
github.com/coinbase/rosetta-cli v0.4.0
github.com/coinbase/rosetta-sdk-go v0.3.3
github.com/dgraph-io/badger v1.6.1
github.com/oasisprotocol/ed25519 v0.0.0-20200528083105-55566edd6df0
github.com/oasisprotocol/oasis-core/go v0.0.0-20200702171459-20d1a2dc6b66
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1
google.golang.org/grpc v1.29.1
)
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/logging"

"github.com/oasisprotocol/oasis-core-rosetta-gateway/oasis-client"
Expand All @@ -33,6 +34,8 @@ func NewBlockchainRouter(oasisClient oasis_client.OasisClient) (http.Handler, er
}

asserter, err := asserter.NewServer(
services.SupportedOperationTypes,
true,
[]*types.NetworkIdentifier{
&types.NetworkIdentifier{
Blockchain: services.OasisBlockchainName,
Expand Down Expand Up @@ -94,6 +97,9 @@ func main() {
os.Exit(1)
}

// Set the chain context for preparing signing payloads.
signature.SetChainContext(cid)

// Initialize logging.
err = logging.Initialize(os.Stdout, logging.FmtLogfmt, logging.LevelDebug, nil)
if err != nil {
Expand Down
29 changes: 11 additions & 18 deletions services/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"

oc "github.com/oasisprotocol/oasis-core-rosetta-gateway/oasis-client"
"github.com/oasisprotocol/oasis-core/go/common/logging"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

// SubAccountGeneral specifies the name of the general subaccount.
const SubAccountGeneral = "general"
oc "github.com/oasisprotocol/oasis-core-rosetta-gateway/oasis-client"
)

// SubAccountEscrow specifies the name of the escrow subaccount.
const SubAccountEscrow = "escrow"
Expand Down Expand Up @@ -65,17 +63,12 @@ func (s *accountAPIService) AccountBalance(
return nil, ErrInvalidAccountAddress
}

if request.AccountIdentifier.SubAccount == nil {
loggerAcct.Error("AccountBalance: invalid sub-account (empty)")
switch {
case request.AccountIdentifier.SubAccount == nil:
case request.AccountIdentifier.SubAccount.Address == SubAccountEscrow:
default:
loggerAcct.Error("AccountBalance: invalid subaccount", "sub_account", request.AccountIdentifier.SubAccount)
return nil, ErrMustSpecifySubAccount
} else {
switch request.AccountIdentifier.SubAccount.Address {
case SubAccountGeneral:
case SubAccountEscrow:
default:
loggerAcct.Error("AccountBalance: invalid sub-account", "subaccount", request.AccountIdentifier.SubAccount.Address)
return nil, ErrMustSpecifySubAccount
}
}

act, err := s.oasisClient.GetAccount(ctx, height, owner)
Expand All @@ -101,10 +94,10 @@ func (s *accountAPIService) AccountBalance(
md[NonceKey] = act.General.Nonce

var value string
switch request.AccountIdentifier.SubAccount.Address {
case SubAccountGeneral:
switch {
case request.AccountIdentifier.SubAccount == nil:
value = act.General.Balance.String()
case SubAccountEscrow:
case request.AccountIdentifier.SubAccount.Address == SubAccountEscrow:
// Total is Active + Debonding.
total := act.Escrow.Active.Balance.Clone()
if err := total.Add(&act.Escrow.Debonding.Balance); err != nil {
Expand Down Expand Up @@ -141,7 +134,7 @@ func (s *accountAPIService) AccountBalance(
loggerAcct.Debug("AccountBalance OK",
"response", jr,
"account_id", owner.String(),
"subaccount", request.AccountIdentifier.SubAccount.Address,
"sub_account", request.AccountIdentifier.SubAccount,
)

return resp, nil
Expand Down
29 changes: 14 additions & 15 deletions services/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"

oc "github.com/oasisprotocol/oasis-core-rosetta-gateway/oasis-client"
"github.com/oasisprotocol/oasis-core/go/common/logging"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"

oc "github.com/oasisprotocol/oasis-core-rosetta-gateway/oasis-client"
)

// OpTransfer is the Transfer operation.
Expand Down Expand Up @@ -41,7 +42,7 @@ func NewBlockAPIService(oasisClient oc.OasisClient) server.BlockAPIServicer {
}

// Helper for making ops in a succinct way.
func appendOp(ops []*types.Operation, kind string, acct string, subacct string, amt string) []*types.Operation {
func appendOp(ops []*types.Operation, kind string, acct string, subacct *types.SubAccountIdentifier, amt string) []*types.Operation {
opidx := int64(len(ops))
op := &types.Operation{
OperationIdentifier: &types.OperationIdentifier{
Expand All @@ -50,10 +51,8 @@ func appendOp(ops []*types.Operation, kind string, acct string, subacct string,
Type: kind,
Status: OpStatusOK,
Account: &types.AccountIdentifier{
Address: acct,
SubAccount: &types.SubAccountIdentifier{
Address: subacct,
},
Address: acct,
SubAccount: subacct,
},
Amount: &types.Amount{
Value: amt,
Expand Down Expand Up @@ -137,24 +136,24 @@ func (s *blockAPIService) Block(

switch {
case evt.Transfer != nil:
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, evt.Transfer.From.String(), SubAccountGeneral, "-"+evt.Transfer.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, evt.Transfer.To.String(), SubAccountGeneral, evt.Transfer.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, StringFromAddress(evt.Transfer.From), nil, "-"+evt.Transfer.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, StringFromAddress(evt.Transfer.To), nil, evt.Transfer.Tokens.String())
case evt.Burn != nil:
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpBurn, evt.Burn.Owner.String(), SubAccountGeneral, "-"+evt.Burn.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpBurn, StringFromAddress(evt.Burn.Owner), nil, "-"+evt.Burn.Tokens.String())
case evt.Escrow != nil:
ee := evt.Escrow
switch {
case ee.Add != nil:
// Owner's general account -> escrow account.
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, ee.Add.Owner.String(), SubAccountGeneral, "-"+ee.Add.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, ee.Add.Escrow.String(), SubAccountEscrow, ee.Add.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, StringFromAddress(ee.Add.Owner), nil, "-"+ee.Add.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, StringFromAddress(ee.Add.Escrow), &types.SubAccountIdentifier{Address: SubAccountEscrow}, ee.Add.Tokens.String())
case ee.Take != nil:
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, ee.Take.Owner.String(), SubAccountEscrow, "-"+ee.Take.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, staking.CommonPoolAddress.String(), SubAccountGeneral, ee.Take.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, StringFromAddress(ee.Take.Owner), &types.SubAccountIdentifier{Address: SubAccountEscrow}, "-"+ee.Take.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, StringFromAddress(staking.CommonPoolAddress), nil, ee.Take.Tokens.String())
case ee.Reclaim != nil:
// Escrow account -> owner's general account.
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, ee.Reclaim.Escrow.String(), SubAccountEscrow, "-"+ee.Reclaim.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, ee.Reclaim.Owner.String(), SubAccountGeneral, ee.Reclaim.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, StringFromAddress(ee.Reclaim.Escrow), &types.SubAccountIdentifier{Address: SubAccountEscrow}, "-"+ee.Reclaim.Tokens.String())
txns[txidx].Operations = appendOp(txns[txidx].Operations, OpTransfer, StringFromAddress(ee.Reclaim.Owner), nil, ee.Reclaim.Tokens.String())
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions services/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/coinbase/rosetta-sdk-go/types"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"

oc "github.com/oasisprotocol/oasis-core-rosetta-gateway/oasis-client"
)
Expand All @@ -17,6 +18,11 @@ var OasisCurrency = &types.Currency{
Decimals: 9,
}

// PoolShare is the currency used for debonding.
var PoolShare = &types.Currency{
Symbol: "(pool share)",
}

// GetChainID returns the chain ID.
func GetChainID(ctx context.Context, oc oc.OasisClient) (string, *types.Error) {
chainID, err := oc.GetChainID(ctx)
Expand Down Expand Up @@ -47,3 +53,13 @@ func ValidateNetworkIdentifier(ctx context.Context, oc oc.OasisClient, ni *types
}
return nil
}

// StringFromAddress converts a staking API address to string using MarshalText.
// If marshalling fails, this panics.
func StringFromAddress(address staking.Address) string {
buf, err := address.MarshalText()
if err != nil {
panic(err)
}
return string(buf)
}
Loading

0 comments on commit 248f78d

Please sign in to comment.