Skip to content

Commit

Permalink
fix resolve accounts and interaction types
Browse files Browse the repository at this point in the history
  • Loading branch information
nialexsan committed Nov 2, 2023
1 parent eb422ea commit 9313eff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"devDependencies": {
"@onflow/fcl-bundle": "^1.4.0-typescript.0",
"@onflow/typedefs": "^1.2.0-typescript.0",
"@types/uuid": "^9.0.6",
"eslint": "^8.35.0",
"eslint-plugin-jsdoc": "^40.0.1",
"jest": "^29.5.0",
Expand Down
12 changes: 6 additions & 6 deletions packages/sdk/src/interaction/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ export const interaction = () => {
return initInteraction()
}

export const isNumber = d => typeof d === "number"
export const isArray = d => Array.isArray(d)
export const isObj = d => d !== null && typeof d === "object"
export const isNull = d => d == null
export const isFn = d => typeof d === "function"
export const isNumber = (d: any): d is number => typeof d === "number"
export const isArray = (d: any): d is any[] => Array.isArray(d)
export const isObj = (d: any): d is Record<string, any> => d !== null && typeof d === "object"
export const isNull = (d: any): d is null => d == null
export const isFn = (d: any): d is Function => typeof d === "function"

export const isInteraction = (ix: IIx) => {
if (!isObj(ix) || isNull(ix) || isNumber(ix)) return false
Expand Down Expand Up @@ -198,7 +198,7 @@ export const prepAccount = (acct: IAcct | IAcctFn, opts: IPrepAccountOpts = {})
return ix
}

export const makeArgument = arg => ix => {
export const makeArgument = (arg: Record<string, any>) => (ix: IIx) => {
let tempId = uuidv4()
ix.message.arguments.push(tempId)

Expand Down
28 changes: 15 additions & 13 deletions packages/sdk/src/resolve/resolve-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const ROLES = {
function debug() {
const SPACE = " "
const SPACE_COUNT_PER_INDENT = 4
const DEBUG_MESSAGE = []
const DEBUG_MESSAGE: string[] = []
return [
function (msg, indent = 0) {
function (msg = '', indent = 0) {
DEBUG_MESSAGE.push(
Array(indent * SPACE_COUNT_PER_INDENT)
.fill(SPACE)
Expand All @@ -40,7 +40,7 @@ function debug() {
]
}

function recurseFlatMap(el, depthLimit = 3) {
function recurseFlatMap<T>(el: T, depthLimit = 3) {
if (depthLimit <= 0) return el
if (!Array.isArray(el)) return el
return recurseFlatMap(
Expand All @@ -67,12 +67,14 @@ export function buildPreSignable(acct: IAcct, ix: IIx) {
}
}

async function removeUnusedIxAccounts(ix: IIx) {
async function removeUnusedIxAccounts(ix: IIx, opts: Record<string, any>) {
const payerTempIds = Array.isArray(ix.payer) ? ix.payer : [ix.payer]
const authorizersTempIds = Array.isArray(ix.authorizations)
? ix.authorizations
: [ix.authorizations]
const proposerTempIds = Array.isArray(ix.proposer)
const proposerTempIds = ix.proposer === null
? []
: Array.isArray(ix.proposer)
? ix.proposer
: [ix.proposer]

Expand Down Expand Up @@ -115,7 +117,7 @@ function addAccountToIx(ix, newAccount) {
return ix.accounts[newAccount.tempId]
}

function uniqueAccountsFlatMap(accounts) {
function uniqueAccountsFlatMap(accounts: IAcct[]) {
const flatMapped = recurseFlatMap(accounts)
const seen = new Set()

Expand All @@ -132,7 +134,7 @@ function uniqueAccountsFlatMap(accounts) {
seen.add(accountId)
return account
})
.filter(e => e !== null)
.filter(e => e !== null) as IAcct[]

return uniqueAccountsFlatMapped
}
Expand Down Expand Up @@ -214,7 +216,7 @@ async function recurseResolveAccount(
return account.tempId
}

async function resolveAccountType(ix, type, {debugLogger}) {
async function resolveAccountType(ix: IIx, type, {debugLogger}) {
invariant(
ix && typeof ix === "object",
"resolveAccountType Error: ix not defined"
Expand All @@ -228,10 +230,10 @@ async function resolveAccountType(ix, type, {debugLogger}) {

let accountTempIDs = Array.isArray(ix[type]) ? ix[type] : [ix[type]]

let allResolvedAccounts = []
let allResolvedAccounts: IAcct[] = []
for (let accountId of accountTempIDs) {
let account = ix.accounts[accountId]
invariant(account, `resolveAccountType Error: account not found`)
invariant(Boolean(account), `resolveAccountType Error: account not found`)

let resolvedAccountTempIds = await recurseResolveAccount(
ix,
Expand All @@ -246,8 +248,8 @@ async function resolveAccountType(ix, type, {debugLogger}) {
? resolvedAccountTempIds
: [resolvedAccountTempIds]

let resolvedAccounts = resolvedAccountTempIds.map(
resolvedAccountTempId => ix.accounts[resolvedAccountTempId]
let resolvedAccounts: IAcct[] = resolvedAccountTempIds.map(
(resolvedAccountTempId: string) => ix.accounts[resolvedAccountTempId]
)

let flatResolvedAccounts = uniqueAccountsFlatMap(resolvedAccounts)
Expand Down Expand Up @@ -295,7 +297,7 @@ async function resolveAccountType(ix, type, {debugLogger}) {
}
}

export async function resolveAccounts(ix: IIx, opts = {}) {
export async function resolveAccounts(ix: IIx, opts: Record<string, any> = {}) {
if (isTransaction(ix)) {
if (!Array.isArray(ix.payer)) {
log.deprecate({
Expand Down

0 comments on commit 9313eff

Please sign in to comment.