Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create @audius/spl package #6334

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ jobs:
- ./packages/harmony/dist
- ./packages/common/dist
- ./packages/libs/dist
- ./packages/spl/dist

generate-release:
working_directory: ~/audius-protocol
Expand Down
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"private": true,
"scripts": {
"build": "turbo run build --filter=audius-client^... --filter=audius-mobile-client^...",
"build": "turbo run build --filter=audius-client^... --filter=audius-mobile-client^... --filter=identity-service^...",
"check-secrets": "sh ./scripts/check_secrets.sh",
"clean:modules": "rm -rf node_modules && rm -rf packages/*/node_modules",
"clean": "npm run clean:auto -w audius-mobile-client && npm run clean:modules",
Expand Down
3 changes: 2 additions & 1 deletion packages/compose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
},
"dependencies": {
"@audius/sdk": "*",
"@audius/spl": "*",
"@pedalboard/relay": "*",
"@pedalboard/sla-auditor": "*",
"@pedalboard/trending-challenge-rewards": "*"
},
"license": "ISC"
}
}
1 change: 1 addition & 0 deletions packages/identity-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@amplitude/node": "1.9.2",
"@audius/sdk": "*",
"@audius/spl": "*",
"@certusone/wormhole-sdk": "0.1.1",
"@improbable-eng/grpc-web-node-http-transport": "0.15.0",
"@opentelemetry/api": "1.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import {
} from './programs/associatedToken'
import {
decodeClaimableTokenInstruction,
isTransferClaimableTokenInstruction
} from './programs/claimable-tokens'
import { decodeRewardManagerInstruction } from './programs/reward-manager'
isTransferClaimableTokenInstruction,
decodeRewardManagerInstruction
} from '@audius/spl'
import config from '../../config'

const MEMO_PROGRAM_ID = 'Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo'
Expand Down
10 changes: 4 additions & 6 deletions packages/identity-service/test/routes/solanaRelayTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ import config from '../../src/config'
import audiusLibsWrapper from '../../src/audiusLibsInstance'
import {
createClaimableTokenAccountInstruction,
createTransferClaimableTokenInstruction
} from '../../src/typed-routes/solana/programs/claimable-tokens'
import {
createTransferClaimableTokenInstruction,
createEvaluateAttestationsInstruction,
createSenderPublicInstruction,
createSubmitAttestationInstruction
} from '../../src/typed-routes/solana/programs/reward-manager'
import { RewardManagerInstruction } from '../../src/typed-routes/solana/programs/reward-manager/constants'
createSubmitAttestationInstruction,
RewardManagerInstruction
} from '@audius/spl'
import { InvalidRelayInstructionError } from '../../src/typed-routes/solana/InvalidRelayInstructionError'

const CLAIMABLE_TOKEN_PROGRAM_ID = new PublicKey(
Expand Down
5 changes: 5 additions & 0 deletions packages/spl/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"extends": "audius"
}
9 changes: 9 additions & 0 deletions packages/spl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `@audius/spl`

A Typescript package for creating and decoding Solana `TransactionInstruction`s for the Audius [Solana Programs](../../solana-programs/).

This is useful both for making Typescript/Javascript clients as well as for making indexers for the programs, and also lends itself to writing tests for indexers.

## Usage

See [solanaRelayTest.ts](../identity-service/test/routes/solanaRelayTest.ts) for example usages.
30 changes: 30 additions & 0 deletions packages/spl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"private": true,
"name": "@audius/spl",
"version": "0.0.0",
"description": "The core Typescript mappings to the Audius Solana Programs",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"scripts": {
"start": "tsc --build --verbose --watch tsconfig.all.json",
"build": "tsc --build --verbose tsconfig.all.json",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AudiusProject/audius-protocol/tree/main/packages/spl"
},
"author": "Audius",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/AudiusProject/audius-protocol/issues"
},
"homepage": "https://github.com/AudiusProject/audius-protocol/tree/main/packages/spl",
"dependencies": {
"@solana/buffer-layout": "4.0.1",
"@solana/buffer-layout-utils": "0.2.0",
"@solana/spl-token": "0.3.8",
"@solana/web3.js": "1.78.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
TransactionInstruction,
AccountMeta
} from '@solana/web3.js'
import { ethAddress } from '../../layout-utils'

import { ethAddress } from '../layout-utils'

import {
CLAIMABLE_TOKEN_PROGRAM_ID,
ClaimableTokenInstruction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TransactionInstruction } from '@solana/web3.js'

import { ClaimableTokenInstruction } from './constants'
import {
DecodedCreateClaimableTokenAccountInstruction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './constants'
export * from './decode'
export * from './create'
export * from './transfer'
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TransactionInstruction,
AccountMeta
} from '@solana/web3.js'

import {
ClaimableTokenInstruction,
CLAIMABLE_TOKEN_PROGRAM_ID
Expand Down
2 changes: 2 additions & 0 deletions packages/spl/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './claimable-tokens'
export * from './reward-manager'
91 changes: 91 additions & 0 deletions packages/spl/src/layout-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
Blob,
Layout,
blob,
u32,
uint8ArrayToBuffer
} from '@solana/buffer-layout'

