-
Notifications
You must be signed in to change notification settings - Fork 31
/
test.ts
141 lines (113 loc) · 3.68 KB
/
test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import dotenv from "dotenv"
import { ProgramAccount, PublicKey, SPLToken, Wallet } from "solray"
dotenv.config()
import { AppContext, conn, network } from "./src/context"
import { AggregatorDeployFile, Deployer } from "./src/Deployer"
import { coinbase } from "./src/feeds"
import { jsonReplacer, jsonReviver, loadJSONFile } from "./src/json"
import { log } from "./src/log"
import { PriceFeeder } from "./src/PriceFeeder"
import { stateFromJSON } from "./src/state"
interface State {
rewardTokenPK: PublicKey
faucetPK: PublicKey
isMinted: boolean
}
class TestHarness {
public state: State
constructor(
stateFile: string,
private wallet: Wallet,
) {
this.state = stateFromJSON<State>(stateFile, {} as State, {
replacer: jsonReplacer,
reviver: jsonReviver,
})
// this.state = stateFromJSON<State>(`state.${network}.json`, {} as State)
}
// async setup() {
// // setup reward token plus mint
// await this.setupRewardTokenPK()
// await this.setupFaucet()
// await this.mintToFaucet(1e6 * 1e9)
// }
get spltoken() {
return new SPLToken(this.wallet)
}
// async mintToFaucet(amount: number) {
// if (this.state.isMinted) {
// return
// }
// await this.spltoken.mintTo({
// token: this.state.rewardTokenPK,
// to: await this.faucetOwnerPK(),
// amount: BigInt(amount), // 1M
// authority: this.wallet.pubkey,
// })
// }
// async faucetOwnerPK(): Promise<PublicKey> {
// const rewardTokenOwner = await ProgramAccount.forSeed(
// Buffer.from("solink"),
// this.aggregatorProgramID
// )
// return rewardTokenOwner.pubkey
// }
async setupFaucet(aggregatorProgramID: PublicKey): Promise<PublicKey> {
if (this.state.faucetPK) {
return this.state.faucetPK
}
const faucetOwner = await ProgramAccount.forSeeds(
[Buffer.from("solink")],
aggregatorProgramID
)
const faucet = await this.spltoken.initializeAccount({
token: this.state.rewardTokenPK,
owner: faucetOwner.pubkey,
})
await this.spltoken.mintTo({
token: this.state.rewardTokenPK,
to: faucet.publicKey,
amount: BigInt(1e6*1e9), // 1M
authority: this.wallet.pubkey,
})
this.state.faucetPK = faucet.publicKey
return faucet.publicKey
}
async setupRewardTokenPK(): Promise<PublicKey> {
if (this.state.rewardTokenPK) {
return this.state.rewardTokenPK
}
log.info("setup reward token")
const token = await this.spltoken.initializeMint({
mintAuthority: this.wallet.pubkey,
decimals: 9,
// account: this.wallet.deriveAccount("0")
})
this.state.rewardTokenPK = token.publicKey
return token.publicKey
}
}
async function main() {
// persistent JSON state for test script
const setupFile = `config/setup.${network}.json`
const deployFile = `deploy.${network}.json`
const feederConfigFile = "feeder.json"
let ctx = new AppContext()
let adminWallet = await ctx.adminWallet()
let oracleWallet = await ctx.oracleWallet()
await conn.requestAirdrop(adminWallet.pubkey, 10 * 1e9)
await conn.requestAirdrop(oracleWallet.pubkey, 10 * 1e9)
const t = new TestHarness(`test.${network}.json`, adminWallet)
await t.setupRewardTokenPK()
// return
const deployer = new Deployer(deployFile, setupFile, adminWallet)
await deployer.runAll()
// TODO: actually, creating the faucet should be in the deployer
// await t.setupFaucet(deployer.state.programID)
const deploy = loadJSONFile<AggregatorDeployFile>(deployFile)
const feeder = new PriceFeeder(deploy, {} as any, oracleWallet)
feeder.start()
// TODO: try to to harvest rewards
return
}
main().catch((err) => console.log(err))