Skip to content

Commit

Permalink
Add SDK Example (#208)
Browse files Browse the repository at this point in the history
* Add Example

* remove extra tsconfig lines

* dedupe
  • Loading branch information
PabloSzx authored Apr 3, 2023
1 parent 5a3b035 commit 9bfff34
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 181 deletions.
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
strict-peer-dependencies=false
shared-workspace-lockfile=true
prefer-workspace-packages=true

26 changes: 26 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@soundxyz/sdk-example",
"version": "0.0.0",
"private": true,
"scripts": {
"prepare": "node pull-env.mjs",
"snoop-xyz": "bob-tsm src/snoop-xyz.ts",
"mint": "bob-tsm src/mint.ts"
},
"dependencies": {
"@soundxyz/sdk": "workspace:^",
"@soundxyz/sound-protocol": "^1.4.0",
"alchemy-sdk": "^2.6.2",
"date-fns": "^2.29.3",
"ethers": "^5.7.2",
"require-env-variable": "^4.0.1"
},
"devDependencies": {
"@types/node": "18.15.11",
"bob-tsm": "^1.1.2",
"dotenv": "^16.0.3",
"esbuild": "^0.17.15",
"typescript": "5.0.3",
"undici": "^5.21.0"
}
}
12 changes: 12 additions & 0 deletions example/pull-env.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { promises } from 'fs'
import { fetch } from 'undici'

await fetch('https://vault.dotenv.org/pull.txt', {
method: 'POST',
body: 'DOTENV_VAULT=vlt_4011aac495b93f704eea755ec8deba6c5eac6f1a6af55ba645a32b3015dce13c&DOTENV_ME=it_a106570406176c5a9233bd477704bd3c863b660e00cbd4f179023a11714cec70',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
})
.then((response) => response.text())
.then((env) => promises.writeFile('.env', env))
21 changes: 21 additions & 0 deletions example/src/alchemy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dotenv/config'

import { Alchemy, Network } from 'alchemy-sdk'
import { requireEnv } from 'require-env-variable'

const { PUBLIC_ALCHEMY_KEY } = requireEnv('PUBLIC_ALCHEMY_KEY')

export const alchemyMainnetClient = new Alchemy({
apiKey: PUBLIC_ALCHEMY_KEY,
network: Network.ETH_MAINNET,
})

export const alchemyGoerliClient = new Alchemy({
apiKey: PUBLIC_ALCHEMY_KEY,
network: Network.ETH_GOERLI,
})

export const [mainnetProvider, goerliProvider] = await Promise.all([
alchemyMainnetClient.config.getProvider(),
alchemyGoerliClient.config.getProvider(),
])
7 changes: 7 additions & 0 deletions example/src/clientMainnetProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SoundClient } from '@soundxyz/sdk'

import { mainnetProvider } from './alchemy'

export const clientMainnetProvider = SoundClient({
provider: mainnetProvider,
})
13 changes: 13 additions & 0 deletions example/src/clientSigner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import './fetch'

import { SoundClient } from '@soundxyz/sdk'
import { LanyardMerkleProofProvider } from '@soundxyz/sdk/merkle/lanyard'

import { goerliProvider } from './alchemy'
import { signer } from './signer'

export const clientSigner = SoundClient({
provider: goerliProvider,
signer,
merkleProvider: LanyardMerkleProofProvider,
})
4 changes: 4 additions & 0 deletions example/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { fetch } from 'undici'

// @ts-expect-error Incomplete spec not needed for examples
globalThis.fetch ||= fetch
98 changes: 98 additions & 0 deletions example/src/mint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import assert from 'assert'

import { clientSigner } from './clientSigner'
import { signer } from './signer'
import { goerliProvider } from './alchemy'
import { fromUnixTime } from 'date-fns'

const exampleEditionAddress = '0xFC6Bdee583f3738F69FeeF4BaCf9069aC086Ad77'

const editionContract = clientSigner.edition.info({
contractAddress: exampleEditionAddress,
}).contract

const editionInfo = await editionContract.info

assert.strictEqual(editionInfo.name, 'SDK Example')

console.log(editionInfo)

assert.strictEqual(editionInfo.mintConcluded, false)

const { schedules, activeSchedules } = await clientSigner.edition.mintSchedules({
editionAddress: exampleEditionAddress,
})

assert.strictEqual(activeSchedules.length, 2, 'Unexpected amount of schedules')
assert.strictEqual(schedules.length, 2, 'Unexpected amount of schedules')

