Skip to content

Commit

Permalink
fix sdk e2e test (rooch-network#1523)
Browse files Browse the repository at this point in the history
  • Loading branch information
wow-sven authored Apr 6, 2024
1 parent 0c908f8 commit 8852953
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 311 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
'out',
'generated',
'templates',
'grow-rooch-v1',
'dist',
'coverage',
'next-env.d.ts',
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
!/dashboard
!/sdk
!/rooch-portal-v1
!/grow-rooch-v1
!/.config
!/*.js

# ignore generated files
grow-rooch-v1
.vscode
.next
node_modules
Expand Down
2 changes: 1 addition & 1 deletion grow-rooch-v1/src/pages/mint/mint-page.tsx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const MintPage = () => {}
// export const MintPage = () => {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

import {createContext, useRef, useEffect, useState} from 'react'
import { createContext, useRef, useEffect, useState } from 'react'
import type { ReactNode } from 'react'

import {
Expand Down
3 changes: 1 addition & 2 deletions sdk/typescript/rooch-sdk/src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import { IAuthorizer } from '../auth'

export interface IAccount {
getAuthorizer(): IAuthorizer
getAddress(): string | undefined
getRoochAddress(): Promise<string>
getAddress(): string
}
31 changes: 14 additions & 17 deletions sdk/typescript/rooch-sdk/src/account/roochAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,24 @@ import { IAccount } from '../account/interface'
export class RoochAccount implements IAccount {
private readonly keypair: Ed25519Keypair
private readonly client: RoochClient
private roochAddress?: string
private address?: string
private authorizer?: IAuthorizer

public constructor(client: RoochClient) {
public constructor(client: RoochClient, keyPair?: Ed25519Keypair) {
this.client = client
this.keypair = new Ed25519Keypair()
this.keypair = keyPair ?? Ed25519Keypair.generate()
}

public getKeypar(): Ed25519Keypair {
return this.keypair
}

public getAddress(): string | undefined {
return undefined
}

public getRoochAddress(): Promise<string> {
if (!this.roochAddress) {
this.roochAddress = this.keypair.getPublicKey().toRoochAddress()
public getAddress(): string {
if (!this.address) {
this.address = this.keypair.getPublicKey().toRoochAddress()
}
return Promise.resolve(this.roochAddress)

return this.address
}

public getAuthorizer(): IAuthorizer {
Expand All @@ -47,12 +44,12 @@ export class RoochAccount implements IAccount {

async sendTransaction(
funcId: FunctionId,
args: Arg[],
tyArgs: TypeTag[],
opts: SendRawTransactionOpts,
args?: Arg[],
tyArgs?: TypeTag[],
opts?: SendRawTransactionOpts,
): Promise<string> {
return this.client.sendRawTransaction({
address: await this.getRoochAddress(),
address: this.getAddress(),
authorizer: this.getAuthorizer(),
funcId,
args,
Expand All @@ -63,14 +60,14 @@ export class RoochAccount implements IAccount {

async getBalance(coinType: string): Promise<BalanceInfoView> {
return this.client.getBalance({
address: await this.getRoochAddress(),
address: this.getAddress(),
coinType,
})
}

async getBalances(cursor: string, limit: string): Promise<BalanceInfoPageView> {
return this.client.getBalances({
address: await this.getRoochAddress(),
address: this.getAddress(),
cursor,
limit,
})
Expand Down
106 changes: 56 additions & 50 deletions sdk/typescript/rooch-sdk/src/account/roochSessionAccount.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

import { FunctionId, TypeTag, Arg } from '../types'
import { FunctionId, TypeTag, Arg, IPage } from '../types'
import { addressToListTuple, addressToSeqNumber, encodeArg, encodeFunctionCall } from '../utils'

import { RoochAccount } from './roochAccount'
import { RoochClient } from '../client/roochClient'
import { SendRawTransactionOpts } from '../client/roochClientTypes'
import { SendRawTransactionOpts, SessionInfo } from '../client/roochClientTypes'
import { IAccount } from '../account/interface.ts'
import { IAuthorizer } from '../auth'
import {
RoochTransactionData,
AccountAddress as BCSAccountAddress,
RoochTransaction,
} from '../generated/runtime/rooch_types/mod'
import { DEFAULT_MAX_GAS_AMOUNT } from '../constants'
import { BcsSerializer } from '../generated/runtime/bcs/bcsSerializer'

const SCOPE_LENGTH = 3
const SCOPE_MODULE_ADDRESSS = 0
Expand Down Expand Up @@ -83,7 +81,7 @@ export class RoochSessionAccount implements IAccount {
const args: Arg[] = [
{
type: { Vector: 'U8' },
value: addressToSeqNumber(await this.getAuthKey()),
value: addressToSeqNumber(this.getAuthKey()),
},
{
type: { Vector: 'Address' },
Expand All @@ -102,16 +100,16 @@ export class RoochSessionAccount implements IAccount {
value: BigInt(this.maxInactiveInterval),
},
]
const number = await this.client.getSequenceNumber(await this.getRoochAddress())
const sequenceNumber = await this.client.getSequenceNumber(this.getAddress())
const bcsArgs = args.map((arg) => encodeArg(arg))
const scriptFunction = encodeFunctionCall(
'0x3::session_key::create_session_key_with_multi_scope_entry',
[],
bcsArgs,
)
const txData = new RoochTransactionData(
new BCSAccountAddress(addressToListTuple(await this.getRoochAddress())),
BigInt(number),
new BCSAccountAddress(addressToListTuple(this.getAddress())),
BigInt(sequenceNumber),
BigInt(this.client.getChainId()),
BigInt(opts?.maxGasAmount ?? DEFAULT_MAX_GAS_AMOUNT),
scriptFunction,
Expand All @@ -121,36 +119,21 @@ export class RoochSessionAccount implements IAccount {
}

protected async register(txData: RoochTransactionData): Promise<RoochSessionAccount> {
const transactionDataPayload = (() => {
const se = new BcsSerializer()
txData.serialize(se)
return se.getBytes()
})()

const authResult = await this.account.getAuthorizer().auth(transactionDataPayload)
const transaction = new RoochTransaction(txData, authResult)

const transactionPayload = (() => {
const se = new BcsSerializer()
transaction.serialize(se)
return se.getBytes()
})()

await this.client.sendRawTransaction(transactionPayload)
const s = await this.client.sendRawTransaction({
authorizer: this.account.getAuthorizer(),
data: txData,
})
console.log(s)

return this
}

public getAuthKey(): Promise<string> {
return this.sessionAccount.getRoochAddress()
}

getAddress(): string | undefined {
return undefined
public getAuthKey(): string {
return this.sessionAccount.getAddress()
}

getRoochAddress(): Promise<string> {
return this.account.getRoochAddress()
getAddress(): string {
return this.account.getAddress()
}

getAuthorizer(): IAuthorizer {
Expand All @@ -172,8 +155,8 @@ export class RoochSessionAccount implements IAccount {
opts?: SendRawTransactionOpts,
): Promise<string> {
return this.client.sendRawTransaction({
address: await this.account.getRoochAddress(),
authorizer: this.sessionAccount.getAuthorizer(),
address: this.getAddress(),
authorizer: this.getAuthorizer(),
funcId,
args,
tyArgs,
Expand All @@ -182,29 +165,52 @@ export class RoochSessionAccount implements IAccount {
}

public async isExpired(): Promise<boolean> {
if (this.localCreateSessionTime + this.maxInactiveInterval > Date.now() / 1000) {
return Promise.resolve(true)
}
// if (this.localCreateSessionTime + this.maxInactiveInterval > Date.now() / 1000) {
// return Promise.resolve(true)
// }

return this.client.sessionIsExpired(
await this.account.getRoochAddress(),
await this.getAuthKey(),
)
return this.client.sessionIsExpired(this.account.getAddress(), this.getAuthKey())
}

public async getSessionKey() {
const session = this.client.executeViewFunction({
funcId: '0x3::session_key::get_session_key',
tyArgs: [],
args: [
{
type: 'Address',
value: this.getAddress(),
},
{
type: { Vector: 'U8' },
value: addressToSeqNumber(this.getAuthKey()),
},
],
})

return session
}

public async querySessionKeys(
cursor: string | null,
limit: number,
): Promise<IPage<SessionInfo, string>> {
return this.client.querySessionKeys(this.getAddress(), cursor, limit)
}

public async destroy(opts?: SendRawTransactionOpts): Promise<string> {
return await this.sendTransaction(
'0x3::session_key::remove_session_key_entry',
[
return await this.client.sendRawTransaction({
funcId: '0x3::session_key::remove_session_key_entry',
args: [
{
type: { Vector: 'U8' },
value: addressToSeqNumber(await this.getAuthKey()),
value: addressToSeqNumber(this.getAuthKey()),
},
],
[],
opts || {
maxGasAmount: 100000000,
},
)
tyArgs: [],
address: this.getAddress(),
authorizer: this.account.getAuthorizer(),
opts: opts,
})
}
}
9 changes: 4 additions & 5 deletions sdk/typescript/rooch-sdk/src/client/roochClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class RoochClient {
if (
typeof params === 'object' &&
params !== null &&
'authenticator' in params &&
'authorizer' in params &&
'data' in params
) {
const { data, authorizer } = params as SendTransactionDataParams
Expand Down Expand Up @@ -347,7 +347,6 @@ export class RoochClient {
return 0
}

// @ts-ignore
/**
* Query account's sessionKey
*
Expand All @@ -361,12 +360,12 @@ export class RoochClient {
limit: number,
): Promise<IPage<SessionInfo, string>> {
const accessPath = `/resource/${address}/0x3::session_key::SessionKeys`
const state = await this.getStates(accessPath)
const states = await this.getStates(accessPath)

if (!state) {
if (!states || (Array.isArray(states) && states.length === 0)) {
throw new Error('not found state')
}
const stateView = state as any
const stateView = states as any

const tableId = stateView[0].decoded_value.value.keys.value.handle.value.id

Expand Down
2 changes: 1 addition & 1 deletion sdk/typescript/rooch-sdk/src/constants/gas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

export const DEFAULT_MAX_GAS_AMOUNT = 1000000
export const DEFAULT_MAX_GAS_AMOUNT = 100000000
Loading

0 comments on commit 8852953

Please sign in to comment.