Skip to content

Commit

Permalink
add native transfer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samtin0x committed Aug 19, 2024
1 parent fd229a0 commit 2517fe3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
22 changes: 15 additions & 7 deletions v4-client-py-v2/dydx_v4_client/indexer/rest/noble_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from v4_proto.cosmos.bank.v1beta1 import query_pb2_grpc as bank_query_grpc
from v4_proto.cosmos.base.v1beta1.coin_pb2 import Coin
from v4_proto.cosmos.tx.v1beta1 import service_pb2_grpc
from v4_proto.cosmos.tx.v1beta1.service_pb2 import SimulateRequest, BroadcastTxRequest, BroadcastMode
from v4_proto.cosmos.tx.v1beta1.service_pb2 import (
SimulateRequest,
BroadcastTxRequest,
BroadcastMode,
)
from v4_proto.cosmos.tx.v1beta1.tx_pb2 import Tx, TxBody, AuthInfo, Fee
from dydx_v4_client.wallet import from_mnemonic, Wallet

Expand Down Expand Up @@ -100,7 +104,7 @@ async def simulate_transfer_native_token(self, amount: str, recipient: str) -> F
msg_send = bank_tx.MsgSend(
from_address=self.wallet.address,
to_address=recipient,
amount=[Coin(amount=amount, denom="uusdc")]
amount=[Coin(amount=amount, denom="uusdc")],
)
any_msg = Any()
any_msg.Pack(msg_send)
Expand All @@ -113,8 +117,10 @@ async def simulate_transfer_native_token(self, amount: str, recipient: str) -> F
simulate_request = SimulateRequest(tx=tx)
simulate_response = stub.Simulate(simulate_request)

return Fee(amount=simulate_response.gas_info.fee.amount, gas_limit=simulate_response.gas_info.gas_used)

return Fee(
amount=simulate_response.gas_info.fee.amount,
gas_limit=simulate_response.gas_info.gas_used,
)

async def transfer_native(self, amount: str, recipient: str) -> str:
"""
Expand All @@ -137,12 +143,11 @@ async def transfer_native(self, amount: str, recipient: str) -> str:
msg_send = bank_tx.MsgSend(
from_address=self.wallet.address,
to_address=recipient,
amount=[Coin(amount=amount, denom="uusdc")]
amount=[Coin(amount=amount, denom="uusdc")],
)
any_msg = Any()
any_msg.Pack(msg_send)


tx_body = TxBody(messages=[any_msg], memo=self.default_client_memo)
fee = await self.simulate_transfer_native_token(amount, recipient)

Expand All @@ -151,7 +156,10 @@ async def transfer_native(self, amount: str, recipient: str) -> str:
signed_tx = self.wallet.sign_tx(tx)

stub = service_pb2_grpc.ServiceStub(self.channel)
broadcast_request = BroadcastTxRequest(tx_bytes=signed_tx.SerializeToString(), mode=BroadcastMode.BROADCAST_MODE_BLOCK)
broadcast_request = BroadcastTxRequest(
tx_bytes=signed_tx.SerializeToString(),
mode=BroadcastMode.BROADCAST_MODE_BLOCK,
)
broadcast_response = stub.BroadcastTx(broadcast_request)

return broadcast_response.tx_response.txhash
2 changes: 1 addition & 1 deletion v4-client-py-v2/dydx_v4_client/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def make_config(
)
TESTNET = make_testnet()
TESTNET_FAUCET = "https://faucet.v4testnet.dydx.exchange"
TESTNET_NOBLE = "https://rpc.testnet.noble.strange.love"
TESTNET_NOBLE = "https://noble-testnet-rpc.polkachu.com"


local_node = partial(
Expand Down
11 changes: 6 additions & 5 deletions v4-client-py-v2/tests/indexer/rest/test_noble_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ async def test_get_address(noble_client):

assert isinstance(address, str)
assert len(address) == 43
assert address.startswith('dydx')
assert address.startswith("dydx")


@pytest.mark.asyncio
@pytest.mark.skip(reason="NobleClient testnet is not available")
async def test_get_account_balances(noble_client):
balances = await noble_client.get_account_balances()

Expand All @@ -26,12 +27,13 @@ async def test_get_account_balances(noble_client):
assert all(isinstance(coin, Coin) for coin in balances)

# Check if there's a balance for the native token (uusdc)
uusdc_balance = next((coin for coin in balances if coin.denom == 'uusdc'), None)
uusdc_balance = next((coin for coin in balances if coin.denom == "uusdc"), None)
assert uusdc_balance is not None
assert int(uusdc_balance.amount) > 0


@pytest.mark.asyncio
@pytest.mark.skip(reason="NobleClient testnet is not available")
async def test_simulate_transfer_native_token(noble_client, test_address):
amount = "1000000" # 1 USDC (assuming 6 decimal places)
recipient = "dydx15ndn9c895f8ntck25qughtuck9spv2d9svw5qx"
Expand All @@ -42,18 +44,17 @@ async def test_simulate_transfer_native_token(noble_client, test_address):
assert len(fee.amount) > 0
assert fee.gas_limit > 0

# Check if the fee is reasonable (e.g., less than 1% of the transfer amount)
fee_amount = sum(int(coin.amount) for coin in fee.amount)
assert fee_amount < int(amount) * 0.01


@pytest.mark.asyncio
@pytest.mark.skip(reason="NobleClient testnet is not available")
async def test_transfer_native_token(noble_client):
amount = "1000000" # 1 USDC (6 decimal places)
recipient = "dydx15ndn9c895f8ntck25qughtuck9spv2d9svw5qx"

tx_hash = await noble_client.transfer_native(amount, recipient)

assert isinstance(tx_hash, str)
assert len(tx_hash) == 64 # Typical length of a transaction hash

assert len(tx_hash) == 64

0 comments on commit 2517fe3

Please sign in to comment.