Skip to content

Commit

Permalink
Merge pull request #6 from StauroXYZ/new-providers-and-revamped-logs
Browse files Browse the repository at this point in the history
New providers and revamped logs
  • Loading branch information
talentlessguy authored Nov 19, 2023
2 parents 01d31b7 + dcef0a2 commit c44aa12
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 63 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@stauro/piggybank": "^0.0.5",
"ascii-bar": "^1.0.3",
"cac": "^6.7.14",
"consola": "^3.2.3",
"globby": "^14.0.0",
"multiformats": "^12.1.3",
"node-fetch": "^3.3.2",
Expand All @@ -56,6 +55,7 @@
"@types/semver": "^7.5.5",
"@types/varint": "^6.0.3",
"@typescript-eslint/parser": "^6.11.0",
"colorette": "^2.0.20",
"globals": "^13.23.0",
"semantic-release": "^22.0.8",
"typescript": "^5.2.2"
Expand Down
58 changes: 28 additions & 30 deletions pnpm-lock.yaml

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

6 changes: 3 additions & 3 deletions src/actions/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import mod from 'ascii-bar'
import { ensAction } from './ens.js'
import { ChainName } from '../types.js'
import { Address } from 'viem'
import { colors } from 'consola/utils'
import { logger } from '../utils/logger.js'
import * as colors from 'colorette'

const AsciiBar = mod.default

export const deployAction = async (
dir: string,
{ strict, ens, chain, safe, name, dist }: { strict: boolean, chain: ChainName, ens: string, safe: Address, name?: string, dist?: string },
{ strict, ens, chain = 'mainnet', safe, name: customName, dist }: { strict: boolean, chain?: ChainName, ens?: string, safe?: Address, name?: string, dist?: string },
) => {
if (!dir) {
if (await exists('dist')) dir = 'dist'
else dir = '.'
}
const normalizedPath = path.join(process.cwd(), dir)
name = name || path.basename(normalizedPath)
const name = customName || path.basename(normalizedPath)
const [size, files] = await walk(normalizedPath)

if (size === 0) throw new MissingDirectoryError(dir)
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { EIP3770Address, OperationType } from '@stauro/piggybank/types'
import { getEip3770Address } from '@stauro/piggybank/utils'
import { ApiClient } from '@stauro/piggybank/api'
import { chainIdToSafeApiUrl } from '../utils/safe.js'
import { colors } from 'consola/utils'
import * as colors from 'colorette'
import { logger } from '../utils/logger.js'

export const ensAction = async (
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// import { uploadOnEstuary } from './providers/estuary.js'
import { uploadOnDolpin } from './providers/dolpin.js'
import { statusOnGW3, uploadOnGW3 } from './providers/gw3.js'
import { statusOnW3S, uploadOnW3S } from './providers/w3s.js'
import type { StatusFunction, UploadFunction } from './types.js'
Expand All @@ -21,4 +22,8 @@ string,
upload: uploadOnGW3,
status: statusOnGW3,
},
DOLPIN_TOKEN: {
name: 'Dolpin',
upload: uploadOnDolpin,
},
}
4 changes: 2 additions & 2 deletions src/polyfills/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch, { Headers, Request, Response } from 'node-fetch'
import fetch, { Headers, Request, Response, FormData } from 'node-fetch'

if (!('fetch' in globalThis)) {
Object.assign(globalThis, { fetch, Headers, Request, Response })
Object.assign(globalThis, { fetch, Headers, Request, Response, FormData })
}
27 changes: 27 additions & 0 deletions src/providers/dolpin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CID } from 'multiformats'
import { PinningNotSupportedError } from '../errors.js'
import type { UploadFunction } from '../types.js'

const baseURL = 'https://gateway.dolpin.io'
const providerName = 'Dolpin'

export const uploadOnDolpin: UploadFunction = async ({
token, car, cid, name,
}) => {
if (cid) throw new PinningNotSupportedError(providerName)

const fd = new FormData()

fd.append('files', car as Blob)

fd.append('name', name)

const res = await fetch(new URL(`/api/v1/documents/upload-in-cluster-with-api?api_token=${token}`, baseURL), {
method: 'POST',
body: fd,
})

const json = await res.json()

return { cid: CID.parse(json.data.cid).toV1().toString(), status: 'pinned' }
}
88 changes: 69 additions & 19 deletions src/providers/gw3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MissingKeyError,
UploadNotSupportedError,
} from '../errors.js'
import { CID } from 'multiformats'

type GW3PinStatus = 'pinned' | 'unpinning' | 'failure' | 'pinning'

Expand All @@ -29,32 +30,81 @@ export const uploadOnGW3: UploadFunction = async ({
cid,
accessKey,
}) => {
if (car) throw new UploadNotSupportedError(providerName)
if (!accessKey) throw new MissingKeyError('GW3_ACCESS_KEY')
if (cid) {
const res = await fetch(
new URL(`/api/v0/pin/add?arg=${cid}&ts=${getTs()}`, baseURL),
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Access-Key': accessKey,
'X-Access-Secret': token,
},
},
)

const res = await fetch(
new URL(`/api/v0/pin/add?arg=${cid}&ts=${getTs()}`, baseURL),
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Access-Key': accessKey,
'X-Access-Secret': token,
const json = await res.json()
if (!res.ok) {
throw new DeployError(
providerName,
(json).msg,
)
}

return { cid }
}
else {
console.log('url', new URL(`ipfs/?size=${car!.size}&ts=${getTs()}`, baseURL).toString())
const res1 = await fetch(
new URL(`ipfs/?size=${car!.size}&ts=${getTs()}`, baseURL),
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Access-Key': accessKey,
'X-Access-Secret': token,
},
},
body: car,
},
)
)
const json = await res1.json()
if (!res1.ok || json.code !== 200) {
throw new DeployError(
providerName,
(json).msg,
)
}
const url = json.data.url

console.log(url)

const res2 = await fetch(
url,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Access-Key': accessKey,
'X-Access-Secret': token,
},
body: car as Blob,
},
)

if (!res.ok) {
const json = await res.text()
throw new DeployError(
if (!res2.ok) throw new DeployError(
providerName,
(JSON.parse(json) as { msg: string }).msg,
'Unknown upload error',
)
}

return { cid }
const text = await res2.text()

console.log(text)

return { cid: CID.parse(text.split(': ')[1]).toV1().toString() }
}
}

export const statusOnGW3: StatusFunction = async (cid, auth) => {
Expand Down
Loading

0 comments on commit c44aa12

Please sign in to comment.