Skip to content

Commit

Permalink
get average storage price feature for payment
Browse files Browse the repository at this point in the history
  • Loading branch information
rykci committed Jun 30, 2022
1 parent d9b1fab commit 0b3feaa
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sdk/helper/lockToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const lockToken = async (apiUrl, web3, payer, wCid, amount, size) => {
const approveTx = await USDCInstance.methods
.approve(
gatewayContractAddress,
web3.utils.toWei((amount * multiplyFactor).toString(), 'ether'),
(web3.utils.toWei(amount, 'ether') * multiplyFactor).toString(),
)
.send(optionsObj)

Expand Down
43 changes: 43 additions & 0 deletions sdk/helper/mcsApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,48 @@ const getDealList = async (
}
}

const sendRequest = async (apiLink) => {
try {
const response = await axios.get(apiLink)
return response.data
} catch (err) {
console.error(err)
}
}

const getAverageAmount = async (
apiUrl,
storageApiUrl,
walletAddress,
fileSize,
duration = 525,
) => {
let fileSizeInGB = Number(fileSize) / 10 ** 9
let storageCostPerUnit = 0

const storageRes = await sendRequest(
`${storageApiUrl}/stats/storage?wallet_address=${walletAddress}`,
)
let cost = storageRes.data.average_price_per_GB_per_year
? storageRes.data.average_price_per_GB_per_year.split(' ')
: []
if (cost[0]) storageCostPerUnit = cost[0]

const bilingRes = await sendRequest(
`${apiUrl}/billing/price/filecoin?wallet_address=${walletAddress}`,
)
let billingPrice = bilingRes.data

let price =
((fileSizeInGB * duration * storageCostPerUnit * 5) / 365) * billingPrice

let numberPrice = Number(price).toFixed(9)
let averagePrice =
numberPrice > 0 ? Number(price * 3).toFixed(9) : '0.000000002'

return averagePrice
}

module.exports = {
getParams,
getFileStatus,
Expand All @@ -88,4 +130,5 @@ module.exports = {
postMintInfo,
postLockPayment,
getDealList,
getAverageAmount,
}
35 changes: 32 additions & 3 deletions sdk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const Web3 = require('web3')

const { mcsUpload } = require('./helper/upload')
const { lockToken } = require('./helper/lockToken')
const { getFileStatus, getDealList, getDealDetail } = require('./helper/mcsApi')
const {
getFileStatus,
getDealList,
getDealDetail,
getAverageAmount,
} = require('./helper/mcsApi')
const { mint } = require('./helper/mint')

class mcsSdk {
Expand Down Expand Up @@ -36,6 +41,8 @@ class mcsSdk {
} else {
this.apiUrl = 'https://mcs-api.filswan.com/api/v1'
}

this.storageApiUrl = 'https://api.filswan.com'
}

/**
Expand Down Expand Up @@ -67,8 +74,30 @@ class mcsSdk {
* @param {string} amount - pass amount as string to avoid BN precision errors
* @returns {Object} payment transaction response
*/
makePayment = async (wCid, amount, size) =>
await lockToken(this.apiUrl, this.web3, this.publicKey, wCid, amount, size)
makePayment = async (wCid, amount, size) => {
let tx = {}
if (amount == '0' || amount == '') {
let averageAmount = await getAverageAmount(
this.apiUrl,
this.storageApiUrl,
this.publicKey,
size,
)

tx = await lockToken(
this.apiUrl,
this.web3,
this.publicKey,
wCid,
averageAmount,
size,
)
} else {
tx = lockToken(this.apiUrl, this.web3, this.publicKey, wCid, amount, size)
}

return tx
}

/**
* get filecoin status for file
Expand Down
4 changes: 2 additions & 2 deletions sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-mcs-sdk",
"version": "2.0.4",
"version": "2.1.0",
"description": "A SDK for the MCS service. It provides a convenient interface for working with the MCS API from a web browser or Node.js",
"homepage": "https://github.com/filswan/js-mcs-sdk",
"main": "index.js",
Expand Down

0 comments on commit 0b3feaa

Please sign in to comment.