Bitswap API

May 16, 2022 ยท View on GitHub

ipfs.bitswap.wantlist([options])

Returns the wantlist for your node

Parameters

None

Options

An optional object which may have the following keys:

NameTypeDefaultDescription
timeoutNumberundefinedA timeout in ms
signalAbortSignalundefinedCan be used to cancel any long running requests started as a result of this call

Returns

TypeDescription
Promise<CID[]>An array of CIDs currently in the wantlist

Example

const list = await ipfs.bitswap.wantlist()
console.log(list)
// [ CID('QmHash') ]

A great source of examples can be found in the tests for this API.

ipfs.bitswap.wantlistForPeer(peerId, [options])

Returns the wantlist for a connected peer

Parameters

NameTypeDefaultDescription
peerIdPeerIdA peer ID to return the wantlist for

Options

An optional object which may have the following keys:

NameTypeDefaultDescription
timeoutNumberundefinedA timeout in ms
signalAbortSignalundefinedCan be used to cancel any long running requests started as a result of this call

Returns

TypeDescription
Promise<CID[]>An array of CIDs currently in the wantlist

Example

const list = await ipfs.bitswap.wantlistForPeer(peerId)
console.log(list)
// [ CID('QmHash') ]

A great source of examples can be found in the tests for this API.

ipfs.bitswap.unwant(cids, [options])

Removes one or more CIDs from the wantlist

Parameters

NameTypeDescription
cidsA CID or Array of CIDsThe CIDs to remove from the wantlist

Options

An optional object which may have the following keys:

NameTypeDefaultDescription
timeoutNumberundefinedA timeout in ms
signalAbortSignalundefinedCan be used to cancel any long running requests started as a result of this call

Returns

TypeDescription
Promise<void>A promise that resolves once the request is complete

Example

let list = await ipfs.bitswap.wantlist()
console.log(list)
// [ CID('QmHash') ]

await ipfs.bitswap.unwant(cid)

list = await ipfs.bitswap.wantlist()
console.log(list)
// []

A great source of examples can be found in the tests for this API.

ipfs.bitswap.stat([options])

Show diagnostic information on the bitswap agent.

Note: bitswap.stat and stats.bitswap can be used interchangeably.

Parameters

None

Options

An optional object which may have the following keys:

NameTypeDefaultDescription
timeoutNumberundefinedA timeout in ms
signalAbortSignalundefinedCan be used to cancel any long running requests started as a result of this call

Returns

TypeDescription
Promise<Object>An object that contains information about the bitswap agent

The returned object contains the following keys:

  • provideBufLen is an integer.
  • wantlist (array of CIDs)
  • peers (array of PeerIds)
  • blocksReceived is a BigInt
  • dataReceived is a BigInt
  • blocksSent is a BigInt
  • dataSent is a BigInt
  • dupBlksReceived is a BigInt
  • dupDataReceived is a BigInt

Example

const stats = await ipfs.bitswap.stat()
console.log(stats)
// {
//   provideBufLen: 0,
//   wantlist: [ CID('QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM') ],
//   peers:
//    [ 'QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
//      'QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
//      'QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd' ],
//   blocksReceived: 0,
//   dataReceived: 0,
//   blocksSent: 0,
//   dataSent: 0,
//   dupBlksReceived: 0,
//   dupDataReceived: 0
// }

A great source of examples can be found in the tests for this API.