/
client.ts
76 lines (64 loc) 路 2.43 KB
/
client.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
import { PublicKey, Keypair, Connection } from '@solana/web3.js';
import * as anchor from '@project-serum/anchor';
import { CreateMarketParams, HoneyMarket } from './market';
import { DerivedAccount } from './derived-account';
import devnetIdl from '../idl/devnet/honey.json';
import mainnetBetaIdl from '../idl/mainnet-beta/honey.json';
interface ToBytes {
toBytes(): Uint8Array;
}
interface HasPublicKey {
publicKey: PublicKey;
}
type DerivedAccountSeed = HasPublicKey | ToBytes | Uint8Array | string;
export class HoneyClient {
constructor(public program: anchor.Program, public devnet?: boolean) {}
/**
* Create a new client for interacting with the Honey lending program.
* @param provider The provider with wallet/network access that can be used to send transactions.
* @returns The client
*/
static async connect(provider: anchor.AnchorProvider, honeyPubKey: string, devnet?: boolean): Promise<HoneyClient> {
const idl = devnet ? devnetIdl : mainnetBetaIdl;
const HONEY_PROGRAM_ID = new PublicKey(honeyPubKey);
const program = new anchor.Program(idl as any, HONEY_PROGRAM_ID, provider);
return new HoneyClient(program, devnet);
}
/**
* Find a PDA
* @param seeds
* @returns
*/
async findDerivedAccount(seeds: DerivedAccountSeed[]): Promise<DerivedAccount> {
const seedBytes = seeds.map((s) => {
if (typeof s === 'string') {
return Buffer.from(s);
} else if ('publicKey' in s) {
return s.publicKey.toBytes();
} else if ('toBytes' in s) {
return s.toBytes();
} else {
return s;
}
});
const [address, bumpSeed] = await PublicKey.findProgramAddress(seedBytes, this.program.programId);
return new DerivedAccount(address, bumpSeed);
}
async createMarket(params: CreateMarketParams): Promise<HoneyMarket> {
let account = params.account;
if (account === undefined) {
account = Keypair.generate();
}
const tx = await this.program.methods
.initMarket(params.owner, params.quoteCurrencyName, params.quoteCurrencyMint, params.nftCollectionCreator)
.accounts({
market: account.publicKey,
oraclePrice: params.nftOraclePrice,
})
.signers([account])
.preInstructions([await this.program.account.market.createInstruction(account)])
.rpc();
await this.program.provider.connection.confirmTransaction(tx, 'processed');
return HoneyMarket.load(this, account.publicKey);
}
}