Skip to content

Commit

Permalink
Merge pull request #35 from LinkdropHQ/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
spacehaz authored Jun 10, 2024
2 parents baf0013 + 45d969b commit 0c64de8
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 25 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Linkdrop SDK


## 2.1.3
- updates for src=d param (getLinks method of batch)

## 2.1.2
- updates for claimHostUrl param (getLinks method of batch)

## 2.1.1
- added testnets apiUrl

## 2.1.0
- added apiKey as param for LinkdropSDK

## 2.0.9
- added src param to link (src=d)

Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ import LinkdropSDK from 'linkdrop-sdk'
To use SDK on a tesnet:
```ts
// initializing Linkdrop SDK on a testnet (Goerli or Mumbai)
const sdk = new LinkdropSDK({ mode: 'testnets' });
const apiKey = /* to request an API key, please contact us at [email protected] */
const sdk = new LinkdropSDK({ mode: 'testnets', apiKey });
```
To use SDK on a production network (Ethereum Mainnet or Polygon):
```ts
// initializing Linkdrop SDK on a production network
const sdk = new LinkdropSDK();
const apiKey = /* to request an API key, please contact us at [email protected] */
const sdk = new LinkdropSDK({ apiKey });
```

## Claim methods (Can be used on Front-end & Back-end)
Expand Down Expand Up @@ -248,6 +250,16 @@ To fetch all links created for that batch, use the `batch.getLinks` method:
const links = await batch.getLinks()
```

Response data includes claim links in format `https://claim.linkdrop.io/#/...`
If you need links in custom format please provide optional parameter to `getLinks` method
```ts
const links = await batch.getLinks('https://wallet.coinbase.com/claim?tk=code&k=<CODE>&c=<CHAIN_ID>&v=3')
```

As a result you will get claim links with actual claim code (`<CODE>`) and chain id (`<CHAIN_ID>`)



### Deactivating / reactivating links

