Skip to content

Commit

Permalink
fix(aqua-ipfs): Update example (#159)
Browse files Browse the repository at this point in the history
* Use aqua keyword, use FCLI

* Add new line

* Use js-client

* Fix index.ts

* Fix compile

* Remove any

* Use helia, update addr

* Return to ipfs-http-client

* Update README

* Use js-kubo-rpc-client

* Update README and comments

---------

Co-authored-by: Akim Mamedov <[email protected]>
  • Loading branch information
InversionSpaces and Akim Mamedov authored Jan 11, 2024
1 parent 55414f6 commit 32238f1
Show file tree
Hide file tree
Showing 6 changed files with 3,092 additions and 13,091 deletions.
10 changes: 5 additions & 5 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Run example on TestNet
While in `example` directory, `npm start` will run `index.ts` against Fluence TestNet
While in `example` directory, `npm run start` will run `index.ts` against Fluence TestNet

# Run example locally
1. Go to `local-network`
2. Run `docker compose up -d` to start Fluence cluster of 3 nodes
3. Go back to `../example`
4. Run `npm run start:local`
To run example locally:
1. Spin up local environment through [Fluence CLI](https://github.com/fluencelabs/cli)'s `local` command
2. Pass local fluence peers multi addresses to `main` in `index.ts`
3. (Optional) Change `IPFS_MULTIADDR` to address of your preferred IPFS node
3 changes: 2 additions & 1 deletion example/aqua/export.aqua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module Exports
aqua Exports

import put, get_from, set_timeout from "@fluencelabs/aqua-ipfs/ipfs-api.aqua"
export put, get_from, set_timeout
118 changes: 48 additions & 70 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,94 +13,72 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Fluence, testNet, Relay } from "@fluencelabs/js-client";

import { put, get_from, set_timeout } from "./generated/export";
import { Fluence } from "@fluencelabs/fluence";
import { Node, testNet } from "@fluencelabs/fluence-network-environment";
import { put, get_from, set_timeout } from "./generated/export.js";

const { create, urlSource } = require("ipfs-http-client");
const all = require("it-all");
const uint8ArrayConcat = require("uint8arrays/concat");
import { multiaddr } from "@multiformats/multiaddr";
import { create } from "kubo-rpc-client";
import all from "it-all";
import uint8ArrayConcat from "uint8arrays/concat.js";

let local: Node[] = [
{
peerId: "12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK",
multiaddr:
"/ip4/127.0.0.1/tcp/9990/ws/p2p/12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK",
},
{
peerId: "12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv",
multiaddr:
"/ip4/127.0.0.1/tcp/9991/ws/p2p/12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv",
},
{
peerId: "12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5",
multiaddr:
"/ip4/127.0.0.1/tcp/9992/ws/p2p/12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5",
},
];
// Multi address of the IPFS node
// we will work with through the Fluence Network
const IPFS_MULTIADDR = multiaddr("/dns4/ipfs.fluence.dev/tcp/5001");

async function main(environment: Node[]) {
// setLogLevel('DEBUG');
await Fluence.start({ connectTo: environment[1] });
console.log(
"πŸ“— created a fluence peer %s with relay %s",
Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId
);
/**
* @param environment - array of fluence network nodes (two are needed)
* @note Pass addresses of local nodes to experiment locally
*/
async function main(environment: Relay[]) {
const relay = environment[0];
const node = environment[1];

let ipfsAddr = "https://stage.fluence.dev:15001";
let ipfsMultiaddr =
"/ip4/134.209.186.43/tcp/5001/p2p/12D3KooWEhCqQ9NBnmtSfNeXSNfhgccmH86xodkCUxZNEXab6pkw";
const ipfs = create(ipfsAddr);
console.log("πŸ“— created ipfs client");
const ipfs = await create({ url: IPFS_MULTIADDR });
console.log("πŸ“— Created IPFS HTTP Client");

await ipfs.id();
console.log("πŸ“— connected to ipfs");
const content = "Hola, Fluence!";
const encoder = new TextEncoder();

let source = urlSource(
"https://images.adsttc.com/media/images/5ecd/d4ac/b357/65c6/7300/009d/large_jpg/02C.jpg?1590547607"
);
const file = await ipfs.add(source);
console.log("πŸ“— uploaded file:", file);
const added = await ipfs.add(encoder.encode(content));
console.log("πŸ“— Uploaded content, got CID:", added.cid.toString());

let stream = await ipfs.cat(added.path);
let data = uint8ArrayConcat(await all(stream));
const decoder = new TextDecoder();
console.log("πŸ“— Retrieved content: ", decoder.decode(data));

let files = await ipfs.get(file.cid);
for await (const file of files) {
const content = uint8ArrayConcat(await all(file.content));
console.log("πŸ“— downloaded file of length ", content.length);
}
await Fluence.connect(relay);
const client = Fluence.getClient();

// default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node
await set_timeout(environment[2].peerId, 10);
console.log(
"πŸ“— Created a Fluence Peer %s with Relay %s",
client.getPeerId(),
client.getRelayPeerId()
);

// default IPFS timeout is 1 sec,
// set to 10 secs to retrieve file from remote node
await set_timeout(node.peerId, 10);
console.log("πŸ“˜ Ipfs.set_timeout");

console.log("πŸ“˜ file hash: ", file.cid);
let getResult = await get_from(
environment[2].peerId,
file.cid.toString(),
ipfsMultiaddr,
{ ttl: 10000 }
node.peerId,
added.cid.toString(),
IPFS_MULTIADDR.toString(),
{ ttl: 20000 }
);
console.log("πŸ“˜ Ipfs.get", getResult);
console.log("πŸ“˜ Ipfs.get_from", getResult);

let putResult = await put(environment[2].peerId, getResult.path, {
ttl: 10000,
let putResult = await put(node.peerId, getResult.path, {
ttl: 20000,
});
console.log("πŸ“˜ Ipfs.put", putResult);

return;
}

let args = process.argv.slice(2);
var environment: Node[];
if (args.length >= 1 && args[0] == "local") {
environment = local;
console.log("πŸ“˜ Will connect to local nodes");
} else {
environment = testNet;
console.log("πŸ“˜ Will connect to testNet");
await ipfs.stop();
}

main(environment)
main(testNet)
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
Expand Down
Loading

0 comments on commit 32238f1

Please sign in to comment.