From b83d64165e0c82d4a5c7ba6e3fc86ce36b29f4b2 Mon Sep 17 00:00:00 2001 From: Sebastian Pape Date: Sun, 17 Jul 2022 13:01:32 +0200 Subject: [PATCH] v9.1.0: add more solana requests --- README.md | 29 ++++++++- dist/esm/index.js | 8 ++- dist/umd/index.js | 8 ++- package.json | 4 +- src/platforms/solana/request.js | 8 ++- ...account.spec.js => getAccountInfo.spec.js} | 46 +++++++++++++- .../solana/request/getProgramAccounts.spec.js | 62 +++++++++++++++++++ .../request/getTokenAccountBalance.spec.js | 44 +++++++++++++ yarn.lock | 8 +-- 9 files changed, 203 insertions(+), 14 deletions(-) rename tests/units/solana/request/{account.spec.js => getAccountInfo.spec.js} (60%) create mode 100644 tests/units/solana/request/getProgramAccounts.spec.js create mode 100644 tests/units/solana/request/getTokenAccountBalance.spec.js diff --git a/README.md b/README.md index 4526d14..c0a7fcb 100644 --- a/README.md +++ b/README.md @@ -64,13 +64,36 @@ request('ethereum://0x7a250d5630b4cf539739df2c5dacb4c659f2488d/getAmountsOut', { ##### Solana: request data +###### Solana: getAccountInfo + +Either pass `/getAccountInfo` as explicit method, or pass no method to a request and it will perform a `getAccountInfo` explicitly. + ```javascript import { request } from '@depay/web3-client' import { struct, publicKey, u64, u32, u8 } from '@depay/solana-web3.js' -request('solana://EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', { - api: struct([ publicKey('mint'), publicKey('owner'), u64('amount'), u32('delegateOption'), publicKey('delegate'), u8('state'), u32('isNativeOption'), u64('isNative'), u64('delegatedAmount'), u32('closeAuthorityOption'), publicKey('closeAuthority')]) -}).then((value)=>console.log(value)) +let api = struct([ publicKey('mint'), publicKey('owner'), u64('amount'), u32('delegateOption'), publicKey('delegate'), u8('state'), u32('isNativeOption'), u64('isNative'), u64('delegatedAmount'), u32('closeAuthorityOption'), publicKey('closeAuthority')]) + +request('solana://EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', { api }) +request('solana://EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v/getAccountInfo', { api }) +``` + +###### Solana: other methods + +For all other methods just pass them: + +```javascript + +let wallet = '2UgCJaHU5y8NC4uWQcZYeV9a5RyYLF7iKYCybCsdFFD1' +let mint = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' + +let filters = [ + { dataSize: 165 }, + { memcmp: { offset: 32, bytes: wallet }}, + { memcmp: { offset: 0, bytes: mint }}, +] + +request('solana://TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA/getProgramAccounts', { api, params: { filters } }) ``` #### request account balance diff --git a/dist/esm/index.js b/dist/esm/index.js index 916cbd8..e80b22d 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -15284,7 +15284,13 @@ let balance = ({ address, provider }) => { var request$1 = async ({ provider, address, api, method, params, block }) => { if (api) { - return accountInfo({ address, api, method, params, provider, block }) + if(method == undefined || method === 'getAccountInfo') { + return accountInfo({ address, api, method, params, provider, block }) + } + } else if(method === 'getProgramAccounts') { + return provider.getProgramAccounts(new PublicKey(address), params) + } else if(method === 'getTokenAccountBalance') { + return provider.getTokenAccountBalance(new PublicKey(address)) } else if (method === 'latestBlockNumber') { return provider.getBlockHeight() } else if (method === 'balance') { diff --git a/dist/umd/index.js b/dist/umd/index.js index a943d4f..9b9ba33 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -15286,7 +15286,13 @@ var request$1 = async ({ provider, address, api, method, params, block }) => { if (api) { - return accountInfo({ address, api, method, params, provider, block }) + if(method == undefined || method === 'getAccountInfo') { + return accountInfo({ address, api, method, params, provider, block }) + } + } else if(method === 'getProgramAccounts') { + return provider.getProgramAccounts(new solanaWeb3_js.PublicKey(address), params) + } else if(method === 'getTokenAccountBalance') { + return provider.getTokenAccountBalance(new solanaWeb3_js.PublicKey(address)) } else if (method === 'latestBlockNumber') { return provider.getBlockHeight() } else if (method === 'balance') { diff --git a/package.json b/package.json index 4d34918..f45dd6b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client", "moduleName": "Web3Client", - "version": "9.0.1", + "version": "9.1.0", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.js", "module": "dist/esm/index.js", @@ -46,7 +46,7 @@ "@depay/solana-web3.js": "^1.5.0", "@depay/web3-blockchains": "^5.0.2", "@depay/web3-constants": "^6.1.1", - "@depay/web3-mock": "^13.3.0", + "@depay/web3-mock": "^13.6.0", "@rollup/plugin-commonjs": "^22.0.1", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.3.0", diff --git a/src/platforms/solana/request.js b/src/platforms/solana/request.js index 0781201..1bd4807 100644 --- a/src/platforms/solana/request.js +++ b/src/platforms/solana/request.js @@ -11,7 +11,13 @@ let balance = ({ address, provider }) => { export default async ({ provider, address, api, method, params, block }) => { if (api) { - return accountInfo({ address, api, method, params, provider, block }) + if(method == undefined || method === 'getAccountInfo') { + return accountInfo({ address, api, method, params, provider, block }) + } + } else if(method === 'getProgramAccounts') { + return provider.getProgramAccounts(new PublicKey(address), params) + } else if(method === 'getTokenAccountBalance') { + return provider.getTokenAccountBalance(new PublicKey(address)) } else if (method === 'latestBlockNumber') { return provider.getBlockHeight() } else if (method === 'balance') { diff --git a/tests/units/solana/request/account.spec.js b/tests/units/solana/request/getAccountInfo.spec.js similarity index 60% rename from tests/units/solana/request/account.spec.js rename to tests/units/solana/request/getAccountInfo.spec.js index c30d759..c35f63c 100644 --- a/tests/units/solana/request/account.spec.js +++ b/tests/units/solana/request/getAccountInfo.spec.js @@ -3,7 +3,7 @@ import { request, provider, resetCache } from 'src/' import { struct, publicKey, u64, u32, u8, PublicKey } from '@depay/solana-web3.js' import { supported } from 'src/blockchains' -describe('request balance', () => { +describe('request getAccountInfo', () => { supported.solana.forEach((blockchain)=>{ @@ -14,7 +14,7 @@ describe('request balance', () => { beforeEach(resetCache) beforeEach(()=>mock({ blockchain, accounts: { return: accounts } })) - it('should request account data', async ()=> { + it('should request account data implicitly if just the address is provided', async ()=> { let api = struct([ publicKey('mint'), publicKey('owner'), u64('amount'), u32('delegateOption'), publicKey('delegate'), u8('state'), u32('isNativeOption'), u64('isNative'), u64('delegatedAmount'), u32('closeAuthorityOption'), publicKey('closeAuthority')]) @@ -56,6 +56,48 @@ describe('request balance', () => { expect(data.closeAuthority.toString()).toEqual('11111111111111111111111111111111') }) + it('should request account data explicitly too', async ()=> { + + let api = struct([ publicKey('mint'), publicKey('owner'), u64('amount'), u32('delegateOption'), publicKey('delegate'), u8('state'), u32('isNativeOption'), u64('isNative'), u64('delegatedAmount'), u32('closeAuthorityOption'), publicKey('closeAuthority')]) + + let requestMock = mock({ + provider: provider(blockchain), + blockchain, + request: { + method: 'getAccountInfo', + to: '2wmVCSfPxGPjrnMMn7rchp4uaeoTqN39mXFC2zhPdri9', + api, + return: { + mint: '8rUUP52Bb6Msg6E14odyPWUFafi5wLEMpLjtmNfBp3r', + owner: 'Cq7CPoJ3b84nANKnz61HCCywSMVJNbRzmoaqvAxBi4vX', + amount: '2511210038936013080', + delegateOption: 70962703, + delegate: 'BSFGxQ38xesdoUd3qsvNhjRu2FLPq9CwCBiGE42fc9hR', + state: 0, + isNativeOption: 0, + isNative: '0', + delegatedAmount: '0', + closeAuthorityOption: 0, + closeAuthority: '11111111111111111111111111111111' + } + } + }) + + let data = await request('solana://2wmVCSfPxGPjrnMMn7rchp4uaeoTqN39mXFC2zhPdri9/getAccountInfo', { api }) + + expect(data.mint.toString()).toEqual('8rUUP52Bb6Msg6E14odyPWUFafi5wLEMpLjtmNfBp3r') + expect(data.owner.toString()).toEqual('Cq7CPoJ3b84nANKnz61HCCywSMVJNbRzmoaqvAxBi4vX') + expect(data.amount.toString()).toEqual('2511210038936013080') + expect(data.delegateOption).toEqual(70962703) + expect(data.delegate.toString()).toEqual('BSFGxQ38xesdoUd3qsvNhjRu2FLPq9CwCBiGE42fc9hR') + expect(data.state).toEqual(0) + expect(data.isNativeOption).toEqual(0) + expect(data.isNative.toString()).toEqual('0') + expect(data.delegatedAmount.toString()).toEqual('0') + expect(data.closeAuthorityOption).toEqual(0) + expect(data.closeAuthority.toString()).toEqual('11111111111111111111111111111111') + }) + it('should fail', async ()=> { let api = struct([ publicKey('mint'), publicKey('owner'), u64('amount'), u32('delegateOption'), publicKey('delegate'), u8('state'), u32('isNativeOption'), u64('isNative'), u64('delegatedAmount'), u32('closeAuthorityOption'), publicKey('closeAuthority')]) diff --git a/tests/units/solana/request/getProgramAccounts.spec.js b/tests/units/solana/request/getProgramAccounts.spec.js new file mode 100644 index 0000000..a27baff --- /dev/null +++ b/tests/units/solana/request/getProgramAccounts.spec.js @@ -0,0 +1,62 @@ +import { mock, resetMocks } from '@depay/web3-mock' +import { request, provider, resetCache } from 'src/' +import { struct, publicKey, u64, u32, u8, PublicKey, Buffer } from '@depay/solana-web3.js' +import { supported } from 'src/blockchains' + +describe('request getProgramAccounts', () => { + + supported.solana.forEach((blockchain)=>{ + + describe(blockchain, ()=> { + + const accounts = ['2UgCJaHU5y8NC4uWQcZYeV9a5RyYLF7iKYCybCsdFFD1'] + beforeEach(resetMocks) + beforeEach(resetCache) + beforeEach(()=>mock({ blockchain, accounts: { return: accounts } })) + + it('requests getProgramAccounts with given filters', async ()=> { + + let wallet = '2wmVCSfPxGPjrnMMn7rchp4uaeoTqN39mXFC2zhPdri9' + let mint = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' + + let filters = [ + { dataSize: 165 }, + { memcmp: { offset: 32, bytes: wallet }}, + { memcmp: { offset: 0, bytes: mint }} + ] + + let requestMock = mock({ + provider: provider(blockchain), + blockchain, + request: { + method: 'getProgramAccounts', + to: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', + params: { filters }, + return: [ + { + account: { data: new Buffer([]), executable: false, lamports: 2039280, owner: mint, rentEpoch: 327 }, + pubkey: '3JdKXacGdntfNKXzSGC2EwUDKFPrXdsqowbuc9hEiNBb' + }, { + account: { data: new Buffer([]), executable: false, lamports: 2039280, owner: mint, rentEpoch: 327 }, + pubkey: 'FjtHL8ki3GXMhCqY2Lum9CCAv5tSQMkhJEnXbEkajTrZ' + }, { + account: { data: new Buffer([]), executable: false, lamports: 2039280, owner: mint, rentEpoch: 327 }, + pubkey: 'F7e4iBrxoSmHhEzhuBcXXs1KAknYvEoZWieiocPvrCD9' + } + ] + } + }) + + let accounts = await request('solana://TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA/getProgramAccounts', { + params: { filters } + }) + + expect(accounts.map((account)=>account.pubkey.toString())).toEqual([ + '3JdKXacGdntfNKXzSGC2EwUDKFPrXdsqowbuc9hEiNBb', + 'FjtHL8ki3GXMhCqY2Lum9CCAv5tSQMkhJEnXbEkajTrZ', + 'F7e4iBrxoSmHhEzhuBcXXs1KAknYvEoZWieiocPvrCD9' + ]) + }) + }) + }) +}) diff --git a/tests/units/solana/request/getTokenAccountBalance.spec.js b/tests/units/solana/request/getTokenAccountBalance.spec.js new file mode 100644 index 0000000..2f17808 --- /dev/null +++ b/tests/units/solana/request/getTokenAccountBalance.spec.js @@ -0,0 +1,44 @@ +import { mock, resetMocks } from '@depay/web3-mock' +import { request, provider, resetCache } from 'src/' +import { struct, publicKey, u64, u32, u8, PublicKey, Buffer } from '@depay/solana-web3.js' +import { supported } from 'src/blockchains' + +describe('request getTokenAccountBalance', () => { + + supported.solana.forEach((blockchain)=>{ + + describe(blockchain, ()=> { + + const accounts = ['2UgCJaHU5y8NC4uWQcZYeV9a5RyYLF7iKYCybCsdFFD1'] + beforeEach(resetMocks) + beforeEach(resetCache) + beforeEach(()=>mock({ blockchain, accounts: { return: accounts } })) + + it('requests getTokenAccountBalance with given filters', async ()=> { + + let tokenAccount = '2wmVCSfPxGPjrnMMn7rchp4uaeoTqN39mXFC2zhPdri9' + + let returnedBalance = { + amount: "10000617", + decimals: 6, + uiAmount: 10.000617, + uiAmountString: "10.000617" + } + + let requestMock = mock({ + provider: provider(blockchain), + blockchain, + request: { + method: 'getTokenAccountBalance', + to: tokenAccount, + return: returnedBalance + } + }) + + let balance = await request(`solana://${tokenAccount}/getTokenAccountBalance`) + + expect(balance.value).toEqual(returnedBalance) + }) + }) + }) +}) diff --git a/yarn.lock b/yarn.lock index 2ab7e58..8de57d5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -911,10 +911,10 @@ resolved "https://registry.yarnpkg.com/@depay/web3-constants/-/web3-constants-6.1.1.tgz#4ffa1f63f5af1ac359c6d44eddd8759c58d1f900" integrity sha512-HWRAGZ3x1Xgs0BXjNeARNsvh4pGZBASK+n6tRg5irpxA+e6OW/ncrmva+eA90dMJuCe2b+tJRfJWsFKRrAXJlw== -"@depay/web3-mock@^13.3.0": - version "13.3.0" - resolved "https://registry.yarnpkg.com/@depay/web3-mock/-/web3-mock-13.3.0.tgz#2d216637d8ad62d937b8f329a8a9070b99d83792" - integrity sha512-2yJNj7Y98DeYNXQbgnsw5BtllwSt/DmVSjdd1uJjXkSkQ03OHkYbjqxstYei7nYX6vVyZwKBO9cQ/D3w7XyUJg== +"@depay/web3-mock@^13.6.0": + version "13.6.0" + resolved "https://registry.yarnpkg.com/@depay/web3-mock/-/web3-mock-13.6.0.tgz#7167458916394a3f234b86f760889b9edce709af" + integrity sha512-b+kGqK9eQUfZFCiVVzFSw/HDZ+JPzF+IDBlZL7pSihjwx/N0fC9Wfn2hTsDzSRjYCEO7IXWXmQGw9HXzKBMqWw== "@eslint/eslintrc@^0.4.3": version "0.4.3"