fix: expose token program instructions
This commit is contained in:
parent
e1c2498f74
commit
99e6b44d03
|
@ -101,7 +101,7 @@ declare module '@solana/web3.js' {
|
||||||
fee: number;
|
fee: number;
|
||||||
|
|
||||||
constructor(opts?: TransactionCtorFields): Transaction;
|
constructor(opts?: TransactionCtorFields): Transaction;
|
||||||
add(item: Transaction | TransactionInstructionCtorFields): Transaction;
|
add(item: TransactionInstruction | TransactionInstructionCtorFields): Transaction;
|
||||||
sign(from: Account): void;
|
sign(from: Account): void;
|
||||||
serialize(): Buffer;
|
serialize(): Buffer;
|
||||||
}
|
}
|
||||||
|
@ -167,6 +167,29 @@ declare module '@solana/web3.js' {
|
||||||
account: PublicKey,
|
account: PublicKey,
|
||||||
newOwner: PublicKey
|
newOwner: PublicKey
|
||||||
): Promise<void>;
|
): Promise<void>;
|
||||||
|
|
||||||
|
transferInstruction(
|
||||||
|
owner: PublicKey,
|
||||||
|
source: PublicKey,
|
||||||
|
destination: PublicKey,
|
||||||
|
amount: number | TokenAmount,
|
||||||
|
): Promise<TransactionInstruction>;
|
||||||
|
approveInstruction(
|
||||||
|
owner: PublicKey,
|
||||||
|
account: PublicKey,
|
||||||
|
delegate: PublicKey,
|
||||||
|
amount: number | TokenAmount
|
||||||
|
): TransactionInstruction;
|
||||||
|
revokeInstruction(
|
||||||
|
owner: PublicKey,
|
||||||
|
account: PublicKey,
|
||||||
|
delegate: PublicKey,
|
||||||
|
): TransactionInstruction;
|
||||||
|
setOwnerInstruction(
|
||||||
|
owner: PublicKey,
|
||||||
|
account: PublicKey,
|
||||||
|
newOwner: PublicKey,
|
||||||
|
): TransactionInstruction;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === src/loader.js ===
|
// === src/loader.js ===
|
||||||
|
|
|
@ -8,5 +8,5 @@ export {NativeLoader} from './native-loader';
|
||||||
export {PublicKey} from './publickey';
|
export {PublicKey} from './publickey';
|
||||||
export {SystemProgram} from './system-program';
|
export {SystemProgram} from './system-program';
|
||||||
export {Token, TokenAmount} from './token-program';
|
export {Token, TokenAmount} from './token-program';
|
||||||
export {Transaction} from './transaction';
|
export {Transaction, TransactionInstruction} from './transaction';
|
||||||
export {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
export {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
||||||
|
|
|
@ -12,8 +12,9 @@ import {
|
||||||
PublicKey,
|
PublicKey,
|
||||||
SystemProgram,
|
SystemProgram,
|
||||||
Transaction,
|
Transaction,
|
||||||
|
TransactionInstruction,
|
||||||
|
sendAndConfirmTransaction,
|
||||||
} from '.';
|
} from '.';
|
||||||
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
|
||||||
import type {Connection} from '.';
|
import type {Connection} from '.';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -363,36 +364,18 @@ export class Token {
|
||||||
destination: PublicKey,
|
destination: PublicKey,
|
||||||
amount: number | TokenAmount,
|
amount: number | TokenAmount,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
await sendAndConfirmTransaction(
|
||||||
const accountInfo = await this.accountInfo(source);
|
this.connection,
|
||||||
if (!owner.publicKey.equals(accountInfo.owner)) {
|
owner,
|
||||||
throw new Error('Account owner mismatch');
|
new Transaction().add(
|
||||||
}
|
await this.transferInstruction(
|
||||||
|
owner.publicKey,
|
||||||
const userdataLayout = BufferLayout.struct([
|
source,
|
||||||
BufferLayout.u32('instruction'),
|
destination,
|
||||||
Layout.uint64('amount'),
|
amount,
|
||||||
]);
|
)
|
||||||
|
),
|
||||||
const userdata = Buffer.alloc(userdataLayout.span);
|
|
||||||
userdataLayout.encode(
|
|
||||||
{
|
|
||||||
instruction: 2, // Transfer instruction
|
|
||||||
amount: (new TokenAmount(amount)).toBuffer(),
|
|
||||||
},
|
|
||||||
userdata,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const keys = [owner.publicKey, source, destination];
|
|
||||||
if (accountInfo.source) {
|
|
||||||
keys.push(accountInfo.source);
|
|
||||||
}
|
|
||||||
const transaction = new Transaction().add({
|
|
||||||
keys,
|
|
||||||
programId: this.programId,
|
|
||||||
userdata,
|
|
||||||
});
|
|
||||||
await sendAndConfirmTransaction(this.connection, owner, transaction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -409,27 +392,18 @@ export class Token {
|
||||||
delegate: PublicKey,
|
delegate: PublicKey,
|
||||||
amount: number | TokenAmount
|
amount: number | TokenAmount
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
await sendAndConfirmTransaction(
|
||||||
const userdataLayout = BufferLayout.struct([
|
this.connection,
|
||||||
BufferLayout.u32('instruction'),
|
owner,
|
||||||
Layout.uint64('amount'),
|
new Transaction().add(
|
||||||
]);
|
this.approveInstruction(
|
||||||
|
owner.publicKey,
|
||||||
const userdata = Buffer.alloc(userdataLayout.span);
|
account,
|
||||||
userdataLayout.encode(
|
delegate,
|
||||||
{
|
amount,
|
||||||
instruction: 3, // Approve instruction
|
)
|
||||||
amount: (new TokenAmount(amount)).toBuffer(),
|
),
|
||||||
},
|
|
||||||
userdata,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const transaction = new Transaction().add({
|
|
||||||
keys: [owner.publicKey, account, delegate],
|
|
||||||
programId: this.programId,
|
|
||||||
userdata,
|
|
||||||
});
|
|
||||||
await sendAndConfirmTransaction(this.connection, owner, transaction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -459,7 +433,129 @@ export class Token {
|
||||||
account: PublicKey,
|
account: PublicKey,
|
||||||
newOwner: PublicKey,
|
newOwner: PublicKey,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
await sendAndConfirmTransaction(
|
||||||
|
this.connection,
|
||||||
|
owner,
|
||||||
|
new Transaction().add(
|
||||||
|
this.setOwnerInstruction(
|
||||||
|
owner.publicKey,
|
||||||
|
account,
|
||||||
|
newOwner
|
||||||
|
)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a Transfer instruction
|
||||||
|
*
|
||||||
|
* @param owner Owner of the source token account
|
||||||
|
* @param source Source token account
|
||||||
|
* @param destination Destination token account
|
||||||
|
* @param amount Number of tokens to transfer
|
||||||
|
*/
|
||||||
|
async transferInstruction(
|
||||||
|
owner: PublicKey,
|
||||||
|
source: PublicKey,
|
||||||
|
destination: PublicKey,
|
||||||
|
amount: number | TokenAmount,
|
||||||
|
): Promise<TransactionInstruction> {
|
||||||
|
const accountInfo = await this.accountInfo(source);
|
||||||
|
if (!owner.equals(accountInfo.owner)) {
|
||||||
|
throw new Error('Account owner mismatch');
|
||||||
|
}
|
||||||
|
|
||||||
|
const userdataLayout = BufferLayout.struct([
|
||||||
|
BufferLayout.u32('instruction'),
|
||||||
|
Layout.uint64('amount'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const userdata = Buffer.alloc(userdataLayout.span);
|
||||||
|
userdataLayout.encode(
|
||||||
|
{
|
||||||
|
instruction: 2, // Transfer instruction
|
||||||
|
amount: (new TokenAmount(amount)).toBuffer(),
|
||||||
|
},
|
||||||
|
userdata,
|
||||||
|
);
|
||||||
|
|
||||||
|
const keys = [owner, source, destination];
|
||||||
|
if (accountInfo.source) {
|
||||||
|
keys.push(accountInfo.source);
|
||||||
|
}
|
||||||
|
return new TransactionInstruction({
|
||||||
|
keys,
|
||||||
|
programId: this.programId,
|
||||||
|
userdata,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an Approve instruction
|
||||||
|
*
|
||||||
|
* @param programId Token program
|
||||||
|
* @param owner Owner of the source token account
|
||||||
|
* @param account Public key of the token account
|
||||||
|
* @param delegate Token account authorized to perform a transfer tokens from the source account
|
||||||
|
* @param amount Maximum number of tokens the delegate may transfer
|
||||||
|
*/
|
||||||
|
approveInstruction(
|
||||||
|
owner: PublicKey,
|
||||||
|
account: PublicKey,
|
||||||
|
delegate: PublicKey,
|
||||||
|
amount: number | TokenAmount
|
||||||
|
): TransactionInstruction {
|
||||||
|
const userdataLayout = BufferLayout.struct([
|
||||||
|
BufferLayout.u32('instruction'),
|
||||||
|
Layout.uint64('amount'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const userdata = Buffer.alloc(userdataLayout.span);
|
||||||
|
userdataLayout.encode(
|
||||||
|
{
|
||||||
|
instruction: 3, // Approve instruction
|
||||||
|
amount: (new TokenAmount(amount)).toBuffer(),
|
||||||
|
},
|
||||||
|
userdata,
|
||||||
|
);
|
||||||
|
|
||||||
|
return new TransactionInstruction({
|
||||||
|
keys: [owner, account, delegate],
|
||||||
|
programId: this.programId,
|
||||||
|
userdata,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an Revoke instruction
|
||||||
|
*
|
||||||
|
* @param programId Token program
|
||||||
|
* @param owner Owner of the source token account
|
||||||
|
* @param account Public key of the token account
|
||||||
|
* @param delegate Token account authorized to perform a transfer tokens from the source account
|
||||||
|
* @param amount Maximum number of tokens the delegate may transfer
|
||||||
|
*/
|
||||||
|
revokeInstruction(
|
||||||
|
owner: PublicKey,
|
||||||
|
account: PublicKey,
|
||||||
|
delegate: PublicKey,
|
||||||
|
): TransactionInstruction {
|
||||||
|
return this.approveInstruction(owner, account, delegate, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a SetOwner instruction
|
||||||
|
*
|
||||||
|
* @param programId Token program
|
||||||
|
* @param owner Owner of the token account
|
||||||
|
* @param account Public key of the token account
|
||||||
|
* @param newOwner New owner of the token account
|
||||||
|
*/
|
||||||
|
setOwnerInstruction(
|
||||||
|
owner: PublicKey,
|
||||||
|
account: PublicKey,
|
||||||
|
newOwner: PublicKey,
|
||||||
|
): TransactionInstruction {
|
||||||
const userdataLayout = BufferLayout.struct([
|
const userdataLayout = BufferLayout.struct([
|
||||||
BufferLayout.u32('instruction'),
|
BufferLayout.u32('instruction'),
|
||||||
]);
|
]);
|
||||||
|
@ -472,13 +568,11 @@ export class Token {
|
||||||
userdata,
|
userdata,
|
||||||
);
|
);
|
||||||
|
|
||||||
const keys = [owner.publicKey, account,newOwner];
|
return new TransactionInstruction({
|
||||||
const transaction = new Transaction().add({
|
keys: [owner, account, newOwner],
|
||||||
keys,
|
|
||||||
programId: this.programId,
|
programId: this.programId,
|
||||||
userdata,
|
userdata,
|
||||||
});
|
});
|
||||||
await sendAndConfirmTransaction(this.connection, owner, transaction);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue