From e01e636d22ac8ff28ede24ccb9fed811f49c5611 Mon Sep 17 00:00:00 2001 From: sven Date: Tue, 4 Feb 2025 03:04:10 +0800 Subject: [PATCH 1/4] preview --- .../src/components/DropdownMenu.tsx | 8 +- sdk/typescript/rooch-sdk/README.md | 2 +- sdk/typescript/rooch-sdk/scripts/gen.ts | 8 + sdk/typescript/rooch-sdk/src/address/util.ts | 13 +- sdk/typescript/rooch-sdk/src/client/client.ts | 137 +++++++++++++++--- .../rooch-sdk/src/client/types/generated.ts | 1 + .../rooch-sdk/src/client/types/params.ts | 2 +- .../rooch-sdk/src/transactions/transaction.ts | 11 +- .../src/transactions/transactionData.ts | 14 +- .../rooch-sdk/test-e2e/case/events.test.ts | 2 +- .../rooch-sdk/test-e2e/case/grow.test.ts | 11 +- 11 files changed, 171 insertions(+), 38 deletions(-) diff --git a/sdk/typescript/rooch-sdk-kit/src/components/DropdownMenu.tsx b/sdk/typescript/rooch-sdk-kit/src/components/DropdownMenu.tsx index a30cf52d30..886435e20d 100644 --- a/sdk/typescript/rooch-sdk-kit/src/components/DropdownMenu.tsx +++ b/sdk/typescript/rooch-sdk-kit/src/components/DropdownMenu.tsx @@ -151,7 +151,13 @@ export function ActionDropdownMenu() { { - window.localStorage.clear() + const prefix = 'rooch-sdk-kit' + + Object.keys(localStorage).forEach((key) => { + if (key.startsWith(prefix)) { + localStorage.removeItem(key) + } + }) window.location.reload() }} > diff --git a/sdk/typescript/rooch-sdk/README.md b/sdk/typescript/rooch-sdk/README.md index 0392f7d585..091a53fc5a 100644 --- a/sdk/typescript/rooch-sdk/README.md +++ b/sdk/typescript/rooch-sdk/README.md @@ -84,7 +84,7 @@ const client = new RoochClient({ const tx = new Transaction() tx.callFunction({ target: '0x3::empty::empty_with_signer', - maxGas: 100000000 // 1RGas, DEFAULT_GAS 50000000 = 0.5RGas + maxGas: 100000000, // 1RGas, DEFAULT_GAS 50000000 = 0.5RGas }) const result = await client.signAndExecuteTransaction({ diff --git a/sdk/typescript/rooch-sdk/scripts/gen.ts b/sdk/typescript/rooch-sdk/scripts/gen.ts index 79c8da047c..3c7163b820 100644 --- a/sdk/typescript/rooch-sdk/scripts/gen.ts +++ b/sdk/typescript/rooch-sdk/scripts/gen.ts @@ -75,6 +75,14 @@ const options: { // }, // flattenParams: ['function_call'] // }, + rooch_getObjectStates: { + params: { + object_ids: { + alias: 'ids', + typeAlias: 'string[]', + }, + }, + }, rooch_getBalance: { params: { account_addr: { diff --git a/sdk/typescript/rooch-sdk/src/address/util.ts b/sdk/typescript/rooch-sdk/src/address/util.ts index d47ca6d3d4..ef36bf737f 100644 --- a/sdk/typescript/rooch-sdk/src/address/util.ts +++ b/sdk/typescript/rooch-sdk/src/address/util.ts @@ -40,13 +40,16 @@ export function decodeToRoochAddressStr(input: address): string { } export function decodeToPackageAddressStr(input: address): string { - const packageAddressStr = decodeToRoochAddressStr(input); - if (packageAddressStr.length === ROOCH_ADDRESS_LENGTH * 2){ - return packageAddressStr; + const packageAddressStr = decodeToRoochAddressStr(input) + if (packageAddressStr.length === ROOCH_ADDRESS_LENGTH * 2) { + return packageAddressStr } - if (packageAddressStr.length === ROOCH_ADDRESS_LENGTH * 2 + 2 && packageAddressStr.startsWith('0x')){ - return packageAddressStr.slice(2); + if ( + packageAddressStr.length === ROOCH_ADDRESS_LENGTH * 2 + 2 && + packageAddressStr.startsWith('0x') + ) { + return packageAddressStr.slice(2) } throw Error('Invalid Address') diff --git a/sdk/typescript/rooch-sdk/src/client/client.ts b/sdk/typescript/rooch-sdk/src/client/client.ts index 6f80e70fab..7339863695 100644 --- a/sdk/typescript/rooch-sdk/src/client/client.ts +++ b/sdk/typescript/rooch-sdk/src/client/client.ts @@ -51,9 +51,22 @@ import { ModuleABIView, GetModuleABIParams, BroadcastTXParams, + GetObjectStatesParams, + GetFieldStatesParams, + ListFieldStatesParams, + GetTransactionsByHashParams, + TransactionWithInfoView, + GetTransactionsByOrderParams, + RepairIndexerParams, + SyncStatesParams, + PaginatedStateChangeSetWithTxOrderViews, + DryRunRawTransactionParams, + DryRunTransactionResponseView, } from './types/index.js' import { fixedBalance } from '../utils/balance.js' +const DEFAULT_GAS = 10000000 + /** * Configuration options for the RoochClient * You must provide either a `url` or a `transport` @@ -132,6 +145,13 @@ export class RoochClient { }) } + async dryrun(input: DryRunRawTransactionParams): Promise { + return await this.transport.request({ + method: 'rooch_dryRunRawTransaction', + params: [input.txBcsHex], + }) + } + async signAndExecuteTransaction({ transaction, signer, @@ -153,6 +173,20 @@ export class RoochClient { transaction.setSeqNumber(await this.getSequenceNumber(sender)) transaction.setSender(sender) + // need dry_run + if (!transaction.getMaxGas()) { + transaction.setMaxGas(DEFAULT_GAS) + // const s = transaction.encodeData().toHex() + // const result = await this.dryrun({ txBcsHex: s }) + // + // if (result.raw_output.status.type === 'executed') { + // transaction.setMaxGas(Math.ceil(Number(result.raw_output.gas_used) * 100)) + // } else { + // // TODO: abort? + // throw Error(result.raw_output.status.type) + // } + } + const auth = await signer.signTransaction(transaction) transaction.setAuth(auth) @@ -166,6 +200,24 @@ export class RoochClient { }) } + async repairIndexer(input: RepairIndexerParams) { + await this.transport.request({ + method: 'rooch_repairIndexer', + params: [input.repairType, input.repairParams], + }) + } + + async syncStates(input: SyncStatesParams): Promise { + const opt = input.queryOption || { + decode: true, + showDisplay: true, + } + return await this.transport.request({ + method: 'rooch_repairIndexer', + params: [input.filter, input.cursor, input.limit, opt], + }) + } + // Get the states by access_path async getStates(input: GetStatesParams): Promise { const opt = input.stateOption || { @@ -198,6 +250,7 @@ export class RoochClient { params: [input.moduleAddr, input.moduleName], }) } + async getEvents(input: GetEventsByEventHandleParams): Promise { const opt = input.eventOptions || { decode: true, @@ -207,7 +260,7 @@ export class RoochClient { params: [input.eventHandleType, input.cursor, input.limit, input.descendingOrder, opt], }) } - // curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"rooch_getEventsByEventHandle","params":["0x488e11bd0086861e110586909fd72c8142506f6fc636982051271a694bf5b0ed::event_test::WithdrawEvent", null, "1", null, {"decode":true}],"id":1}' http://127.0.0.1:6767 | jq + async queryEvents(input: QueryEventsParams): Promise { if ('sender' in input.filter) { if (input.filter.sender === '') { @@ -231,7 +284,6 @@ export class RoochClient { }) } - // Query the Inscription via global index by Inscription filter async queryInscriptions(input: QueryInscriptionsParams): Promise { if (typeof input.filter !== 'string' && 'owner' in input.filter) { if (input.filter.owner === '') { @@ -263,6 +315,42 @@ export class RoochClient { }) } + async getObjectStates(input: GetObjectStatesParams): Promise { + const idsStr = input.ids.join(',') + const opt = input.stateOption || { + decode: true, + showDisplay: true, + } + return this.transport.request({ + method: 'rooch_getObjectStates', + params: [idsStr, opt], + }) + } + + async getFieldStates(input: GetFieldStatesParams): Promise { + const opt = input.stateOption || { + decode: true, + showDisplay: true, + } + + return this.transport.request({ + method: 'rooch_getFieldStates', + params: [input.objectId, input.fieldKey, opt], + }) + } + + async listFieldStates(input: ListFieldStatesParams): Promise { + const opt = input.stateOption || { + decode: true, + showDisplay: true, + } + + return this.transport.request({ + method: 'rooch_getFieldStates', + params: [input.objectId, input.cursor, input.limit, opt], + }) + } + async queryObjectStates( input: QueryObjectStatesParams, ): Promise { @@ -288,6 +376,24 @@ export class RoochClient { }) } + async getTransactionsByHash( + input: GetTransactionsByHashParams, + ): Promise { + return this.transport.request({ + method: 'rooch_getTransactionsByHash', + params: [input.txHashes], + }) + } + + async getTransactionsByOrder( + input: GetTransactionsByOrderParams, + ): Promise { + return this.transport.request({ + method: 'rooch_queryTransactions', + params: [input.cursor, input.limit, input.descendingOrder], + }) + } + async queryTransactions( input: QueryTransactionsParams, ): Promise { @@ -463,41 +569,36 @@ export class RoochClient { package_address, limit, cursor, - }:{ + }: { package_address: address } & PaginationArguments): Promise> { - const packageObjectID = `0x14481947570f6c2f50d190f9a13bf549ab2f0c9debc41296cd4d506002379659${decodeToPackageAddressStr(package_address)}`; + const packageObjectID = `0x14481947570f6c2f50d190f9a13bf549ab2f0c9debc41296cd4d506002379659${decodeToPackageAddressStr(package_address)}` const result = await this.transport.request({ method: 'rooch_listFieldStates', - params: [ - packageObjectID, - cursor, - limit, - { decode: true }, - ], - }); + params: [packageObjectID, cursor, limit, { decode: true }], + }) const moduleInfo = result as unknown as ObjectStateView[] - const moduleMap = new Map(); + const moduleMap = new Map() if (moduleInfo && typeof moduleInfo === 'object' && 'data' in moduleInfo) { - const { data } = moduleInfo; + const { data } = moduleInfo if (Array.isArray(data)) { for (const item of data) { - const decodedValue = item?.state?.decoded_value; + const decodedValue = item?.state?.decoded_value if (decodedValue) { - const name = decodedValue?.value?.name; - const byte_codes = decodedValue?.value?.value?.value?.byte_codes; + const name = decodedValue?.value?.name + const byte_codes = decodedValue?.value?.value?.value?.byte_codes if (name && byte_codes) { - moduleMap.set(name, byte_codes); + moduleMap.set(name, byte_codes) } } } } } - return moduleMap; + return moduleMap } async getSessionKeys({ diff --git a/sdk/typescript/rooch-sdk/src/client/types/generated.ts b/sdk/typescript/rooch-sdk/src/client/types/generated.ts index 11e82fe01b..90b3b30518 100644 --- a/sdk/typescript/rooch-sdk/src/client/types/generated.ts +++ b/sdk/typescript/rooch-sdk/src/client/types/generated.ts @@ -73,6 +73,7 @@ export interface BlockHeightHashView { block_height: string } export interface DAInfoView { + avail_backends: [string, string][] last_avail_block_number?: string | null last_avail_block_update_time?: string | null last_avail_tx_order?: string | null diff --git a/sdk/typescript/rooch-sdk/src/client/types/params.ts b/sdk/typescript/rooch-sdk/src/client/types/params.ts index ddf4702c95..fb4c1def81 100644 --- a/sdk/typescript/rooch-sdk/src/client/types/params.ts +++ b/sdk/typescript/rooch-sdk/src/client/types/params.ts @@ -80,7 +80,7 @@ export interface GetModuleABIParams { } /** Get object states by object id */ export interface GetObjectStatesParams { - objectIds: string + ids: string[] stateOption?: RpcTypes.StateOptions | null | undefined } /** diff --git a/sdk/typescript/rooch-sdk/src/transactions/transaction.ts b/sdk/typescript/rooch-sdk/src/transactions/transaction.ts index 8ccfba3225..c60c77251a 100644 --- a/sdk/typescript/rooch-sdk/src/transactions/transaction.ts +++ b/sdk/typescript/rooch-sdk/src/transactions/transaction.ts @@ -29,9 +29,14 @@ export class Transaction { return this.info } + getMaxGas() { + return this.getData().maxGas + } + setMaxGas(input: number) { this.getData().maxGas = BigInt(input) } + setSender(input: address) { this.getData().sender = input } @@ -52,9 +57,13 @@ export class Transaction { return this.getData().hash() } + encodeData() { + return this.data!.encode() + } + encode() { return bcs.RoochTransaction.serialize({ - data: this.data!.encode(), + data: this.data!.encode().toBytes(), auth: this.auth!.encode(), }) } diff --git a/sdk/typescript/rooch-sdk/src/transactions/transactionData.ts b/sdk/typescript/rooch-sdk/src/transactions/transactionData.ts index cc5119e42a..230c79222b 100644 --- a/sdk/typescript/rooch-sdk/src/transactions/transactionData.ts +++ b/sdk/typescript/rooch-sdk/src/transactions/transactionData.ts @@ -7,8 +7,6 @@ import { Args, bcs, Serializer } from '../bcs/index.js' import { address, Bytes, identifier, u8, u64 } from '../types/index.js' import { CallFunctionArgs, CallScript } from './types.js' -const DEFAULT_GAS = BigInt(50000000) - export class CallFunction { address: string module: identifier @@ -69,7 +67,7 @@ export class TransactionData { sender?: address sequenceNumber?: u64 chainId?: u64 - maxGas: u64 + maxGas?: u64 action: MoveAction constructor( @@ -83,17 +81,17 @@ export class TransactionData { this.sequenceNumber = sequenceNumber this.chainId = chainId this.action = action - this.maxGas = maxGas || DEFAULT_GAS + this.maxGas = maxGas } - encode(): Bytes { + encode() { const call = this.action.val as CallFunction return bcs.RoochTransactionData.serialize({ sender: this.sender!, sequenceNumber: this.sequenceNumber!, chainId: this.chainId!, - maxGas: this.maxGas, + maxGas: this.maxGas!, action: { kind: 'CallFunction', functionId: { @@ -106,10 +104,10 @@ export class TransactionData { args: Array.from(call.encodeArgsToByteArrays()), typeArgs: call.typeArgs, }, - }).toBytes() + }) } hash(): Bytes { - return sha3_256(this.encode()) + return sha3_256(this.encode().toBytes()) } } diff --git a/sdk/typescript/rooch-sdk/test-e2e/case/events.test.ts b/sdk/typescript/rooch-sdk/test-e2e/case/events.test.ts index f69f2b9754..07e6297c27 100644 --- a/sdk/typescript/rooch-sdk/test-e2e/case/events.test.ts +++ b/sdk/typescript/rooch-sdk/test-e2e/case/events.test.ts @@ -33,7 +33,7 @@ describe('Events API', () => { expect(await testBox.signAndExecuteTransaction(tx)).toBeTruthy() const result1 = await testBox.getClient().getEvents({ - eventHandleType: `${await testBox.defaultCmdAddress()}::event_test::WithdrawEvent` + eventHandleType: `${await testBox.defaultCmdAddress()}::event_test::WithdrawEvent`, }) expect(result1.next_cursor).eq('0') diff --git a/sdk/typescript/rooch-sdk/test-e2e/case/grow.test.ts b/sdk/typescript/rooch-sdk/test-e2e/case/grow.test.ts index 800b534bac..2495a1aa65 100644 --- a/sdk/typescript/rooch-sdk/test-e2e/case/grow.test.ts +++ b/sdk/typescript/rooch-sdk/test-e2e/case/grow.test.ts @@ -4,7 +4,14 @@ import { beforeAll, describe, it, afterAll } from 'vitest' import { TestBox } from '../setup.js' import { Args, bcs } from '../../src/bcs/index.js' -import { BitcoinAddress, BitcoinNetowkType, fromHEX, getRoochNodeUrl, RoochAddress, StateKVView } from "../../src"; +import { + BitcoinAddress, + BitcoinNetowkType, + fromHEX, + getRoochNodeUrl, + RoochAddress, + StateKVView, +} from '../../src' describe('Checkpoints Transfer API', () => { let testBox: TestBox @@ -119,7 +126,7 @@ describe('Checkpoints Transfer API', () => { const result = await testBox.getClient().listStates({ accessPath: '/table/0xa1e5d1779e9764839a0526e35d5972d28584d3dcfac1d2305e231b6490a59740', limit: '200', - cursor: nextCursor + cursor: nextCursor, }) nextCursor = result.next_cursor votes = votes.concat(result.data) From 87d716a082a2050a847113d287f7a9d9e7110eb8 Mon Sep 17 00:00:00 2001 From: sven Date: Wed, 5 Feb 2025 20:18:39 +0800 Subject: [PATCH 2/4] fix root lint --- .eslintrc.js | 6 +++++- .prettierignore | 6 +++++- sdk/typescript/rooch-sdk-kit/src/components/WalletGuard.tsx | 4 ++-- sdk/typescript/test-suite/src/container/bitcoin.ts | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index d74f75c6e4..d867c88752 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -25,10 +25,14 @@ module.exports = { 'out', 'generated', 'templates', - 'grow-rooch-v1', + 'infra', 'dist', + 'third_party', 'coverage', 'next-env.d.ts', + 'examples', + // # TODO: fix + 'bitseed-sdk', ], rules: { 'no-case-declarations': 'off', diff --git a/.prettierignore b/.prettierignore index ea515489bd..8a78afd572 100644 --- a/.prettierignore +++ b/.prettierignore @@ -8,7 +8,7 @@ !/*.js # ignore generated files -grow-rooch-v1 +infra .vscode .next node_modules @@ -21,3 +21,7 @@ dist generated generator templates +third_party +# TODO: fix +bitseed-sdk +examples diff --git a/sdk/typescript/rooch-sdk-kit/src/components/WalletGuard.tsx b/sdk/typescript/rooch-sdk-kit/src/components/WalletGuard.tsx index a8c71d33c8..f59b0ba5cb 100644 --- a/sdk/typescript/rooch-sdk-kit/src/components/WalletGuard.tsx +++ b/sdk/typescript/rooch-sdk-kit/src/components/WalletGuard.tsx @@ -12,10 +12,10 @@ type ConnectButtonProps = { } & ButtonHTMLAttributes export function WalletGuard({ children, onClick }: ConnectButtonProps) { - const { wallet } = useCurrentWallet() + const { status } = useCurrentWallet() return ( <> - {wallet ? ( + {status === 'connected' ? (