-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bugfix/tx history restore bump #945
Changes from 5 commits
60ba6e7
1976f25
73b39c8
4bf2718
30b4c0a
1ba0450
05fa8fe
b52103c
bb75977
40de096
8a265ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// https://github.com/stellar/soroban-examples/blob/main/token/src/contract.rs | ||
export enum SorobanTokenInterface { | ||
transfer = "transfer", | ||
mint = "mint", | ||
} | ||
|
||
// TODO: can we generate this at build time using the cli TS generator? Also should we? | ||
export interface SorobanToken { | ||
// only currently holds fields we care about | ||
transfer: (from: string, to: string, amount: number) => void; | ||
mint: (to: string, amount: number) => void; | ||
// values below are in storage | ||
name: string; | ||
balance: number; | ||
symbol: string; | ||
decimals: number; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
Transaction, | ||
Memo, | ||
MemoType, | ||
Operation, | ||
Server, | ||
xdr, | ||
scValToNative, | ||
} from "soroban-client"; | ||
import { captureException } from "@sentry/browser"; | ||
|
||
export const simulateTx = async <ArgType>( | ||
tx: Transaction<Memo<MemoType>, Operation[]>, | ||
server: Server, | ||
): Promise<ArgType> => { | ||
const { results } = await server.simulateTransaction(tx); | ||
if (!results || results.length !== 1) { | ||
throw new Error("Invalid response from simulateTransaction"); | ||
} | ||
const result = results[0]; | ||
const scVal = xdr.ScVal.fromXDR(result.xdr, "base64"); | ||
let convertedScVal: any; | ||
try { | ||
// handle a case where scValToNative doesn't properly handle scvString | ||
convertedScVal = scVal.str().toString(); | ||
return convertedScVal; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I believe this should be fixed now in the last soroban-client. So I don't think you need to handle this scvString case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah I did hear that also, I'll test it out to make sure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in 1ba0450 |
||
} catch (e) { | ||
console.error(e); | ||
captureException(`Failed to convert SCVal to native val, ${e}`); | ||
} | ||
return scValToNative(scVal); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { | ||
Contract, | ||
TransactionBuilder, | ||
Memo, | ||
Server, | ||
TimeoutInfinite, | ||
xdr, | ||
} from "soroban-client"; | ||
import { simulateTx } from "./server"; | ||
|
||
export const transfer = async ( | ||
contractId: string, | ||
params: xdr.ScVal[], | ||
memo: string | undefined, | ||
builder: TransactionBuilder, | ||
) => { | ||
const contract = new Contract(contractId); | ||
|
||
const tx = builder | ||
.addOperation(contract.call("transfer", ...params)) | ||
.setTimeout(TimeoutInfinite); | ||
|
||
if (memo) { | ||
tx.addMemo(Memo.text(memo)); | ||
} | ||
|
||
return tx.build(); | ||
}; | ||
|
||
export const getBalance = async ( | ||
contractId: string, | ||
params: xdr.ScVal[], | ||
server: Server, | ||
builder: TransactionBuilder, | ||
) => { | ||
const contract = new Contract(contractId); | ||
|
||
const tx = builder | ||
.addOperation(contract.call("balance", ...params)) | ||
.setTimeout(TimeoutInfinite) | ||
.build(); | ||
|
||
const result = await simulateTx<number>(tx, server); | ||
return result; | ||
}; | ||
|
||
export const getDecimals = async ( | ||
contractId: string, | ||
server: Server, | ||
builder: TransactionBuilder, | ||
) => { | ||
const contract = new Contract(contractId); | ||
|
||
const tx = builder | ||
.addOperation(contract.call("decimals")) | ||
.setTimeout(TimeoutInfinite) | ||
.build(); | ||
|
||
const result = await simulateTx<number>(tx, server); | ||
return result; | ||
}; | ||
|
||
export const getName = async ( | ||
contractId: string, | ||
server: Server, | ||
builder: TransactionBuilder, | ||
) => { | ||
const contract = new Contract(contractId); | ||
|
||
const tx = builder | ||
.addOperation(contract.call("name")) | ||
.setTimeout(TimeoutInfinite) | ||
.build(); | ||
|
||
const result = await simulateTx<string>(tx, server); | ||
return result; | ||
}; | ||
|
||
export const getSymbol = async ( | ||
contractId: string, | ||
server: Server, | ||
builder: TransactionBuilder, | ||
) => { | ||
const contract = new Contract(contractId); | ||
|
||
const tx = builder | ||
.addOperation(contract.call("symbol")) | ||
.setTimeout(TimeoutInfinite) | ||
.build(); | ||
|
||
const result = await simulateTx<string>(tx, server); | ||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to do this now since things could still change - but if this API holds, in the future we could probably refactor these helpers to share some logic as they're all kinda doing the same thing. Also, @Shaptic - do we consider this a common enough use case (getting balance + decimals + name + symbol) that soroban-client should offer this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additionally - there's also a good convo happening about how we should be fetching data for Soroban now that expired state can be involved. It seems like we'll have to at least augment our approach anyway pretty soon. Some good ideas that came up includes -
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this belongs in soroban-client, per se, but maybe this be handled by using soroban-cli to generate the TypeScript bindings for the SAC? cc @chadoh @willemneal |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ export enum OPERATION_TYPES { | |
revokeTrustlineSponsorship = "Revoke Trustline Sponsorship", | ||
setOptions = "Set Options", | ||
setTrustLineFlags = "Set Trustline Flags", | ||
bumpFootprintExpiration = "Bump Footprint Expiration", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes it is, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in 05fa8fe |
||
} | ||
|
||
export enum TRANSACTION_WARNING { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,11 @@ import { | |
|
||
import { settingsNetworkDetailsSelector } from "./ducks/settings"; | ||
|
||
const BASE_FEE = "100"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, I keep meaning to update that. Done in 05fa8fe |
||
|
||
export interface SorobanContextInterface { | ||
server: SorobanClient.Server; | ||
newTxBuilder: () => SorobanClient.TransactionBuilder; | ||
newTxBuilder: (fee?: string) => SorobanClient.TransactionBuilder; | ||
} | ||
|
||
export const SorobanContext = React.createContext( | ||
|
@@ -25,9 +27,6 @@ export const SorobanProvider = ({ | |
children: React.ReactNode; | ||
pubKey: string; | ||
}) => { | ||
// Were only simluating so the fee here should not matter | ||
// AFAIK there is no fee stats for Soroban yet either | ||
const fee = "100"; | ||
const networkDetails = useSelector(settingsNetworkDetailsSelector); | ||
const source = new SorobanClient.Account(pubKey, "0"); | ||
|
||
|
@@ -42,7 +41,7 @@ export const SorobanProvider = ({ | |
allowHttp: networkDetails.networkUrl.startsWith("http://"), | ||
}); | ||
|
||
const newTxBuilder = () => | ||
const newTxBuilder = (fee = BASE_FEE) => | ||
new SorobanClient.TransactionBuilder(source, { | ||
fee, | ||
networkPassphrase: networkDetails.networkPassphrase, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice - I find this pattern a lot more straightforward!