Skip to content

Commit

Permalink
Claim service. Create tx builder on every request
Browse files Browse the repository at this point in the history
  • Loading branch information
arturalbov committed Oct 23, 2018
1 parent bd5d97e commit 9eb693d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
11 changes: 9 additions & 2 deletions cosmos/poc/claim/client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ func ClaimHandlerFn(ctx context.ClaimContext) func(http.ResponseWriter, *http.Re
coins, _ := sdk.ParseCoins(amount + token)
msg := client.CreateMsg(ctx.ClaimFrom, claimTo, coins)

txBytes, err := ctx.TxBuilder.BuildAndSign(ctx.Name, ctx.Passphrase, []sdk.Msg{msg})
txBldr, err := ctx.TxBuilder()
if err != nil {
panic(err)
util.HandleError(err, w)
return
}

txBytes, err := txBldr.BuildAndSign(ctx.Name, ctx.Passphrase, []sdk.Msg{msg})
if err != nil {
util.HandleError(err, w)
return
}

result, err := ctx.CliContext.BroadcastTxSync(txBytes)
Expand Down
35 changes: 14 additions & 21 deletions cosmos/poc/claim/context/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,9 @@ type ClaimContext struct {
ClaimFrom types.AccAddress
Codec *codec.Codec
CliContext *cli.CLIContext
TxBuilder *authtxb.TxBuilder
ipClaims map[string]int
}

func (c ClaimContext) IncrementIp(ip string) error {
cur := c.ipClaims[ip]
if cur > 100 {
return errors.New("Limit for ip exceeded")
}
c.ipClaims[ip] = cur + 1
return nil
}

func NewClaimContext() (ClaimContext, error) {
name := viper.GetString(common.FlagName)
chainId := viper.GetString(common.FlagChainId)
Expand All @@ -47,41 +37,44 @@ func NewClaimContext() (ClaimContext, error) {
return ClaimContext{}, err
}

txBldr, err := newTxBuilder(cliCtx, cdc, address, chainId)
if err != nil {
return ClaimContext{}, err
}

return ClaimContext{
Name: name,
ClaimFrom: address,
Passphrase: viper.GetString(common.FlagPassphrase),
ChainId: chainId,
Codec: cdc,
CliContext: &cliCtx,
TxBuilder: &txBldr,
ipClaims: make(map[string]int),
}, nil
}

func newTxBuilder(cliCtx cli.CLIContext, cdc *codec.Codec, address types.AccAddress, chainId string) (authtxb.TxBuilder, error) {
accountNumber, err := cliCtx.GetAccountNumber(address)
func (ctx ClaimContext) IncrementIp(ip string) error {
cur := ctx.ipClaims[ip]
if cur > 100 {
return errors.New("Limit for ip exceeded")
}
ctx.ipClaims[ip] = cur + 1
return nil
}

func (ctx ClaimContext) TxBuilder() (authtxb.TxBuilder, error) {
accountNumber, err := ctx.CliContext.GetAccountNumber(ctx.ClaimFrom)
if err != nil {
return authtxb.TxBuilder{}, err
}
seq, err := cliCtx.GetAccountSequence(address)
seq, err := ctx.CliContext.GetAccountSequence(ctx.ClaimFrom)
if err != nil {
return authtxb.TxBuilder{}, err
}

return authtxb.TxBuilder{
ChainID: chainId,
ChainID: ctx.ChainId,
Gas: 10000000,
AccountNumber: accountNumber,
Sequence: seq,
Fee: "",
Memo: "",
Codec: cdc,
Codec: ctx.Codec,
}, nil
}

Expand Down

0 comments on commit 9eb693d

Please sign in to comment.