Skip to content
This repository was archived by the owner on Feb 27, 2025. It is now read-only.

Commit a0b90e0

Browse files
committed
feat(contracts): upgrade from soroban-client to stellar-sdk
1 parent 63a9d4e commit a0b90e0

10 files changed

+110
-108
lines changed

packages/contracts/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
},
3131
"dependencies": {
3232
"@soroban-react/core": "^7.4.0",
33-
"@stellar/stellar-sdk": "^11.1.0",
34-
"soroban-client": "1.0.0-beta.4"
33+
"stellar-sdk": "11.1.0"
3534
},
3635
"devDependencies": {
3736
"@babel/preset-react": "^7.22.5",

packages/contracts/src/contractInvoke.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { SorobanContextType } from '@soroban-react/core'
22

3-
import * as SorobanClient from 'soroban-client'
4-
import { SorobanRpc } from 'soroban-client'
3+
import * as StellarSdk from 'stellar-sdk'
4+
import { SorobanRpc } from 'stellar-sdk'
55

66
import { contractTransaction } from './contractTransaction'
77
import { signAndSendTransaction } from './transaction'
88
import { TxResponse } from './types'
99

10-
let xdr = SorobanClient.xdr
10+
let xdr = StellarSdk.xdr
1111

1212
export type InvokeArgs = {
1313
contractAddress: string
1414
method: string
15-
args?: SorobanClient.xdr.ScVal[] | undefined
15+
args?: StellarSdk.xdr.ScVal[] | undefined
1616
signAndSend?: boolean
1717
fee?: number
1818
skipAddingFootprint?: boolean
@@ -35,7 +35,7 @@ export async function contractInvoke({
3535
secretKey,
3636
sorobanContext,
3737
reconnectAfterTx = true,
38-
}: InvokeArgs): Promise<TxResponse | SorobanClient.xdr.ScVal> {
38+
}: InvokeArgs): Promise<TxResponse | StellarSdk.xdr.ScVal> {
3939
const { server, address, activeChain } = sorobanContext
4040

4141
if (!activeChain) {
@@ -55,15 +55,15 @@ export async function contractInvoke({
5555

5656
if (secretKey) {
5757
source = await server.getAccount(
58-
SorobanClient.Keypair.fromSecret(secretKey).publicKey()
58+
StellarSdk.Keypair.fromSecret(secretKey).publicKey()
5959
)
6060
} else {
6161
try {
6262
if (!address) throw new Error('No address')
6363

6464
source = await server.getAccount(address)
6565
} catch (error) {
66-
source = new SorobanClient.Account(defaultAddress, '0')
66+
source = new StellarSdk.Account(defaultAddress, '0')
6767
}
6868
}
6969

@@ -76,10 +76,10 @@ export async function contractInvoke({
7676
args,
7777
})
7878

79-
const simulated: SorobanRpc.SimulateTransactionResponse =
79+
const simulated: SorobanRpc.Api.SimulateTransactionResponse =
8080
await server?.simulateTransaction(txn)
8181

82-
if (SorobanRpc.isSimulationError(simulated)) {
82+
if (SorobanRpc.Api.isSimulationError(simulated)) {
8383
throw new Error(simulated.error)
8484
} else if (!simulated.result) {
8585
throw new Error(`invalid simulation: no result in ${simulated}`)
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import * as SorobanClient from 'soroban-client'
1+
import * as StellarSdk from 'stellar-sdk'
2+
23

34
export interface contractTransactionProps {
45
networkPassphrase: string
5-
source: SorobanClient.Account
6+
source: StellarSdk.Account
67
contractAddress: string
78
method: string
8-
args?: SorobanClient.xdr.ScVal[]
9+
args?: StellarSdk.xdr.ScVal[]
910
}
1011

1112
export function contractTransaction({
@@ -14,15 +15,15 @@ export function contractTransaction({
1415
contractAddress,
1516
method,
1617
args,
17-
}: contractTransactionProps): SorobanClient.Transaction {
18-
let myParams: SorobanClient.xdr.ScVal[] = args || []
19-
const contract = new SorobanClient.Contract(contractAddress)
20-
return new SorobanClient.TransactionBuilder(source, {
18+
}: contractTransactionProps): StellarSdk.Transaction {
19+
let myParams: StellarSdk.xdr.ScVal[] = args || []
20+
const contract = new StellarSdk.Contract(contractAddress)
21+
return new StellarSdk.TransactionBuilder(source, {
2122
// TODO: Figure out the fee
2223
fee: '100',
2324
networkPassphrase,
2425
})
2526
.addOperation(contract.call(method, ...myParams))
26-
.setTimeout(SorobanClient.TimeoutInfinite)
27+
.setTimeout(StellarSdk.TimeoutInfinite)
2728
.build()
2829
}

packages/contracts/src/setTrustline.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SorobanContextType } from '@soroban-react/core'
22

3-
import * as StellarSdk from '@stellar/stellar-sdk'
4-
import * as SorobanClient from 'soroban-client'
3+
import * as StellarSdk from 'stellar-sdk'
4+
55

66
export async function setTrustline({
77
tokenSymbol,
@@ -41,7 +41,7 @@ export async function setTrustline({
4141
networkPassphrase,
4242
})
4343
.addOperation(operation)
44-
.setTimeout(SorobanClient.TimeoutInfinite)
44+
.setTimeout(StellarSdk.TimeoutInfinite)
4545
.build()
4646

4747
const signed = await sorobanContext.activeConnector?.signTransaction(

packages/contracts/src/transaction.tsx

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { SorobanContextType } from '@soroban-react/core'
22

33
import { Sign } from 'crypto'
4-
import * as SorobanClient from 'soroban-client'
5-
import { SorobanRpc } from 'soroban-client'
4+
import * as StellarSdk from 'stellar-sdk'
5+
6+
import { SorobanRpc } from 'stellar-sdk'
67

78
import type { Tx, Transaction, TxResponse } from './types'
89

@@ -29,7 +30,7 @@ export async function signAndSendTransaction({
2930

3031
// preflight and add the footprint !
3132
if (!skipAddingFootprint) {
32-
txn = await server.prepareTransaction(txn, networkPassphrase)
33+
txn = await server.prepareTransaction(txn)
3334
if (!txn) {
3435
throw new Error('No transaction after adding footprint')
3536
}
@@ -42,8 +43,8 @@ export async function signAndSendTransaction({
4243
// if (auth_len > 1) {
4344
// throw new NotImplementedError("Multiple auths not yet supported");
4445
// } else if (auth_len == 1) {
45-
// // TODO: figure out how to fix with new SorobanClient
46-
// // const auth = SorobanClient.xdr.SorobanAuthorizationEntry.fromXDR(auths![0]!, 'base64')
46+
// // TODO: figure out how to fix with new StellarSdk
47+
// // const auth = StellarSdk.xdr.SorobanAuthorizationEntry.fromXDR(auths![0]!, 'base64')
4748
// // if (auth.addressWithNonce() !== undefined) {
4849
// // throw new NotImplementedError(
4950
// // `This transaction needs to be signed by ${auth.addressWithNonce()
@@ -55,21 +56,19 @@ export async function signAndSendTransaction({
5556
let signed = ''
5657
if (secretKey) {
5758
// User as set a secretKey, txn will be signed using the secretKey
58-
const keypair = SorobanClient.Keypair.fromSecret(secretKey)
59+
const keypair = StellarSdk.Keypair.fromSecret(secretKey)
5960
txn.sign(keypair)
6061
signed = txn.toXDR()
6162
} else if (sorobanContext.activeConnector) {
6263
// User has not set a secretKey, txn will be signed using the Connector (wallet) provided in the sorobanContext
63-
signed = await sorobanContext.activeConnector.signTransaction(txn.toXDR(), {
64-
networkPassphrase,
65-
})
64+
signed = await sorobanContext.activeConnector.signTransaction(txn.toXDR())
6665
} else {
6766
throw new Error(
6867
'signAndSendTransaction: no secretKey, neither active Connector'
6968
)
7069
}
7170

72-
const transactionToSubmit = SorobanClient.TransactionBuilder.fromXDR(
71+
const transactionToSubmit = StellarSdk.TransactionBuilder.fromXDR(
7372
signed,
7473
networkPassphrase
7574
)
@@ -89,7 +88,7 @@ export async function sendTx({
8988
}: {
9089
tx: Tx
9190
secondsToWait: number
92-
server: SorobanClient.Server
91+
server: StellarSdk.SorobanRpc.Server
9392
}): Promise<TxResponse> {
9493
const sendTransactionResponse = await server.sendTransaction(tx)
9594
let getTransactionResponse = await server.getTransaction(
@@ -116,7 +115,7 @@ export async function sendTx({
116115
}
117116

118117
if (
119-
getTransactionResponse.status === SorobanRpc.GetTransactionStatus.NOT_FOUND
118+
getTransactionResponse.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND
120119
) {
121120
console.error(
122121
`Waited ${secondsToWait} seconds for transaction to complete, but it did not. ` +

packages/contracts/src/types.tsx

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import * as SorobanClient from 'soroban-client'
2-
import { SorobanRpc } from 'soroban-client'
1+
import * as StellarSdk from 'stellar-sdk'
2+
3+
import { SorobanRpc } from 'stellar-sdk'
34
import type {
45
Memo,
56
MemoType,
67
Operation,
7-
Transaction as SorobanClientTransaction,
8-
} from 'soroban-client'
8+
Transaction as StellarSdkTransaction,
9+
} from 'stellar-sdk'
910

1011
export type Transaction =
11-
| SorobanClient.Transaction
12-
| SorobanClient.FeeBumpTransaction
13-
export type Tx = SorobanClientTransaction<Memo<MemoType>, Operation[]>
14-
export type TxResponse = SorobanRpc.GetTransactionResponse & {
12+
| StellarSdk.Transaction
13+
| StellarSdk.FeeBumpTransaction
14+
export type Tx = StellarSdkTransaction<Memo<MemoType>, Operation[]>
15+
export type TxResponse = SorobanRpc.Api.GetTransactionResponse & {
1516
txHash: string
1617
}
17-
export type Simulation = SorobanRpc.SimulateTransactionResponse
18+
export type Simulation = SorobanRpc.Api.SimulateTransactionResponse

packages/contracts/src/useContractValue.tsx

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import { SorobanContextType } from '@soroban-react/core'
22
import React from 'react'
33

4-
import { SorobanRpc } from 'soroban-client'
5-
import * as SorobanClient from 'soroban-client'
4+
import { SorobanRpc } from 'stellar-sdk'
5+
import * as StellarSdk from 'stellar-sdk'
6+
67

78
import { contractTransaction } from './contractTransaction'
89
import { defaultAddress } from './defaultAddress'
910

10-
let xdr = SorobanClient.xdr
11+
let xdr = StellarSdk.xdr
1112

1213
export type ContractValueType = {
1314
loading?: true
14-
result?: SorobanClient.xdr.ScVal
15+
result?: StellarSdk.xdr.ScVal
1516
error?: string | unknown
1617
}
1718

1819
export interface useContractValueProps {
1920
contractAddress: string
2021
method: string
21-
args?: SorobanClient.xdr.ScVal[] | undefined
22-
source?: SorobanClient.Account
22+
args?: StellarSdk.xdr.ScVal[] | undefined
23+
source?: StellarSdk.Account
2324
sorobanContext: SorobanContextType
2425
}
2526

@@ -42,7 +43,7 @@ export function useContractValue({
4243
)
4344

4445
React.useEffect(() => {
45-
source = source ?? new SorobanClient.Account(address ?? defaultAddress, '0')
46+
source = source ?? new StellarSdk.Account(address ?? defaultAddress, '0')
4647
if (!activeChain) {
4748
setValue({ error: 'No active chain' })
4849
return
@@ -84,12 +85,12 @@ export function useContractValue({
8485
}
8586

8687
export interface fetchContractValueProps {
87-
server: SorobanClient.Server
88+
server: SorobanRpc.Server
8889
networkPassphrase: string
8990
contractAddress: string
9091
method: string
91-
args?: SorobanClient.xdr.ScVal[] | undefined
92-
source: SorobanClient.Account
92+
args?: StellarSdk.xdr.ScVal[] | undefined
93+
source: StellarSdk.Account
9394
}
9495

9596
async function fetchContractValue({
@@ -99,7 +100,7 @@ async function fetchContractValue({
99100
method,
100101
args,
101102
source,
102-
}: fetchContractValueProps): Promise<SorobanClient.xdr.ScVal> {
103+
}: fetchContractValueProps): Promise<StellarSdk.xdr.ScVal> {
103104
//Builds the transaction.
104105
let txn = contractTransaction({
105106
source,
@@ -111,9 +112,9 @@ async function fetchContractValue({
111112

112113
let a = Math.random()
113114

114-
const simulated: SorobanRpc.SimulateTransactionResponse =
115+
const simulated: SorobanRpc.Api.SimulateTransactionResponse =
115116
await server?.simulateTransaction(txn)
116-
if (SorobanRpc.isSimulationError(simulated)) {
117+
if (SorobanRpc.Api.isSimulationError(simulated)) {
117118
throw new Error(simulated.error)
118119
} else if (!simulated.result) {
119120
throw new Error(`invalid simulation: no result in ${simulated}`)

packages/contracts/src/useSendTransaction.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { SorobanContextType } from '@soroban-react/core'
22
import React from 'react'
33

4-
import * as SorobanClient from 'soroban-client'
4+
import * as StellarSdk from 'stellar-sdk'
5+
56

67
import { signAndSendTransaction } from './transaction'
78
import type { Transaction, Tx, TxResponse, Simulation } from './types'
89

910
export type TransactionStatus = 'idle' | 'error' | 'loading' | 'success'
1011

1112
export interface SendTransactionResult<E = Error> {
12-
data?: SorobanClient.xdr.ScVal
13+
data?: StellarSdk.xdr.ScVal
1314
error?: E
1415
isError: boolean
1516
isIdle: boolean

packages/contracts/tests/setTrustline.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { setTrustline } = require('../dist/setTrustline')
2-
const SorobanClient = require('soroban-client')
2+
const StellarSdk = require('soroban-client')
33
describe('setTrustline', () => {
44
const tokenSymbol = 'XLM'
55
const tokenAdmin = 'GAV6GQGSOSGCRX262R4MTGKNT6UDWJTNUQLLWBZK5CHHRB5GMNNC7XAB'
@@ -55,7 +55,7 @@ describe('setTrustline', () => {
5555
}
5656
sorobanContext.server = {
5757
async getAccount() {
58-
return new SorobanClient.Account(
58+
return new StellarSdk.Account(
5959
'GAV6GQGSOSGCRX262R4MTGKNT6UDWJTNUQLLWBZK5CHHRB5GMNNC7XAB',
6060
'231'
6161
)
@@ -82,7 +82,7 @@ describe('setTrustline', () => {
8282
}
8383
sorobanContext.server = {
8484
async getAccount() {
85-
return new SorobanClient.Account(
85+
return new StellarSdk.Account(
8686
'GAV6GQGSOSGCRX262R4MTGKNT6UDWJTNUQLLWBZK5CHHRB5GMNNC7XAB',
8787
'231'
8888
)

0 commit comments

Comments
 (0)