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 8, 2024
1 parent b7a68cc commit 9f9ca13
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
58 changes: 57 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/ioutil"
"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 := ioutil.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,11 @@ func NewChainClient(
go cc.syncTimeoutHeight()
}

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

return cc, nil
}

Expand Down Expand Up @@ -774,6 +807,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 +852,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
74 changes: 74 additions & 0 deletions client/chain/chain_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package chain

import (
"context"
exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"os"
"testing"

Expand Down Expand Up @@ -51,6 +56,75 @@ func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyr
return chainClient, err
}

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

senderAddress, cosmosKeyring, err := InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
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()
marketsAssistant, err := NewMarketsAssistantInitializedFromChain(ctx, exchangeClient)
assert.NoError(t, err)

cc, err := NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)
assert.NoError(t, err)

defaultSubaccountID := cc.DefaultSubaccount(senderAddress)
marketId := "0x01edfab47f124748dc89998eb33144af734484ba07099014594321729a0ca16b"
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
4 changes: 2 additions & 2 deletions examples/chain/8_OfflineSigning/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func LoadTxFromFile(fileName string) ([]byte, error) {
}

func main() {
network := common.LoadNetwork("devnet", "")
network := common.LoadNetwork("devnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
Expand Down 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 9f9ca13

Please sign in to comment.