-
Notifications
You must be signed in to change notification settings - Fork 0
/
stake-transaction.ts
executable file
·94 lines (92 loc) · 3.5 KB
/
stake-transaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
// @ts-ignore
import inquirer from 'inquirer'
import * as anchor from '@project-serum/anchor'
// @ts-ignore
import BN from 'bn.js';
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
getAssociatedTokenAddress,
TOKEN_PROGRAM_ID,
} from '@solana/spl-token'
import { loadKeypairFromFile, readConfig, mintAddress } from './utilties.js'
import { rpcConfig } from './rpcConfig.js'
import { program, get_pda_from_seeds } from './program.js'
import { PublicKey } from '@solana/web3.js';
const { web3 } = anchor
// Questions to ask the user
const questions = [
{
type: 'number',
name: 'stakeAmount',
message: 'Please enter amount to be staked?:',
validate: (input: number) => !!input,
},
{
type: 'input',
name: 'freelancer',
message: 'Please enter Freelancer to stake on:',
validate: (input: string) => !!input,
},
{
type: 'input',
name: 'skill',
message: 'Input name of skill to stake on: ',
validate: (input: string) => !!input,
},
]
inquirer.prompt(questions).then((answers:any) => {
let freelancer = new PublicKey(answers.freelancer)
get_pda_from_seeds([
Buffer.from('skillStake'),
Buffer.from(answers.skill),
freelancer.toBuffer()
]).then((skillStakeAccount) => {
const [skillStake] = skillStakeAccount
getAssociatedTokenAddress(mintAddress, skillStake, true).then(
(skillStakeTokenAccount) => {
const skillStakeTokenAddress = skillStakeTokenAccount
const staker = loadKeypairFromFile(readConfig().solanaKeyPath)
getAssociatedTokenAddress(
mintAddress,
staker.publicKey,
false
).then((stakerToken) => {
const stakerTokenAddress = stakerToken
get_pda_from_seeds([
Buffer.from('freelance'),
freelancer.toBuffer()
]).then(([freelanceAccount]) => {
let stakeAmount = new BN(answers.stakeAmount)
program.methods
.stakeSkillset(
answers.skill,
stakeAmount
)
.accounts({
staker: staker.publicKey,
freelanceAccount: freelanceAccount,
skillStake: skillStake,
skillStakeTokenAccount: skillStakeTokenAddress,
usdcMint: mintAddress,
stakerTokenAccount: stakerTokenAddress,
systemProgram: web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram:
ASSOCIATED_TOKEN_PROGRAM_ID,
})
.signers([staker])
.rpc(rpcConfig)
.then((_) => {
console.log('Stake completed')
})
.catch((err:Error) => {
console.log("Stake failed")
console.log(err.message)
})
})
})
}
)
})
})