--wip-- [skip ci]

This commit is contained in:
microwavedcola1 2022-03-31 15:58:12 +02:00
parent fb16771d1f
commit fce4edb79f
2 changed files with 3007 additions and 2844 deletions

View File

@ -1,4 +1,4 @@
import { Provider, Wallet, web3 } from '@project-serum/anchor'; import { Provider, Wallet, web3, BN } from '@project-serum/anchor';
import { bs58 } from '@project-serum/anchor/dist/cjs/utils/bytes'; import { bs58 } from '@project-serum/anchor/dist/cjs/utils/bytes';
import { TOKEN_PROGRAM_ID } from '@project-serum/anchor/dist/cjs/utils/token'; import { TOKEN_PROGRAM_ID } from '@project-serum/anchor/dist/cjs/utils/token';
import { Connection, Keypair, SystemProgram } from '@solana/web3.js'; import { Connection, Keypair, SystemProgram } from '@solana/web3.js';
@ -80,13 +80,19 @@ async function main() {
// check if token is already registered, iff not, then register // check if token is already registered, iff not, then register
// //
// mngo devnet mint // mngo devnet mint
const mint = new web3.PublicKey( const mngoDevnetMint = new web3.PublicKey(
'Bb9bsTQa1bGEtQ5KagGkvSHyuLqDWumFUcRqFusFNJWC', 'Bb9bsTQa1bGEtQ5KagGkvSHyuLqDWumFUcRqFusFNJWC',
); );
const btcDevnetMint = new web3.PublicKey(
'3UNBZ6o52WTWwjac2kPUb4FyodhU1vFkRJheu1Sh2TvU',
);
// mngo devnet oracle // mngo devnet oracle
const mngoOracle = new web3.PublicKey( const mngoDevnetOracle = new web3.PublicKey(
'8k7F9Xb36oFJsjpCKpsXvg4cgBRoZtwNTc3EzG5Ttd2o', '8k7F9Xb36oFJsjpCKpsXvg4cgBRoZtwNTc3EzG5Ttd2o',
); );
const btcDevnetOracle = new web3.PublicKey(
'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J',
);
let bank; let bank;
gpa = await client.program.account.bank.all([ gpa = await client.program.account.bank.all([
{ {
@ -97,7 +103,7 @@ async function main() {
}, },
{ {
memcmp: { memcmp: {
bytes: bs58.encode(mint.toBuffer()), bytes: bs58.encode(btcDevnetMint.toBuffer()),
offset: 40, offset: 40,
}, },
}, },
@ -106,12 +112,12 @@ async function main() {
bank = gpa[0]; bank = gpa[0];
} else { } else {
await client.program.methods await client.program.methods
.registerToken(0, 0.8, 0.6, 1.2, 1.4, 0.02) .registerToken(1, 0.8, 0.6, 1.2, 1.4, 0.02)
.accounts({ .accounts({
group: group.publicKey, group: group.publicKey,
admin: admin.publicKey, admin: admin.publicKey,
mint, mint: btcDevnetMint,
oracle: mngoOracle, oracle: btcDevnetOracle,
payer: payer.publicKey, payer: payer.publicKey,
token_program: TOKEN_PROGRAM_ID, token_program: TOKEN_PROGRAM_ID,
system_program: SystemProgram.programId, system_program: SystemProgram.programId,
@ -129,7 +135,7 @@ async function main() {
}, },
{ {
memcmp: { memcmp: {
bytes: bs58.encode(mint.toBuffer()), bytes: bs58.encode(btcDevnetMint.toBuffer()),
offset: 40, offset: 40,
}, },
}, },
@ -137,59 +143,138 @@ async function main() {
bank = gpa[0]; bank = gpa[0];
} }
console.log(`Bank address: ${bank.publicKey.toBase58()}`); console.log(`Bank address: ${bank.publicKey.toBase58()}`);
// console.log(bank); // console.log(bank.account.vault);
// //
// mango account // mango account
// //
let mangoAccount; let mangoAccount;
gpa = await client.program.account.mangoAccount.all([ // gpa = await client.program.account.mangoAccount.all([
{ // {
memcmp: { // memcmp: {
bytes: bs58.encode(group.publicKey.toBuffer()), // bytes: bs58.encode(group.publicKey.toBuffer()),
offset: 8, // offset: 8,
}, // },
}, // },
{ // {
memcmp: { // memcmp: {
bytes: bs58.encode(admin.publicKey.toBuffer()), // bytes: bs58.encode(admin.publicKey.toBuffer()),
offset: 40, // offset: 40,
}, // },
}, // },
]); // ]);
if (gpa.length > 0) { // if (gpa.length > 0) {
mangoAccount = gpa[0]; // mangoAccount = gpa[0];
} else { // } else {
await client.program.methods
.createAccount(0)
.accounts({
group: group.publicKey,
owner: admin.publicKey,
payer: payer.publicKey,
system_program: SystemProgram.programId,
})
.signers([admin, payer])
.rpc();
gpa = await client.program.account.mangoAccount.all([ // await client.program.methods
{ // .createAccount(11)
memcmp: { // .accounts({
bytes: bs58.encode(group.publicKey.toBuffer()), // group: group.publicKey,
offset: 8, // owner: admin.publicKey,
}, // payer: payer.publicKey,
}, // system_program: SystemProgram.programId,
{ // })
memcmp: { // .signers([admin, payer])
bytes: bs58.encode(admin.publicKey.toBuffer()), // .rpc();
offset: 40,
}, // gpa = await client.program.account.mangoAccount.all([
}, // {
]); // memcmp: {
mangoAccount = gpa[0]; // bytes: bs58.encode(group.publicKey.toBuffer()),
// offset: 8,
// },
// },
// {
// memcmp: {
// bytes: bs58.encode(admin.publicKey.toBuffer()),
// offset: 40,
// },
// },
// ]);
// mangoAccount = gpa[0];
// }
let mangoAccountPk = new web3.PublicKey(
'CtdYxnaWZPgD5BuchHmo2fKecJbVberhqBRbrspsw9gc',
);
mangoAccount = await client.program.account.mangoAccount.fetch(
mangoAccountPk,
);
console.log(`Mango account address: ${mangoAccountPk.toBase58()}`);
// console.log(mangoAccount);
//
// deposit
//
let mngoTokenAccount = new web3.PublicKey(
'EnaCw8ZooD1sFbqz2J3w8XAZmtp5sAvVx1RxKM2pMVLh',
);
let btcTokenAccount = new web3.PublicKey(
'DS2vYFVtQbbJDowCG4NEM9KGQ8TJpxKo5efBQj96eCPS',
);
await client.program.methods
.deposit(new BN(0))
.accounts({
group: group.publicKey,
account: mangoAccountPk,
bank: bank.publicKey,
vault: bank.account.vault,
tokenAccount: btcTokenAccount,
tokenAuthority: admin.publicKey,
token_program: TOKEN_PROGRAM_ID,
})
.remainingAccounts([
{ pubkey: bank.publicKey, isWritable: false, isSigner: false },
{ pubkey: btcDevnetOracle, isWritable: false, isSigner: false },
])
.signers([admin])
.rpc();
mangoAccount = await client.program.account.mangoAccount.fetch(
mangoAccountPk,
);
for (const tokenAccount of mangoAccount.tokenAccountMap.values) {
if (tokenAccount.tokenIndex !== 65535) {
console.log(
`${
tokenAccount.tokenIndex
} - ${tokenAccount.indexedValue.val.toNumber()}`,
);
}
}
await client.program.methods
.withdraw(new BN(1000), false)
.accounts({
group: group.publicKey,
account: mangoAccountPk,
owner: admin.publicKey,
bank: bank.publicKey,
vault: bank.account.vault,
tokenAccount: btcTokenAccount,
token_program: TOKEN_PROGRAM_ID,
})
.remainingAccounts([
{ pubkey: bank.publicKey, isWritable: false, isSigner: false },
{ pubkey: btcDevnetOracle, isWritable: false, isSigner: false },
])
.signers([admin])
.rpc();
mangoAccount = await client.program.account.mangoAccount.fetch(
mangoAccountPk,
);
for (const tokenAccount of mangoAccount.tokenAccountMap.values) {
if (tokenAccount.tokenIndex !== 65535) {
console.log(
`${
tokenAccount.tokenIndex
} - ${tokenAccount.indexedValue.val.toNumber()}`,
);
}
} }
console.log(`Mango account address: ${mangoAccount.publicKey.toBase58()}`);
console.log(mangoAccount);
} }
main(); main();

File diff suppressed because it is too large Load Diff