solana.js: oracle create packing
This commit is contained in:
parent
19ef0f9e6e
commit
16a8b16f39
|
@ -176,7 +176,9 @@ export class OracleAccount extends Account<types.OracleAccountData> {
|
||||||
queueAccount: QueueAccount;
|
queueAccount: QueueAccount;
|
||||||
} & OracleInitParams &
|
} & OracleInitParams &
|
||||||
OracleStakeParams
|
OracleStakeParams
|
||||||
): Promise<[OracleAccount, TransactionObject]> {
|
): Promise<[OracleAccount, Array<TransactionObject>]> {
|
||||||
|
const txns: Array<TransactionObject> = [];
|
||||||
|
|
||||||
const tokenWallet = params.stakingWalletKeypair ?? Keypair.generate();
|
const tokenWallet = params.stakingWalletKeypair ?? Keypair.generate();
|
||||||
|
|
||||||
const authority = params.authority?.publicKey ?? payer;
|
const authority = params.authority?.publicKey ?? payer;
|
||||||
|
@ -238,6 +240,8 @@ export class OracleAccount extends Account<types.OracleAccountData> {
|
||||||
params.authority ? [params.authority, tokenWallet] : [tokenWallet]
|
params.authority ? [params.authority, tokenWallet] : [tokenWallet]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
txns.push(oracleInit);
|
||||||
|
|
||||||
if (params.stakeAmount && params.stakeAmount > 0) {
|
if (params.stakeAmount && params.stakeAmount > 0) {
|
||||||
const depositTxn = await oracleAccount.stakeInstructions(payer, {
|
const depositTxn = await oracleAccount.stakeInstructions(payer, {
|
||||||
stakeAmount: params.stakeAmount,
|
stakeAmount: params.stakeAmount,
|
||||||
|
@ -245,10 +249,10 @@ export class OracleAccount extends Account<types.OracleAccountData> {
|
||||||
funderTokenAccount: params.funderTokenAccount,
|
funderTokenAccount: params.funderTokenAccount,
|
||||||
tokenAccount: tokenWallet.publicKey,
|
tokenAccount: tokenWallet.publicKey,
|
||||||
});
|
});
|
||||||
oracleInit.combine(depositTxn);
|
txns.push(depositTxn);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [oracleAccount, oracleInit];
|
return [oracleAccount, txns];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async create(
|
public static async create(
|
||||||
|
@ -257,16 +261,16 @@ export class OracleAccount extends Account<types.OracleAccountData> {
|
||||||
queueAccount: QueueAccount;
|
queueAccount: QueueAccount;
|
||||||
} & OracleInitParams &
|
} & OracleInitParams &
|
||||||
OracleStakeParams
|
OracleStakeParams
|
||||||
): Promise<[OracleAccount, TransactionSignature]> {
|
): Promise<[OracleAccount, Array<TransactionSignature>]> {
|
||||||
const [oracleAccount, txnObject] = await OracleAccount.createInstructions(
|
const [oracleAccount, txns] = await OracleAccount.createInstructions(
|
||||||
program,
|
program,
|
||||||
program.walletPubkey,
|
program.walletPubkey,
|
||||||
params
|
params
|
||||||
);
|
);
|
||||||
|
|
||||||
const txnSignature = await program.signAndSend(txnObject);
|
const signatures = await program.signAndSendAll(txns);
|
||||||
|
|
||||||
return [oracleAccount, txnSignature];
|
return [oracleAccount, signatures];
|
||||||
}
|
}
|
||||||
|
|
||||||
async stakeInstructions(
|
async stakeInstructions(
|
||||||
|
|
|
@ -374,7 +374,7 @@ export class QueueAccount extends Account<types.OracleQueueAccountData> {
|
||||||
return [
|
return [
|
||||||
oracleAccount,
|
oracleAccount,
|
||||||
TransactionObject.pack([
|
TransactionObject.pack([
|
||||||
createOracleTxnObject,
|
...createOracleTxnObject,
|
||||||
createPermissionTxnObject,
|
createPermissionTxnObject,
|
||||||
]),
|
]),
|
||||||
];
|
];
|
||||||
|
|
|
@ -411,9 +411,7 @@ export class NativeMint extends Mint {
|
||||||
}
|
}
|
||||||
wrapAmount = params.fundUpTo.sub(userTokenBalance);
|
wrapAmount = params.fundUpTo.sub(userTokenBalance);
|
||||||
} else if ('amount' in params) {
|
} else if ('amount' in params) {
|
||||||
wrapAmount = new Big(params.amount).mul(
|
wrapAmount = new Big(params.amount);
|
||||||
new Big(10).pow(this.mint.decimals)
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Must specify fundUpTo or amount to perform this actions`
|
`Must specify fundUpTo or amount to perform this actions`
|
||||||
|
|
|
@ -4,6 +4,7 @@ import assert from 'assert';
|
||||||
import * as anchor from '@project-serum/anchor';
|
import * as anchor from '@project-serum/anchor';
|
||||||
import { setupTest, TestContext } from './utilts';
|
import { setupTest, TestContext } from './utilts';
|
||||||
import { Keypair, PublicKey } from '@solana/web3.js';
|
import { Keypair, PublicKey } from '@solana/web3.js';
|
||||||
|
import Big from 'big.js';
|
||||||
|
|
||||||
describe('Mint Tests', () => {
|
describe('Mint Tests', () => {
|
||||||
let ctx: TestContext;
|
let ctx: TestContext;
|
||||||
|
@ -22,66 +23,77 @@ describe('Mint Tests', () => {
|
||||||
);
|
);
|
||||||
await ctx.program.connection.confirmTransaction(airdropTxn);
|
await ctx.program.connection.confirmTransaction(airdropTxn);
|
||||||
|
|
||||||
const [tokenAddress] = await ctx.program.mint.createAssocatedUser(
|
[userTokenAddress] = await ctx.program.mint.createAssocatedUser(
|
||||||
ctx.payer.publicKey,
|
ctx.payer.publicKey,
|
||||||
user.publicKey
|
user.publicKey
|
||||||
);
|
);
|
||||||
userTokenAddress = tokenAddress;
|
|
||||||
|
|
||||||
const userTokenBalance =
|
const userTokenBalance =
|
||||||
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
|
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
|
||||||
|
|
||||||
if (userTokenBalance !== 0) {
|
assert(
|
||||||
throw new Error(
|
userTokenBalance === 0,
|
||||||
`Incorrect user token balance, expected 0, received ${userTokenBalance}`
|
`Incorrect user token balance, expected 0, received ${userTokenBalance}`
|
||||||
);
|
);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Wraps SOL', async () => {
|
it('Wraps SOL', async () => {
|
||||||
if (!userTokenAddress) {
|
assert(userTokenAddress, `User token address does not exist`);
|
||||||
throw new Error(`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 =
|
const userTokenBalance =
|
||||||
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
|
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
|
||||||
if (userTokenBalance !== 0.25) {
|
|
||||||
throw new Error(
|
assert(
|
||||||
`Incorrect user token balance, expected 0.25, received ${userTokenBalance}`
|
userTokenBalance === WRAP_AMOUNT,
|
||||||
|
`Incorrect user token balance, expected ${WRAP_AMOUNT} wSOL, received ${userTokenBalance}`
|
||||||
);
|
);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Unwraps SOL', async () => {
|
it('Unwraps SOL', async () => {
|
||||||
if (!userTokenAddress) {
|
assert(userTokenAddress, `User token address does not exist`);
|
||||||
throw new Error(`User token address does not exist`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const initialUserTokenBalance =
|
const UNWRAP_AMOUNT = 0.1;
|
||||||
|
|
||||||
|
let initialUserTokenBalance =
|
||||||
|
(await ctx.program.mint.getAssociatedBalance(user.publicKey)) ?? 0;
|
||||||
|
// 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.getAssociatedBalance(user.publicKey)) ?? 0;
|
||||||
const expectedFinalBalance = initialUserTokenBalance - 0.1;
|
|
||||||
if (expectedFinalBalance < 0) {
|
|
||||||
throw new Error(`Final user token address would be negative`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
const userTokenBalance = await ctx.program.mint.getAssociatedBalance(
|
||||||
user.publicKey
|
user.publicKey
|
||||||
);
|
);
|
||||||
if (userTokenBalance !== expectedFinalBalance) {
|
assert(
|
||||||
throw new Error(
|
userTokenBalance === expectedFinalBalance,
|
||||||
`Incorrect user token balance, expected ${expectedFinalBalance}, received ${userTokenBalance}`
|
`Incorrect user token balance, expected ${expectedFinalBalance}, received ${userTokenBalance}`
|
||||||
);
|
);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Closes associated token account', async () => {
|
it('Closes associated token account', async () => {
|
||||||
if (!userTokenAddress) {
|
assert(userTokenAddress, `User token address does not exist`);
|
||||||
throw new Error(`User token address does not exist`);
|
|
||||||
}
|
|
||||||
|
|
||||||
await ctx.program.mint.getAssociatedBalance(user.publicKey);
|
await ctx.program.mint.getAssociatedBalance(user.publicKey);
|
||||||
|
|
||||||
|
@ -90,8 +102,9 @@ describe('Mint Tests', () => {
|
||||||
const userTokenAccount = await ctx.program.connection.getAccountInfo(
|
const userTokenAccount = await ctx.program.connection.getAccountInfo(
|
||||||
userTokenAddress
|
userTokenAddress
|
||||||
);
|
);
|
||||||
if (userTokenAccount !== null) {
|
assert(
|
||||||
throw new Error(`Failed to close associated token account`);
|
userTokenAccount === null,
|
||||||
}
|
`Failed to close associated token account`
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue