import { Err , Ok , Result } from '@hqoss/monads' ; import BN from 'bn.js' ; import { utils } from 'near-api-js' ; import { bytesToHex } from './utils.js' ; abstract class Assignable { encode ( ) { return utils . serialize . serialize ( SCHEMA , this ) ; } } export class BeginBlockArgs extends Assignable { constructor ( public readonly hash , public readonly coinbase , public readonly timestamp , public readonly number , public readonly difficulty , public readonly gaslimit ) { super ( ) ; } } export class BeginChainArgs extends Assignable { constructor ( public chainID ) { super ( ) ; } } export class SubmitResult { public readonly result : SubmitResultV2 | SubmitResultV1 | LegacyExecutionResult ; constructor ( result ) { this . result = result ; } output ( ) { switch ( this . result . kind ) { case 'SubmitResultV2' : if ( this . result . status . success ) { return Ok ( Buffer . from ( this . result . status . success . output ) ) ; } else if ( this . result . status . revert ) { return Err ( this . result . status . revert ) ; } else if ( this . result . status . outOfFund ) { return Err ( this . result . status . outOfFund ) ; } else if ( this . result . status . outOfGas ) { return Err ( this . result . status . outOfGas ) ; } else if ( this . result . status . outOfOffset ) { return Err ( this . result . status . outOfOffset ) ; } else if ( this . result . status . callTooDeep ) { return Err ( this . result . status . callTooDeep ) ; } else { return Err ( 'LegacyStatusFalse' ) ; } case 'SubmitResultV1' : if ( this . result . status . success ) { return Ok ( Buffer . from ( this . result . status . success . output ) ) ; } else if ( this . result . status . revert ) { return Err ( this . result . status . revert ) ; } else if ( this . result . status . outOfFund ) { return Err ( this . result . status . outOfFund ) ; } else if ( this . result . status . outOfGas ) { return Err ( this . result . status . outOfGas ) ; } else if ( this . result . status . outOfOffset ) { return Err ( this . result . status . outOfOffset ) ; } else if ( this . result . status . callTooDeep ) { return Err ( this . result . status . callTooDeep ) ; } else { return Err ( 'LegacyStatusFalse' ) ; } case 'LegacyExecutionResult' : if ( this . result . status ) { return Ok ( this . result . output ) ; } else { return Err ( 'LegacyStatusFalse' ) ; } } } static decode ( input ) { if ( input [ 0 ] == SubmitResultV2 . VERSION ) { const v2 = SubmitResultV2 . decode ( input ) ; return new SubmitResult ( v2 ) ; } try { const v1 = SubmitResultV1 . decode ( input ) ; return new SubmitResult ( v1 ) ; } catch ( error ) { const legacy = LegacyExecutionResult . decode ( input ) ; return new SubmitResult ( legacy ) ; } } } export type LegacyStatusFalse = 'LegacyStatusFalse' ; export type ExecutionError = RevertStatus | OutOfGas | OutOfFund | OutOfOffset | CallTooDeep | LegacyStatusFalse ; export class SuccessStatus extends utils . enums . Assignable { public readonly output : Uint8Array | number [ ] ; constructor ( args ) { super ( args ) ; this . output = Buffer . from ( args . output ) ; } } export class RevertStatus extends utils . enums . Assignable { public readonly output : Uint8Array | number [ ] ; constructor ( args ) { super ( args ) ; this . output = Buffer . from ( args . output ) ; } } export class OutOfGas extends utils . enums . Assignable { } export class OutOfFund extends utils . enums . Assignable { } export class OutOfOffset extends utils . enums . Assignable { } export class CallTooDeep extends utils . enums . Assignable { } export class TransactionStatus extends utils . enums . Enum { public readonly success ? : SuccessStatus ; public readonly revert ? : RevertStatus ; public readonly outOfGas ? : OutOfGas ; public readonly outOfFund ? : OutOfFund ; public readonly outOfOffset ? : OutOfOffset ; public readonly callTooDeep ? : CallTooDeep ; static decode ( input ) { return utils . serialize . deserialize ( SCHEMA , TransactionStatus , input ) ; } } export class SubmitResultV2 extends Assignable { kind : 'SubmitResultV2' = 'SubmitResultV2' ; public static VERSION : 7 = 7 ; public readonly version : 7 ; public readonly status : TransactionStatus ; public readonly gasUsed : number | bigint ; public readonly logs : LogEventWithAddress [ ] ; constructor ( args ) { super ( ) ; this . version = 7 ; this . status = args . status ; this . gasUsed = BigInt ( args . gasUsed . toString ( ) ) ; this . logs = args . logs ; } static decode ( input ) { return utils . serialize . deserialize ( SCHEMA , SubmitResultV2 , input ) ; } } export class SubmitResultV1 extends Assignable { kind : 'SubmitResultV1' = 'SubmitResultV1' ; public readonly status : TransactionStatus ; public readonly gasUsed : number | bigint ; public readonly logs : LogEvent [ ] ; constructor ( args ) { super ( ) ; this . status = args . status ; this . gasUsed = BigInt ( args . gasUsed . toString ( ) ) ; this . logs = args . logs ; } static decode ( input ) { return utils . serialize . deserialize ( SCHEMA , SubmitResultV1 , input ) ; } } export class LegacyExecutionResult extends Assignable { kind : 'LegacyExecutionResult' = 'LegacyExecutionResult' ; public readonly status : boolean ; public readonly gasUsed : number | bigint ; public readonly output : Uint8Array ; public readonly logs : LogEvent [ ] ; constructor ( args ) { super ( ) ; this . status = Boolean ( args . status ) ; this . gasUsed = BigInt ( args . gasUsed . toString ( ) ) ; this . output = Buffer . from ( args . output ) ; this . logs = args . logs ; } static decode ( input ) { return utils . serialize . deserialize ( SCHEMA , LegacyExecutionResult , input ) ; } } export class FunctionCallArgs extends Assignable { constructor ( public contract , public input ) { super ( ) ; } } export class GetChainID extends Assignable { constructor ( ) { super ( ) ; } } export class GetStorageAtArgs extends Assignable { constructor ( public address , public key ) { super ( ) ; } } export class LogEventWithAddress extends Assignable { public readonly address : Uint8Array ; public readonly topics : RawU256 [ ] ; public readonly data : Uint8Array ; constructor ( args ) { super ( ) ; this . address = Buffer . from ( args . address ) ; this . topics = args . topics ; this . data = Buffer . from ( args . data ) ; } } export class LogEvent extends Assignable { public readonly topics : RawU256 [ ] ; public readonly data : Uint8Array ; constructor ( args ) { super ( ) ; this . topics = args . topics ; this . data = Buffer . from ( args . data ) ; } } export class MetaCallArgs extends Assignable { constructor ( public readonly signature , public readonly v , public readonly nonce , public readonly feeAmount , public readonly feeAddress , public readonly contractAddress , public readonly value , public readonly methodDef , public readonly args ) { super ( ) ; } } export class NewCallArgs extends Assignable { constructor ( public readonly chainID , public readonly ownerID , public readonly bridgeProverID , public readonly upgradeDelayBlocks ) { super ( ) ; } } export class FungibleTokenMetadata extends Assignable { constructor ( public readonly spec , public readonly name , public readonly symbol , public readonly icon , public readonly reference , public readonly reference_hash , public readonly decimals ) { super ( ) ; } static default ( ) { return new FungibleTokenMetadata ( 'ft-1.0.0' , 'Ether' , 'ETH' , 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs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gyBQzhiE1wkDOKwiMsuSr6BJNkBFAENEU45DIyo9nwGGxNs44ERAY5QlxmQsxRcYAIcxMdKubtmS3RVOe7u3Hjx/qKsKXMUAQA0EiKbdKj2XJAiEC2717t/p0M2QUEETaw0so7LREgVCO8l4Sj0HLOCAIB+81FMYSAUIZQmGSkybKSCAs1I7MCseyRIEwaveSJwtDRgJBR48e9RwKewXC+0x0AdtUGQsEMSL3cnMaL0B4j1wWc/Qmy2ggzG/ruXg3ENq8AmHgyCSZyTIaCLp06VLce8DHA8LrrGDxMnEVtowHgjZt2hR1QguLB4R0Su/evdXZzJYVQJBe25UoELK4Nv1PQ2uAPHv2LKo/iQaEv0mNeFn4bYqsAYL4p5IsGfIChOfMb7Dp1CZZBQTRQiJDYTcgerrWNlkHhHVbkV1XJBAemXDirqe2yTog6Ny5c9LJayhOIBgrS1h1b6OsBIKocB0KO4FwtwVu7WSrrAWC9NouDYQsLstCbZbVQNjmwCwjQFjCwzTuqVOn1Lt2ymogiBk/PafOfbdsl/VAEEBs+gfEsZQhgDChxVKgjKAMASQjKROIYcoEYpgygRglIf4D6lp/+XognSwAAAAASUVORK5CYII=' , null , null , 18 ) ; } } export class InitCallArgs extends Assignable { constructor ( public readonly prover_account , public readonly eth_custodian_address , public readonly metadata ) { super ( ) ; } } export class RawU256 extends Assignable { public readonly value : Uint8Array ; constructor ( args ? ) { super ( ) ; if ( ! args ) { this . value = Buffer . alloc ( 32 ) ; } else { const bytes = Buffer . from ( args instanceof Uint8Array ? args : args . value ) ; this . value = bytes ; } } toBytes ( ) { return this . value ; } toString ( ) { return bytesToHex ( this . value ) ; } } export class ViewCallArgs extends Assignable { constructor ( public readonly sender , public readonly address , public readonly amount , public readonly input ) { super ( ) ; } } const SCHEMA = new Map < Function , any > ( [ [ BeginBlockArgs , { kind : 'struct' , fields : [ [ 'hash' , [ 32 ] ] , [ 'coinbase' , [ 32 ] ] , [ 'timestamp' , [ 32 ] ] , [ 'number' , [ 32 ] ] , [ 'difficulty' , [ 32 ] ] , [ 'gaslimit' , [ 32 ] ] , ] , } , ] , [ BeginChainArgs , { kind : 'struct' , fields : [ [ 'chainID' , [ 32 ] ] ] } ] , [ TransactionStatus , { kind : 'enum' , field : 'enum' , values : [ [ 'success' , SuccessStatus ] , [ 'revert' , RevertStatus ] , [ 'outOfGas' , OutOfGas ] , [ 'outOfFund' , OutOfFund ] , [ 'outOfOffset' , OutOfOffset ] , [ 'callTooDeep' , CallTooDeep ] , ] , } , ] , [ SuccessStatus , { kind : 'struct' , fields : [ [ 'output' , [ 'u8' ] ] ] , } , ] , [ RevertStatus , { kind : 'struct' , fields : [ [ 'output' , [ 'u8' ] ] ] , } , ] , [ OutOfGas , { kind : 'struct' , fields : [ ] , } , ] , [ OutOfFund , { kind : 'struct' , fields : [ ] , } , ] , [ OutOfOffset , { kind : 'struct' , fields : [ ] , } , ] , [ CallTooDeep , { kind : 'struct' , fields : [ ] , } , ] , [ SubmitResultV2 , { kind : 'struct' , fields : [ [ 'version' , 'u8' ] , [ 'status' , TransactionStatus ] , [ 'gasUsed' , 'u64' ] , [ 'logs' , [ LogEventWithAddress ] ] , ] , } , ] , [ SubmitResultV1 , { kind : 'struct' , fields : [ [ 'status' , TransactionStatus ] , [ 'gasUsed' , 'u64' ] , [ 'logs' , [ LogEvent ] ] , ] , } , ] , [ LegacyExecutionResult , { kind : 'struct' , fields : [ [ 'status' , 'u8' ] , [ 'gasUsed' , 'u64' ] , [ 'output' , [ 'u8' ] ] , [ 'logs' , [ LogEvent ] ] , ] , } , ] , [ FunctionCallArgs , { kind : 'struct' , fields : [ [ 'contract' , [ 20 ] ] , [ 'input' , [ 'u8' ] ] , ] , } , ] , [ GetChainID , { kind : 'struct' , fields : [ ] } ] , [ GetStorageAtArgs , { kind : 'struct' , fields : [ [ 'address' , [ 20 ] ] , [ 'key' , [ 32 ] ] , ] , } , ] , [ LogEventWithAddress , { kind : 'struct' , fields : [ [ 'address' , [ 20 ] ] , [ 'topics' , [ RawU256 ] ] , [ 'data' , [ 'u8' ] ] , ] , } , ] , [ LogEvent , { kind : 'struct' , fields : [ [ 'topics' , [ RawU256 ] ] , [ 'data' , [ 'u8' ] ] , ] , } , ] , [ MetaCallArgs , { kind : 'struct' , fields : [ [ 'signature' , [ 64 ] ] , [ 'v' , 'u8' ] , [ 'nonce' , [ 32 ] ] , [ 'feeAmount' , [ 32 ] ] , [ 'feeAddress' , [ 20 ] ] , [ 'contractAddress' , [ 20 ] ] , [ 'value' , [ 32 ] ] , [ 'methodDef' , 'string' ] , [ 'args' , [ 'u8' ] ] , ] , } , ] , [ NewCallArgs , { kind : 'struct' , fields : [ [ 'chainID' , [ 32 ] ] , [ 'ownerID' , 'string' ] , [ 'bridgeProverID' , 'string' ] , [ 'upgradeDelayBlocks' , 'u64' ] , ] , } , ] , [ FungibleTokenMetadata , { kind : 'struct' , fields : [ [ 'spec' , 'string' ] , [ 'name' , 'string' ] , [ 'symbol' , 'string' ] , [ 'icon' , { kind : 'option' , type : 'string' } ] , [ 'reference' , { kind : 'option' , type : 'string' } ] , [ 'reference_hash' , { kind : 'option' , type : [ 32 ] } ] , [ 'decimals' , 'u8' ] , ] , } , ] , [ InitCallArgs , { kind : 'struct' , fields : [ [ 'prover_account' , 'string' ] , [ 'eth_custodian_address' , 'string' ] , [ 'metadata' , FungibleTokenMetadata ] , ] , } , ] , [ ViewCallArgs , { kind : 'struct' , fields : [ [ 'sender' , [ 20 ] ] , [ 'address' , [ 20 ] ] , [ 'amount' , [ 32 ] ] , [ 'input' , [ 'u8' ] ] , ] , } , ] , [ RawU256 , { kind : 'struct' , fields : [ [ 'value' , [ 32 ] ] ] } ] , ] ) ;