#### Deactivate Link
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linkdrop-sdk",
"version": "2.0.9",
"version": "2.1.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
12 changes: 8 additions & 4 deletions src/api/batch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,36 @@ import { defineRequestKeyHeader } from '../../helpers'
const requests: TRequests = {
getBatches: (
apiHost,
apiKey,
campaignSig,
campaignId
) => {
const headers = defineRequestKeyHeader(campaignSig)
const headers = defineRequestKeyHeader(campaignSig, apiKey)
return axios.get(`${apiHost}/api/v2/dashboard/linkdrop/campaigns/${campaignId}/batches`, {
headers,
})
},
getBatch: (
apiHost,
apiKey,
campaignSig,
campaignId,
batchId
) => {
const headers = defineRequestKeyHeader(campaignSig)
const headers = defineRequestKeyHeader(campaignSig, apiKey)
return axios.get(`${apiHost}/api/v2/dashboard/linkdrop/campaigns/${campaignId}/batches/${batchId}`, {
headers,
})
},
createBatch: (
apiHost,
apiKey,
campaignSig,
campaignId,
claimLinks,
batchDescription
) => {
const headers = defineRequestKeyHeader(campaignSig)
const headers = defineRequestKeyHeader(campaignSig, apiKey)
return axios.post(`${apiHost}/api/v2/dashboard/linkdrop/campaigns/${campaignId}/save-batch`, {
claim_links: claimLinks,
batch_description: batchDescription
Expand All @@ -41,12 +44,13 @@ const requests: TRequests = {
},
addLinks: (
apiHost,
apiKey,
campaignSig,
campaignId,
batchId,
claimLinks,
) => {
const headers = defineRequestKeyHeader(campaignSig)
const headers = defineRequestKeyHeader(campaignSig, apiKey)
return axios.post(`${apiHost}/api/v2/dashboard/linkdrop/campaigns/${campaignId}/batches/${batchId}/add-links `, {
claim_links: claimLinks
}, {
Expand Down
4 changes: 4 additions & 0 deletions src/api/batch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type TAddLinksResponse = {

export type TGetBatches = (
apiHost: string,
apiKey: string,
campaignSig: string,
campaignId: string
) => Promise<
Expand All @@ -39,6 +40,7 @@ export type TGetBatches = (

export type TGetBatch = (
apiHost: string,
apiKey: string,
campaignSig: string,
campaignId: string,
batchId: string
Expand All @@ -50,6 +52,7 @@ export type TGetBatch = (

export type TCreateBatch = (
apiHost: string,
apiKey: string,
campaignSig: string,
campaignId: string,
claimLinks: TLinkItem[],
Expand All @@ -62,6 +65,7 @@ export type TCreateBatch = (

export type TAddLinks = (
apiHost: string,
apiKey: string,
campaignSig: string,
campaignId: string,
batchId: string,
Expand Down
3 changes: 2 additions & 1 deletion src/api/campaign/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { defineRequestKeyHeader } from '../../helpers'
const requests: TRequests = {
getCampaign: (
apiHost,
apiKey,
campaignSig,
campaignId
) => {
const headers = defineRequestKeyHeader(campaignSig)
const headers = defineRequestKeyHeader(campaignSig, apiKey)
return axios.get(`${apiHost}/api/v2/dashboard/linkdrop/campaigns/${campaignId}`, {
headers
})
Expand Down
1 change: 1 addition & 0 deletions src/api/campaign/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type TGetCampaignResponse = {

export type TGetCampaign = (
apiHost: string,
apiKey: string,
campaignSig: string,
campaignId: string,
) => Promise<
Expand Down
6 changes: 4 additions & 2 deletions src/api/link/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ const requests: TRequests = {
},
deactivateLink: (
apiHost,
apiKey,
campaignSig,
linkId
) => {
const headers = defineRequestKeyHeader(campaignSig)
const headers = defineRequestKeyHeader(campaignSig, apiKey)
return axios.post(`${apiHost}/api/v2/dashboard/linkdrop/claim-links/${linkId}/deactivate`, {}, {
headers
})
},
reactivateLink: (
apiHost,
apiKey,
campaignSig,
linkId
) => {
const headers = defineRequestKeyHeader(campaignSig)
const headers = defineRequestKeyHeader(campaignSig, apiKey)
return axios.post(`${apiHost}/api/v2/dashboard/linkdrop/claim-links/${linkId}/reactivate`, {}, {
headers
})
Expand Down
2 changes: 2 additions & 0 deletions src/api/link/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type TGetLinkStatus = (

export type TDeactivateLink = (
apiHost: string,
apiKey: string,
campaignSig: string,
linkId: string
) => Promise<
Expand All @@ -69,6 +70,7 @@ export type TRedeemLink = (

export type TReactivateLink = (
apiHost: string,
apiKey: string,
campaignSig: string,
linkId: string
) => Promise<
Expand Down
5 changes: 4 additions & 1 deletion src/configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export const apiUrl = 'https://dashboard-api.linkdrop.io'
export const testnetsApiUrl = 'https://testnets.dashboard-api.linkdrop.io'
export const testnetsApiUrl = 'https://linkdrop-api-testnets-b0fceb9.zuplo.app'

export const claimHostUrl = 'https://claim.linkdrop.io'
export const testnetsClaimHostUrl = 'https://testnets.claim.linkdrop.io'

export const polygonJSONRPCUrl = 'https://rpc-mainnet.maticvigil.com/v1/ad4cd2ea018ddb1ccd0418ffa43c27b3d99fbd55'
export const mainnetJSONRPCUrl = 'https://mainnet.infura.io/v3/620c738fbe1843a18f47ada0e60e738a'
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/define-claim-host-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { claimHostUrl, testnetsClaimHostUrl } from '../configs'
import { TMode } from '../types'

type TDefineClaimHostUrl = (
mode: TMode
) => string

const defineClaimHostUrl: TDefineClaimHostUrl = (
chainId
) => {
switch (chainId) {
case 'testnets':
return testnetsClaimHostUrl
case 'mainnets':
default:
return claimHostUrl
}
}

export default defineClaimHostUrl
16 changes: 14 additions & 2 deletions src/helpers/define-request-key-header.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
type TDefineRequestKeyHeader = (campaignSig: string) => Record<string, string>
const defineRequestKeyHeader: TDefineRequestKeyHeader = (campaignSig) => {
type TDefineRequestKeyHeader = (
campaignSig: string,
apiKey: string
) => Record<string, string>

const defineRequestKeyHeader: TDefineRequestKeyHeader = (
campaignSig,
apiKey
) => {
const headers = {}
headers[
'X-CAMPAIGN-KEY'
] = campaignSig

if (apiKey) {
headers['authorization'] = `Bearer ${apiKey}`
}

return headers
}

Expand Down
2 changes: 2 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import redeemLink from './redeem-link'
import getBignumberInterval from './get-bignumber-interval'
import defineCampaignSig from './define-campaign-sig'
import prepareAsset from './prepare-asset'
import defineClaimHostUrl from './define-claim-host-url'

export {
parseLinkParams,
defineClaimHostUrl,
defineCampaignSig,
getBignumberInterval,
getLinkParams,
Expand Down
22 changes: 19 additions & 3 deletions src/modules/batch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Batch implements IBatch {
claimHostUrl: string
campaignData: TCampaignItem
signerKey: string
apiKey: string
chainId: number

constructor (
batchId: string,
Expand All @@ -25,7 +27,9 @@ class Batch implements IBatch {
campaignData: TCampaignItem,
signerKey: string,
campaignSig: string,
apiHost: string
apiHost: string,
apiKey: string,
chainId: number
) {
this.batchId = batchId
this.data = data
Expand All @@ -36,6 +40,8 @@ class Batch implements IBatch {
this.claimHostUrl = claimHostUrl
this.campaignData = campaignData
this.signerKey = signerKey
this.apiKey = apiKey
this.chainId = chainId
}

addLinks: TAddLinks = async (
Expand Down Expand Up @@ -66,24 +72,34 @@ class Batch implements IBatch {
if (!transformedAssets) { return alert('Error with assets') }
return await batchesApi.addLinks(
this.apiHost,
this.apiKey,
this.campaignSig,
this.campaignData.campaign_id,
this.batchId,
transformedAssets
)
}

getLinks: TGetLinks = () => {
getLinks: TGetLinks = (
linkPattern
) => {
if (!this.claimLinks) {
return []
}
return this.claimLinks.map(link => {
const encryptedClaimCode = link.encrypted_claim_code
const claimCode = crypto.decrypt(encryptedClaimCode, this.encryptionKey)

let finalLink = `${this.claimHostUrl}/#/redeem/${claimCode}`
if (linkPattern) {
finalLink = linkPattern.replace('<CODE>', claimCode)
.replace('<CHAIN_ID>', String(this.chainId))
}
const sourceParam = finalLink.includes('?') ? `&src=d` : '?src=d'
return {
linkId: link.link_id,
claimCode,
claimLink: `${this.claimHostUrl}/#/redeem/${claimCode}?src=d`
claimLink: `${finalLink}${sourceParam}`
}
})
}
Expand Down
Loading

0 comments on commit 0c64de8

Please sign in to comment.