Skip to content

Commit

Permalink
v9.1.0: add more solana requests
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Jul 17, 2022
1 parent a9e2acd commit b83d641
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 14 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
8 changes: 7 additions & 1 deletion dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion src/platforms/solana/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)=>{

Expand All @@ -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')])

Expand Down Expand Up @@ -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')])
Expand Down
62 changes: 62 additions & 0 deletions tests/units/solana/request/getProgramAccounts.spec.js
Original file line number Diff line number Diff line change
@@ -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'
])
})
})
})
})
44 changes: 44 additions & 0 deletions tests/units/solana/request/getTokenAccountBalance.spec.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b83d641

Please sign in to comment.