Skip to content

Commit

Permalink
BigInt message for amounts (#13)
Browse files Browse the repository at this point in the history
BigInt message for amounts
  • Loading branch information
vitiko authored Dec 21, 2022
1 parent a6844ec commit 3a9691e
Show file tree
Hide file tree
Showing 25 changed files with 1,349 additions and 963 deletions.
3 changes: 2 additions & 1 deletion examples/erc20_utxo/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package erc20_utxo

import (
"errors"
"math/big"

"github.com/hyperledger-labs/cckit/examples/erc20_utxo/service/allowance"
"github.com/hyperledger-labs/cckit/examples/erc20_utxo/service/config"
Expand All @@ -15,7 +16,7 @@ var (
Name: `SomeToken`,
Symbol: `@`,
Decimals: 2,
TotalSupply: `10000000`,
TotalSupply: token.NewBigInt(big.NewInt(10000000)),
}
)

Expand Down
7 changes: 4 additions & 3 deletions examples/erc20_utxo/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package erc20_utxo_test

import (
"encoding/base64"
"math/big"
"testing"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -97,13 +98,13 @@ var _ = Describe(`ERC`, func() {
&token.BalanceId{Address: user1Address, Symbol: erc20_utxo.Token.Symbol}),
&token.Balance{}).(*token.Balance)

Expect(b.Amount).To(Equal(`0`))
Expect(b.Amount).To(Equal(token.NewBigInt(big.NewInt(0))))
})

})

Context(`transfer`, func() {
var transferAmount = `100`
var transferAmount = token.NewBigInt(big.NewInt(100))

It(`Disallow to transfer balance by user with zero balance`, func() {
expectcc.ResponseError(
Expand Down Expand Up @@ -149,7 +150,7 @@ var _ = Describe(`ERC`, func() {

Context(`Allowance`, func() {

var allowAmount = `50`
var allowAmount = token.NewBigInt(big.NewInt(50))

It(`Allow to approve amount by owner for spender even if balance is zero`, func() {
a := expectcc.PayloadIs(
Expand Down
9 changes: 4 additions & 5 deletions examples/erc20_utxo/service/allowance/allowance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package allowance
import (
"errors"
"fmt"
"math/big"

"github.com/hyperledger-labs/cckit/extensions/account"
"github.com/hyperledger-labs/cckit/extensions/token"
Expand Down Expand Up @@ -41,7 +40,7 @@ func (s *Service) GetAllowance(ctx router.Context, id *AllowanceId) (*Allowance,
Spender: id.Spender,
Symbol: id.Symbol,
Group: id.Group,
Amount: ``,
Amount: nil,
}, nil
}
return nil, fmt.Errorf(`get allowance: %w`, err)
Expand Down Expand Up @@ -107,12 +106,12 @@ func (s *Service) TransferFrom(ctx router.Context, req *TransferFromRequest) (*T
return nil, err
}

reqAmount, err := token.IntVal(req.Amount)
reqAmount, err := req.Amount.BigInt()
if err != nil {
return nil, fmt.Errorf(`req amount: %w`, err)
}

curAmount, err := token.IntVal(allowance.Amount)
curAmount, err := allowance.Amount.BigInt()
if err != nil {
return nil, fmt.Errorf(`cur amount: %w`, err)
}
Expand All @@ -121,7 +120,7 @@ func (s *Service) TransferFrom(ctx router.Context, req *TransferFromRequest) (*T
req.Amount, allowance.Amount, ErrAllowanceInsufficient)
}

allowance.Amount = new(big.Int).Sub(curAmount, reqAmount).String()
allowance.Amount = token.NewBigIntSub(curAmount, reqAmount)

// sub from allowance
if err := State(ctx).Put(allowance); err != nil {
Expand Down
363 changes: 194 additions & 169 deletions examples/erc20_utxo/service/allowance/allowance.pb.go

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions examples/erc20_utxo/service/allowance/allowance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "google/api/annotations.proto";
import "google/protobuf/empty.proto";
import "mwitkow/go-proto-validators/validator.proto";

import "token/balance.proto";

service AllowanceService {
// Returns the remaining number of tokens that spender will be allowed to spend on behalf of owner through transfersender.
// This is zero by default.
Expand Down Expand Up @@ -38,23 +40,23 @@ message ApproveRequest {
string spender = 2 [(validator.field) = {string_not_empty : true}];
string symbol = 3 [(validator.field) = {string_not_empty : true}];
repeated string group = 4;
string amount = 5 [(validator.field) = {string_not_empty : true}];
cckit.extensions.token.BigInt amount = 5 [(validator.field) = {string_not_empty : true}];
}

message TransferFromRequest {
string owner = 1 [(validator.field) = {string_not_empty : true}];
string recipient = 2 [(validator.field) = {string_not_empty : true}];
string symbol = 3 [(validator.field) = {string_not_empty : true}];
repeated string group = 4;
string amount = 5 [(validator.field) = {string_not_empty : true}];
cckit.extensions.token.BigInt amount = 5 [(validator.field) = {string_not_empty : true}];
}

message TransferFromResponse {
string owner = 1;
string recipient = 2;
string symbol = 3 ;
repeated string group = 4;
string amount = 5;
cckit.extensions.token.BigInt amount = 5;
}

// Id: Allowance identifier
Expand All @@ -71,15 +73,15 @@ message Allowance {
string spender = 2;
string symbol = 3 ;
repeated string group = 4;
string amount = 5;
cckit.extensions.token.BigInt amount = 5;
}

message Operation {
string owner = 1;
string spender = 2;
string symbol = 3 ;
repeated string group = 4;
string amount = 5;
cckit.extensions.token.BigInt amount = 5;
}

// List:
Expand All @@ -93,7 +95,7 @@ message Approved {
string spender = 2;
string symbol = 3 ;
repeated string group = 4;
string amount = 5;
cckit.extensions.token.BigInt amount = 5;
}

// Event: TransferredFrom event is emitted when TransferFrom method has been invoked
Expand All @@ -103,5 +105,5 @@ message TransferredFrom {
string recipient = 3;
string symbol = 4 ;
repeated string group = 5;
string amount = 6;
cckit.extensions.token.BigInt amount = 6;
}
12 changes: 10 additions & 2 deletions examples/erc20_utxo/service/allowance/allowance.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
}
},
"amount": {
"type": "string"
"$ref": "#/definitions/tokenBigInt"
}
},
"title": "State: Allowance"
Expand All @@ -156,7 +156,7 @@
}
},
"amount": {
"type": "string"
"$ref": "#/definitions/tokenBigInt"
}
}
},
Expand Down Expand Up @@ -192,6 +192,14 @@
}
}
}
},
"tokenBigInt": {
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
}
}
38 changes: 34 additions & 4 deletions examples/erc20_utxo/service/allowance/allowance.validator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3a9691e

Please sign in to comment.