2021-02-17 20:15:50 -08:00
|
|
|
import dotenv from "dotenv"
|
2021-02-24 18:52:20 -08:00
|
|
|
import { ProgramAccount, PublicKey, SPLToken, Wallet } from "solray"
|
2021-02-17 20:15:50 -08:00
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
import { AppContext, conn, network } from "./src/context"
|
2021-02-20 03:28:39 -08:00
|
|
|
import { AggregatorDeployFile, Deployer } from "./src/Deployer"
|
2021-02-20 02:00:17 -08:00
|
|
|
import { coinbase } from "./src/feeds"
|
2021-02-24 18:52:20 -08:00
|
|
|
import { jsonReplacer, jsonReviver, loadJSONFile } from "./src/json"
|
2021-02-20 02:00:17 -08:00
|
|
|
import { log } from "./src/log"
|
2021-02-19 05:13:56 -08:00
|
|
|
import { PriceFeeder } from "./src/PriceFeeder"
|
2021-02-24 18:52:20 -08:00
|
|
|
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.forSeed(
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 20:15:50 -08:00
|
|
|
|
|
|
|
async function main() {
|
2021-02-24 18:52:20 -08:00
|
|
|
// persistent JSON state for test script
|
|
|
|
|
|
|
|
|
2021-02-19 05:13:56 -08:00
|
|
|
const setupFile = `config/setup.${network}.json`
|
|
|
|
const deployFile = `deploy.${network}.json`
|
|
|
|
const feederConfigFile = "feeder.json"
|
2021-02-19 02:35:30 -08:00
|
|
|
let ctx = new AppContext()
|
2021-02-17 20:15:50 -08:00
|
|
|
let adminWallet = await ctx.adminWallet()
|
2021-02-19 05:13:56 -08:00
|
|
|
let oracleWallet = await ctx.oracleWallet()
|
|
|
|
await conn.requestAirdrop(adminWallet.pubkey, 10 * 1e9)
|
|
|
|
await conn.requestAirdrop(oracleWallet.pubkey, 10 * 1e9)
|
2021-02-19 02:35:30 -08:00
|
|
|
|
2021-02-24 18:52:20 -08:00
|
|
|
const t = new TestHarness(`test.${network}.json`, adminWallet)
|
|
|
|
await t.setupRewardTokenPK()
|
2021-02-19 02:35:30 -08:00
|
|
|
|
2021-02-24 18:52:20 -08:00
|
|
|
// return
|
|
|
|
|
|
|
|
const deployer = new Deployer(deployFile, setupFile, adminWallet)
|
2021-02-19 05:13:56 -08:00
|
|
|
await deployer.runAll()
|
2021-02-19 02:35:30 -08:00
|
|
|
|
2021-02-24 18:52:20 -08:00
|
|
|
// TODO: actually, creating the faucet should be in the deployer
|
|
|
|
// await t.setupFaucet(deployer.state.programID)
|
|
|
|
|
2021-02-20 03:28:39 -08:00
|
|
|
const deploy = loadJSONFile<AggregatorDeployFile>(deployFile)
|
|
|
|
const feeder = new PriceFeeder(deploy, oracleWallet)
|
2021-02-19 05:13:56 -08:00
|
|
|
feeder.start()
|
2021-02-20 02:00:17 -08:00
|
|
|
|
2021-02-24 18:52:20 -08:00
|
|
|
// TODO: try to to harvest rewards
|
2021-02-19 02:35:30 -08:00
|
|
|
|
2021-02-24 18:52:20 -08:00
|
|
|
return
|
2021-02-17 20:15:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((err) => console.log(err))
|