/**
* Wrapper that makes Ethereum wallet addresses encoded and decoded as 20 byte blobs
*/
export class EthereumAddress extends Layout<string> {
private blob: Blob
constructor(property?: string) {
super(20, property)
this.blob = blob(20, property)
}

/** @override */
getSpan(b: Uint8Array, offset?: number) {
return this.blob.getSpan(b, offset)
}

/**
* Proxies the decoding to the underlying Blob, then
* converts the buffer to hex and prepends '0x'.
* @override
* */
decode(b: Uint8Array, offset = 0): string {
const buffer = this.blob.decode(b, offset)
return '0x' + Buffer.from(buffer).toString('hex')
}

/**
* Strips the 0x and converts the address to a buffer, then
* proxies the encoding to the underlying Blob.
* @override
* */
encode(src: string, b: Uint8Array, offset: number): number {
const strippedEthAddress = src.replace('0x', '')
// Need to pad the array to length 20 - otherwise, hex eth keys starting with '0' would
// result in truncated arrays, while eth spec is always 20 bytes
const buf = Buffer.from(strippedEthAddress, 'hex')
const fixedBuf = Buffer.alloc(20, 0)
buf.copy(fixedBuf, 20 - buf.length)
return this.blob.encode(buf, b, offset)
}
}

/**
* Wrapper that encodes strings the way Borsh does, with the length prepended
*/
export class BorshString extends Layout<string> {
constructor(maxLength: number, property?: string) {
super(u32().span + maxLength, property)
}

getSpan(b: Uint8Array, offset = 0): number {
if (!b) {
return this.span
}
const length = u32().decode(b, offset)
return u32().span + length
}

decode(b: Uint8Array, offset = 0): string {
const length = u32().decode(b, offset)
const value = blob(length).decode(b, offset + u32().span)
return uint8ArrayToBuffer(value).toString('utf-8')
}

encode(src: string, b: Uint8Array, offset: number): number {
const srcb = Buffer.from(src, 'utf-8')
if (srcb.length > this.span) {
throw new RangeError('text exceeds maxLength')
}
if (offset + srcb.length > b.length) {
throw new RangeError('text length exceeds buffer')
}
return (
u32().encode(srcb.length, b, offset) +
blob(srcb.length).encode(srcb, b, offset + u32().span)
)
}
}

/** Factory for EthereumAddress layouts */
export const ethAddress = (property?: string) => new EthereumAddress(property)
/** Factory for BorshString layouts */
export const borshString = (maxLength: number, property?: string) =>
new BorshString(maxLength, property)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
SYSVAR_RENT_PUBKEY,
SystemProgram
} from '@solana/web3.js'
import { ethAddress } from '../../layout-utils'

import { ethAddress } from '../layout-utils'

import {
REWARD_MANAGER_PROGRAM_ID,
RewardManagerInstruction
Expand All @@ -19,7 +21,7 @@ type CreateSenderPublicInstructionData = {
operatorEthAddress: string
}

const CreateSenderPublicInstructionData =
const createSenderPublicInstructionData =
struct<CreateSenderPublicInstructionData>([
u8('instruction'),
ethAddress('senderEthAddress'),
Expand All @@ -36,8 +38,8 @@ export const createSenderPublicInstruction = (
existingSenders: PublicKey[],
rewardManagerProgramId: PublicKey = REWARD_MANAGER_PROGRAM_ID
) => {
const data = Buffer.alloc(CreateSenderPublicInstructionData.span)
CreateSenderPublicInstructionData.encode(
const data = Buffer.alloc(createSenderPublicInstructionData.span)
createSenderPublicInstructionData.encode(
{
instruction: RewardManagerInstruction.CreateSenderPublic,
senderEthAddress,
Expand Down Expand Up @@ -106,5 +108,5 @@ export const decodeCreateSenderPublicInstruction = ({
systemProgramId,
existingSenders
},
data: CreateSenderPublicInstructionData.decode(data)
data: createSenderPublicInstructionData.decode(data)
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TransactionInstruction } from '@solana/web3.js'

import { RewardManagerInstruction } from './constants'
import {
DecodedCreateSenderPublicInstruction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PublicKey, AccountMeta, TransactionInstruction } from '@solana/web3.js'

import { RewardManagerInstruction } from './constants'

export type DecodedDeleteSenderPublicInstruction = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { struct, u8, cstr } from '@solana/buffer-layout'
import { struct, u8 } from '@solana/buffer-layout'
import { u64 } from '@solana/buffer-layout-utils'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import {
PublicKey,
AccountMeta,
TransactionInstruction,
SYSVAR_RENT_PUBKEY,
SystemProgram
} from '@solana/web3.js'
import { borshString, ethAddress } from '../../layout-utils'

import { borshString, ethAddress } from '../layout-utils'

import {
REWARD_MANAGER_PROGRAM_ID,
RewardManagerInstruction
} from './constants'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'

type EvaluateAttestationsInstructionData = {
instruction: RewardManagerInstruction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './constants'
export * from './decode'
export * from './createSenderPublic'
export * from './deleteSenderPublic'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
SYSVAR_INSTRUCTIONS_PUBKEY,
SystemProgram
} from '@solana/web3.js'

import { borshString } from '../layout-utils'

import {
REWARD_MANAGER_PROGRAM_ID,
RewardManagerInstruction
} from './constants'
import { borshString } from '../../layout-utils'

type SubmitAttestationInstructionData = {
instruction: RewardManagerInstruction
Expand Down
Loading