From 1d58b37d2429c16c3597161c72447457692f36f9 Mon Sep 17 00:00:00 2001 From: Maximilian Schneider Date: Fri, 2 Jul 2021 01:18:25 +0200 Subject: [PATCH] update ido-pool & anchor versions and implement account loading --- @types/types.tsx | 1 + components/ContributionModal.tsx | 1 + hooks/useWallet.tsx | 10 +-- idls/ido_pool.json | 34 ++++++---- package.json | 2 +- stores/useWalletStore.tsx | 112 +++++++++++++++++++++---------- utils/index.tsx | 6 +- utils/tokens.tsx | 13 ++++ yarn.lock | 81 +++++++++++----------- 9 files changed, 163 insertions(+), 97 deletions(-) diff --git a/@types/types.tsx b/@types/types.tsx index 60234af..8dd2f80 100644 --- a/@types/types.tsx +++ b/@types/types.tsx @@ -20,6 +20,7 @@ export interface EndpointInfo { url: string websocket: string programId: string + poolKey: string } export interface WalletContextValues { diff --git a/components/ContributionModal.tsx b/components/ContributionModal.tsx index d9a1ca7..c683d3c 100644 --- a/components/ContributionModal.tsx +++ b/components/ContributionModal.tsx @@ -103,6 +103,7 @@ const ContributionModal = () => { useEffect(() => { if (submitting) { + // TODO: add submission here const submitTimer = setTimeout(() => { setSubmitted(true) setSubmitting(false) diff --git a/hooks/useWallet.tsx b/hooks/useWallet.tsx index dab925f..51ba957 100644 --- a/hooks/useWallet.tsx +++ b/hooks/useWallet.tsx @@ -102,9 +102,12 @@ export default function useWallet() { '...' + wallet.publicKey.toString().substr(-5), }) - //await actions.fetchWalletTokenAccounts() - //await actions.fetchWalletMints() await actions.fetchPool() + await actions.fetchVault() + await Promise.all([ + actions.fetchWalletTokenAccounts(), + actions.fetchVaultMint(), + ]) }) wallet.on('disconnect', () => { setWalletStore((state) => { @@ -128,8 +131,7 @@ export default function useWallet() { }, [wallet, setWalletStore]) useInterval(async () => { - //await actions.fetchWalletTokenAccounts() - //await actions.fetchWalletMints() + await actions.fetchVault() }, 20 * SECONDS) return { connected, wallet } diff --git a/idls/ido_pool.json b/idls/ido_pool.json index ce35c87..8f638bb 100644 --- a/idls/ido_pool.json +++ b/idls/ido_pool.json @@ -54,6 +54,11 @@ "name": "rent", "isMut": false, "isSigner": false + }, + { + "name": "clock", + "isMut": false, + "isSigner": false } ], "args": [ @@ -341,52 +346,57 @@ ], "errors": [ { - "code": 100, - "name": "InitTime", + "code": 300, + "name": "IdoFuture", + "msg": "IDO must start in the future" + }, + { + "code": 301, + "name": "SeqTimes", "msg": "IDO times are non-sequential" }, { - "code": 101, + "code": 302, "name": "StartIdoTime", "msg": "IDO has not started" }, { - "code": 102, + "code": 303, "name": "EndDepositsTime", "msg": "Deposits period has ended" }, { - "code": 103, + "code": 304, "name": "EndIdoTime", "msg": "IDO has ended" }, { - "code": 104, + "code": 305, "name": "IdoNotOver", "msg": "IDO has not finished yet" }, { - "code": 105, + "code": 306, "name": "LowUsdc", "msg": "Insufficient USDC" }, { - "code": 106, + "code": 307, "name": "LowRedeemable", "msg": "Insufficient redeemable tokens" }, { - "code": 107, + "code": 308, "name": "UsdcNotEqRedeem", "msg": "USDC total and redeemable total don't match" }, { - "code": 108, + "code": 309, "name": "InvalidNonce", "msg": "Given nonce is invalid" } ], "metadata": { - "address": "4vZP5tE6sjKrSSuFwSxvmx76kc99BCVUeDo1dYT5tXAF" + "address": "5JTPKQJPkvrMzfDyEHX6EiaEYbR5QxXDkNQ66BoadgUn" } -} \ No newline at end of file +} diff --git a/package.json b/package.json index f6fc364..66da659 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "@emotion/styled": "^11.3.0", "@headlessui/react": "^1.0.0", "@heroicons/react": "^1.0.1", - "@project-serum/anchor": "^0.4.5", + "@project-serum/anchor": "^0.10.0", "@project-serum/sol-wallet-adapter": "^0.2.0", "@solana/spl-token": "^0.1.3", "@solana/web3.js": "^1.5.0", diff --git a/stores/useWalletStore.tsx b/stores/useWalletStore.tsx index d5ee063..b8cd5fe 100644 --- a/stores/useWalletStore.tsx +++ b/stores/useWalletStore.tsx @@ -14,6 +14,7 @@ import { ProgramAccount, TokenAccount, MintAccount, + getTokenAccount, } from '../utils/tokens' export const ENDPOINTS: EndpointInfo[] = [ @@ -22,20 +23,43 @@ export const ENDPOINTS: EndpointInfo[] = [ url: 'https://api.mainnet-beta.solana.com/', websocket: 'https://api.mainnet-beta.solana.com/', programId: '', + poolKey: '', }, { name: 'devnet', url: 'https://devnet.solana.com', websocket: 'https://devnet.solana.com', programId: 'E5s3D6B3PJinuB9kb3dicxfi3qUNLUGX6hoPawhbqagt', + poolKey: '', + }, + { + name: 'localnet', + url: 'http://localhost:8899', + websocket: 'http://localhost:8899', + programId: 'FF8zcQ1aEmyXeBt99hohoyYprgpEVmWsRK44qta3emno', + poolKey: '8gswb9g1JdYEVj662KXr9p6p9SMgR77NryyqvWn9GPXJ', }, ] -const CLUSTER = 'devnet' +const CLUSTER = 'localnet' const ENDPOINT = ENDPOINTS.find((e) => e.name === CLUSTER) const DEFAULT_CONNECTION = new Connection(ENDPOINT.url, 'recent') const WEBSOCKET_CONNECTION = new Connection(ENDPOINT.websocket, 'recent') const PROGRAM_ID = new PublicKey(ENDPOINT.programId) +const POOL_PK = new PublicKey(ENDPOINT.poolKey) + +interface PoolAccount { + distributionAuthority: PublicKey + endDepositsTs: anchor.BN + endIdoTs: anchor.BN + nonce: number + numIdoTokens: anchor.BN + poolUsdc: PublicKey + poolWatermelon: PublicKey + redeemableMint: PublicKey + startIdoTs: anchor.BN + watermelonMint: PublicKey +} interface WalletStore extends State { connected: boolean @@ -50,7 +74,8 @@ interface WalletStore extends State { providerUrl: string tokenAccounts: ProgramAccount[] mints: { [pubkey: string]: MintAccount } - pool: anchor.web3.Account | undefined + pool: PoolAccount | undefined + vault: TokenAccount | undefined set: (x: any) => void actions: any } @@ -68,8 +93,35 @@ const useWalletStore = create((set, get) => ({ providerUrl: null, tokenAccounts: [], mints: {}, - pool: null, + pool: undefined, + vault: undefined, actions: { + async fetchPool() { + const connection = get().connection.current + const connected = get().connected + const wallet = get().current + const programId = get().connection.programId + + console.log('fetchPool', connected, poolIdl) + if (connection && connected) { + const provider = new anchor.Provider( + connection, + wallet, + anchor.Provider.defaultOptions() + ) + const program = new anchor.Program(poolIdl, programId, provider) + console.log(program) + + const pool = (await program.account.poolAccount.fetch( + POOL_PK + )) as PoolAccount + console.log(pool) + + set((state) => { + state.pool = pool + }) + } + }, async fetchWalletTokenAccounts() { const connection = get().connection.current const connected = get().connected @@ -92,48 +144,34 @@ const useWalletStore = create((set, get) => ({ }) } }, - async fetchWalletMints() { + async fetchVault() { const connection = get().connection.current - const connected = get().connected - const tokenAccounts = get().tokenAccounts + const pool = get().pool const set = get().set - if (connected) { - const fetchMints = tokenAccounts.map((a) => - getMint(connection, a.account.mint) - ) - const mintResults = await Promise.all(fetchMints) + if (!pool) return - const newMints: { [pubkey: string]: MintAccount } = {} - mintResults.forEach( - (m) => (newMints[m.publicKey.toBase58()] = m.account) - ) + const { account: vault } = await getTokenAccount( + connection, + pool.poolUsdc + ) + console.log('fetchVault', vault) - set((state) => { - state.mints = newMints - }) - } else { - set((state) => { - state.mints = {} - }) - } + set((state) => { + state.vault = vault + }) }, - async fetchPool() { + async fetchVaultMint() { const connection = get().connection.current - const connected = get().connected - const wallet = get().current - const programId = get().connection.programId + const vault = get().vault + const set = get().set - console.log('fetchPool', connected, poolIdl) - if (connection && connected) { - const provider = new anchor.Provider( - connection, - wallet, - anchor.Provider.defaultOptions() - ) - const program = new anchor.Program(poolIdl, programId, provider) - console.log(program) - } + const { account: mint } = await getMint(connection, vault.mint) + console.log('fetchVaultMint', mint) + + set((state) => { + state.mints[vault.mint.toBase58()] = mint + }) }, }, set: (fn) => set(produce(fn)), diff --git a/utils/index.tsx b/utils/index.tsx index 3bc828b..3c786d3 100644 --- a/utils/index.tsx +++ b/utils/index.tsx @@ -9,14 +9,16 @@ function fixedPointToNumber(value: BN, decimals: number) { } export function getUsdcBalance() { - const { tokenAccounts, mints } = useWalletStore((state) => state) + const { tokenAccounts, mints, vault } = useWalletStore((state) => state) + + if (!vault) return 0 const calculateBalance = (a) => { const mint = mints[a.account?.mint?.toBase58()] return mint ? fixedPointToNumber(a.account.amount, mint.decimals) : 0 } - const usdcAddress = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' + const usdcAddress = vault.mint.toBase58() const usdcAccount = tokenAccounts.filter( (a) => a.account.mint.toBase58() === usdcAddress diff --git a/utils/tokens.tsx b/utils/tokens.tsx index 3abd98a..6543d91 100644 --- a/utils/tokens.tsx +++ b/utils/tokens.tsx @@ -42,6 +42,19 @@ export async function getMint( } } +export async function getTokenAccount( + connection: Connection, + publicKey: PublicKey +): Promise> { + const result = await connection.getAccountInfo(publicKey) + const data = Buffer.from(result.data) + const account = parseTokenAccountData(publicKey, data) + return { + publicKey, + account, + } +} + // copied from @solana/spl-token const TOKEN_PROGRAM_ID = new PublicKey( diff --git a/yarn.lock b/yarn.lock index d8d4759..8118e8f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -704,17 +704,13 @@ version "0.14.0" resolved "https://registry.yarnpkg.com/@opentelemetry/context-base/-/context-base-0.14.0.tgz#c67fc20a4d891447ca1a855d7d70fa79a3533001" -"@project-serum/anchor@^0.4.5": - version "0.4.5" - resolved "https://registry.yarnpkg.com/@project-serum/anchor/-/anchor-0.4.5.tgz#6c52b5722a3133d5f1df793bfbc1af5c07b0316d" - integrity sha512-LxcV8yZae/sOBPZULDlVGqsIyFkjhmJKi2kx1id9EybECX5QqYQTMS69ozdH2e+5VEDjw0dCfRqBCMJ97ylmmQ== +"@project-serum/anchor@^0.10.0": + version "0.10.0" + resolved "https://registry.yarnpkg.com/@project-serum/anchor/-/anchor-0.10.0.tgz#e08396b7bb3cc94b279d45ac38595118dffdd617" + integrity sha512-FWw3bQy1otINPq40uOTX0h8eE3drnespaq9Xkwpy3UXj6i46xuLjsWC+x9hp76EN4pC174POeNPvB0aivatswg== dependencies: - "@project-serum/borsh" "^0.1.1" - "@solana/web3.js" "^1.2.0" - "@types/bn.js" "^4.11.6" - "@types/bs58" "^4.0.1" - "@types/crypto-hash" "^1.1.2" - "@types/pako" "^1.0.1" + "@project-serum/borsh" "^0.2.2" + "@solana/web3.js" "^1.17.0" base64-js "^1.5.1" bn.js "^5.1.2" bs58 "^4.0.1" @@ -726,11 +722,12 @@ js-sha256 "^0.9.0" pako "^2.0.3" snake-case "^3.0.4" + toml "^3.0.0" -"@project-serum/borsh@^0.1.1": - version "0.1.1" - resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.1.1.tgz#a810aad74e4b458fbd1ab63188f175bee765a8af" - integrity sha512-bzCea8KTyc7CNkcMn0V2HW/FdU9pnJUcVRrwLqvJkYeo+mhkXE4AxxWErN3q+UxhEm8ypGIX1OYTKJaTJvj5cQ== +"@project-serum/borsh@^0.2.2": + version "0.2.2" + resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.2.2.tgz#63e558f2d6eb6ab79086bf499dea94da3182498f" + integrity sha512-Ms+aWmGVW6bWd3b0+MWwoaYig2QD0F90h0uhr7AzY3dpCb5e2S6RsRW02vFTfa085pY2VLB7nTZNbFECQ1liTg== dependencies: bn.js "^5.1.2" buffer-layout "^1.2.0" @@ -765,13 +762,14 @@ buffer-layout "^1.2.0" dotenv "8.2.0" -"@solana/web3.js@^1.2.0": - version "1.9.0" - resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.9.0.tgz#1676215a2e89e2cb6e4694c5970c3c7c8ac12a19" - integrity sha512-wal/itYezfH7ePMoBL6mtSa+J2jTkd7bOwlaen2jeJA+aHmQaLfP+XpI27Y+hPmSRChmHggVgl+/ayX4LX8sZg== +"@solana/web3.js@^1.17.0": + version "1.20.0" + resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.20.0.tgz#9a1855a239c96c5b946bdbe4cc5e3768ee3b2a77" + integrity sha512-s/hmbeC7h0QeMKDHl5HTJbY4NSrT3IK1oGerNwdmFfnbs9ygtBbXo1MXaeYVDcNazsthKgiYE+unNd+cpVH8HA== dependencies: "@babel/runtime" "^7.12.5" bn.js "^5.0.0" + borsh "^0.4.0" bs58 "^4.0.1" buffer "6.0.1" buffer-layout "^1.2.0" @@ -855,33 +853,19 @@ dependencies: "@babel/types" "^7.3.0" -"@types/bn.js@^4.11.6": +"@types/bn.js@^4.11.5": version "4.11.6" resolved "https://registry.yarnpkg.com/@types/bn.js/-/bn.js-4.11.6.tgz#c306c70d9358aaea33cd4eda092a742b9505967c" integrity sha512-pqr857jrp2kPuO9uRjZ3PwnJTjoQy+fcdxvBTvHm6dkmEL9q+hDD/2j/0ELOBPtPnS8LjCX0gI9nbl8lVkadpg== dependencies: "@types/node" "*" -"@types/bs58@^4.0.1": - version "4.0.1" - resolved "https://registry.yarnpkg.com/@types/bs58/-/bs58-4.0.1.tgz#3d51222aab067786d3bc3740a84a7f5a0effaa37" - integrity sha512-yfAgiWgVLjFCmRv8zAcOIHywYATEwiTVccTLnRp6UxTNavT55M9d/uhK3T03St/+8/z/wW+CRjGKUNmEqoHHCA== - dependencies: - base-x "^3.0.6" - "@types/connect@^3.4.33": version "3.4.34" resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.34.tgz#170a40223a6d666006d93ca128af2beb1d9b1901" dependencies: "@types/node" "*" -"@types/crypto-hash@^1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@types/crypto-hash/-/crypto-hash-1.1.2.tgz#5a993deb0e6ba7c42f86eaa65d9bf563378f4569" - integrity sha512-sOmi+4Go2XKodLV4+lfP+5QMQ+6ZYqRJhK8D/n6xsxIUvlerEulmU9S4Lo02pXCH3qPBeJXEy+g8ZERktDJLSg== - dependencies: - crypto-hash "*" - "@types/express-serve-static-core@^4.17.9": version "4.17.19" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.19.tgz#00acfc1632e729acac4f1530e9e16f6dd1508a1d" @@ -939,11 +923,6 @@ version "2.4.0" resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" -"@types/pako@^1.0.1": - version "1.0.1" - resolved "https://registry.yarnpkg.com/@types/pako/-/pako-1.0.1.tgz#33b237f3c9aff44d0f82fe63acffa4a365ef4a61" - integrity sha512-GdZbRSJ3Cv5fiwT6I0SQ3ckeN2PWNqxd26W9Z2fCK1tGrrasGy4puvNFtnddqH9UJFMQYXxEuuB7B8UK+LLwSg== - "@types/parse-json@^4.0.0": version "4.0.0" resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" @@ -1399,7 +1378,7 @@ balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" -base-x@^3.0.2, base-x@^3.0.6: +base-x@^3.0.2: version "3.0.8" resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.8.tgz#1e1106c2537f0162e8b52474a557ebb09000018d" dependencies: @@ -1443,6 +1422,16 @@ bn.js@^5.0.0, bn.js@^5.1.0, bn.js@^5.1.1, bn.js@^5.1.2: version "5.2.0" resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002" +borsh@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/borsh/-/borsh-0.4.0.tgz#9dd6defe741627f1315eac2a73df61421f6ddb9f" + integrity sha512-aX6qtLya3K0AkT66CmYWCCDr77qsE9arV05OmdFpmat9qu8Pg9J5tBUPDztAW5fNh/d/MyVG/OYziP52Ndzx1g== + dependencies: + "@types/bn.js" "^4.11.5" + bn.js "^5.0.0" + bs58 "^4.0.0" + text-encoding-utf-8 "^1.0.2" + brace-expansion@^1.1.7: version "1.1.11" resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" @@ -1554,7 +1543,7 @@ browserslist@^4.12.0, browserslist@^4.14.5, browserslist@^4.16.3, browserslist@^ escalade "^3.1.1" node-releases "^1.1.71" -bs58@^4.0.1: +bs58@^4.0.0, bs58@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.1.tgz#be161e76c354f6f788ae4071f63f34e8c4f0a42a" dependencies: @@ -1976,7 +1965,7 @@ crypto-browserify@3.12.0, crypto-browserify@^3.11.0: randombytes "^2.0.0" randomfill "^1.0.3" -crypto-hash@*, crypto-hash@^1.2.2, crypto-hash@^1.3.0: +crypto-hash@^1.2.2, crypto-hash@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/crypto-hash/-/crypto-hash-1.3.0.tgz#b402cb08f4529e9f4f09346c3e275942f845e247" @@ -6112,6 +6101,11 @@ test-exclude@^6.0.0: glob "^7.1.4" minimatch "^3.0.4" +text-encoding-utf-8@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/text-encoding-utf-8/-/text-encoding-utf-8-1.0.2.tgz#585b62197b0ae437e3c7b5d0af27ac1021e10d13" + integrity sha512-8bw4MY9WjdsD2aMtO0OzOCY3pXGYNx2d2FfHRVUKkiCPDWjKuOlhLVASS+pD7VkLTVjW268LYJHwsnPFlBpbAg== + text-table@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" @@ -6178,6 +6172,11 @@ toidentifier@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553" +toml@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee" + integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w== + tough-cookie@^2.3.3, tough-cookie@~2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"