Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Partially Resolves #19
Browse files Browse the repository at this point in the history
  • Loading branch information
corrie-sloot authored and MantisClone committed Nov 9, 2022
1 parent 13aa2c1 commit 84fe69c
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ POST DBS_URI/register

```bash
npm install
<<<<<<< HEAD
```

## Run
Expand All @@ -189,6 +190,11 @@ export ACCEPTED_PAYMENTS=ethereum,matic
export NODE_RPC_URIS=default,default
export BUNDLR_URI="https://node1.bundlr.network"
#export BUNDLR_URI="https://devnet.bundlr.network" # Use Budnlr devnet when interacting with testnets
=======
export ACCEPTED_PAYMENTS=ethereum,matic,boba,boba-eth
export BUNDLR_URI="https://node1.bundlr.network"
#export BUNDLR_URI="https://devnet.bundlr.network"
>>>>>>> 03f4b2f (Partially Resolves #19)
export PORT=8081
export PRIVATE_KEY="0000000000000000000000000000000000000000000000000000000000000000"
export SQLITE_DB_PATH=/path/to/db/file
Expand Down Expand Up @@ -262,6 +268,7 @@ curl -d '{ "type":"arweave", "userAddress": "0x000000000000000000000000000000000
curl -d '{ "quoteId":"60f7d48ccd08653b2ef2edfe4bbe4620", "signature": "0x0000000000000000000000000000000000000000", "files": ["https://example.com/", "ipfs://xxx"], "nonce": 0 }' -X POST -H 'Content-Type: application/json' http://localhost:8081/upload

curl -d '{ "type":"arweave", "userAddress": "0x0000000000000000000000000000000000000000", "files": [{"length": 1256}, {"length": 5969}], "payment": {"chainId": 80001, "tokenAddress": "0x0000000000000000000000000000000000001010"} }' -X POST -H 'Content-Type: application/json' http://localhost:8081/getQuote
<<<<<<< HEAD
curl -d '{ "quoteId":"40acc6937e1bd98631f47e7cbda72920", "signature": "0x0000000000000000000000000000000000000000", "files": ["https://example.com/", "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"], "nonce": 0 }' -X POST -H 'Content-Type: application/json' http://localhost:8081/upload
curl 'http://localhost:8081/getStatus?quoteId=40acc6937e1bd98631f47e7cbda72920'
curl 'http://localhost:8081/getLink?quoteId=40acc6937e1bd98631f47e7cbda72920&signature=0x0000000000000000000000000000000000000000&nonce=0'
Expand All @@ -287,3 +294,7 @@ npm start
curl -d '{ "type":"arweave", "userAddress": "0x0000000000000000000000000000000000000000", "files": [{"length": 100}], "payment": {"chainId": 137, "tokenAddress": "0x0000000000000000000000000000000000000000"} }' -X POST -H 'Content-Type: application/json' http://localhost:8081/getQuote
```
>>>>>>> 1988b4e (Framework + getQuote endpoint)
=======
curl -d '{ "quoteId":"047a6425546f8e9023e8af0ab47ba99f", "signature": "0x0000000000000000000000000000000000000000", "files": ["https://example.com/", "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"], "nonce": 0 }' -X POST -H 'Content-Type: application/json' http://localhost:8081/upload
```
>>>>>>> 03f4b2f (Partially Resolves #19)
107 changes: 107 additions & 0 deletions app/controllers/quote.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const Bundlr = require("@bundlr-network/client");
const crypto = require("crypto");
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> 03f4b2f (Partially Resolves #19)
const ethers = require('ethers');

const Quote = require("../models/quote.model.js");
Expand Down Expand Up @@ -581,10 +584,37 @@ exports.getLink = async (req, res) => {
return;
}

<<<<<<< HEAD
const bundlr = new Bundlr.default(process.env.ARWEAVE_GATEWAY_URI, paymentToken, process.env.PRIVATE_KEY);

const priceWei = await bundlr.getPrice(totalLength);
const price = bundlr.utils.unitConverter(priceWei);
=======
let bundlr;
try {
bundlr = new Bundlr.default(process.env.BUNDLR_URI, paymentToken.name, process.env.PRIVATE_KEY, paymentToken.providerUrl ? {providerUrl: paymentToken.providerUrl, contractAddress: paymentToken.tokenAddress} : {});
}
catch(err) {
res.status(500).send({
message: err.message
});
return;
}

let priceWei;
try {
priceWei = await bundlr.getPrice(totalLength);
priceWei = ethers.BigNumber.from(priceWei.toString()); // need to convert so we can add buffer
}
catch(err) {
res.status(500).send({
message: err.message
});
return;
}

const tokenAmount = priceWei.add(priceWei.div(10)); // add 10% buffer since prices fluctuate
>>>>>>> 03f4b2f (Partially Resolves #19)

const quoteId = crypto.randomBytes(16).toString("hex");
const data = {
Expand All @@ -595,6 +625,83 @@ exports.getLink = async (req, res) => {
"quoteId": quoteId
};

<<<<<<< HEAD
res.send(data);
>>>>>>> 1988b4e (Framework + getQuote endpoint)
};
=======
// save data in database
const quote = new Quote({
quoteId: quoteId,
status: Quote.QUOTE_STATUS_WAITING,
created: Date.now(),
chainId: chainId,
tokenAddress: tokenAddress,
userAddress: userAddress,
tokenAmount: tokenAmount.toString(),
approveAddress: "0x0000000000000000000000000000000000000000", // TODO: replace with real address
files: file_lengths

});

// Save Reading in the database
Quote.create(quote, (err, data) => {
if(err) {
res.status(500).send({
message:
err.message || "Error occurred while creating the quote."
});
}
else {
// send receipt for data
res.send(data);
}
});
};

exports.status = async (req, res) => {
const quoteidRegex = /^[a-fA-F0-9]{32}$/;

if(!req.query || !req.query.quoteId) {
res.status(400).send({
message: "Error, quoteId required."
});
return;
}
const quoteId = req.query.quoteId;

if(!quoteidRegex.test(quoteId)) {
res.status(400).send({
message: "Invalid quoteId format."
});
return;
}

Quote.getStatus(quoteId, (err, data) => {
if(err) {
if(err.code == 404) {
res.status(404).send({
status: 0
});
return;
}
res.status(500).send({
message:
err.message || "Error occurred while looking up status."
});
}
else {
// send receipt for data
res.send(data);
}
});
};

exports.setStatus = async (quoteId, status) => {
Quote.setStatus(quoteId, status, (err, data) => {
if(err) {
console.log(err);
}
});
};
>>>>>>> 03f4b2f (Partially Resolves #19)
12 changes: 12 additions & 0 deletions app/controllers/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<<<<<<< HEAD
// TODO: Update `confirms` fields
const tokens =[
<<<<<<< HEAD
// Mainnets, used with public Bundlr URIs. See for details https://docs.bundlr.network/docs/bundlers
{bundlrName: "ethereum", chainId: 1, symbol: "ETH", providerUrl: "https://cloudflare-eth.com/", tokenAddress: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", confirms: 1},
{bundlrName: "matic", chainId: 137, symbol: "MATIC", providerUrl: "https://polygon-rpc.com", tokenAddress: "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270", confirms: 30},
Expand All @@ -12,6 +13,17 @@ const tokens =[
// Testnets, used with devnet Bundlr URI. See for details: https://docs.bundlr.network/docs/devnet
{bundlrName: "matic", chainId: 80001, symbol: "MATIC", providerUrl: "https://rpc-mumbai.maticvigil.com/", tokenAddress: "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889", confirms: 1},
{bundlrName: "ethereum", chainId: 5, symbol: "ETH", providerUrl: "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161", tokenAddress: "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", confirms: 1},
=======
{name: "ethereum", chainId: 1, tokenAddress: "0x0000000000000000000000000000000000000000", symbol: "ETH"},
{name: "matic", chainId: 137, tokenAddress: "0x0000000000000000000000000000000000000000", symbol: "MATIC"},
{name: "bnb", chainId: 56, tokenAddress: "0x0000000000000000000000000000000000000000", symbol: "BNB"},
{name: "arbitrum", chainId: 42161, tokenAddress: "0x0000000000000000000000000000000000000000", symbol: "ETH"},
{name: "avalanche", chainId: 43114, tokenAddress: "0x0000000000000000000000000000000000000000", symbol: "AVAX"},
{name: "boba", chainId: 288, tokenAddress: "0xa18bF3994C0Cc6E3b63ac420308E5383f53120D7", symbol: "ETH"},
{name: "boba-eth", chainId: 288, tokenAddress: "0x0000000000000000000000000000000000000000", symbol: "BOBA"},

{name: "matic", chainId: 80001, tokenAddress: "0x0000000000000000000000000000000000001010", providerUrl: "https://rpc-mumbai.maticvigil.com/"}
>>>>>>> 03f4b2f (Partially Resolves #19)
];

getToken = (chainId, tokenAddress) => {
Expand Down
Loading

0 comments on commit 84fe69c

Please sign in to comment.