Skip to content

Commit

Permalink
Initial commit to use server to handle claims
Browse files Browse the repository at this point in the history
  • Loading branch information
toshiSat committed Jul 30, 2024
1 parent bce2f08 commit 2606b36
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 78 deletions.
14 changes: 11 additions & 3 deletions proto/arkeo/claim/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,36 @@ option go_package = "github.com/arkeonetwork/arkeo/x/claim/types";
service Msg {
rpc ClaimEth(MsgClaimEth) returns (MsgClaimEthResponse);
rpc ClaimArkeo(MsgClaimArkeo) returns (MsgClaimArkeoResponse);
rpc ClaimThorchain(MsgClaimThorchain) returns (MsgClaimThorchainResponse);
rpc TransferClaim(MsgTransferClaim) returns (MsgTransferClaimResponse);
rpc AddClaim(MsgAddClaim) returns (MsgAddClaimResponse);
// this line is used by starport scaffolding # proto/tx/rpc
}
message MsgClaimEth {
bytes creator = 1 [ (gogoproto.casttype) =
"github.com/cosmos/cosmos-sdk/types.AccAddress" ];
string eth_address = 2; // the adress the claim is for
string eth_address = 2; // the address the claim is for
string signature = 3; // EIP712 signature that has to be signed by ethAddress
string thor_tx = 4; // the tx hash of the thorchain tx that delegates the arkeo claim
}

message MsgClaimEthResponse {}

message MsgClaimArkeo {
bytes creator = 1 [ (gogoproto.casttype) =
"github.com/cosmos/cosmos-sdk/types.AccAddress" ];
string thor_tx = 2; // the tx hash of the thorchain tx that delegates the arkeo claim
}

message MsgClaimArkeoResponse {}

message MsgClaimThorchain {
bytes creator = 1 [ (gogoproto.casttype) =
"github.com/cosmos/cosmos-sdk/types.AccAddress" ];
string fromAddress = 2; // the address that is delegating the claim
string toAddress = 3; // the address to delegate the claim to
}

message MsgClaimThorchainResponse {}

