Skip to content

Commit

Permalink
upgrade to latest sdk (#17)
Browse files Browse the repository at this point in the history
* upgrade to latest sdk

* api change

* replaces expiration ledger key with just contract ledger key

* removing unused comments

---------

Co-authored-by: Aristides Staffieri <aristides.staffieri@stellar.org>
  • Loading branch information
piyalbasu and aristidesstaffieri authored May 29, 2024
1 parent 2a15d89 commit 462c55b
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 93 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@
"dependencies": {
"@stellar/design-system": "^1.0.0-beta.12",
"@stellar/freighter-api": "1.7.0",
"@stellar/stellar-sdk": "^11.3.0",
"bignumber.js": "^9.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.14.1",
"soroban-client": "1.0.0-beta.2",
"stellar-wallets-kit": "github:Creit-Tech/Stellar-Wallets-Kit"
}
}
12 changes: 3 additions & 9 deletions src/components/atomic-swap/exchange.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { ChangeEvent } from "react";
import {
assembleTransaction,
BASE_FEE,
Memo,
MemoType,
Expand All @@ -10,7 +9,7 @@ import {
Transaction,
TransactionBuilder,
xdr,
} from "soroban-client";
} from "@stellar/stellar-sdk";
import {
Button,
Card,
Expand Down Expand Up @@ -164,16 +163,12 @@ export const Exchange = (props: ExchangeProps) => {

const txSim = await server.simulateTransaction(tx);

if (!SorobanRpc.isSimulationSuccess(txSim)) {
if (!SorobanRpc.Api.isSimulationSuccess(txSim)) {
props.setError(ERRORS.TX_SIM_FAILED);
return;
}

const preparedTransaction = assembleTransaction(
tx,
props.networkDetails.networkPassphrase,
txSim,
);
const preparedTransaction = SorobanRpc.assembleTransaction(tx, txSim);

if (originalFootprint) {
const finalTx = preparedTransaction
Expand Down Expand Up @@ -288,7 +283,6 @@ export const Exchange = (props: ExchangeProps) => {
swapperBAddress,
memo,
server,
props.networkDetails.networkPassphrase,
txBuilder,
);
setOriginalFootprint(footprint);
Expand Down
2 changes: 1 addition & 1 deletion src/components/atomic-swap/swapper-A.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Operation,
Transaction,
TransactionBuilder,
} from "soroban-client";
} from "@stellar/stellar-sdk";
import { Button, Heading, Select, Profile } from "@stellar/design-system";
import {
WalletNetwork,
Expand Down
2 changes: 1 addition & 1 deletion src/components/atomic-swap/swapper-B.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Memo,
MemoType,
Operation,
} from "soroban-client";
} from "@stellar/stellar-sdk";
import {
WalletNetwork,
WalletType,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/sign-auth-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
nativeToScVal,
Keypair,
hash,
} from "soroban-client";
} from "@stellar/stellar-sdk";

// This can be replaced with the same helper in the sdk when it lands
// https://github.com/stellar/js-stellar-base/pull/678
Expand Down
58 changes: 23 additions & 35 deletions src/helpers/soroban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
MemoType,
Operation,
scValToNative,
Server,
SorobanRpc,
StrKey,
TimeoutInfinite,
Expand All @@ -14,9 +13,7 @@ import {
xdr,
scValToBigInt,
ScInt,
assembleTransaction,
hash,
} from "soroban-client";
} from "@stellar/stellar-sdk";
import BigNumber from "bignumber.js";
import { StellarWalletsKit } from "stellar-wallets-kit";

Expand All @@ -25,7 +22,7 @@ import { ERRORS } from "./error";
import { authorizeEntry } from "./sign-auth-entry";

