Skip to content

Commit 3cf5949

Browse files
authored
fix: anchor action (#104)
* fixed anchor node binary cache issue, tests skipping on main push * solana releases url update * ts format fix * fix node cache version
1 parent 21a52cb commit 3cf5949

File tree

3 files changed

+84
-152
lines changed

3 files changed

+84
-152
lines changed

.github/workflows/anchor.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
3737
- name: Setup Anchor
3838
if: steps.changes.outputs.anchor == 'true' || steps.changes.outputs.anchor_action == 'true' || github.event_name == 'schedule' || github.event_name == 'push'
39-
uses: heyAyushh/setup-anchor@v3.12
39+
uses: heyAyushh/setup-anchor@v4.0
4040
with:
4141
anchor-version: ${{ matrix.anchor-version }}
4242
solana-cli-version: ${{ matrix.solana-version }}
@@ -154,13 +154,13 @@ jobs:
154154
# Skip Installing and Displaying versions if theres no change in anchor programs or anchor action workflow file or isn't a schedule event
155155
- name: Setup Anchor
156156
if: steps.changes.outputs.anchor == 'true' || steps.changes.outputs.anchor_action == 'true' || github.event_name == 'schedule' || github.event_name == 'push'
157-
uses: heyAyushh/setup-anchor@v3.12
157+
uses: heyAyushh/setup-anchor@v4.0
158158
with:
159159
anchor-version: ${{ matrix.anchor-version }}
160160
solana-cli-version: ${{ matrix.solana-version }}
161161
node-version: ${{ matrix.node-version }}
162162
- name: Display versions and Install pnpm
163-
if: steps.changes.outputs.anchor == 'true' || steps.changes.outputs.anchor_action == 'true' || github.event_name == 'schedule'
163+
if: steps.changes.outputs.anchor == 'true' || steps.changes.outputs.anchor_action == 'true' || github.event_name == 'schedule' || github.event_name == 'push'
164164
run: |
165165
solana -V
166166
solana-keygen new --no-bip39-passphrase
@@ -201,7 +201,7 @@ jobs:
201201
shell: bash
202202
# Skip Testing all Programs if it's a CRON job or a change in the workflow file
203203
- name: Test All Anchor Programs
204-
if: github.event_name == 'schedule' || steps.changes.outputs.anchor_action == 'true'
204+
if: github.event_name == 'schedule' || github.event_name == 'push' || steps.changes.outputs.anchor_action == 'true'
205205
run: |
206206
declare -a ProjectDirs=($(find . -type d -name "anchor"| grep -v -f <(grep . .github/.ghaignore | grep -v '^$')))
207207
echo "Projects to Test:"

.github/workflows/solana-native.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
with:
2626
node-version: ${{ matrix.node-version }}
2727
check-latest: true
28-
- uses: heyAyushh/setup-solana@v5.2
28+
- uses: heyAyushh/setup-solana@v5.4
2929
with:
3030
solana-cli-version: ${{ matrix.solana-version }}
3131
- run: solana block
@@ -75,7 +75,7 @@ jobs:
7575
with:
7676
node-version: ${{ matrix.node-version }}
7777
check-latest: true
78-
- uses: heyAyushh/setup-solana@v5.2
78+
- uses: heyAyushh/setup-solana@v5.4
7979
with:
8080
solana-cli-version: ${{ matrix.solana-version }}
8181
- run: solana block

tokens/escrow/anchor/tests/escrow.ts

Lines changed: 78 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { randomBytes } from "node:crypto";
2-
import * as anchor from "@coral-xyz/anchor";
3-
import { BN, type Program } from "@coral-xyz/anchor";
1+
import { randomBytes } from 'node:crypto';
2+
import * as anchor from '@coral-xyz/anchor';
3+
import { BN, type Program } from '@coral-xyz/anchor';
44
import {
55
MINT_SIZE,
66
TOKEN_2022_PROGRAM_ID,
@@ -10,27 +10,20 @@ import {
1010
createMintToInstruction,
1111
getAssociatedTokenAddressSync,
1212
getMinimumBalanceForRentExemptMint,
13-
} from "@solana/spl-token";
14-
import {
15-
LAMPORTS_PER_SOL,
16-
PublicKey,
17-
SystemProgram,
18-
Transaction,
19-
type TransactionInstruction,
20-
} from "@solana/web3.js";
21-
import { assert } from "chai";
22-
import type { Escrow } from "../target/types/escrow";
13+
} from '@solana/spl-token';
14+
import { LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, type TransactionInstruction } from '@solana/web3.js';
15+
import { assert } from 'chai';
16+
import type { Escrow } from '../target/types/escrow';
2317

24-
import { confirmTransaction, makeKeypairs } from "@solana-developers/helpers";
18+
import { confirmTransaction, makeKeypairs } from '@solana-developers/helpers';
2519

26-
const TOKEN_PROGRAM: typeof TOKEN_2022_PROGRAM_ID | typeof TOKEN_PROGRAM_ID =
27-
TOKEN_2022_PROGRAM_ID;
20+
const TOKEN_PROGRAM: typeof TOKEN_2022_PROGRAM_ID | typeof TOKEN_PROGRAM_ID = TOKEN_2022_PROGRAM_ID;
2821

2922
const getRandomBigNumber = (size = 8) => {
3023
return new BN(randomBytes(size));
3124
};
3225

33-
describe("escrow", async () => {
26+
describe('escrow', async () => {
3427
anchor.setProvider(anchor.AnchorProvider.env());
3528

3629
const provider = anchor.getProvider();
@@ -46,112 +39,66 @@ describe("escrow", async () => {
4639

4740
const [alice, bob, tokenMintA, tokenMintB] = makeKeypairs(4);
4841

49-
before(
50-
"Creates Alice and Bob accounts, 2 token mints, and associated token accounts for both tokens for both users",
51-
async () => {
52-
const [
53-
aliceTokenAccountA,
54-
aliceTokenAccountB,
55-
bobTokenAccountA,
56-
bobTokenAccountB,
57-
] = [alice, bob].flatMap((keypair) =>
58-
[tokenMintA, tokenMintB].map((mint) =>
59-
getAssociatedTokenAddressSync(
60-
mint.publicKey,
61-
keypair.publicKey,
62-
false,
63-
TOKEN_PROGRAM
64-
)
65-
)
66-
);
67-
68-
// Airdrops to users, and creates two tokens mints 'A' and 'B'"
69-
const minimumLamports = await getMinimumBalanceForRentExemptMint(
70-
connection
71-
);
72-
73-
const sendSolInstructions: Array<TransactionInstruction> = [
74-
alice,
75-
bob,
76-
].map((account) =>
77-
SystemProgram.transfer({
78-
fromPubkey: provider.publicKey,
79-
toPubkey: account.publicKey,
80-
lamports: 10 * LAMPORTS_PER_SOL,
81-
})
82-
);
83-
84-
const createMintInstructions: Array<TransactionInstruction> = [
85-
tokenMintA,
86-
tokenMintB,
87-
].map((mint) =>
88-
SystemProgram.createAccount({
89-
fromPubkey: provider.publicKey,
90-
newAccountPubkey: mint.publicKey,
91-
lamports: minimumLamports,
92-
space: MINT_SIZE,
93-
programId: TOKEN_PROGRAM,
94-
})
95-
);
96-
97-
// Make tokenA and tokenB mints, mint tokens and create ATAs
98-
const mintTokensInstructions: Array<TransactionInstruction> = [
99-
{
100-
mint: tokenMintA.publicKey,
101-
authority: alice.publicKey,
102-
ata: aliceTokenAccountA,
103-
},
104-
{
105-
mint: tokenMintB.publicKey,
106-
authority: bob.publicKey,
107-
ata: bobTokenAccountB,
108-
},
109-
].flatMap((mintDetails) => [
110-
createInitializeMint2Instruction(
111-
mintDetails.mint,
112-
6,
113-
mintDetails.authority,
114-
null,
115-
TOKEN_PROGRAM
116-
),
117-
createAssociatedTokenAccountIdempotentInstruction(
118-
provider.publicKey,
119-
mintDetails.ata,
120-
mintDetails.authority,
121-
mintDetails.mint,
122-
TOKEN_PROGRAM
123-
),
124-
createMintToInstruction(
125-
mintDetails.mint,
126-
mintDetails.ata,
127-
mintDetails.authority,
128-
1_000_000_000,
129-
[],
130-
TOKEN_PROGRAM
131-
),
132-
]);
133-
134-
// Add all these instructions to our transaction
135-
const tx = new Transaction();
136-
tx.instructions = [
137-
...sendSolInstructions,
138-
...createMintInstructions,
139-
...mintTokensInstructions,
140-
];
141-
142-
await provider.sendAndConfirm(tx, [tokenMintA, tokenMintB, alice, bob]);
143-
144-
// Save the accounts for later use
145-
accounts.maker = alice.publicKey;
146-
accounts.taker = bob.publicKey;
147-
accounts.tokenMintA = tokenMintA.publicKey;
148-
accounts.makerTokenAccountA = aliceTokenAccountA;
149-
accounts.takerTokenAccountA = bobTokenAccountA;
150-
accounts.tokenMintB = tokenMintB.publicKey;
151-
accounts.makerTokenAccountB = aliceTokenAccountB;
152-
accounts.takerTokenAccountB = bobTokenAccountB;
153-
}
154-
);
42+
before('Creates Alice and Bob accounts, 2 token mints, and associated token accounts for both tokens for both users', async () => {
43+
const [aliceTokenAccountA, aliceTokenAccountB, bobTokenAccountA, bobTokenAccountB] = [alice, bob].flatMap((keypair) =>
44+
[tokenMintA, tokenMintB].map((mint) => getAssociatedTokenAddressSync(mint.publicKey, keypair.publicKey, false, TOKEN_PROGRAM)),
45+
);
46+
47+
// Airdrops to users, and creates two tokens mints 'A' and 'B'"
48+
const minimumLamports = await getMinimumBalanceForRentExemptMint(connection);
49+
50+
const sendSolInstructions: Array<TransactionInstruction> = [alice, bob].map((account) =>
51+
SystemProgram.transfer({
52+
fromPubkey: provider.publicKey,
53+
toPubkey: account.publicKey,
54+
lamports: 10 * LAMPORTS_PER_SOL,
55+
}),
56+
);
57+
58+
const createMintInstructions: Array<TransactionInstruction> = [tokenMintA, tokenMintB].map((mint) =>
59+
SystemProgram.createAccount({
60+
fromPubkey: provider.publicKey,
61+
newAccountPubkey: mint.publicKey,
62+
lamports: minimumLamports,
63+
space: MINT_SIZE,
64+
programId: TOKEN_PROGRAM,
65+
}),
66+
);
67+
68+
// Make tokenA and tokenB mints, mint tokens and create ATAs
69+
const mintTokensInstructions: Array<TransactionInstruction> = [
70+
{
71+
mint: tokenMintA.publicKey,
72+
authority: alice.publicKey,
73+
ata: aliceTokenAccountA,
74+
},
75+
{
76+
mint: tokenMintB.publicKey,
77+
authority: bob.publicKey,
78+
ata: bobTokenAccountB,
79+
},
80+
].flatMap((mintDetails) => [
81+
createInitializeMint2Instruction(mintDetails.mint, 6, mintDetails.authority, null, TOKEN_PROGRAM),
82+
createAssociatedTokenAccountIdempotentInstruction(provider.publicKey, mintDetails.ata, mintDetails.authority, mintDetails.mint, TOKEN_PROGRAM),
83+
createMintToInstruction(mintDetails.mint, mintDetails.ata, mintDetails.authority, 1_000_000_000, [], TOKEN_PROGRAM),
84+
]);
85+
86+
// Add all these instructions to our transaction
87+
const tx = new Transaction();
88+
tx.instructions = [...sendSolInstructions, ...createMintInstructions, ...mintTokensInstructions];
89+
90+
await provider.sendAndConfirm(tx, [tokenMintA, tokenMintB, alice, bob]);
91+
92+
// Save the accounts for later use
93+
accounts.maker = alice.publicKey;
94+
accounts.taker = bob.publicKey;
95+
accounts.tokenMintA = tokenMintA.publicKey;
96+
accounts.makerTokenAccountA = aliceTokenAccountA;
97+
accounts.takerTokenAccountA = bobTokenAccountA;
98+
accounts.tokenMintB = tokenMintB.publicKey;
99+
accounts.makerTokenAccountB = aliceTokenAccountB;
100+
accounts.takerTokenAccountB = bobTokenAccountB;
101+
});
155102

156103
const tokenAOfferedAmount = new BN(1_000_000);
157104
const tokenBWantedAmount = new BN(1_000_000);
@@ -163,20 +110,11 @@ describe("escrow", async () => {
163110

164111
// Then determine the account addresses we'll use for the offer and the vault
165112
const offer = PublicKey.findProgramAddressSync(
166-
[
167-
Buffer.from("offer"),
168-
accounts.maker.toBuffer(),
169-
offerId.toArrayLike(Buffer, "le", 8),
170-
],
171-
program.programId
113+
[Buffer.from('offer'), accounts.maker.toBuffer(), offerId.toArrayLike(Buffer, 'le', 8)],
114+
program.programId,
172115
)[0];
173116

174-
const vault = getAssociatedTokenAddressSync(
175-
accounts.tokenMintA,
176-
offer,
177-
true,
178-
TOKEN_PROGRAM
179-
);
117+
const vault = getAssociatedTokenAddressSync(accounts.tokenMintA, offer, true, TOKEN_PROGRAM);
180118

181119
accounts.offer = offer;
182120
accounts.vault = vault;
@@ -215,24 +153,18 @@ describe("escrow", async () => {
215153

216154
// Check the offered tokens are now in Bob's account
217155
// (note: there is no before balance as Bob didn't have any offered tokens before the transaction)
218-
const bobTokenAccountBalanceAfterResponse =
219-
await connection.getTokenAccountBalance(accounts.takerTokenAccountA);
220-
const bobTokenAccountBalanceAfter = new BN(
221-
bobTokenAccountBalanceAfterResponse.value.amount
222-
);
156+
const bobTokenAccountBalanceAfterResponse = await connection.getTokenAccountBalance(accounts.takerTokenAccountA);
157+
const bobTokenAccountBalanceAfter = new BN(bobTokenAccountBalanceAfterResponse.value.amount);
223158
assert(bobTokenAccountBalanceAfter.eq(tokenAOfferedAmount));
224159

225160
// Check the wanted tokens are now in Alice's account
226161
// (note: there is no before balance as Alice didn't have any wanted tokens before the transaction)
227-
const aliceTokenAccountBalanceAfterResponse =
228-
await connection.getTokenAccountBalance(accounts.makerTokenAccountB);
229-
const aliceTokenAccountBalanceAfter = new BN(
230-
aliceTokenAccountBalanceAfterResponse.value.amount
231-
);
162+
const aliceTokenAccountBalanceAfterResponse = await connection.getTokenAccountBalance(accounts.makerTokenAccountB);
163+
const aliceTokenAccountBalanceAfter = new BN(aliceTokenAccountBalanceAfterResponse.value.amount);
232164
assert(aliceTokenAccountBalanceAfter.eq(tokenBWantedAmount));
233165
};
234166

235-
it("Puts the tokens Alice offers into the vault when Alice makes an offer", async () => {
167+
it('Puts the tokens Alice offers into the vault when Alice makes an offer', async () => {
236168
await make();
237169
});
238170

0 commit comments

Comments
 (0)