message MsgTransferClaim {
bytes creator = 1 [ (gogoproto.casttype) =
"github.com/cosmos/cosmos-sdk/types.AccAddress" ];
Expand Down
3 changes: 3 additions & 0 deletions scripts/genesis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ if [ ! -f ~/.arkeo/config/genesis.json ]; then
# Thorchain derived test addresses
add_account "tarkeo1dllfyp57l4xj5umqfcqy6c2l3xfk0qk6zpc3t7" $TOKEN 1000000000000000 # bob, 10m
add_claim_records "ARKEO" "tarkeo1dllfyp57l4xj5umqfcqy6c2l3xfk0qk6zpc3t7" 1000 1000 1000 true
add_account "tarkeo1xrz7z3zwtpc45xm72tpnevuf3wn53re8q4u4nr" $TOKEN 1000000000000000
add_claim_records "ARKEO" "tarkeo1xrz7z3zwtpc45xm72tpnevuf3wn53re8q4u4nr" 1000 1000 1000 true

# add_claim_records "ARKEO" "{YOUR ARKEO ADDRESS}" 500000 500000 500000 true
# add_account "{YOUR ARKEO ADDRESS}" $TOKEN 1000000000000000

# add_claim_records "ETHEREUM" "{YOUR ETH ADDRESS}" 500000 600000 700000 true
add_claim_records "ETHEREUM" "0x92E14917A0508Eb56C90C90619f5F9Adbf49f47d" 500000 600000 700000 true

# enable CORs on testnet/localnet
sed -i 's/enabled-unsafe-cors = false/enabled-unsafe-cors = true/g' ~/.arkeo/config/app.toml
Expand Down
7 changes: 0 additions & 7 deletions x/claim/keeper/msg_server_claim_arkeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ func (k msgServer) ClaimArkeo(goCtx context.Context, msg *types.MsgClaimArkeo) (
return nil, errors.Wrapf(err, "failed to get claim record for %s", msg.Creator)
}

if msg.ThorTx != "" {
arkeoClaimRecord, err = k.updateThorClaimRecord(ctx, msg.Creator.String(), msg.ThorTx, arkeoClaimRecord)
if err != nil {
return nil, errors.Wrapf(err, "failed to get claim record for %s", msg.ThorTx)
}
}

if arkeoClaimRecord.IsEmpty() || arkeoClaimRecord.AmountClaim.IsZero() {
return nil, errors.Wrapf(types.ErrNoClaimableAmount, "no claimable amount for %s", msg.Creator)
}
Expand Down
7 changes: 0 additions & 7 deletions x/claim/keeper/msg_server_claim_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ func (k msgServer) ClaimEth(goCtx context.Context, msg *types.MsgClaimEth) (*typ
return nil, errors.Wrapf(err, "failed to set claim record for %s", msg.Creator)
}

if msg.ThorTx != "" {
arkeoClaim, err = k.updateThorClaimRecord(ctx, msg.Creator.String(), msg.ThorTx, arkeoClaim)
if err != nil {
return nil, errors.Wrapf(err, "failed to get claim record for %s", msg.ThorTx)
}
}

// call claim on arkeo to claim arkeo (note: this could CLAIM for all tokens that are now merged)
_, err = k.ClaimCoinsForAction(ctx, msg.Creator.String(), types.ACTION_CLAIM)
if err != nil {
Expand Down
65 changes: 4 additions & 61 deletions x/claim/keeper/msg_server_claim_thorchain.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,21 @@
package keeper

import (
"encoding/json"
"fmt"
"github.com/arkeonetwork/arkeo/x/claim/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
"io"
"net/http"
"strings"
)

type ThorTxData struct {
ObservedTx struct {
Tx struct {
FromAddress string `json:"from_address"`
Memo string `json:"memo"`
} `json:"tx"`
} `json:"observed_tx"`
}

// Verify and update the claim record based on the memo of the thorchain tx
func (k msgServer) updateThorClaimRecord(ctx sdk.Context, creator string, thorTx string, arkeoClaimRecord types.ClaimRecord) (types.ClaimRecord, error) {
url := fmt.Sprintf("https://thornode.ninerealms.com/thorchain/tx/%s", thorTx)

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return types.ClaimRecord{}, errors.Wrapf(err, "failed to build request %s", req.RequestURI)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return types.ClaimRecord{}, errors.Wrapf(err, "failed to get thorchain tx for %s", thorTx)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return types.ClaimRecord{}, fmt.Errorf("received non-OK HTTP status: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("error reading response body: %w", err)
}

var txData ThorTxData
if err := json.Unmarshal(body, &txData); err != nil {
return types.ClaimRecord{}, fmt.Errorf("error unmarshalling transaction data: %w", err)
}
thorAddress := txData.ObservedTx.Tx.FromAddress
memo := txData.ObservedTx.Tx.Memo
func (k msgServer) ClaimThorchain(ctx sdk.Context, msg *types.MsgClaimThorchain) (types.ClaimRecord, error) {

thorAddressBytes, err := sdk.GetFromBech32(thorAddress, "thor")
thorClaimRecord, err := k.GetClaimRecord(ctx, originalArkeoAddress, types.ARKEO)

Check failure on line 13 in x/claim/keeper/msg_server_claim_thorchain.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: originalArkeoAddress
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("invalid thorchain address: %w", err)
}
prefix := sdk.GetConfig().GetBech32AccountAddrPrefix()

// Re-encode the raw bytes with the new prefix
thorDerivedArkeoAddress, err := sdk.Bech32ifyAddressBytes(prefix, thorAddressBytes)
if err != nil {
return types.ClaimRecord{}, fmt.Errorf("failed to encode address bytes with new prefix: %w", err)
}

thorClaimRecord, err := k.GetClaimRecord(ctx, thorDerivedArkeoAddress, types.ARKEO)
if err != nil {
return types.ClaimRecord{}, errors.Wrapf(err, "failed to get claim record for %s", thorDerivedArkeoAddress)
return types.ClaimRecord{}, errors.Wrapf(err, "failed to get claim record for %s", originalArkeoAddress)

Check failure on line 15 in x/claim/keeper/msg_server_claim_thorchain.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: originalArkeoAddress
}
if thorClaimRecord.IsEmpty() || thorClaimRecord.AmountClaim.IsZero() {
return types.ClaimRecord{}, errors.Wrapf(types.ErrNoClaimableAmount, "no claimable amount for %s", thorDerivedArkeoAddress)
}
parts := strings.Split(memo, ":")
if len(parts) != 3 || parts[0] != "delegate" || parts[1] != "arkeo" {
return types.ClaimRecord{}, fmt.Errorf("invalid memo: %s", memo)
return types.ClaimRecord{}, errors.Wrapf(types.ErrNoClaimableAmount, "no claimable amount for %s", originalArkeoAddress)

Check failure on line 18 in x/claim/keeper/msg_server_claim_thorchain.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: originalArkeoAddress
}

combinedClaimRecord := types.ClaimRecord{
Expand Down

0 comments on commit 2606b36

Please sign in to comment.