Skip to content

Commit

Permalink
1.2.1 (#87)
Browse files Browse the repository at this point in the history
* Adding return values to response

* Rewriting/regenerating tests

* Version 1.2.0

* Detect and pass index to web render for default selection

* Renamed return value interface

* Updating dependencies

* Version 1.2.1-rc1

* Version 1.2.1
  • Loading branch information
aaroncox authored Nov 30, 2023
1 parent bc1e748 commit e84bf64
Show file tree
Hide file tree
Showing 39 changed files with 1,086 additions and 4,435 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wharfkit/session",
"description": "Create account-based sessions, perform transactions, and allow users to login using Antelope-based blockchains.",
"version": "1.1.1",
"version": "1.2.1",
"homepage": "https://github.com/wharfkit/session",
"license": "BSD-3-Clause",
"main": "lib/session.js",
Expand All @@ -17,8 +17,8 @@
"prepare": "make"
},
"dependencies": {
"@wharfkit/abicache": "^1.2.0",
"@wharfkit/antelope": "^1.0.1",
"@wharfkit/abicache": "^1.2.1",
"@wharfkit/antelope": "^1.0.2",
"@wharfkit/common": "^1.2.0",
"@wharfkit/signing-request": "^3.1.0",
"pako": "^2.0.4",
Expand Down
6 changes: 4 additions & 2 deletions src/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,10 @@ export class SessionKit {
walletPlugin = this.walletPlugins[0] // Default to first when only one.
context.uiRequirements.requiresWalletSelect = false
} else if (options?.walletPlugin) {
walletPlugin = this.walletPlugins.find((p) => p.id === options.walletPlugin)
if (walletPlugin) {
const index = this.walletPlugins.findIndex((p) => p.id === options.walletPlugin)
if (index >= 0) {
walletPlugin = this.walletPlugins[index]
context.walletPluginIndex = index
context.uiRequirements.requiresWalletSelect = false
}
}
Expand Down
1 change: 1 addition & 0 deletions src/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class LoginContext {
requiresPermissionEntry: false,
requiresWalletSelect: true,
}
walletPluginIndex?: number
walletPlugins: UserInterfaceWalletPlugin[] = []
constructor(options: LoginContextOptions) {
this.appName = String(options.appName)
Expand Down
45 changes: 45 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
TransactPlugin,
TransactPluginsOptions,
TransactResult,
TransactResultReturnValue,
TransactRevisions,
} from './transact'
import {SessionStorage} from './storage'
Expand Down Expand Up @@ -397,6 +398,7 @@ export class Session {
chain: this.chain,
request,
resolved: undefined,
returns: [],
revisions: new TransactRevisions(request),
signatures: [],
signer: this.permissionLevel,
Expand Down Expand Up @@ -486,6 +488,11 @@ export class Session {
// Broadcast the SignedTransaction and save the API response to the TransactResult
result.response = await context.client.v1.chain.send_transaction(signed)

// Find and process any return values from the transaction
if (result.response.processed && result.response.processed.action_traces) {
result.returns = await processReturnValues(result.response, abiCache)
}

// Run the `afterBroadcast` hooks that were registered by the TransactPlugins
for (const hook of context.hooks.afterBroadcast) await hook(result, context)

Expand Down Expand Up @@ -637,3 +644,41 @@ export class Session {
return abiCache
}
}

async function processReturnValues(
response: any,
abiCache: ABICacheInterface
): Promise<TransactResultReturnValue[]> {
const returns: TransactResultReturnValue[] = []
for (const actionTrace of response.processed.action_traces) {
if (actionTrace.return_value_hex_data) {
const contract = Name.from(actionTrace.act.account)
const action = Name.from(actionTrace.act.name)
const abi = await abiCache.getAbi(contract)
const returnType = abi.action_results.find((a) => Name.from(a.name).equals(action))
if (returnType) {
try {
const data = Serializer.decode({
data: actionTrace.return_value_hex_data,
type: returnType.result_type,
abi,
})
returns.push({
contract,
action,
hex: actionTrace.return_value_hex_data,
data,
returnType,
})
} catch (error) {
// eslint-disable-next-line no-console -- warn the developer since this may be unintentional
console.warn(`Error decoding return value for ${contract}::${action}:`, error)
}
} else {
// eslint-disable-next-line no-console -- warn the developer since this may be unintentional
console.warn(`No return type found for ${contract}::${action}`)
}
}
}
return returns
}
21 changes: 21 additions & 0 deletions src/transact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,25 @@ export class TransactRevisions {
}
}

/**
* An interface to define a return type
*/
export interface TransactResultReturnType {
name: NameType
result_type: string
}

/**
* The return values from a [[Session.transact]] call that have been processed and decoded.
*/
export interface TransactResultReturnValue {
contract: Name
action: Name
hex: string
data: any
returnType: TransactResultReturnType
}

/**
* The response from a [[Session.transact]] call.
*/
Expand All @@ -308,6 +327,8 @@ export interface TransactResult {
resolved: ResolvedSigningRequest | undefined
/** The response from the API after sending the transaction, only present if transaction was broadcast. */
response?: {[key: string]: any}
/** The return values provided by the transaction */
returns: TransactResultReturnValue[]
/** An array containing revisions of the transaction as modified by plugins as ESR payloads */
revisions: TransactRevisions
/** The transaction signatures. */
Expand Down
Loading

0 comments on commit e84bf64

Please sign in to comment.