solana.js: oracle create packing

This commit is contained in:
Conner Gallagher 2022-12-12 16:26:18 -07:00
parent 19ef0f9e6e
commit 16a8b16f39
4 changed files with 61 additions and 46 deletions

View File

@ -176,7 +176,9 @@ export class OracleAccount extends Account<types.OracleAccountData> {
queueAccount: QueueAccount;
} & OracleInitParams &
OracleStakeParams
): Promise<[OracleAccount, TransactionObject]> {
): Promise<[OracleAccount, Array<TransactionObject>]> {
const txns: Array<TransactionObject> = [];
const tokenWallet = params.stakingWalletKeypair ?? Keypair.generate();
const authority = params.authority?.publicKey ?? payer;
@ -238,6 +240,8 @@ export class OracleAccount extends Account<types.OracleAccountData> {
params.authority ? [params.authority, tokenWallet] : [tokenWallet]
);
txns.push(oracleInit);
if (params.stakeAmount && params.stakeAmount > 0) {
const depositTxn = await oracleAccount.stakeInstructions(payer, {
stakeAmount: params.stakeAmount,
@ -245,10 +249,10 @@ export class OracleAccount extends Account<types.OracleAccountData> {
funderTokenAccount: params.funderTokenAccount,
tokenAccount: tokenWallet.publicKey,
});
oracleInit.combine(depositTxn);
txns.push(depositTxn);
}
return [oracleAccount, oracleInit];
return [oracleAccount, txns];
}
public static async create(
@ -257,16 +261,16 @@ export class OracleAccount extends Account<types.OracleAccountData> {
queueAccount: QueueAccount;
} & OracleInitParams &
OracleStakeParams
): Promise<[OracleAccount, TransactionSignature]> {
const [oracleAccount, txnObject] = await OracleAccount.createInstructions(
): Promise<[OracleAccount, Array<TransactionSignature>]> {
const [oracleAccount, txns] = await OracleAccount.createInstructions(
program,
program.walletPubkey,
params
);
const txnSignature = await program.signAndSend(txnObject);
const signatures = await program.signAndSendAll(txns);
return [oracleAccount, txnSignature];
return [oracleAccount, signatures];
}
async stakeInstructions(

View File

@ -374,7 +374,7 @@ export class QueueAccount extends Account<types.OracleQueueAccountData> {
return [
oracleAccount,
TransactionObject.pack([
createOracleTxnObject,
...createOracleTxnObject,
createPermissionTxnObject,
]),
];

View File

@ -411,9 +411,7 @@ export class NativeMint extends Mint {
}
wrapAmount = params.fundUpTo.sub(userTokenBalance);
} else if ('amount' in params) {
wrapAmount = new Big(params.amount).mul(
new Big(10).pow(this.mint.decimals)
);
wrapAmount = new Big(params.amount);
} else {
throw new Error(
`Must specify fundUpTo or amount to perform this actions`

View File

@ -4,6 +4,7 @@ import assert from 'assert';
import * as anchor from '@project-serum/anchor';
import { setupTest, TestContext } from './utilts';
import { Keypair, PublicKey } from '@solana/web3.js';
import Big from 'big.js';
describe('Mint Tests', () => {
let ctx: TestContext;
@ -22,66 +23,77 @@ describe('Mint Tests', () => {
);
await ctx.program.connection.confirmTransaction(airdropTxn);
const [tokenAddress] = await ctx.program.mint.createAssocatedUser(
[userTokenAddress] = await ctx.program.mint.createAssocatedUser(
ctx.payer.publicKey,
user.publicKey
);
userTokenAddress = tokenAddress;
const userTokenBalance =
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
if (userTokenBalance !== 0) {
throw new Error(
`Incorrect user token balance, expected 0, received ${userTokenBalance}`
);
}
assert(
userTokenBalance === 0,
`Incorrect user token balance, expected 0, received ${userTokenBalance}`
);
});
it('Wraps SOL', async () => {
if (!userTokenAddress) {
throw new Error(`User token address does not exist`);
}
assert(userTokenAddress, `User token address does not exist`);
await ctx.program.mint.wrap(ctx.payer.publicKey, { amount: 0.25 }, user);
const WRAP_AMOUNT = 0.25;
await ctx.program.mint.wrap(
ctx.payer.publicKey,
{ amount: WRAP_AMOUNT },
user
);
const userTokenBalance =
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
if (userTokenBalance !== 0.25) {
throw new Error(
`Incorrect user token balance, expected 0.25, received ${userTokenBalance}`
);
}
assert(
userTokenBalance === WRAP_AMOUNT,
`Incorrect user token balance, expected ${WRAP_AMOUNT} wSOL, received ${userTokenBalance}`
);
});
it('Unwraps SOL', async () => {
if (!userTokenAddress) {
throw new Error(`User token address does not exist`);
}
assert(userTokenAddress, `User token address does not exist`);
const initialUserTokenBalance =
const UNWRAP_AMOUNT = 0.1;
let initialUserTokenBalance =
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
const expectedFinalBalance = initialUserTokenBalance - 0.1;
if (expectedFinalBalance < 0) {
throw new Error(`Final user token address would be negative`);
// if previous test failed, wrap some funds
if (initialUserTokenBalance <= 0) {
await ctx.program.mint.wrap(
ctx.payer.publicKey,
{ fundUpTo: new Big(0.25) },
user
);
initialUserTokenBalance =
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
}
await ctx.program.mint.unwrap(ctx.payer.publicKey, 0.1, user);
const expectedFinalBalance = initialUserTokenBalance - UNWRAP_AMOUNT;
assert(
expectedFinalBalance >= 0,
`Final user token address would be negative`
);
await ctx.program.mint.unwrap(ctx.payer.publicKey, UNWRAP_AMOUNT, user);
const userTokenBalance = await ctx.program.mint.getAssociatedBalance(
user.publicKey
);
if (userTokenBalance !== expectedFinalBalance) {
throw new Error(
`Incorrect user token balance, expected ${expectedFinalBalance}, received ${userTokenBalance}`
);
}
assert(
userTokenBalance === expectedFinalBalance,
`Incorrect user token balance, expected ${expectedFinalBalance}, received ${userTokenBalance}`
);
});
it('Closes associated token account', async () => {
if (!userTokenAddress) {
throw new Error(`User token address does not exist`);
}
assert(userTokenAddress, `User token address does not exist`);
await ctx.program.mint.getAssociatedBalance(user.publicKey);
@ -90,8 +102,9 @@ describe('Mint Tests', () => {
const userTokenAccount = await ctx.program.connection.getAccountInfo(
userTokenAddress
);
if (userTokenAccount !== null) {
throw new Error(`Failed to close associated token account`);
}
assert(
userTokenAccount === null,
`Failed to close associated token account`
);
});
});