import { ImplicationProofItem } from '@pigi/ovm' ; import { getLogger , logError } from '@pigi/core-utils' ; import * as assert from 'assert' ; import { PIGI_TOKEN_TYPE , RollupBlock , RollupBlockSubmitter , RollupStateSolver , SignedStateReceipt , State , StateReceipt , UNI_TOKEN_TYPE , UNISWAP_ADDRESS , UNISWAP_STORAGE_SLOT , } from '../../src' ; const log = getLogger ( 'helpers' , true ) ; export const AGGREGATOR_ADDRESS = '0xAc001762c6424F4959852A516368DBf970C835a7' ; export const UNISWAP_GENESIS_STATE_INDEX = UNISWAP_STORAGE_SLOT ; export const ALICE_GENESIS_STATE_INDEX = 1 ; export const ALICE_ADDRESS = '0xaaaf2795C3013711c240244aFF600aD9e8D9727D' ; export const BOB_ADDRESS = '0xbbbCAAe85dfE709a25545E610Dba4082f6D02D73' ; export const AGGREGATOR_MNEMONIC = 'rebel talent argue catalog maple duty file taxi dust hire funny steak' ; export const getGenesisState = ( aliceAddress = ALICE_ADDRESS ) : State [ ] => { return [ { pubkey : UNISWAP_ADDRESS , balances : { [ UNI_TOKEN_TYPE ] : 50 , [ PIGI_TOKEN_TYPE ] : 50 , } , } , { pubkey : aliceAddress , balances : { [ UNI_TOKEN_TYPE ] : 50 , [ PIGI_TOKEN_TYPE ] : 50 , } , } , { pubkey : AGGREGATOR_ADDRESS , balances : { [ UNI_TOKEN_TYPE ] : 1000000 , [ PIGI_TOKEN_TYPE ] : 1000000 , } , } , ] ; } ; export const getGenesisStateLargeEnoughForFees = ( aliceAddress = ALICE_ADDRESS ) : State [ ] => { return [ { pubkey : UNISWAP_ADDRESS , balances : { [ UNI_TOKEN_TYPE ] : 650000 , [ PIGI_TOKEN_TYPE ] : 650000 , } , } , { pubkey : aliceAddress , balances : { [ UNI_TOKEN_TYPE ] : 5000 , [ PIGI_TOKEN_TYPE ] : 5000 , } , } , { pubkey : AGGREGATOR_ADDRESS , balances : { [ UNI_TOKEN_TYPE ] : 1000000 , [ PIGI_TOKEN_TYPE ] : 1000000 , } , } , ] ; } ; param inputAmount param inputTokenLiquidity param outputTokenLiquidity param feeBasisPoints returns export const calculateSwapWithFees = ( inputAmount , inputTokenLiquidity , outputTokenLiquidity , feeBasisPoints ) : number => { const exchangeRate = outputTokenLiquidity / inputTokenLiquidity ; const expectedOutputBeforeFees = inputAmount * exchangeRate ; const volumeFeePct = expectedOutputBeforeFees / ( outputTokenLiquidity + expectedOutputBeforeFees ) ; const feePct = volumeFeePct + feeBasisPoints / 10000 ; return Math . floor ( expectedOutputBeforeFees - expectedOutputBeforeFees * feePct ) ; } ; export const assertThrows = ( func , errorType ? ) : void => { let succeeded = true ; try { func ( ) ; succeeded = false ; } catch ( e ) { if ( ! ! errorType && ! ( e instanceof errorType ) ) { succeeded = false ; logError ( log , `Threw wrong error. Expected ${ typeof errorType }` , e ) ; } } assert ( succeeded , "Function didn't throw as expected or threw the wrong error." ) ; } ; export const assertThrowsAsync = async ( func , errorType ? ) : Promise < void > => { let succeeded = true ; try { await func ( ) ; succeeded = false ; } catch ( e ) { if ( ! ! errorType && ! ( e instanceof errorType ) ) { succeeded = false ; } } assert ( succeeded , "Function didn't throw as expected or threw the wrong error." ) ; } ; export class DummyRollupStateSolver implements RollupStateSolver { private validityResult : boolean = true ; private fraudProof : ImplicationProofItem [ ] ; private storeErrorToThrow : Error ; public setFraudProof ( fraudProof ) { this . fraudProof = fraudProof ; } public setValidityResult ( validityResult ) { this . validityResult = validityResult ; } public setStoreErrorToThrow ( storeErrorToThrow ) { this . storeErrorToThrow = storeErrorToThrow ; } public async getFraudProof ( stateReceipt , signer ) { return this . fraudProof ; } public async isStateReceiptProvablyValid ( stateReceipt , signer ) { return this . validityResult ; } public async storeSignedStateReceipt ( signedReceipt ) { if ( ! ! this . storeErrorToThrow ) { throw this . storeErrorToThrow ; } } } export class DummyBlockSubmitter implements RollupBlockSubmitter { public submitedBlocks : RollupBlock [ ] = [ ] ; public async handleNewRollupBlock ( rollupBlockNumber ) { } public async submitBlock ( block ) { this . submitedBlocks . push ( block ) ; } public getLastConfirmed ( ) { return 0 ; } public getLastQueued ( ) { return 0 ; } public getLastSubmitted ( ) { return 0 ; } }