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

first setup for api project to use web3 solana program #42

Closed
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
20 changes: 0 additions & 20 deletions .env.example

This file was deleted.

132 changes: 132 additions & 0 deletions config/solana/crowdfunding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"address": "6BsMtttdteCnV3b6XmxTiLS9VQfb57yu7cRH8SKfP4u3",
"metadata": {
"name": "crowdfunding",
"version": "0.1.0",
"spec": "0.1.0",
"description": "Created with Anchor"
},
"instructions": [
{
"name": "contribute",
"discriminator": [
82,
33,
68,
131,
32,
0,
205,
95
],
"accounts": [
{
"name": "project",
"writable": true
},
{
"name": "contributor",
"writable": true,
"signer": true
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
},
{
"name": "create_project",
"discriminator": [
148,
219,
181,
42,
221,
114,
145,
190
],
"accounts": [
{
"name": "project",
"writable": true,
"pda": {
"seeds": [
{
"kind": "arg",
"path": "title"
},
{
"kind": "account",
"path": "artist"
}
]
}
},
{
"name": "artist",
"writable": true,
"signer": true
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
}
],
"args": [
{
"name": "title",
"type": "string"
}
]
}
],
"accounts": [
{
"name": "ProjectState",
"discriminator": [
41,
49,
200,
239,
125,
191,
219,
242
]
}
],
"types": [
{
"name": "ProjectState",
"type": {
"kind": "struct",
"fields": [
{
"name": "artist",
"type": "pubkey"
},
{
"name": "title",
"type": "bytes"
},
{
"name": "project_id",
"type": "u64"
},
{
"name": "current_funding",
"type": "u64"
}
]
}
}
]
}
Empty file.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"lint": "eslint"
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.1",
"@solana/web3.js": "^1.95.3",
"@strapi/plugin-cloud": "4.25.1",
"@strapi/plugin-i18n": "4.25.1",
"@strapi/plugin-users-permissions": "^4.25.6",
Expand Down
40 changes: 39 additions & 1 deletion src/api/project/services/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,43 @@
*/

import { factories } from '@strapi/strapi';
import * as anchor from '@coral-xyz/anchor';
import { Connection, PublicKey } from '@solana/web3.js';
import idl from '../../../../config/solana/crowdfunding.json';

export default factories.createCoreService('api::project.project');
const PROGRAM_ID = new PublicKey(process.env.PROGRAM_ID);
const SOLANA_CLUSTER_URL = process.env.SOLANA_CLUSTER_URL;

export default factories.createCoreService('api::project.project', ({ strapi }) => ({
// Override default service functions if necessary
async interactWithSolanaProgram(wallet: any, instructionData: any) {
try {
// Setup connection to Solana
const connection = new Connection(SOLANA_CLUSTER_URL, 'confirmed');

// Load provider with wallet (this can be a keypair for backend wallets or user wallets from frontend)
const provider = new anchor.AnchorProvider(connection, wallet, {
preflightCommitment: 'processed',
});

// Fetch the IDL for your program
const idl = await anchor.Program.fetchIdl(PROGRAM_ID, provider);
const program = new anchor.Program(idl, PROGRAM_ID, provider);

// Interact with the program method
const tx = await program.methods
.yourMethodName(instructionData) // Change to your program method
.accounts({
// Specify the accounts needed for your transaction, e.g.:
// user: wallet.publicKey,
})
.signers([]) // Add signers if necessary
.rpc(); // Send the transaction

return tx; // Return the transaction ID
} catch (error) {
strapi.log.error('Failed to interact with Solana program:', error);
throw new Error('Interaction failed: ' + error.message);
}
},
}));
Loading
Loading