Skip to content

Commit

Permalink
(feat) Add OFAC list check
Browse files Browse the repository at this point in the history
  • Loading branch information
shibaeff committed Sep 9, 2024
1 parent b7a68cc commit ca0da1a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
59 changes: 58 additions & 1 deletion client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -61,6 +63,7 @@ const (
defaultBroadcastTimeout = 40 * time.Second
defaultTimeoutHeight = 20
defaultTimeoutHeightSyncInterval = 10 * time.Second
defaultOfacListURL = "https://raw.githubusercontent.com/InjectiveLabs/injective-lists/master/wallets/ofac.json"
SpotOrderbook = "injective.exchange.v1beta1.EventOrderbookUpdate.spot_orderbooks"
DerivativeOrderbook = "injective.exchange.v1beta1.EventOrderbookUpdate.derivative_orderbooks"
)
Expand Down Expand Up @@ -321,6 +324,8 @@ type chainClient struct {

sessionEnabled bool

ofacTxList []string

authQueryClient authtypes.QueryClient
authzQueryClient authztypes.QueryClient
bankQueryClient banktypes.QueryClient
Expand All @@ -342,6 +347,29 @@ type chainClient struct {
canSign bool
}

func (cc *chainClient) loadOfacList() error {
response, err := http.Get(defaultOfacListURL)
if err != nil {
return err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return fmt.Errorf("request to the OFAC upstream failed with code: %s", response.Status)
}

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

var ofacList []string
if err := json.Unmarshal(body, &ofacList); err != nil {
return err
}
return nil
}

func NewChainClient(
ctx client.Context,
network common.Network,
Expand Down Expand Up @@ -453,6 +481,12 @@ func NewChainClient(
go cc.syncTimeoutHeight()
}

err = cc.loadOfacList()
if err != nil {
err = errors.Wrap(err, "failed to load the OFAC list")
return nil, err
}

return cc, nil
}

Expand Down Expand Up @@ -774,6 +808,29 @@ func (c *chainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, in
}

func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) ([]byte, error) {
isAddrInTheList := func(list []string, addr string) bool {
for _, item := range list {
if item == addr {
return true
}
}
return false
}
k, err := txf.Keybase().Key(clientCtx.FromName)
if err != nil {
err = errors.Wrap(err, "error parsing signer account address")
return nil, err
}
signerAddressPubKey, err := k.GetPubKey()
if err != nil {
err = errors.Wrap(err, "error getting signer public key")
return nil, err
}
if isAddrInTheList(c.ofacTxList, sdk.AccAddress(signerAddressPubKey.Address()).String()) {
err = errors.Errorf("Address is in the OFAC list")
return nil, err
}

ctx := context.Background()
if clientCtx.Simulate {
simTxBytes, err := txf.BuildSimTx(msgs...)
Expand All @@ -796,7 +853,7 @@ func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, ms
c.gasWanted = adjustedGas
}

txf, err := PrepareFactory(clientCtx, txf)
txf, err = PrepareFactory(clientCtx, txf)
if err != nil {
return nil, errors.Wrap(err, "failed to prepareFactory")
}
Expand Down
69 changes: 69 additions & 0 deletions client/chain/chain_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package chain

import (
"context"
"os"
"testing"

exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange"
spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"

"github.com/InjectiveLabs/sdk-go/client"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
Expand Down Expand Up @@ -51,6 +58,68 @@ func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyr
return chainClient, err
}

func TestOfacList(t *testing.T) {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
assert.NoError(t, err)

senderAddress, cosmosKeyring, err := accountForTests()
assert.NoError(t, err)

clientCtx, err := NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)
assert.NoError(t, err)

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

exchangeClient, err := exchangeclient.NewExchangeClient(network)
assert.NoError(t, err)
ctx := context.Background()
req := spotExchangePB.MarketsRequest{
MarketStatus: "active",
}
res, err := exchangeClient.GetSpotMarkets(ctx, &req)
assert.NoError(t, err)

marketsAssistant, err := NewMarketsAssistantInitializedFromChain(ctx, exchangeClient)
assert.NoError(t, err)

cc, err := createClient(senderAddress, cosmosKeyring, network)
assert.NoError(t, err)

defaultSubaccountID := cc.DefaultSubaccount(senderAddress)
marketId := res.Markets[0].MarketId
amount := decimal.NewFromFloat(2)
price := decimal.NewFromFloat(1.02)

order := cc.CreateSpotOrder(
defaultSubaccountID,
&SpotOrderData{
OrderType: exchangetypes.OrderType_BUY, //BUY SELL BUY_PO SELL_PO
Quantity: amount,
Price: price,
FeeRecipient: senderAddress.String(),
MarketId: marketId,
},
marketsAssistant,
)

msg := new(exchangetypes.MsgCreateSpotLimitOrder)
msg.Sender = senderAddress.String()
msg.Order = exchangetypes.SpotOrder(*order)

accNum, accSeq := cc.GetAccNonce()

cc.(*chainClient).ofacTxList = []string{
senderAddress.String(),
}
_, err = cc.BuildSignedTx(clientCtx, accNum, accSeq, 20000, msg)
assert.Error(t, err)
}

func TestDefaultSubaccount(t *testing.T) {
network := common.LoadNetwork("devnet", "lb")
senderAddress, cosmosKeyring, err := accountForTests()
Expand Down
2 changes: 1 addition & 1 deletion examples/chain/8_OfflineSigning/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func main() {
}

defaultSubaccountID := chainClient.DefaultSubaccount(senderAddress)
marketId := "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
marketId := "0x01edfab47f124748dc89998eb33144af734484ba07099014594321729a0ca16b"
amount := decimal.NewFromFloat(2)
price := decimal.NewFromFloat(1.02)

Expand Down

0 comments on commit ca0da1a

Please sign in to comment.