Skip to content

Commit

Permalink
fix: sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjukim committed Mar 8, 2022
1 parent 959dc61 commit f27b532
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 63 deletions.
97 changes: 43 additions & 54 deletions faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,56 +287,35 @@ func checkAndUpdateLimit(db *leveldb.DB, account []byte, denom string) error {
return nil
}

func drip(encodedAddress string, denom string, amount int64, isDetectMismatch bool) []byte {
url := fmt.Sprintf("%v/bank/accounts/%v/transfers", lcdURL, encodedAddress)
func drip(encodedAddress string, denom string, amount int64, isDetectMismatch bool) string {
data := strings.TrimSpace(fmt.Sprintf(`{
"base_req": {
"from": "%v",
"memo": "%v",
"chain_id": "%v",
"sequence": "%v",
"gas": "auto",
"gas_adjustment": "1.8",
"gas_prices": [
{
"denom": "ukrw",
"amount": "169.77"
"type": "core/StdTx",
"value": {
"msg": [{
"type": "bank/MsgSend",
"value": {
"from_address": "%v",
"to_address": "%v",
"amount": [{
"denom": "%v",
"amount": "%v"
}]
}
]
},
"coins": [
{
"denom": "%v",
"amount": "%v"
}
]
}`, address, "faucet", chainID, sequence, denom, amount))

response, err := http.Post(url, "application/json", bytes.NewReader([]byte(data)))
if err != nil {
panic(err)
}

defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)

if err != nil {
panic(err)
}

if response.StatusCode != 200 {
stringBody := string(body)

if isDetectMismatch && strings.Contains(stringBody, "sequence mismatch") {
return make([]byte, 0)
}],
"fee": {
"amount": [{
"denom": "ukrw",
"amount": "25500000"
}],
"gas": "150000"
},
"signatures": [],
"memo": "%v",
"timeout_height": "0"
}
}`, address, encodedAddress, denom, amount, "faucet"))

err := errors.New(stringBody)
panic(err)
}

return body
return signAndBroadcast([]byte(data), isDetectMismatch)
}

func createGetCoinsHandler(db *leveldb.DB) http.HandlerFunc {
Expand Down Expand Up @@ -406,14 +385,15 @@ func createGetCoinsHandler(db *leveldb.DB) http.HandlerFunc {
}
}

resJSON := signAndBroadcast(body)
sequence = sequence + 1
if len(body) != 0 {
sequence = sequence + 1
}

fmt.Println(time.Now().UTC().Format(time.RFC3339), encodedAddress, "[1] ", amount, claim.Denom)
fmt.Println(resJSON)
fmt.Println(body)

w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"amount": %v, "response": %v}`, amount, resJSON)
fmt.Fprintf(w, `{"amount": %v, "response": %v}`, amount, body)
} else {
err := errors.New("captcha failed, please refresh page and try again")
panic(err)
Expand All @@ -423,11 +403,13 @@ func createGetCoinsHandler(db *leveldb.DB) http.HandlerFunc {

// BroadcastReq defines a tx broadcasting request.
type BroadcastReq struct {
Tx auth.StdTx `json:"tx"`
Mode string `json:"mode"`
Tx auth.StdTx `json:"tx"`
Mode string `json:"mode"`
Sequences []uint64 `json:"sequences" yaml:"sequences"`
FeeGranter string `json:"fee_granter" yaml:"fee_granter"`
}

func signAndBroadcast(txJSON []byte) string {
func signAndBroadcast(txJSON []byte, isDetectMismatch bool) string {
var broadcastReq BroadcastReq
var stdTx auth.StdTx

Expand All @@ -453,6 +435,7 @@ func signAndBroadcast(txJSON []byte) string {
tx := auth.NewStdTx(stdTx.Msgs, stdTx.Fee, sigs, stdTx.Memo)
broadcastReq.Tx = tx
broadcastReq.Mode = "sync"
broadcastReq.Sequences = []uint64{sequence}

bz := cdc.MustMarshalJSON(broadcastReq)

Expand All @@ -469,10 +452,16 @@ func signAndBroadcast(txJSON []byte) string {
panic(err)
}

stringBody := string(body)

if response.StatusCode != 200 {
err := fmt.Errorf("status: %v, message: %v", response.Status, string(body))
err := fmt.Errorf("status: %v, message: %v", response.Status, stringBody)
panic(err)
}

if isDetectMismatch && strings.Contains(stringBody, "sequence mismatch") {
return ""
}

return string(body)
}
20 changes: 12 additions & 8 deletions frontend/src/Pages/Home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ class HomeComponent extends React.Component {
denom: values.denom,
response: this.state.response,
})
.then((response) => {
const { amount } = response.data;

toast.success(
`Successfully Sent ${amount / 1000000} ${
DENUMS_TO_TOKEN[values.denom]
} to ${values.address}`
);
.then((res) => {
const { amount, response } = res.data;

if (response.code) {
toast.error(`Error: ${response.raw_log || `code: ${response.code}`}`);
} else {
toast.success(
`Successfully Sent ${amount / 1000000} ${
DENUMS_TO_TOKEN[values.denom]
} to ${values.address}`
);
}

resetForm();
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/cosmos/cosmos-sdk v0.37.4
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d
github.com/dpapathanasiou/go-recaptcha v0.0.0-20190121160230-be5090b17804
github.com/rs/cors v1.7.0 // indirect
github.com/rs/cors v1.7.0
github.com/syndtr/goleveldb v1.0.1-0.20190318030020-c3a204f8e965
github.com/tendermint/tendermint v0.32.7
github.com/tendermint/tmlibs v0.0.0-20180607034639-640af0205d98
Expand Down

0 comments on commit f27b532

Please sign in to comment.