Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/solid-zebras-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@mimicprotocol/test-ts": patch
"@mimicprotocol/lib-ts": patch
"@mimicprotocol/cli": patch
---

Ensure early returns in favor of else blocks
19 changes: 7 additions & 12 deletions packages/lib-ts/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export namespace environment {
*/
export function rawTokenPriceQuery(token: Token, timestamp: Date | null = null): Result<USD[], string> {
if (token.isUSD()) return Result.ok<USD[], string>([USD.fromI32(1)])
else if (!(token instanceof BlockchainToken)) return Result.err<USD[], string>('Price query not supported for token ' + token.toString())
if (!(token instanceof BlockchainToken)) return Result.err<USD[], string>('Price query not supported for token ' + token.toString())

const responseStr = _tokenPriceQuery(JSON.stringify(TokenPriceQuery.fromToken(changetype<BlockchainToken>(token), timestamp)))
const parsed = TokenPriceQueryResponse.fromJson<TokenPriceQueryResponse>(responseStr)
Expand Down Expand Up @@ -121,17 +121,12 @@ export namespace environment {
const sortedPrices = prices.sort((a: USD, b: USD) => a.compare(b))

const length = sortedPrices.length
let median: USD
if (length % 2 === 1) {
median = sortedPrices[length / 2]
} else {
const left = sortedPrices[length / 2 - 1]
const right = sortedPrices[length / 2]
const sum = left.plus(right)
median = sum.div(BigInt.fromI32(2))
}

return Result.ok<USD, string>(median)
if (length % 2 === 1) return Result.ok<USD, string>(sortedPrices[length / 2])

const left = sortedPrices[length / 2 - 1]
const right = sortedPrices[length / 2]
const sum = left.plus(right)
return Result.ok<USD, string>(sum.div(BigInt.fromI32(2)))
}

/**
Expand Down
8 changes: 5 additions & 3 deletions packages/lib-ts/src/helpers/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ export function normalizeScientificNotation(input: string): string {
if (newDecimalPos <= 0) {
const zeros = '0'.repeat(-newDecimalPos)
return sign + '0.' + zeros + fullDigits
} else if (newDecimalPos >= fullDigits.length) {
}

if (newDecimalPos >= fullDigits.length) {
const zeros = '0'.repeat(newDecimalPos - fullDigits.length)
return sign + fullDigits + zeros
} else {
return sign + fullDigits.substring(0, newDecimalPos) + '.' + fullDigits.substring(newDecimalPos)
}

return sign + fullDigits.substring(0, newDecimalPos) + '.' + fullDigits.substring(newDecimalPos)
}

export function isHex(str: string, strict: boolean = false): boolean {
Expand Down
9 changes: 3 additions & 6 deletions packages/lib-ts/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,9 @@ function format<T extends Stringable>(fmt: string, args: Array<T>): string {
const argsStr = args.map<string>(a => a.toString())
for (let i: i32 = 0, len: i32 = fmt.length; i < len; i++) {
if (i < len - 1 && fmt.charCodeAt(i) == 0x7b /* '{' */ && fmt.charCodeAt(i + 1) == 0x7d /* '}' */) {
if (argIndex >= argsStr.length) {
throw new Error('Too few arguments for format string: ' + fmt)
} else {
out += argsStr[argIndex++]
i++
}
if (argIndex >= argsStr.length) throw new Error('Too few arguments for format string: ' + fmt)
out += argsStr[argIndex++]
i++
} else {
out += fmt.charAt(i)
}
Expand Down
12 changes: 6 additions & 6 deletions packages/lib-ts/src/types/BigInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ export class BigInt extends Uint8Array {
return aIsNeg ? resultAbs.neg() : resultAbs
}
const cmp = BigInt.compare(this.abs(), other.abs())
if (cmp === 0) {
return BigInt.zero()
} else if (cmp > 0) {
if (cmp === 0) return BigInt.zero()

if (cmp > 0) {
const resultAbs = BigInt.subUnsigned(this.abs(), other.abs())
return this.isNegative() ? resultAbs.neg() : resultAbs
} else {
const resultAbs = BigInt.subUnsigned(other.abs(), this.abs())
return other.isNegative() ? resultAbs.neg() : resultAbs
}

const resultAbs = BigInt.subUnsigned(other.abs(), this.abs())
return other.isNegative() ? resultAbs.neg() : resultAbs
}

/**
Expand Down
15 changes: 5 additions & 10 deletions packages/test-ts/src/RunnerMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ export default class RunnerMock {
run(fnName = 'main'): void {
try {
const fn = this.instance.exports[fnName]
if (typeof fn === 'function') {
fn()
} else {
if (typeof fn !== 'function') {
const availableExports = Object.keys(this.instance.exports).join(', ')
throw Error(`No "${fnName}" found in exports. Available exports: ${availableExports}`)
}
fn()
} catch (error) {
throw Error(`Task Execution Error - ${error}`)
}
Expand Down Expand Up @@ -200,13 +199,9 @@ export default class RunnerMock {
}

private createMockFunction(functionName: string, mockValue: MockResponseValue): CallableFunction {
if (mockValue === 'log') {
return this.createLogFn(functionName)
} else if (this.isParameterizedResponse(mockValue)) {
return this.createParameterizedFunction(functionName, mockValue)
} else {
return this.createConstantFunction(String(mockValue))
}
if (mockValue === 'log') return this.createLogFn(functionName)
if (this.isParameterizedResponse(mockValue)) return this.createParameterizedFunction(functionName, mockValue)
return this.createConstantFunction(String(mockValue))
}

private isParameterizedResponse(value: unknown): value is ParameterizedResponse {
Expand Down
6 changes: 3 additions & 3 deletions packages/test-ts/src/run-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ function toIntents(intentsJson: string) {
if (intent.op == OpType.Swap) {
const { sourceChain, destinationChain } = intent as Swap
return { ...intent, sourceChain: Number(sourceChain), destinationChain: Number(destinationChain) }
} else {
const { chainId } = intent as Transfer | Call
return { ...intent, chainId: Number(chainId) }
}

const { chainId } = intent as Transfer | Call
return { ...intent, chainId: Number(chainId) }
})
}