-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into ui-fixes-contract-details
- Loading branch information
Showing
12 changed files
with
193 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import { ChainId } from '@/services/useink/chains' | ||
import { TokenType } from '../TokenType' | ||
import { UserContractDetailsDraft } from '../UserContractDetails' | ||
import { | ||
UserContractDetails, | ||
UserContractDetailsDraft | ||
} from '../UserContractDetails' | ||
|
||
export type ContractType = TokenType | 'custom' | ||
export type ContractType = TokenType | 'custom' // custom contracts are generally deployed outside the PCW | ||
export type UpdateDeployment = Partial<UserContractDetailsDraft> | ||
|
||
export interface IDeploymentsRepository<A, B> { | ||
add: (deployment: UserContractDetailsDraft) => Promise<A> | ||
findBy: (userAddress: string, network?: ChainId) => Promise<B> | ||
updateBy: (deployment: UpdateDeployment) => Promise<A> | ||
get(uuid: string): Promise<UserContractDetails | undefined> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import JSON5 from 'json5' | ||
import { | ||
AbiMessage, | ||
AnyJson, | ||
Bytes, | ||
ContractExecResult, | ||
ContractReturnFlags, | ||
Registry, | ||
TypeDef | ||
} from '@/services/substrate/types' | ||
|
||
type ContractResult = { | ||
Err?: AnyJson | ||
Ok?: AnyJson | ||
} | ||
|
||
function isContractResult(obj: unknown): obj is ContractResult { | ||
return ( | ||
typeof obj === 'object' && obj !== null && ('Err' in obj || 'Ok' in obj) | ||
) | ||
} | ||
|
||
function getReturnTypeName(type: TypeDef | null | undefined): string { | ||
return type?.lookupName || type?.type || '' | ||
} | ||
|
||
function stringify(obj: unknown): string { | ||
return JSON5.stringify(obj, null, 2) | ||
} | ||
|
||
function decodeReturnValue( | ||
returnType: TypeDef | null | undefined, | ||
data: Bytes, | ||
registry: Registry | ||
): AnyJson { | ||
if (!returnType) return '()' | ||
const returnTypeName = getReturnTypeName(returnType) | ||
try { | ||
return registry.createTypeUnsafe(returnTypeName, [data]).toHuman() | ||
} catch (exception) { | ||
console.error(exception) | ||
return 'Decoding error' | ||
} | ||
} | ||
|
||
function checkRevertFlag(flags: ContractReturnFlags): boolean { | ||
return flags.toHuman().includes('Revert') | ||
} | ||
|
||
function extractOutcome(returnValue: AnyJson): AnyJson { | ||
if (!isContractResult(returnValue)) return returnValue | ||
return returnValue.Err ?? returnValue.Ok ?? returnValue | ||
} | ||
|
||
function getOutcomeText(outcome: AnyJson): string { | ||
if (!isContractResult(outcome)) { | ||
return typeof outcome === 'object' && outcome !== null | ||
? stringify(outcome) | ||
: outcome?.toString() ?? 'Error' | ||
} | ||
|
||
const outcomeJson = outcome.Err ?? outcome.Ok | ||
return typeof outcomeJson === 'object' && outcomeJson !== null | ||
? stringify(outcomeJson) | ||
: outcomeJson?.toString() ?? 'Error' | ||
} | ||
|
||
export function getDecodedOutput( | ||
{ result }: Pick<ContractExecResult, 'result' | 'debugMessage'>, | ||
{ returnType }: AbiMessage, | ||
registry: Registry | ||
): { | ||
decodedOutput: string | ||
isError: boolean | ||
} { | ||
if (!result.isOk) return { decodedOutput: 'Error', isError: true } | ||
|
||
const isError = checkRevertFlag(result.asOk.flags) | ||
const returnValue = decodeReturnValue(returnType, result.asOk.data, registry) | ||
const outcome = extractOutcome(returnValue) | ||
const decodedOutput = isError | ||
? getOutcomeText(outcome) | ||
: getOutcomeText(outcome) || '<empty>' | ||
|
||
return { | ||
decodedOutput, | ||
isError | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.