assert.deepStrictEqual(schedules, activeSchedules)

const [merkleDrop, openEditionDrop] = activeSchedules

assert(merkleDrop?.mintType === 'MerkleDrop', "Unexpected first example drop isn't merkle")

assert(openEditionDrop?.mintType === 'RangeEdition', "Unexpected second example drop isn't range edition")

assert(
(await clientSigner.edition.eligibleQuantity({
mintSchedule: merkleDrop,
userAddress: signer.address,
})) > 0,
"Example account can't collect from presale anymore",
)

const currentlyOwnedNumberOfTokens = await clientSigner.edition.numberOfTokensOwned({
editionAddress: exampleEditionAddress,
userAddress: signer.address,
})

console.log(
`Wallet ${signer.address} currently owns ${currentlyOwnedNumberOfTokens} tokens. Trying to buy another one from a merkle drop...`,
)

{
const mintTransaction = await clientSigner.edition.mint({
mintSchedule: merkleDrop,
quantity: 1,
})

console.log('Mint transaction has been sent, waiting for completion...')

const receipt = await mintTransaction.wait()

const confirmedDate = fromUnixTime((await goerliProvider.getBlock(receipt.blockNumber)).timestamp)

console.log(
`Mint has been confirmed at ${confirmedDate}. Now wallet ${
signer.address
} owns ${await clientSigner.edition.numberOfTokensOwned({
editionAddress: exampleEditionAddress,
userAddress: signer.address,
})}`,
)
}

console.log(`Wallet ${signer.address} is now trying to buy another one from the public open edition...`)

{
const mintTransaction = await clientSigner.edition.mint({
mintSchedule: openEditionDrop,
quantity: 1,
})

console.log('Mint transaction has been sent, waiting for completion...')

const receipt = await mintTransaction.wait()

const confirmedDate = fromUnixTime((await goerliProvider.getBlock(receipt.blockNumber)).timestamp)

console.log(
`Mint has been confirmed at ${confirmedDate}. Now wallet ${
signer.address
} owns ${await clientSigner.edition.numberOfTokensOwned({
editionAddress: exampleEditionAddress,
userAddress: signer.address,
})}`,
)
}
10 changes: 10 additions & 0 deletions example/src/signer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dotenv/config'

import { Wallet } from 'ethers'
import { requireEnv } from 'require-env-variable'

import { goerliProvider } from './alchemy'

const { PUBLIC_WALLET_PRIVATE_KEY } = requireEnv('PUBLIC_WALLET_PRIVATE_KEY')

export const signer = new Wallet(PUBLIC_WALLET_PRIVATE_KEY, goerliProvider)
18 changes: 18 additions & 0 deletions example/src/snoop-xyz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'assert'

import { clientMainnetProvider } from './clientMainnetProvider'

const XYZ_EditionAddress = '0xA6c4df945DBB1D71Fe9a8D71Ae93B8d5C2BbeBE4'

assert.strictEqual(
await clientMainnetProvider.isSoundEdition({
editionAddress: XYZ_EditionAddress,
}),
true,
)

const XYZ_EditionInfo = clientMainnetProvider.edition.info({
contractAddress: XYZ_EditionAddress,
}).contract

console.log(await XYZ_EditionInfo.info)
13 changes: 13 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "esnext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "esnext" /* Specify what module code is generated. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"moduleResolution": "node",
"strict": true /* Enable all strict type-checking options. */,
"noUncheckedIndexedAccess": true /* Add 'undefined' to a type when accessed using an index. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"noEmit": true
}
}
7 changes: 4 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"types": "./dist/index.d.ts",
"scripts": {
"prepack": "node build.mjs",
"prepare": "graphql-codegen",
"prepare": "node build.mjs",
"postpublish": "gh-release",
"pull-env": "dotenv-vault pull",
"test": "hardhat test",
Expand Down Expand Up @@ -74,7 +74,7 @@
"chai": "^4.3.7",
"changesets-github-release": "^0.1.0",
"dotenv": "^16.0.3",
"esbuild": "^0.17.14",
"esbuild": "^0.17.15",
"ethereum-waffle": "^4.0.10",
"ethers": "^5.7.2",
"graphql": "^16.6.0",
Expand All @@ -91,6 +91,7 @@
},
"publishConfig": {
"access": "public",
"directory": "dist"
"directory": "dist",
"linkDirectory": true
}
}
Loading

0 comments on commit 9bfff34

Please sign in to comment.