import { ContextInterface } from '@textile/context' ; import { GrpcConnection } from '@textile/grpc-connection' ; import { AddressesRequest , AddressesResponse , AddrInfo , BalanceRequest , BalanceResponse , CidInfo as _CidInfo , CidInfoRequest , CidInfoResponse , CidSummary as _CidSummary , CidSummaryRequest , CidSummaryResponse , ColdConfig as _ColdConfig , ColdInfo as _ColdInfo , DealError as _DealError , DealInfo as _DealInfo , DealRecordsConfig as _DealRecordsConfig , FilConfig as _FilConfig , FilInfo as _FilInfo , FilRenew as _FilRenew , FilStorage as _FilStorage , HotConfig as _HotConfig , HotInfo as _HotInfo , IpfsConfig as _IpfsConfig , IpfsHotInfo as _IpfsHotInfo , JobStatus as _JobStatus , JobStatusMap , RetrievalDealInfo as _RetrievalDealInfo , RetrievalDealRecord as _RetrievalDealRecord , RetrievalDealRecordsRequest , RetrievalDealRecordsResponse , StorageConfig as _StorageConfig , StorageDealRecord as _StorageDealRecord , StorageDealRecordsRequest , StorageDealRecordsResponse , StorageInfo as _StorageInfo , StorageJob as _StorageJob , } from '@textile/grpc-powergate-client/dist/powergate/user/v1/user_pb' ; import { UserService } from '@textile/grpc-powergate-client/dist/powergate/user/v1/user_pb_service' ; import log from 'loglevel' ; import { AddressInfo , CidInfo , CidSummary , ColdConfig , ColdInfo , DealError , DealRecordsConfig , FilConfig , FilInfo , FilRenew , FilStorage , HotConfig , HotInfo , IpfsConfig , IpfsHotInfo , JobStatus , RetrievalDealInfo , RetrievalDealRecord , StorageConfig , StorageDealInfo , StorageDealRecord , StorageInfo , StorageJob , } from '../types' ; const logger = log . getLogger ( 'filecoin-api' ) ; function fromPbAddressInfo ( item ) { return { ... item , balance : BigInt ( item . balance ) , } ; } function fromPbIpfsConfig ( item ) { return { ... item } ; } function fromPbHotConfig ( item ) { return { ... item , ipfs : item . ipfs ? fromPbIpfsConfig ( item . ipfs ) : undefined , } ; } function fromPbFilRenew ( item ) { return { ... item } ; } function fromPbFilConfig ( item ) { return { ... item , countryCodes : item . countryCodesList , excludedMiners : item . excludedMinersList , trustedMiners : item . trustedMinersList , renew : item . renew ? fromPbFilRenew ( item . renew ) : undefined , } ; } function fromPbColdConfig ( item ) { return { ... item , filecoin : item . filecoin ? fromPbFilConfig ( item . filecoin ) : undefined , } ; } function fromPbStorageConfig ( item ) { return { ... item , cold : item . cold ? fromPbColdConfig ( item . cold ) : undefined , hot : item . hot ? fromPbHotConfig ( item . hot ) : undefined , } ; } function fromPbIpfsHotInfo ( item ) { return { ... item , created : new Date ( item . created / 1000000 ) , } ; } function fromPbHotInfo ( item ) { return { ... item , ipfs : item . ipfs ? fromPbIpfsHotInfo ( item . ipfs ) : undefined , } ; } function fromPbFilStorage ( item ) { return { ... item } ; } function fromPbFilInfo ( item ) { return { ... item , proposals : item . proposalsList . map ( fromPbFilStorage ) , } ; } function fromPbColdInfo ( item ) { return { ... item , filecoin : item . filecoin ? fromPbFilInfo ( item . filecoin ) : undefined , } ; } function fromPbStorageInfo ( item ) { return { ... item , created : new Date ( item . created / 1000000 ) , cold : item . cold ? fromPbColdInfo ( item . cold ) : undefined , hot : item . hot ? fromPbHotInfo ( item . hot ) : undefined , } ; } function fromPbDealInfo ( item ) { return { ... item } ; } function fromPbDealError ( item ) { return { ... item } ; } function fromPbJobStatus ( item ) { switch ( item ) { case _JobStatus . JOB_STATUS_CANCELED : return JobStatus . Canceled ; case _JobStatus . JOB_STATUS_EXECUTING : return JobStatus . Executing ; case _JobStatus . JOB_STATUS_FAILED : return JobStatus . Failed ; case _JobStatus . JOB_STATUS_QUEUED : return JobStatus . Queued ; case _JobStatus . JOB_STATUS_SUCCESS : return JobStatus . Success ; case _JobStatus . JOB_STATUS_UNSPECIFIED : return JobStatus . Unspecified ; } } function fromPbStorageJob ( item ) { return { ... item , createdAt : new Date ( item . createdAt * 1000 ) , status : fromPbJobStatus ( item . status ) , dealInfo : item . dealInfoList . map ( fromPbDealInfo ) , dealErrors : item . dealErrorsList . map ( fromPbDealError ) , } ; } function fromPbCidSummary ( item ) { return { ... item , queuedJobs : item . queuedJobsList , executingJob : item . executingJob != '' ? item . executingJob : undefined , } ; } function fromPbCidInfo ( item ) { return { ... item , latestStorageConfig : item . latestPushedStorageConfig ? fromPbStorageConfig ( item . latestPushedStorageConfig ) : undefined , currentStorageInfo : item . currentStorageInfo ? fromPbStorageInfo ( item . currentStorageInfo ) : undefined , queuedStorageJobs : item . queuedStorageJobsList . map ( fromPbStorageJob ) , executingStorageJob : item . executingStorageJob ? fromPbStorageJob ( item . executingStorageJob ) : undefined , } ; } function fromPbStorageDealRecord ( item ) { return { ... item , time : new Date ( item . time * 1000 ) , dealInfo : item . dealInfo ? fromPbDealInfo ( item . dealInfo ) : undefined , } ; } function fromPbRetrievalDealInfo ( item ) { return { ... item } ; } function fromPbRetrievalDealRecord ( item ) { return { ... item , time : new Date ( item . time * 1000 ) , dealInfo : item . dealInfo ? fromPbRetrievalDealInfo ( item . dealInfo ) : undefined , } ; } internal export async function addresses ( api , ctx ? ) { logger . debug ( 'addresses request' ) ; const res = await api . unary ( UserService . Addresses , new AddressesRequest ( ) , ctx ) ; return res . toObject ( ) . addressesList . map ( fromPbAddressInfo ) ; } internal export async function balance ( api , address , ctx ? ) { const req = new BalanceRequest ( ) ; req . setAddress ( address ) ; const res = await api . unary ( UserService . Balance , req , ctx ) ; return BigInt ( res . getBalance ( ) ) ; } internal export async function cidSummary ( api , ctx ? , ... cids ) { const req = new CidSummaryRequest ( ) ; req . setCidsList ( cids ) ; const res = await api . unary ( UserService . CidSummary , req , ctx ) ; return res . toObject ( ) . cidSummaryList . map ( fromPbCidSummary ) ; } internal export async function cidInfo ( api , ctx ? , cid ? ) { const req = new CidInfoRequest ( ) ; if ( cid ) { req . setCid ( cid ) ; } const res = await api . unary ( UserService . CidInfo , req , ctx ) ; const info = res . toObject ( ) . cidInfo ; if ( info ) { return fromPbCidInfo ( info ) ; } else { throw new Error ( "didn't receive cid info in response" ) ; } } internal export async function storageDealRecords ( api , config , ctx ? ) { const c = new _DealRecordsConfig ( ) ; c . setAscending ( config . ascending ) ; c . setDataCidsList ( config . dataCids ) ; c . setFromAddrsList ( config . fromAddrs ) ; c . setIncludeFinal ( config . includeFinal ) ; c . setIncludePending ( config . includePending ) ; const req = new StorageDealRecordsRequest ( ) ; req . setConfig ( c ) ; const res = await api . unary ( UserService . StorageDealRecords , req , ctx ) ; return res . toObject ( ) . recordsList . map ( fromPbStorageDealRecord ) ; } internal export async function retrievalDealRecords ( api , config , ctx ? ) { const c = new _DealRecordsConfig ( ) ; c . setAscending ( config . ascending ) ; c . setDataCidsList ( config . dataCids ) ; c . setFromAddrsList ( config . fromAddrs ) ; c . setIncludeFinal ( config . includeFinal ) ; c . setIncludePending ( config . includePending ) ; const req = new RetrievalDealRecordsRequest ( ) ; req . setConfig ( c ) ; const res = await api . unary ( UserService . RetrievalDealRecords , req , ctx ) ; return res . toObject ( ) . recordsList . map ( fromPbRetrievalDealRecord ) ; }