Testnet v3.
This commit is contained in:
		@@ -8,6 +8,7 @@ import { ref } from 'valtio'
 | 
			
		||||
import estimateFee from '../../utils/estimateFee'
 | 
			
		||||
import { SetHookData } from '../../utils/setHook'
 | 
			
		||||
import ResultLink from '../../components/ResultLink'
 | 
			
		||||
import { xrplSend } from './xrpl-client'
 | 
			
		||||
 | 
			
		||||
export const sha256 = async (string: string) => {
 | 
			
		||||
  const utf8 = new TextEncoder().encode(string)
 | 
			
		||||
@@ -64,9 +65,6 @@ export const prepareDeployHookTx = async (
 | 
			
		||||
  if (!activeFile?.compiledContent) {
 | 
			
		||||
    return
 | 
			
		||||
  }
 | 
			
		||||
  if (!state.client) {
 | 
			
		||||
    return
 | 
			
		||||
  }
 | 
			
		||||
  const HookNamespace = (await sha256(data.HookNamespace)).toUpperCase()
 | 
			
		||||
  const hookOnValues: (keyof TTS)[] = data.Invoke.map(tt => tt.value)
 | 
			
		||||
  const { HookParameters } = data
 | 
			
		||||
@@ -87,30 +85,30 @@ export const prepareDeployHookTx = async (
 | 
			
		||||
  //     }
 | 
			
		||||
  //   }
 | 
			
		||||
  // });
 | 
			
		||||
  if (typeof window !== 'undefined') {
 | 
			
		||||
    const tx = {
 | 
			
		||||
      Account: account.address,
 | 
			
		||||
      TransactionType: 'SetHook',
 | 
			
		||||
      Sequence: account.sequence,
 | 
			
		||||
      Fee: data.Fee,
 | 
			
		||||
      Hooks: [
 | 
			
		||||
        {
 | 
			
		||||
          Hook: {
 | 
			
		||||
            CreateCode: arrayBufferToHex(activeFile?.compiledContent).toUpperCase(),
 | 
			
		||||
            HookOn: calculateHookOn(hookOnValues),
 | 
			
		||||
            HookNamespace,
 | 
			
		||||
            HookApiVersion: 0,
 | 
			
		||||
            Flags: 1,
 | 
			
		||||
            // ...(filteredHookGrants.length > 0 && { HookGrants: filteredHookGrants }),
 | 
			
		||||
            ...(filteredHookParameters.length > 0 && {
 | 
			
		||||
              HookParameters: filteredHookParameters
 | 
			
		||||
            })
 | 
			
		||||
          }
 | 
			
		||||
  if (typeof window === 'undefined') return;
 | 
			
		||||
  const tx = {
 | 
			
		||||
    Account: account.address,
 | 
			
		||||
    TransactionType: 'SetHook',
 | 
			
		||||
    Sequence: account.sequence,
 | 
			
		||||
    Fee: data.Fee,
 | 
			
		||||
    NetworkID: process.env.NEXT_PUBLIC_NETWORK_ID || state.client.getState().server.networkId,
 | 
			
		||||
    Hooks: [
 | 
			
		||||
      {
 | 
			
		||||
        Hook: {
 | 
			
		||||
          CreateCode: arrayBufferToHex(activeFile?.compiledContent).toUpperCase(),
 | 
			
		||||
          HookOn: calculateHookOn(hookOnValues),
 | 
			
		||||
          HookNamespace,
 | 
			
		||||
          HookApiVersion: 0,
 | 
			
		||||
          Flags: 1,
 | 
			
		||||
          // ...(filteredHookGrants.length > 0 && { HookGrants: filteredHookGrants }),
 | 
			
		||||
          ...(filteredHookParameters.length > 0 && {
 | 
			
		||||
            HookParameters: filteredHookParameters
 | 
			
		||||
          })
 | 
			
		||||
        }
 | 
			
		||||
      ]
 | 
			
		||||
    }
 | 
			
		||||
    return tx
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
  return tx
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* deployHook function turns the wasm binary into
 | 
			
		||||
@@ -127,9 +125,6 @@ export const deployHook = async (account: IAccount & { name?: string }, data: Se
 | 
			
		||||
    if (!tx) {
 | 
			
		||||
      return
 | 
			
		||||
    }
 | 
			
		||||
    if (!state.client) {
 | 
			
		||||
      return
 | 
			
		||||
    }
 | 
			
		||||
    const keypair = derive.familySeed(account.secret)
 | 
			
		||||
 | 
			
		||||
    const { signedTransaction } = sign(tx, keypair)
 | 
			
		||||
@@ -140,7 +135,7 @@ export const deployHook = async (account: IAccount & { name?: string }, data: Se
 | 
			
		||||
    let submitRes
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      submitRes = await state.client?.send({
 | 
			
		||||
      submitRes = await xrplSend({
 | 
			
		||||
        command: 'submit',
 | 
			
		||||
        tx_blob: signedTransaction
 | 
			
		||||
      })
 | 
			
		||||
@@ -199,9 +194,6 @@ export const deployHook = async (account: IAccount & { name?: string }, data: Se
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const deleteHook = async (account: IAccount & { name?: string }) => {
 | 
			
		||||
  if (!state.client) {
 | 
			
		||||
    return
 | 
			
		||||
  }
 | 
			
		||||
  const currentAccount = state.accounts.find(acc => acc.address === account.address)
 | 
			
		||||
  if (currentAccount?.isLoading || !currentAccount?.hooks.length) {
 | 
			
		||||
    return
 | 
			
		||||
@@ -239,7 +231,7 @@ export const deleteHook = async (account: IAccount & { name?: string }) => {
 | 
			
		||||
    let submitRes
 | 
			
		||||
    const toastId = toast.loading('Deleting hook...')
 | 
			
		||||
    try {
 | 
			
		||||
      submitRes = await state.client.send({
 | 
			
		||||
      submitRes = await xrplSend({
 | 
			
		||||
        command: 'submit',
 | 
			
		||||
        tx_blob: signedTransaction
 | 
			
		||||
      })
 | 
			
		||||
 
 | 
			
		||||
@@ -1,66 +0,0 @@
 | 
			
		||||
import { derive, sign } from 'xrpl-accountlib'
 | 
			
		||||
 | 
			
		||||
import state from '..'
 | 
			
		||||
import type { IAccount } from '..'
 | 
			
		||||
 | 
			
		||||
interface TransactionOptions {
 | 
			
		||||
  TransactionType: string
 | 
			
		||||
  Account?: string
 | 
			
		||||
  Fee?: string
 | 
			
		||||
  Destination?: string
 | 
			
		||||
  [index: string]: any
 | 
			
		||||
}
 | 
			
		||||
interface OtherOptions {
 | 
			
		||||
  logPrefix?: string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const sendTransaction = async (
 | 
			
		||||
  account: IAccount,
 | 
			
		||||
  txOptions: TransactionOptions,
 | 
			
		||||
  options?: OtherOptions
 | 
			
		||||
) => {
 | 
			
		||||
  if (!state.client) throw Error('XRPL client not initalized')
 | 
			
		||||
 | 
			
		||||
  const { Fee = '1000', ...opts } = txOptions
 | 
			
		||||
  const tx: TransactionOptions = {
 | 
			
		||||
    Account: account.address,
 | 
			
		||||
    Sequence: account.sequence,
 | 
			
		||||
    Fee, // TODO auto-fillable default
 | 
			
		||||
    ...opts
 | 
			
		||||
  }
 | 
			
		||||
  const { logPrefix = '' } = options || {}
 | 
			
		||||
  try {
 | 
			
		||||
    const signedAccount = derive.familySeed(account.secret)
 | 
			
		||||
    const { signedTransaction } = sign(tx, signedAccount)
 | 
			
		||||
    const response = await state.client.send({
 | 
			
		||||
      command: 'submit',
 | 
			
		||||
      tx_blob: signedTransaction
 | 
			
		||||
    })
 | 
			
		||||
    if (response.engine_result === 'tesSUCCESS') {
 | 
			
		||||
      state.transactionLogs.push({
 | 
			
		||||
        type: 'success',
 | 
			
		||||
        message: `${logPrefix}[${response.engine_result}] ${response.engine_result_message}`
 | 
			
		||||
      })
 | 
			
		||||
    } else {
 | 
			
		||||
      state.transactionLogs.push({
 | 
			
		||||
        type: 'error',
 | 
			
		||||
        message: `${logPrefix}[${response.error || response.engine_result}] ${
 | 
			
		||||
          response.error_exception || response.engine_result_message
 | 
			
		||||
        }`
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
    const currAcc = state.accounts.find(acc => acc.address === account.address)
 | 
			
		||||
    if (currAcc && response.account_sequence_next) {
 | 
			
		||||
      currAcc.sequence = response.account_sequence_next
 | 
			
		||||
    }
 | 
			
		||||
  } catch (err) {
 | 
			
		||||
    console.error(err)
 | 
			
		||||
    state.transactionLogs.push({
 | 
			
		||||
      type: 'error',
 | 
			
		||||
      message:
 | 
			
		||||
        err instanceof Error
 | 
			
		||||
          ? `${logPrefix}Error: ${err.message}`
 | 
			
		||||
          : `${logPrefix}Something went wrong, try again later`
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,7 @@ import state from '..'
 | 
			
		||||
import type { IAccount } from '..'
 | 
			
		||||
import ResultLink from '../../components/ResultLink'
 | 
			
		||||
import { ref } from 'valtio'
 | 
			
		||||
import { xrplSend } from './xrpl-client'
 | 
			
		||||
 | 
			
		||||
interface TransactionOptions {
 | 
			
		||||
  TransactionType: string
 | 
			
		||||
@@ -21,20 +22,19 @@ export const sendTransaction = async (
 | 
			
		||||
  txOptions: TransactionOptions,
 | 
			
		||||
  options?: OtherOptions
 | 
			
		||||
) => {
 | 
			
		||||
  if (!state.client) throw Error('XRPL client not initalized')
 | 
			
		||||
 | 
			
		||||
  const { Fee = '1000', ...opts } = txOptions
 | 
			
		||||
  const tx: TransactionOptions = {
 | 
			
		||||
    Account: account.address,
 | 
			
		||||
    Sequence: account.sequence,
 | 
			
		||||
    Fee, // TODO auto-fillable default
 | 
			
		||||
    Fee,
 | 
			
		||||
    NetworkID: process.env.NEXT_PUBLIC_NETWORK_ID || state.client.getState().server.networkId,
 | 
			
		||||
    ...opts
 | 
			
		||||
  }
 | 
			
		||||
  const { logPrefix = '' } = options || {}
 | 
			
		||||
  try {
 | 
			
		||||
    const signedAccount = derive.familySeed(account.secret)
 | 
			
		||||
    const { signedTransaction } = sign(tx, signedAccount)
 | 
			
		||||
    const response = await state.client.send({
 | 
			
		||||
    const response = await xrplSend({
 | 
			
		||||
      command: 'submit',
 | 
			
		||||
      tx_blob: signedTransaction
 | 
			
		||||
    })
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								state/actions/xrpl-client.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								state/actions/xrpl-client.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
import { XrplClient } from 'xrpl-client';
 | 
			
		||||
import state from '..';
 | 
			
		||||
 | 
			
		||||
export const xrplSend = async(...params: Parameters<XrplClient['send']>) => {
 | 
			
		||||
    const client = await state.client.ready()
 | 
			
		||||
    return client.send(...params);
 | 
			
		||||
}
 | 
			
		||||
@@ -78,7 +78,7 @@ export interface IState {
 | 
			
		||||
  splits: {
 | 
			
		||||
    [id: string]: SplitSize
 | 
			
		||||
  }
 | 
			
		||||
  client: XrplClient | null
 | 
			
		||||
  client: XrplClient
 | 
			
		||||
  clientStatus: 'offline' | 'online'
 | 
			
		||||
  mainModalOpen: boolean
 | 
			
		||||
  mainModalShowed: boolean
 | 
			
		||||
@@ -113,7 +113,7 @@ let initialState: IState = {
 | 
			
		||||
    tabSize: 2
 | 
			
		||||
  },
 | 
			
		||||
  splits: {},
 | 
			
		||||
  client: null,
 | 
			
		||||
  client: undefined!, // set below only.
 | 
			
		||||
  clientStatus: 'offline' as 'offline',
 | 
			
		||||
  mainModalOpen: false,
 | 
			
		||||
  mainModalShowed: false,
 | 
			
		||||
@@ -153,9 +153,9 @@ const state = proxy<IState>({
 | 
			
		||||
})
 | 
			
		||||
// Initialize socket connection
 | 
			
		||||
const client = new XrplClient(`wss://${process.env.NEXT_PUBLIC_TESTNET_URL}`)
 | 
			
		||||
state.client = ref(client);
 | 
			
		||||
 | 
			
		||||
client.on('online', () => {
 | 
			
		||||
  state.client = ref(client)
 | 
			
		||||
  state.clientStatus = 'online'
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user