forked from solana-labs/solana-program-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gumball-machine): add SDK and small refactor program
- Loading branch information
Showing
11 changed files
with
539 additions
and
288 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
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,89 @@ | ||
import { | ||
PublicKey | ||
} from '@solana/web3.js'; | ||
import * as borsh from 'borsh'; | ||
import { BN } from '@project-serum/anchor'; | ||
import { readPublicKey } from '../../utils'; | ||
|
||
/** | ||
* Manually create a model for GumballMachine accounts to deserialize manually | ||
*/ | ||
export type OnChainGumballMachine = { | ||
header: GumballMachineHeader, | ||
configData: ConfigData | ||
} | ||
|
||
export const GUMBALL_MACHINE_HEADER_SIZE = 360; | ||
|
||
type GumballMachineHeader = { | ||
urlBase: Buffer, // [u8; 64] | ||
nameBase: Buffer, // [u8; 32] | ||
symbol: Buffer, // [u8; 8] | ||
sellerFeeBasisPoints: number, // u16 | ||
isMutable: boolean, // u8 | ||
retainAuthority: boolean, // u8 | ||
_padding: Buffer, // [u8; 4], | ||
price: BN, // u64 | ||
goLiveDate: BN, // i64 | ||
mint: PublicKey, | ||
botWallet: PublicKey, | ||
receiver: PublicKey, | ||
authority: PublicKey, | ||
collectionKey: PublicKey, | ||
creatorAddress: PublicKey, | ||
extensionLen: BN, // usize | ||
maxMintSize: BN, // u64 | ||
remaining: BN, // usize | ||
maxItems: BN, // u64 | ||
totalItemsAdded: BN, // usize | ||
} | ||
|
||
type ConfigData = { | ||
indexArray: Buffer, | ||
configLines: Buffer | ||
} | ||
|
||
// Deserialize on-chain gumball machine account to OnChainGumballMachine type | ||
export function decodeGumballMachine(buffer: Buffer, accountSize: number): OnChainGumballMachine { | ||
let reader = new borsh.BinaryReader(buffer); | ||
|
||
// Deserialize header | ||
let header: GumballMachineHeader = { | ||
urlBase: Buffer.from(reader.readFixedArray(64)), | ||
nameBase: Buffer.from(reader.readFixedArray(32)), | ||
symbol: Buffer.from(reader.readFixedArray(8)), | ||
sellerFeeBasisPoints: reader.readU16(), | ||
isMutable: !!reader.readU8(), | ||
retainAuthority: !!reader.readU8(), | ||
_padding: Buffer.from(reader.readFixedArray(4)), | ||
price: reader.readU64(), | ||
goLiveDate: new BN(reader.readFixedArray(8), null, 'le'), | ||
mint: readPublicKey(reader), | ||
botWallet: readPublicKey(reader), | ||
receiver: readPublicKey(reader), | ||
authority: readPublicKey(reader), | ||
collectionKey: readPublicKey(reader), | ||
creatorAddress: readPublicKey(reader), | ||
extensionLen: new BN(reader.readFixedArray(8), null, 'le'), // Assume 8 byte size of usize...technically could break | ||
maxMintSize: reader.readU64(), | ||
remaining: new BN(reader.readFixedArray(8), null, 'le'), // Assume 8 byte size of usize...technically could break | ||
maxItems: reader.readU64(), | ||
totalItemsAdded: new BN(reader.readFixedArray(8), null, 'le'), // Assume 8 byte size of usize...technically could break | ||
}; | ||
|
||
// Deserailize indices and config section | ||
let numIndexArrayBytes = header.maxItems.toNumber() * 4; | ||
let numConfigBytes = header.extensionLen.toNumber() * header.maxItems.toNumber(); | ||
let configData: ConfigData = { | ||
indexArray: Buffer.from(reader.readFixedArray(numIndexArrayBytes)), | ||
configLines: Buffer.from(reader.readFixedArray(numConfigBytes)), | ||
} | ||
|
||
if (accountSize != reader.offset) { | ||
throw new Error("Reader processed different number of bytes than account size") | ||
} | ||
return { | ||
header, | ||
configData | ||
} | ||
} |
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,4 @@ | ||
export * from './instructions'; | ||
export * from './accounts'; | ||
export * from './types'; | ||
export * from './utils'; |
Oops, something went wrong.