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

Feature/positions2 WIP #6

Merged
merged 21 commits into from
Mar 8, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/anchor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ jobs:
run: ./prepare.sh
- name: Anchor test
working-directory: ./staking
run: anchor test
run: yarn test

15 changes: 0 additions & 15 deletions staking/Anchor.toml

This file was deleted.

1 change: 1 addition & 0 deletions staking/Anchor.toml
18 changes: 18 additions & 0 deletions staking/app/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This Anchor.toml file is used to setup programs and accounts for UI development
# Instead of running the normal test suite, we are running app/setup.ts
# Instead of reinventing the wheel, we are using anchor test to facilitate deployment
[features]
seeds = true

[programs.localnet]
staking = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"

[registry]
url = "https://anchor.projectserum.com"

[provider]
cluster = "localnet"
wallet = "./tests/default_wallet.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 app/setup.ts"
ptaffet marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions staking/app/keypairs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe ignore the entire keypairs/ directory? This directory only exists for testing purposes right?

209 changes: 209 additions & 0 deletions staking/app/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { Staking } from "../target/types/staking";
import { positions_account_size } from "../tests/utils/constant";
import {
TOKEN_PROGRAM_ID,
Token,
ASSOCIATED_TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import {
PublicKey,
Keypair,
Transaction,
SystemProgram,
} from "@solana/web3.js";
import { createMint } from "../tests/utils/utils";
import BN from "bn.js";
import fs from "fs"

describe("setup", async () => {
let program: Program<Staking>;

const pyth_mint_account = new Keypair();
const pyth_mint_authority = new Keypair();

const alice = new Keypair();
const bob = new Keypair();

const alice_stake_account = new Keypair();
const bob_stake_account = new Keypair();

const alice_ata = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
pyth_mint_account.publicKey,
alice.publicKey
);

const bob_ata = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
pyth_mint_account.publicKey,
bob.publicKey
);

const provider = anchor.Provider.local();

before(async () => {

fs.writeFileSync(`./app/keypairs/alice.json`, JSON.stringify(alice));
fs.writeFileSync(`./app/keypairs/bob.json`, JSON.stringify(bob));
fs.writeFileSync(`./app/keypairs/pyth_mint.json`, JSON.stringify(pyth_mint_account.publicKey.toBase58()));

anchor.setProvider(anchor.Provider.env());
program = anchor.workspace.Staking as Program<Staking>;


});

it("initializes config", async () => {
await createMint(
provider,
pyth_mint_account,
pyth_mint_authority.publicKey,
null,
0,
TOKEN_PROGRAM_ID
);

await program.methods
.initConfig({
governanceAuthority: provider.wallet.publicKey,
pythTokenMint: pyth_mint_account.publicKey,
unlockingDuration: 2,
// Epoch time set to 1 second
epochDuration: new BN(1),
})
.rpc();
});

it("alice and bob receive sol", async () => {
for (let owner of [alice.publicKey, bob.publicKey]) {
await provider.connection.requestAirdrop(owner, 1_000_000_000_000);
}
});

it("alice and bob receive tokens", async () => {
const transaction = new Transaction();

for (let [owner, to_account] of [
[alice.publicKey, alice_ata],
[bob.publicKey, bob_ata],
]) {
const create_ata_ix = Token.createAssociatedTokenAccountInstruction(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
pyth_mint_account.publicKey,
to_account,
owner,
provider.wallet.publicKey
);
transaction.add(create_ata_ix);

// Mint 1000 tokens.
const mint_ix = Token.createMintToInstruction(
TOKEN_PROGRAM_ID,
pyth_mint_account.publicKey,
to_account,
pyth_mint_authority.publicKey,
[],
1000
);

transaction.add(mint_ix);
}

const tx = await provider.send(transaction, [pyth_mint_authority]);
});

it("alice and bob get staking accounts", async () => {
for (let user of [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(comment for the future) would be nice to make a typescript API for the staking program to wrap up these transactions into easy-to-use methods.

Definitely do not make this change as part of this PR.

{ owner: alice.publicKey, stake_account: alice_stake_account },
{ owner: bob.publicKey, stake_account: bob_stake_account },
]) {
const tx = await program.methods
.createStakeAccount(user.owner, { fullyVested: {} })
.preInstructions([
SystemProgram.createAccount({
fromPubkey: provider.wallet.publicKey,
newAccountPubkey: user.stake_account.publicKey,
lamports:
await provider.connection.getMinimumBalanceForRentExemption(
positions_account_size
),
space: positions_account_size,
programId: program.programId,
}),
])
.accounts({
stakeAccountPositions: user.stake_account.publicKey,
mint: pyth_mint_account.publicKey,
})
.signers([user.stake_account])
.rpc();
}
});

it("alice and bob deposit tokens", async () => {
for (let user of [
{
owner: alice,
stake_account: alice_stake_account,
from_account: alice_ata,
},
{
owner: bob,
stake_account: bob_stake_account,
from_account: bob_ata,
},
]) {
const transaction = new Transaction();

const to_account = (
await PublicKey.findProgramAddress(
[
anchor.utils.bytes.utf8.encode("custody"),
user.stake_account.publicKey.toBuffer(),
],
program.programId
)
)[0];

const ix = Token.createTransferInstruction(
TOKEN_PROGRAM_ID,
user.from_account,
to_account,
user.owner.publicKey,
[],
1000
);
transaction.add(ix);
const tx = await provider.send(transaction, [user.owner]);
}
});

it("alice and bob lock their tokens", async () => {
for (let user of [
{
owner: alice,
stake_account: alice_stake_account,
from_account: alice_ata,
},
{
owner: bob,
stake_account: bob_stake_account,
from_account: bob_ata,
},
]) {
const tx = await program.methods
.createPosition(null, null, new BN(1))
.accounts({
payer: user.owner.publicKey,
stakeAccountPositions: user.stake_account.publicKey,
})
.signers([user.owner])
.rpc();
}
});
});
4 changes: 4 additions & 0 deletions staking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
"mocha": "^9.0.3",
"ts-mocha": "^8.0.0",
"typescript": "^4.3.5"
},
"scripts": {
"test" : "ln -f -s ./tests/Anchor.toml ./Anchor.toml && anchor test",
"setup_ui" : "ln -f -s ./app/Anchor.toml ./Anchor.toml && anchor test --detach"
}
}
1 change: 1 addition & 0 deletions staking/programs/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ default = []
[dependencies]
anchor-lang = "0.22.0"
anchor-spl = "0.22.0"

Loading