export const SendTxStatus: {
[index: string]: SorobanRpc.SendTransactionStatus;
[index: string]: SorobanRpc.Api.SendTransactionStatus;
} = {
Pending: "PENDING",
Duplicate: "DUPLICATE",
Expand Down Expand Up @@ -81,20 +78,20 @@ export const valueToI128String = (value: xdr.ScVal) =>

// Get a server configfured for a specific network
export const getServer = (networkDetails: NetworkDetails) =>
new Server(RPC_URLS[networkDetails.network], {
new SorobanRpc.Server(RPC_URLS[networkDetails.network], {
allowHttp: networkDetails.networkUrl.startsWith("http://"),
});

// Can be used whenever we need to perform a "read-only" operation
// Used in getTokenSymbol, getTokenName, and getTokenDecimals
export const simulateTx = async <ArgType>(
tx: Transaction<Memo<MemoType>, Operation[]>,
server: Server,
server: SorobanRpc.Server,
): Promise<ArgType> => {
const response = await server.simulateTransaction(tx);

if (
SorobanRpc.isSimulationSuccess(response) &&
SorobanRpc.Api.isSimulationSuccess(response) &&
response.result !== undefined
) {
return scValToNative(response.result.retval);
Expand All @@ -107,7 +104,7 @@ export const simulateTx = async <ArgType>(
export const getTokenDecimals = async (
tokenId: string,
txBuilder: TransactionBuilder,
server: Server,
server: SorobanRpc.Server,
) => {
const contract = new Contract(tokenId);
const tx = txBuilder
Expand All @@ -123,7 +120,7 @@ export const getTokenDecimals = async (
export const getTxBuilder = async (
pubKey: string,
fee: string,
server: Server,
server: SorobanRpc.Server,
networkPassphrase: string,
) => {
const source = await server.getAccount(pubKey);
Expand All @@ -148,8 +145,7 @@ export const buildSwap = async (
swapperAPubKey: string,
swapperBPubKey: string,
memo: string,
server: Server,
networkPassphrase: string,
server: SorobanRpc.Server,
txBuilder: TransactionBuilder,
) => {
const swapContract = new Contract(contractID);
Expand Down Expand Up @@ -179,14 +175,12 @@ export const buildSwap = async (
}

const built = tx.build();
const sim = await server.simulateTransaction(built);
const preparedTransaction = assembleTransaction(
const sim = (await server.simulateTransaction(
built,
networkPassphrase,
sim,
);
)) as SorobanRpc.Api.SimulateTransactionSuccessResponse;
const preparedTransaction = SorobanRpc.assembleTransaction(built, sim);

if (!SorobanRpc.isSimulationSuccess(sim)) {
if (!SorobanRpc.Api.isSimulationSuccess(sim)) {
throw new Error(ERRORS.TX_SIM_FAILED);
}

Expand All @@ -200,7 +194,7 @@ export const buildSwap = async (
export const getTokenSymbol = async (
tokenId: string,
txBuilder: TransactionBuilder,
server: Server,
server: SorobanRpc.Server,
) => {
const contract = new Contract(tokenId);

Expand All @@ -218,7 +212,7 @@ export const buildContractAuth = async (
signerPubKey: string,
networkPassphrase: string,
contractID: string,
server: Server,
server: SorobanRpc.Server,
kit: StellarWalletsKit,
) => {
const signedAuthEntries = [];
Expand All @@ -245,20 +239,12 @@ export const buildContractAuth = async (
}),
);

const expirationKey = xdr.LedgerKey.expiration(
new xdr.LedgerKeyExpiration({ keyHash: hash(key.toXDR()) }),
);

// Fetch the current contract ledger seq
// eslint-disable-next-line no-await-in-loop
const entryRes = await server.getLedgerEntries(expirationKey);
const entryRes = await server.getLedgerEntries(key);
if (entryRes.entries && entryRes.entries.length) {
const parsed = xdr.LedgerEntryData.fromXDR(
entryRes.entries[0].xdr,
"base64",
);
// set auth entry to expire when contract data expires, but could any number of blocks in the future
expirationLedgerSeq = parsed.expiration().expirationLedgerSeq();
expirationLedgerSeq = entryRes.entries[0].liveUntilLedgerSeq || 0;
} else {
throw new Error(ERRORS.CANNOT_FETCH_LEDGER_ENTRY);
}
Expand Down Expand Up @@ -299,7 +285,7 @@ export const signContractAuth = async (
contractID: string,
signerPubKey: string,
tx: Transaction,
server: Server,
server: SorobanRpc.Server,
networkPassphrase: string,
kit: StellarWalletsKit,
) => {
Expand Down Expand Up @@ -370,21 +356,23 @@ export const getArgsFromEnvelope = (
export const submitTx = async (
signedXDR: string,
networkPassphrase: string,
server: Server,
server: SorobanRpc.Server,
) => {
const tx = TransactionBuilder.fromXDR(signedXDR, networkPassphrase);

const sendResponse = await server.sendTransaction(tx);

if (sendResponse.errorResultXdr) {
if (sendResponse.errorResult) {
throw new Error(ERRORS.UNABLE_TO_SUBMIT_TX);
}

if (sendResponse.status === SendTxStatus.Pending) {
let txResponse = await server.getTransaction(sendResponse.hash);

// Poll this until the status is not "NOT_FOUND"
while (txResponse.status === SorobanRpc.GetTransactionStatus.NOT_FOUND) {
while (
txResponse.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND
) {
// See if the transaction is complete
// eslint-disable-next-line no-await-in-loop
txResponse = await server.getTransaction(sendResponse.hash);
Expand All @@ -393,7 +381,7 @@ export const submitTx = async (
await new Promise((resolve) => setTimeout(resolve, 1000));
}

if (txResponse.status === SorobanRpc.GetTransactionStatus.SUCCESS) {
if (txResponse.status === SorobanRpc.Api.GetTransactionStatus.SUCCESS) {
return txResponse.resultXdr.toXDR("base64");
}

Expand Down
Loading

0 comments on commit 462c55b

Please sign in to comment.