|
| 1 | +import { TextEncoder } from 'util'; |
| 2 | + |
| 3 | +import { toBase64 } from '@cosmjs/encoding'; |
| 4 | +import { Order_TimeInForce } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/order'; |
| 5 | + |
| 6 | +import { BECH32_PREFIX } from '../src'; |
| 7 | +import { CompositeClient } from '../src/clients/composite-client'; |
| 8 | +import { AuthenticatorType, Network, OrderSide, SelectedGasDenom } from '../src/clients/constants'; |
| 9 | +import LocalWallet from '../src/clients/modules/local-wallet'; |
| 10 | +import { SubaccountInfo } from '../src/clients/subaccount'; |
| 11 | +import { DYDX_TEST_MNEMONIC, DYDX_TEST_MNEMONIC_2 } from './constants'; |
| 12 | + |
| 13 | +async function test(): Promise<void> { |
| 14 | + const wallet1 = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX); |
| 15 | + const wallet2 = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC_2, BECH32_PREFIX); |
| 16 | + |
| 17 | + const network = Network.staging(); |
| 18 | + const client = await CompositeClient.connect(network); |
| 19 | + client.setSelectedGasDenom(SelectedGasDenom.NATIVE); |
| 20 | + |
| 21 | + console.log('**Client**'); |
| 22 | + console.log(client); |
| 23 | + |
| 24 | + const subaccount1 = new SubaccountInfo(wallet1, 0); |
| 25 | + const subaccount2 = new SubaccountInfo(wallet2, 0); |
| 26 | + |
| 27 | + // Change second wallet pubkey |
| 28 | + // Add an authenticator to allow wallet2 to place orders |
| 29 | + console.log("** Adding authenticator **"); |
| 30 | + await addAuthenticator(client, subaccount1, wallet2.pubKey!.value); |
| 31 | + |
| 32 | + const authenticators = await client.getAuthenticators(wallet1.address!); |
| 33 | + // Last element in authenticators array is the most recently created |
| 34 | + const lastElement = authenticators.accountAuthenticators.length - 1; |
| 35 | + const authenticatorID = authenticators.accountAuthenticators[lastElement].id; |
| 36 | + |
| 37 | + // Placing order using subaccount2 for subaccount1 succeeds |
| 38 | + console.log("** Placing order with authenticator **"); |
| 39 | + await placeOrder(client, subaccount2, subaccount1, authenticatorID); |
| 40 | + |
| 41 | + // Remove authenticator |
| 42 | + console.log("** Removing authenticator **"); |
| 43 | + await removeAuthenticator(client, subaccount1, authenticatorID); |
| 44 | + |
| 45 | + // Placing an order using subaccount2 will now fail |
| 46 | + console.log("** Placing order with invalid authenticator should fail **"); |
| 47 | + await placeOrder(client, subaccount2, subaccount1, authenticatorID); |
| 48 | +} |
| 49 | + |
| 50 | +async function removeAuthenticator(client: CompositeClient,subaccount: SubaccountInfo, id: Long): Promise<void> { |
| 51 | + await client.removeAuthenticator(subaccount, id); |
| 52 | +} |
| 53 | + |
| 54 | +async function addAuthenticator(client: CompositeClient, subaccount: SubaccountInfo, authedPubKey:string): Promise<void> { |
| 55 | + const subAuthenticators = [{ |
| 56 | + type: AuthenticatorType.SIGNATURE_VERIFICATION, |
| 57 | + config: authedPubKey, |
| 58 | + }, |
| 59 | + { |
| 60 | + type: AuthenticatorType.MESSAGE_FILTER, |
| 61 | + config: toBase64(new TextEncoder().encode("/dydxprotocol.clob.MsgPlaceOrder")), |
| 62 | + }, |
| 63 | +]; |
| 64 | + |
| 65 | + const jsonString = JSON.stringify(subAuthenticators); |
| 66 | + const encodedData = new TextEncoder().encode(jsonString); |
| 67 | + |
| 68 | + await client.addAuthenticator(subaccount, AuthenticatorType.ALL_OF, encodedData); |
| 69 | +} |
| 70 | + |
| 71 | +async function placeOrder(client: CompositeClient, fromAccount: SubaccountInfo, forAccount: SubaccountInfo, authenticatorId: Long): Promise<void> { |
| 72 | + try { |
| 73 | + const side = OrderSide.BUY |
| 74 | + const price = Number("1000"); |
| 75 | + const currentBlock = await client.validatorClient.get.latestBlockHeight(); |
| 76 | + const nextValidBlockHeight = currentBlock + 5; |
| 77 | + const goodTilBlock = nextValidBlockHeight + 10; |
| 78 | + |
| 79 | + const timeInForce = Order_TimeInForce.TIME_IN_FORCE_UNSPECIFIED; |
| 80 | + |
| 81 | + const clientId = Math.floor(Math.random() * 10000); |
| 82 | + |
| 83 | + const tx = await client.placeShortTermOrder( |
| 84 | + fromAccount, |
| 85 | + 'ETH-USD', |
| 86 | + side, |
| 87 | + price, |
| 88 | + 0.01, |
| 89 | + clientId, |
| 90 | + goodTilBlock, |
| 91 | + timeInForce, |
| 92 | + false, |
| 93 | + undefined, |
| 94 | + { |
| 95 | + authenticators: [authenticatorId], |
| 96 | + accountForOrder: forAccount, |
| 97 | + } |
| 98 | + ); |
| 99 | + console.log('**Order Tx**'); |
| 100 | + console.log(Buffer.from(tx.hash).toString('hex')); |
| 101 | + } catch (error) { |
| 102 | + console.log(error.message); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +test() |
| 107 | + .then(() => {}) |
| 108 | + .catch((error) => { |
| 109 | + console.log(error.message); |
| 110 | + }); |
0 commit comments