Skip to content

Commit

Permalink
demo sdk code (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackrieck authored Mar 11, 2022
1 parent 590b0d9 commit 054eccd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ cd app/ && yarn run dev
# navigate to webapp with a browser at http://localhost:3000

# connect to webapp with Phantom and airdrop some SOL for transaction fees
solana airdrop 3 $PHANTOM_WALLET
solana airdrop 100 $PHANTOM_WALLET

# stake deposit tokens
ts-node ./sdk/scripts/stake.ts

# draw winning ticket numbers
ts-node ./sdk/scripts/draw.ts
Expand Down
40 changes: 27 additions & 13 deletions sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface ClientAccounts {
amm: anchor.web3.PublicKey;
ammAuthority: anchor.web3.PublicKey;
poolFee: anchor.web3.PublicKey;
mintAuthority: anchor.web3.Account;
}

// filepath where env file lives
Expand All @@ -38,13 +39,15 @@ export class Client {
public async initialize(
drawDurationSeconds: number,
ticketPrice: number,
userDepositAta: string,
): Promise<string> {
userDepositAta: string
): Promise<void> {
// init accounts
const accounts = await this.createClientAccounts(new anchor.web3.PublicKey(userDepositAta));
const accounts = await this.createClientAccounts(
new anchor.web3.PublicKey(userDepositAta)
);

// init lottery
return this.program.rpc.initialize(
await this.program.rpc.initialize(
new anchor.BN(drawDurationSeconds),
new anchor.BN(ticketPrice),
{
Expand All @@ -62,6 +65,15 @@ export class Client {
},
}
);

await spl.mintTo(
this.program.provider.connection,
accounts.mintAuthority,
accounts.depositMint,
accounts.depositVault,
accounts.mintAuthority.publicKey,
1000
)
}

// buy lottery ticket
Expand All @@ -74,8 +86,8 @@ export class Client {
this.program.provider.wallet.publicKey
);

for (let i = 0; i < count; i++) {
let numbers: Array<number> = [i, 1, 2, 3, 4, 5];
for (let i = 1; i <= count; i++) {
let numbers: Array<number> = [i, 12, 2, 3, 4, 5];

// create ticket PDA
const [ticket, _ticketBump] =
Expand Down Expand Up @@ -196,7 +208,9 @@ export class Client {
}

// create no loss lottery program accounts
private async createClientAccounts(userDepositAtaAddress?: anchor.web3.PublicKey): Promise<ClientAccounts> {
private async createClientAccounts(
userDepositAtaAddress?: anchor.web3.PublicKey
): Promise<ClientAccounts> {
const mintAuthority = await newAccountWithLamports(
this.program.provider.connection
);
Expand Down Expand Up @@ -261,10 +275,10 @@ export class Client {
this.program.provider.connection,
mintAuthority,
depositMint,
userDepositAtaAddress,
userDepositAtaAddress
);

// mint tokens to deployer
// mint tokens to deployer
await spl.mintTo(
this.program.provider.connection,
mintAuthority,
Expand All @@ -279,7 +293,7 @@ export class Client {
this.program.provider.connection,
mintAuthority,
depositMint,
userDepositAtaAddress,
userDepositAtaAddress
);

// mint tokens to user_ata
Expand Down Expand Up @@ -410,6 +424,7 @@ export class Client {
amm: tokenSwapAccount.publicKey,
ammAuthority: tokenSwapAccountAuthority,
poolFee: feeAccount.address,
mintAuthority: mintAuthority,
};

const envFileString = await envfile.stringify(accounts);
Expand Down Expand Up @@ -451,14 +466,13 @@ export class Client {
tickets: new anchor.web3.PublicKey(process.env.tickets),
vaultManager: new anchor.web3.PublicKey(process.env.vaultManager),
userDepositAta: new anchor.web3.PublicKey(process.env.userDepositAta),
swapDepositVault: new anchor.web3.PublicKey(
process.env.swapDepositVault
),
swapDepositVault: new anchor.web3.PublicKey(process.env.swapDepositVault),
swapYieldVault: new anchor.web3.PublicKey(process.env.swapYieldVault),
poolMint: new anchor.web3.PublicKey(process.env.poolMint),
amm: new anchor.web3.PublicKey(process.env.amm),
ammAuthority: new anchor.web3.PublicKey(process.env.ammAuthority),
poolFee: new anchor.web3.PublicKey(process.env.poolFee),
mintAuthority: new anchor.web3.Account,
};

return accounts;
Expand Down
4 changes: 2 additions & 2 deletions sdk/scripts/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ async function initialize() {
// read from env var
const userAddress = process.env.PHANTOM_WALLET;

const initializeTxSig = await lotteryClient.initialize(120, 1, userAddress);
console.log("initializeTxSig:", initializeTxSig);
await lotteryClient.initialize(120, 1, userAddress);
console.log("init complete");
}

initialize();

0 comments on commit 054eccd

Please sign in to comment.