solana.js: added payer verification

This commit is contained in:
Conner Gallagher 2022-11-30 11:57:43 -07:00
parent 03cdd248d2
commit e6b5499758
5 changed files with 26 additions and 11 deletions

View File

@ -90,7 +90,7 @@ export class LeaseAccount extends Account<types.LeaseAccountData> {
payer: PublicKey,
params: {
loadAmount?: number;
funder?: PublicKey;
funderTokenAccount?: PublicKey;
funderAuthority?: Keypair;
queuePubkey: PublicKey;
aggregatorPubkey: PublicKey;
@ -107,11 +107,14 @@ export class LeaseAccount extends Account<types.LeaseAccountData> {
const funderAuthority = params.funderAuthority
? params.funderAuthority.publicKey
: payer;
const funder = params.funder
? params.funder
const funderTokenAccount = params.funderTokenAccount
? params.funderTokenAccount
: program.mint.getAssociatedAddress(funderAuthority);
const funderBalance = (await program.mint.getBalance(funderAuthority)) ?? 0;
if (loadAmount && funderBalance < loadAmount) {
const funderTokenBalance =
(await program.mint.getBalance(funderAuthority)) ?? 0;
if (loadAmount && funderTokenBalance < loadAmount) {
const wrapIxns = await program.mint.wrapInstruction(
payer,
{ amount: loadAmount },
@ -166,7 +169,7 @@ export class LeaseAccount extends Account<types.LeaseAccountData> {
payer: payer,
systemProgram: SystemProgram.programId,
tokenProgram: spl.TOKEN_PROGRAM_ID,
funder: funder,
funder: funderTokenAccount,
owner: funderAuthority,
escrow: escrow,
programState: program.programState.publicKey,

View File

@ -336,6 +336,7 @@ export class QueueAccount extends Account<types.OracleQueueAccountData> {
const userTokenAccountInfo = await this.program.connection.getAccountInfo(
userTokenAddress
);
if (userTokenAccountInfo === null) {
const [createTokenAccount] =
this.program.mint.createAssocatedUserInstruction(payer);
@ -387,7 +388,7 @@ export class QueueAccount extends Account<types.OracleQueueAccountData> {
payer,
{
loadAmount: params.fundAmount,
funder: params.funderTokenAccount,
funderTokenAccount: params.funderTokenAccount,
funderAuthority: params.funderAuthority,
aggregatorPubkey: aggregatorAccount.publicKey,
queuePubkey: this.publicKey,

View File

@ -220,18 +220,18 @@ export class Mint {
// );
// }
const balance = userAccount
const tokenBalance = userAccount
? new Big(this.fromTokenAmount(userAccount.amount))
: new Big(0);
let wrapAmount: Big;
if ('fundUpTo' in params) {
if (balance.gte(params.fundUpTo)) {
if (tokenBalance.gte(params.fundUpTo)) {
return new TransactionObject(payer, [], []);
}
wrapAmount = params.fundUpTo.sub(balance);
wrapAmount = params.fundUpTo.sub(tokenBalance);
} else if ('amount' in params) {
wrapAmount = balance.add(params.amount);
wrapAmount = tokenBalance.add(params.amount);
} else {
throw new Error(
`Must specify fundUpTo or amount to perform this actions`

View File

@ -177,6 +177,12 @@ export class SwitchboardProgram {
);
}
public verifyPayer(): void {
if (this.isReadOnly) {
throw new errors.SwitchboardProgramReadOnlyError();
}
}
public get account(): anchor.AccountNamespace {
return this._program.account;
}

View File

@ -104,6 +104,11 @@ export class TransactionObject implements ITransactionObject {
ixns: Array<TransactionInstruction>,
signers: Array<Keypair>
) {
// verify payer is not default pubkey
if (payer.equals(PublicKey.default)) {
throw new errors.SwitchboardProgramReadOnlyError();
}
// verify num ixns
if (ixns.length > 10) {
throw new errors.TransactionInstructionOverflowError(ixns.length);