import frontMatter from 'front-matter' ; import { Abi , AbiAction , ContractMetadata , RicardianContractConfig , SpecVersion , Transaction , TransactionAction } from '../interfaces' ; import { RicardianContractRenderError } from '../RicardianContractRenderError' ; param actionIndex returns export function getTransactionAction ( transaction , actionIndex ) { const action = transaction . actions [ actionIndex ] ; return action ; } return export function getContractTextFromAbi ( abi , action ) { const abiAction = abi . actions . find ( ( aa ) => aa . name === action . name ) ; if ( ! abiAction ) { throw new RicardianContractRenderError ( 'Matching ABI not provided for given action.' ) ; } return abiAction . ricardian_contract ; } param contractText return export function getMetadataAndContent ( contractText ) { try { const { attributes : metadata , body : content } = frontMatter ( contractText ) ; return { metadata , content } ; } catch ( e ) { throw new RicardianContractRenderError ( e . message ) ; } } export function getContractSpecVersion ( config ) { const versionMatcher = /^([0-9]+)\.([0-9]+)\.([0-9]+)$/ ; const action = getTransactionAction ( config . transaction , config . actionIndex ) ; const contractText = getContractTextFromAbi ( config . abi , action ) ; const { metadata } = getMetadataAndContent ( contractText ) ; const versionStr = metadata . spec_version ; if ( ! versionStr ) { return { major : 0 , minor : 0 , patch : 0 , } ; } const parts = versionMatcher . exec ( versionStr ) ; if ( parts ) { return { major : parseInt ( parts [ 1 ] , 10 ) , minor : parseInt ( parts [ 2 ] , 10 ) , patch : parseInt ( parts [ 3 ] , 10 ) , } ; } else { throw new RicardianContractRenderError ( 'Unable to determine contract specification version' ) ; } }