import * as R from "ramda" ; import * as fs from "fs-extra" ; import { LCDClientConfig , MnemonicKey , RawKey } from "@terra-money/terra.js" ; import { cli } from "cli-ux" ; type Fee = { gasLimit : number ; amount : { [ coin ] : number ; } ; } ; export type ContractConfig = { store : { fee : Fee ; } ; instantiation : { fee : Fee ; instantiateMsg : Record < string , any > ; } ; } ; type Config = { [ contract ] : ContractConfig ; } ; type GlobalConfig = { _base : ContractConfig ; [ contract ] : ContractConfig ; } ; export type ContractRef = { codeId : number ; contractAddresses : { [ key ] : string ; } ; } ; export type Refs = { [ network ] : { [ contract ] : ContractRef ; } ; } ; export const connection = ( networks ) => ( network ) => networks [ network ] . _connection || cli . error ( `network '${ network }' not found in config` ) ; export const loadConnections = ( path = `${ __dirname }/template/config.terrain.json` ) => connection ( fs . readJSONSync ( path ) ) ; export const config = ( allConfig ) => ( network , contract ) : ContractConfig => { const globalBaseConfig = ( allConfig . _global && allConfig . _global . _base ) || { } ; const globalContractConfig = ( allConfig . _global && allConfig . _global [ contract ] ) || { } ; const baseConfig = ( allConfig [ network ] && allConfig [ network ] . _base ) || { } ; const contractConfig = ( allConfig [ network ] && allConfig [ network ] [ contract ] ) || { } ; return [ allConfig . _global . _base , globalBaseConfig , globalContractConfig , baseConfig , contractConfig , ] . reduce ( R . mergeDeepRight ) as any ; } ; export const saveConfig = ( valuePath , value , path ) => { const conf = fs . readJSONSync ( path ) ; const updated = R . set ( R . lensPath ( valuePath ) , value , conf ) ; fs . writeJSONSync ( path , updated , { spaces : 2 } ) ; } ; export const loadConfig = ( path = `${ __dirname }/template/config.terrain.json` ) => config ( fs . readJSONSync ( path ) ) ; export const loadKeys = ( path = `${ __dirname }/template/keys.terrain.js` ) : { [ keyName ] : RawKey ; } => { const keys = require ( path ) ; return R . map ( ( w ) => w . privateKey ? new RawKey ( Buffer . from ( w . privateKey , "base64" ) ) : w . mnemonic ? new MnemonicKey ( w ) : cli . error ( "Error: Key must be defined with either `privateKey` or `mnemonic`" ) , keys ) ; } ; export const setCodeId = ( network , contract , codeId ) => R . set ( R . lensPath ( [ network , contract , "codeId" ] ) , codeId ) ; export const setContractAddress = ( network , contract , instanceId , contractAddress ) => R . set ( R . lensPath ( [ network , contract , "contractAddresses" , instanceId ] ) , contractAddress ) ; export const loadRefs = ( path = `${ __dirname }/template/refs.terrain.json` ) : Refs => fs . readJSONSync ( path ) ; export const saveRefs = ( refs , path ) => { fs . writeJSONSync ( path , refs , { spaces : 2 } ) ; } ;