Skip to content

Commit 1e6767f

Browse files
authored
send_transaction2 (#102)
* Switching to send_transaction2 w/ options * Fixing edge case with session key updates * v2.0.0-rc5
1 parent 7ebd222 commit 1e6767f

33 files changed

+728
-837
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@wharfkit/session",
33
"description": "Create account-based sessions, perform transactions, and allow users to login using Antelope-based blockchains.",
4-
"version": "2.0.0-rc4",
4+
"version": "2.0.0-rc5",
55
"homepage": "https://github.com/wharfkit/session",
66
"license": "BSD-3-Clause",
77
"main": "lib/session.js",

src/kit.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {BrowserLocalStorage, SessionStorage} from './storage'
2020
import {
2121
AbstractTransactPlugin,
2222
BaseTransactPlugin,
23+
BroadcastOptions,
2324
TransactABIDef,
2425
TransactPlugin,
2526
TransactPluginsOptions,
@@ -86,6 +87,8 @@ export interface SessionKitOptions {
8687
storage?: SessionStorage
8788
transactPlugins?: TransactPlugin[]
8889
transactPluginsOptions?: TransactPluginsOptions
90+
awaitIrreversible?: boolean
91+
broadcastOptions?: BroadcastOptions
8992
accountCreationPlugins?: AccountCreationPlugin[]
9093
sessionKey?: SessionKeyConfig
9194
}
@@ -97,6 +100,8 @@ export class SessionKit {
97100
readonly abis: TransactABIDef[] = []
98101
readonly allowModify: boolean = true
99102
readonly appName: string
103+
readonly awaitIrreversible: boolean = false
104+
readonly broadcastOptions?: BroadcastOptions
100105
readonly expireSeconds: number = 120
101106
readonly fetch: Fetch
102107
readonly loginPlugins: AbstractLoginPlugin[]
@@ -153,6 +158,12 @@ export class SessionKit {
153158
if (typeof options.allowModify !== 'undefined') {
154159
this.allowModify = options.allowModify
155160
}
161+
if (options.awaitIrreversible !== undefined) {
162+
this.awaitIrreversible = options.awaitIrreversible
163+
}
164+
if (options.broadcastOptions !== undefined) {
165+
this.broadcastOptions = options.broadcastOptions
166+
}
156167
// Override default expireSeconds for all sessions if specified
157168
if (options.expireSeconds) {
158169
this.expireSeconds = options.expireSeconds
@@ -790,6 +801,8 @@ export class SessionKit {
790801
storage: this.storage,
791802
transactPlugins: options?.transactPlugins || this.transactPlugins,
792803
transactPluginsOptions: options?.transactPluginsOptions || this.transactPluginsOptions,
804+
awaitIrreversible: this.awaitIrreversible,
805+
broadcastOptions: this.broadcastOptions,
793806
ui: this.ui,
794807
sessionKeyManager: this.sessionKeyManager,
795808
onPersist: (session: Session) => this.persistSession(session),

src/session.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {ABICache, ABICacheInterface} from '@wharfkit/abicache'
3131
import {
3232
AbstractTransactPlugin,
3333
BaseTransactPlugin,
34+
BroadcastOptions,
3435
TransactABIDef,
3536
TransactArgs,
3637
TransactContext,
@@ -44,6 +45,7 @@ import {
4445
import {SessionStorage} from './storage'
4546
import {
4647
actionMatchesPermission,
48+
buildSendTransaction2Options,
4749
extractActions,
4850
getFetch,
4951
getPluginTranslations,
@@ -87,6 +89,8 @@ export interface SessionOptions {
8789
transactPlugins?: AbstractTransactPlugin[]
8890
transactPluginsOptions?: TransactPluginsOptions
8991
ui?: UserInterface
92+
awaitIrreversible?: boolean
93+
broadcastOptions?: BroadcastOptions
9094
sessionKeyManager?: SessionKeyManager
9195
onPersist?: (session: Session) => Promise<void>
9296
}
@@ -108,7 +112,9 @@ export class Session {
108112
readonly abis: TransactABIDef[] = []
109113
readonly abiCache: ABICacheInterface
110114
readonly allowModify: boolean = true
115+
readonly awaitIrreversible: boolean = false
111116
readonly broadcast: boolean = true
117+
readonly broadcastOptions?: BroadcastOptions
112118
readonly chain: ChainDefinition
113119
readonly expireSeconds: number = 120
114120
readonly fetch: Fetch
@@ -201,6 +207,12 @@ export class Session {
201207
if (options.broadcast !== undefined) {
202208
this.broadcast = options.broadcast
203209
}
210+
if (options.awaitIrreversible !== undefined) {
211+
this.awaitIrreversible = options.awaitIrreversible
212+
}
213+
if (options.broadcastOptions !== undefined) {
214+
this.broadcastOptions = options.broadcastOptions
215+
}
204216
if (options.expireSeconds) {
205217
this.expireSeconds = options.expireSeconds
206218
}
@@ -448,6 +460,16 @@ export class Session {
448460
? options.broadcast
449461
: this.broadcast
450462

463+
const awaitIrreversible =
464+
options && options.awaitIrreversible !== undefined
465+
? options.awaitIrreversible
466+
: this.awaitIrreversible
467+
468+
const broadcastOptions =
469+
options && options.broadcastOptions !== undefined
470+
? options.broadcastOptions
471+
: this.broadcastOptions
472+
451473
// The abi provider to use for this transaction, falling back to the session instance
452474
const abiCache = this.getMergedAbiCache(args, options)
453475

@@ -596,8 +618,19 @@ export class Session {
596618
signatures: result.signatures,
597619
})
598620

599-
// Broadcast the SignedTransaction and save the API response to the TransactResult
600-
result.response = await context.client.v1.chain.send_transaction(signed)
621+
const tx2Options = buildSendTransaction2Options(awaitIrreversible, broadcastOptions)
622+
try {
623+
result.response = await context.client.v1.chain.send_transaction2(
624+
signed,
625+
tx2Options
626+
)
627+
} catch (error: any) {
628+
if (error?.response?.status === 404) {
629+
result.response = await context.client.v1.chain.send_transaction(signed)
630+
} else {
631+
throw error
632+
}
633+
}
601634

602635
// Find and process any return values from the transaction
603636
if (result.response.processed && result.response.processed.action_traces) {

src/sessionkey/login.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,25 @@ export class SessionKeyLoginPlugin extends AbstractLoginPlugin {
5151
})),
5252
})
5353

54-
if (choice === 'update') {
55-
await manager.updateLinks(session)
54+
if (choice !== 'update') {
55+
return
5656
}
57+
} else {
58+
return
5759
}
58-
return
59-
}
60-
61-
if (!manager.config.skipConsent && context.ui?.onSessionKeyConsent) {
62-
const consent = await context.ui.onSessionKeyConsent({
63-
appName: String(session.appName || 'this app'),
64-
whitelist: manager.whitelist.map((e) => ({
65-
contract: String(e.contract),
66-
actions: e.actions?.map((a) => String(a)),
67-
})),
68-
})
60+
} else {
61+
if (!manager.config.skipConsent && context.ui?.onSessionKeyConsent) {
62+
const consent = await context.ui.onSessionKeyConsent({
63+
appName: String(session.appName || 'this app'),
64+
whitelist: manager.whitelist.map((e) => ({
65+
contract: String(e.contract),
66+
actions: e.actions?.map((a) => String(a)),
67+
})),
68+
})
6969

70-
if (!consent) {
71-
return
70+
if (!consent) {
71+
return
72+
}
7273
}
7374
}
7475

src/transact.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,22 @@ export interface TransactOptions {
247247
* Optional parameter to control whether signatures returned from plugins are validated.
248248
*/
249249
validatePluginSignatures?: boolean
250+
/**
251+
* Wait for the transaction to become irreversible before returning.
252+
* Uses send_transaction2 with retry enabled and no block limit (waits for LIB).
253+
*/
254+
awaitIrreversible?: boolean
255+
/**
256+
* Advanced options for send_transaction2. Provides fine-grained control over
257+
* retry behavior and failure tracing.
258+
*/
259+
broadcastOptions?: BroadcastOptions
260+
}
261+
262+
export interface BroadcastOptions {
263+
returnFailureTrace?: boolean
264+
retryTrx?: boolean
265+
retryTrxNumBlocks?: number
250266
}
251267

252268
export interface TransactABIDef {

src/utils.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '@wharfkit/antelope'
1010
import type {Fetch, LocaleDefinitions} from '@wharfkit/common'
1111
import {PlaceholderAuth, SigningRequest} from '@wharfkit/signing-request'
12-
import {TransactArgs, TransactPlugin} from './transact'
12+
import {BroadcastOptions, TransactArgs, TransactPlugin} from './transact'
1313
import {WalletPlugin} from './wallet'
1414

1515
/**
@@ -215,3 +215,28 @@ export function rewriteAuthorizations(
215215

216216
return args
217217
}
218+
219+
export interface SendTransaction2Options {
220+
return_failure_trace?: boolean
221+
retry_trx?: boolean
222+
retry_trx_num_blocks?: number
223+
}
224+
225+
export function buildSendTransaction2Options(
226+
awaitIrreversible: boolean,
227+
broadcastOptions?: BroadcastOptions
228+
): SendTransaction2Options {
229+
const options: SendTransaction2Options = {
230+
return_failure_trace: broadcastOptions?.returnFailureTrace ?? true,
231+
}
232+
if (awaitIrreversible) {
233+
options.retry_trx = true
234+
options.retry_trx_num_blocks = undefined
235+
} else if (broadcastOptions) {
236+
options.retry_trx = broadcastOptions.retryTrx ?? true
237+
if (broadcastOptions.retryTrxNumBlocks !== undefined) {
238+
options.retry_trx_num_blocks = broadcastOptions.retryTrxNumBlocks
239+
}
240+
}
241+
return options
242+
}

test/data/0672555e54e5f8adde3a189f52eed3ae86b4d3e5.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)