-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setStoreV2 with StoreConfig (#90)
* feat: setStoreV2 with StoreConfig * chore: add test * fix: fix settingsUri type * fix: (store) store init config pda * fix: fix StoreConfig type Co-authored-by: austbot <me@austbot.com>
- Loading branch information
Showing
8 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { Wallet } from '../wallet'; | ||
import { Connection } from '../Connection'; | ||
import { sendTransaction } from './transactions'; | ||
import { SetStoreV2, Store, StoreConfig } from '../programs/metaplex'; | ||
|
||
interface IInitStoreV2Params { | ||
connection: Connection; | ||
wallet: Wallet; | ||
isPublic?: boolean; | ||
settingsUri?: string | null; | ||
} | ||
|
||
interface IInitStoreV2Response { | ||
storeId: PublicKey; | ||
configId: PublicKey; | ||
txId: string; | ||
} | ||
|
||
export const initStoreV2 = async ({ | ||
connection, | ||
wallet, | ||
settingsUri = null, | ||
isPublic = true, | ||
}: IInitStoreV2Params): Promise<IInitStoreV2Response> => { | ||
const storeId = await Store.getPDA(wallet.publicKey); | ||
const configId = await StoreConfig.getPDA(storeId); | ||
const tx = new SetStoreV2( | ||
{ feePayer: wallet.publicKey }, | ||
{ | ||
admin: new PublicKey(wallet.publicKey), | ||
store: storeId, | ||
config: configId, | ||
isPublic, | ||
settingsUri, | ||
}, | ||
); | ||
|
||
const txId = await sendTransaction({ connection, wallet, txs: [tx] }); | ||
|
||
return { storeId, configId, txId }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ERROR_INVALID_ACCOUNT_DATA, ERROR_INVALID_OWNER } from '@metaplex/errors'; | ||
import { AnyPublicKey } from '@metaplex/types'; | ||
import { Borsh } from '@metaplex/utils'; | ||
import { AccountInfo, PublicKey } from '@solana/web3.js'; | ||
import { Buffer } from 'buffer'; | ||
import { Account } from '../../../Account'; | ||
import { MetaplexKey, MetaplexProgram } from '../MetaplexProgram'; | ||
|
||
type Args = { | ||
settingsUri: string; | ||
}; | ||
export class StoreConfigData extends Borsh.Data<Args> { | ||
static readonly SCHEMA = this.struct([ | ||
['key', 'u8'], | ||
['settingsUri', { kind: 'option', type: 'string' }], | ||
]); | ||
|
||
key: MetaplexKey = MetaplexKey.StoreConfigV1; | ||
settingsUri: string; | ||
|
||
constructor(args: Args) { | ||
super(args); | ||
this.key = MetaplexKey.StoreConfigV1; | ||
} | ||
} | ||
|
||
export class StoreConfig extends Account<StoreConfigData> { | ||
constructor(pubkey: AnyPublicKey, info: AccountInfo<Buffer>) { | ||
super(pubkey, info); | ||
|
||
if (!this.assertOwner(MetaplexProgram.PUBKEY)) { | ||
throw ERROR_INVALID_OWNER(); | ||
} | ||
|
||
if (!StoreConfig.isCompatible(this.info.data)) { | ||
throw ERROR_INVALID_ACCOUNT_DATA(); | ||
} | ||
|
||
this.data = StoreConfigData.deserialize(this.info.data); | ||
} | ||
|
||
static isCompatible(data: Buffer) { | ||
return data[0] === MetaplexKey.StoreConfigV1; | ||
} | ||
|
||
static async getPDA(store: AnyPublicKey) { | ||
return MetaplexProgram.findProgramAddress([ | ||
Buffer.from(MetaplexProgram.PREFIX), | ||
MetaplexProgram.PUBKEY.toBuffer(), | ||
Buffer.from(MetaplexProgram.CONFIG), | ||
new PublicKey(store).toBuffer(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'; | ||
import { | ||
PublicKey, | ||
SystemProgram, | ||
SYSVAR_RENT_PUBKEY, | ||
TransactionCtorFields, | ||
TransactionInstruction, | ||
} from '@solana/web3.js'; | ||
import { Borsh } from '@metaplex/utils'; | ||
import { Transaction } from '../../../Transaction'; | ||
import { VaultProgram } from '../../vault'; | ||
import { MetadataProgram } from '../../metadata'; | ||
import { AuctionProgram } from '../../auction'; | ||
import { MetaplexProgram } from '../MetaplexProgram'; | ||
import { ParamsWithStore } from '@metaplex/types'; | ||
|
||
export class SetStoreV2Args extends Borsh.Data<{ public: boolean; settingsUri: string | null }> { | ||
static readonly SCHEMA = this.struct([ | ||
['instruction', 'u8'], | ||
['public', 'u8'], | ||
['settingsUri', { kind: 'option', type: 'string' }], | ||
]); | ||
|
||
instruction = 23; | ||
public: boolean; | ||
settingsUri: string | null; | ||
} | ||
|
||
type SetStoreV2Params = { | ||
admin: PublicKey; | ||
config: PublicKey; | ||
isPublic: boolean; | ||
settingsUri: string | null; | ||
}; | ||
|
||
export class SetStoreV2 extends Transaction { | ||
constructor(options: TransactionCtorFields, params: ParamsWithStore<SetStoreV2Params>) { | ||
super(options); | ||
const { feePayer } = options; | ||
const { admin, store, config, isPublic, settingsUri } = params; | ||
|
||
const data = SetStoreV2Args.serialize({ public: isPublic, settingsUri }); | ||
|
||
this.add( | ||
new TransactionInstruction({ | ||
keys: [ | ||
{ | ||
pubkey: store, | ||
isSigner: false, | ||
isWritable: true, | ||
}, | ||
{ | ||
pubkey: config, | ||
isSigner: false, | ||
isWritable: true, | ||
}, | ||
{ | ||
pubkey: admin, | ||
isSigner: true, | ||
isWritable: false, | ||
}, | ||
{ | ||
pubkey: feePayer, | ||
isSigner: true, | ||
isWritable: false, | ||
}, | ||
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, | ||
{ | ||
pubkey: VaultProgram.PUBKEY, | ||
isSigner: false, | ||
isWritable: false, | ||
}, | ||
{ | ||
pubkey: MetadataProgram.PUBKEY, | ||
isSigner: false, | ||
isWritable: false, | ||
}, | ||
{ | ||
pubkey: AuctionProgram.PUBKEY, | ||
isSigner: false, | ||
isWritable: false, | ||
}, | ||
{ | ||
pubkey: SystemProgram.programId, | ||
isSigner: false, | ||
isWritable: false, | ||
}, | ||
{ | ||
pubkey: SYSVAR_RENT_PUBKEY, | ||
isSigner: false, | ||
isWritable: false, | ||
}, | ||
], | ||
programId: MetaplexProgram.PUBKEY, | ||
data, | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Keypair } from '@solana/web3.js'; | ||
import { Connection, NodeWallet } from '../../src'; | ||
import { initStore, initStoreV2 } from '../../src/actions'; | ||
import { FEE_PAYER, NETWORK } from '../utils'; | ||
import { Store } from '../../src/programs/metaplex'; | ||
import { uri } from './shared'; | ||
|
||
describe('init Store', () => { | ||
const connection = new Connection(NETWORK); | ||
const wallet = new NodeWallet(FEE_PAYER); | ||
let mint: Keypair; | ||
|
||
beforeEach(() => { | ||
mint = Keypair.generate(); | ||
jest.spyOn(Keypair, 'generate').mockReturnValue(mint); | ||
}); | ||
|
||
test('creates store with initStore', async () => { | ||
const storeResponse = await initStore({ | ||
connection, | ||
wallet, | ||
isPublic: false, | ||
}); | ||
|
||
const storeId = await Store.getPDA(wallet.publicKey); | ||
|
||
expect(storeResponse).toMatchObject({ | ||
storeId, | ||
}); | ||
}); | ||
|
||
test('creates store with initStoreV2', async () => { | ||
const storeResponse = await initStoreV2({ | ||
connection, | ||
wallet, | ||
isPublic: false, | ||
settingsUri: uri, | ||
}); | ||
|
||
const storeId = await Store.getPDA(wallet.publicKey); | ||
|
||
expect(storeResponse).toMatchObject({ | ||
storeId, | ||
}); | ||
}); | ||
|
||
test('creates store with initStoreV2 without storeV2', async () => { | ||
const storeResponse = await initStoreV2({ | ||
connection, | ||
wallet, | ||
isPublic: false, | ||
}); | ||
|
||
const storeId = await Store.getPDA(wallet.publicKey); | ||
|
||
expect(storeResponse).toMatchObject({ | ||
storeId, | ||
}); | ||
}); | ||
}); |