add support for groupNum when creating, and fetching a group

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-05-27 14:43:53 +02:00
parent b1fcb4a7e6
commit d95c5d80ab
5 changed files with 93 additions and 20 deletions

View File

@ -5,13 +5,24 @@ import { PerpMarket } from './perp';
import { Serum3Market } from './serum3';
export class Group {
static from(publicKey: PublicKey, obj: { admin: PublicKey }): Group {
return new Group(publicKey, obj.admin, new Map(), new Map(), new Map());
static from(
publicKey: PublicKey,
obj: { admin: PublicKey; groupNum: number },
): Group {
return new Group(
publicKey,
obj.admin,
obj.groupNum,
new Map(),
new Map(),
new Map(),
);
}
constructor(
public publicKey: PublicKey,
public admin: PublicKey,
public groupNum: number,
public banksMap: Map<string, Bank>,
public serum3MarketsMap: Map<string, Serum3Market>,
public perpMarketsMap: Map<string, PerpMarket>,

View File

@ -37,10 +37,10 @@ export class MangoClient {
// Group
public async createGroup(): Promise<TransactionSignature> {
public async createGroup(groupNum: number): Promise<TransactionSignature> {
const adminPk = (this.program.provider as AnchorProvider).wallet.publicKey;
return await this.program.methods
.createGroup()
.createGroup(groupNum)
.accounts({
admin: adminPk,
payer: adminPk,
@ -55,17 +55,33 @@ export class MangoClient {
return group;
}
public async getGroupForAdmin(adminPk: PublicKey): Promise<Group> {
const groups = (
await this.program.account.group.all([
{
memcmp: {
bytes: adminPk.toBase58(),
offset: 8,
},
public async getGroupForAdmin(
adminPk: PublicKey,
groupNum?: number,
): Promise<Group> {
const filters: MemcmpFilter[] = [
{
memcmp: {
bytes: adminPk.toBase58(),
offset: 8,
},
])
).map((tuple) => Group.from(tuple.publicKey, tuple.account));
},
];
if (groupNum) {
const bbuf = Buffer.alloc(4);
bbuf.writeUInt32LE(groupNum);
filters.push({
memcmp: {
bytes: bs58.encode(bbuf),
offset: 44,
},
});
}
const groups = (await this.program.account.group.all(filters)).map(
(tuple) => Group.from(tuple.publicKey, tuple.account),
);
await groups[0].reload(this);
return groups[0];
}

View File

@ -20,6 +20,11 @@ export type MangoV4 = {
"kind": "account",
"type": "publicKey",
"path": "admin"
},
{
"kind": "arg",
"type": "u32",
"path": "group_num"
}
]
}
@ -40,7 +45,12 @@ export type MangoV4 = {
"isSigner": false
}
],
"args": []
"args": [
{
"name": "groupNum",
"type": "u32"
}
]
},
{
"name": "registerToken",
@ -1745,12 +1755,25 @@ export type MangoV4 = {
"name": "bump",
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
3
]
}
},
{
"name": "groupNum",
"type": "u32"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
7
8
]
}
}
@ -2893,6 +2916,11 @@ export const IDL: MangoV4 = {
"kind": "account",
"type": "publicKey",
"path": "admin"
},
{
"kind": "arg",
"type": "u32",
"path": "group_num"
}
]
}
@ -2913,7 +2941,12 @@ export const IDL: MangoV4 = {
"isSigner": false
}
],
"args": []
"args": [
{
"name": "groupNum",
"type": "u32"
}
]
},
{
"name": "registerToken",
@ -4618,12 +4651,25 @@ export const IDL: MangoV4 = {
"name": "bump",
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
3
]
}
},
{
"name": "groupNum",
"type": "u32"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
7
8
]
}
}

View File

@ -44,7 +44,7 @@ async function main() {
// group
console.log(`Creating Group...`);
try {
await client.createGroup();
await client.createGroup(0);
} catch (error) {}
const group = await client.getGroupForAdmin(admin.publicKey);
console.log(`...registered group ${group.publicKey}`);

View File

@ -39,7 +39,7 @@ async function main() {
JSON.parse(fs.readFileSync(process.env.ADMIN_KEYPAIR!, 'utf-8')),
),
);
const group = await client.getGroupForAdmin(admin.publicKey);
const group = await client.getGroupForAdmin(admin.publicKey, 0);
console.log(`Found group ${group.publicKey.toBase58()}`);
// create + fetch account