fix: add Token::setOwner
This commit is contained in:
parent
5cfe43b980
commit
00890ef9e0
|
@ -145,15 +145,20 @@ declare module '@solana/web3.js' {
|
||||||
): Promise<void>;
|
): Promise<void>;
|
||||||
approve(
|
approve(
|
||||||
owner: Account,
|
owner: Account,
|
||||||
source: PublicKey,
|
account: PublicKey,
|
||||||
delegate: PublicKey,
|
delegate: PublicKey,
|
||||||
amount: number | TokenAmount
|
amount: number | TokenAmount
|
||||||
): Promise<void>;
|
): Promise<void>;
|
||||||
revoke(
|
revoke(
|
||||||
owner: Account,
|
owner: Account,
|
||||||
source: PublicKey,
|
account: PublicKey,
|
||||||
delegate: PublicKey
|
delegate: PublicKey
|
||||||
): Promise<void>;
|
): Promise<void>;
|
||||||
|
setOwner(
|
||||||
|
owner: Account,
|
||||||
|
account: PublicKey,
|
||||||
|
newOwner: PublicKey
|
||||||
|
): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === src/loader.js ===
|
// === src/loader.js ===
|
||||||
|
|
|
@ -402,13 +402,13 @@ export class Token {
|
||||||
* Grant a third-party permission to transfer up the specified number of tokens from an account
|
* Grant a third-party permission to transfer up the specified number of tokens from an account
|
||||||
*
|
*
|
||||||
* @param owner Owner of the source token account
|
* @param owner Owner of the source token account
|
||||||
* @param source 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 delegate Token account authorized to perform a transfer tokens from the source account
|
||||||
* @param amount Maximum number of tokens the delegate may transfer
|
* @param amount Maximum number of tokens the delegate may transfer
|
||||||
*/
|
*/
|
||||||
async approve(
|
async approve(
|
||||||
owner: Account,
|
owner: Account,
|
||||||
source: PublicKey,
|
account: PublicKey,
|
||||||
delegate: PublicKey,
|
delegate: PublicKey,
|
||||||
amount: number | TokenAmount
|
amount: number | TokenAmount
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
@ -429,7 +429,7 @@ export class Token {
|
||||||
|
|
||||||
const transaction = new Transaction({
|
const transaction = new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [owner.publicKey, source, delegate],
|
keys: [owner.publicKey, account, delegate],
|
||||||
programId: this.programId,
|
programId: this.programId,
|
||||||
userdata,
|
userdata,
|
||||||
});
|
});
|
||||||
|
@ -440,15 +440,50 @@ export class Token {
|
||||||
* Remove approval for the transfer of any remaining tokens
|
* Remove approval for the transfer of any remaining tokens
|
||||||
*
|
*
|
||||||
* @param owner Owner of the source token account
|
* @param owner Owner of the source token account
|
||||||
* @param source Source token account
|
* @param account Public key of the token account
|
||||||
* @param delegate Token account to revoke authorization from
|
* @param delegate Token account to revoke authorization from
|
||||||
*/
|
*/
|
||||||
revoke(
|
revoke(
|
||||||
owner: Account,
|
owner: Account,
|
||||||
source: PublicKey,
|
account: PublicKey,
|
||||||
delegate: PublicKey
|
delegate: PublicKey
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return this.approve(owner, source, delegate, 0);
|
return this.approve(owner, account, delegate, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign a new owner to the account
|
||||||
|
*
|
||||||
|
* @param owner Owner of the token account
|
||||||
|
* @param account Public key of the token account
|
||||||
|
* @param newOwner New owner of the token account
|
||||||
|
*/
|
||||||
|
async setOwner(
|
||||||
|
owner: Account,
|
||||||
|
account: PublicKey,
|
||||||
|
newOwner: PublicKey,
|
||||||
|
): Promise<void> {
|
||||||
|
|
||||||
|
const userdataLayout = BufferLayout.struct([
|
||||||
|
BufferLayout.u32('instruction'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const userdata = Buffer.alloc(userdataLayout.span);
|
||||||
|
userdataLayout.encode(
|
||||||
|
{
|
||||||
|
instruction: 4, // SetOwner instruction
|
||||||
|
},
|
||||||
|
userdata,
|
||||||
|
);
|
||||||
|
|
||||||
|
const keys = [owner.publicKey, account,newOwner];
|
||||||
|
const transaction = new Transaction({
|
||||||
|
fee: 0,
|
||||||
|
keys,
|
||||||
|
programId: this.programId,
|
||||||
|
userdata,
|
||||||
|
});
|
||||||
|
await sendAndConfirmTransaction(this.connection, owner, transaction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -473,7 +473,7 @@ test('invalid approve', async () => {
|
||||||
const account2 = await testToken.newAccount(owner);
|
const account2 = await testToken.newAccount(owner);
|
||||||
|
|
||||||
// account2 is not a delegate account of account1
|
// account2 is not a delegate account of account1
|
||||||
expect(
|
await expect(
|
||||||
testToken.approve(
|
testToken.approve(
|
||||||
owner,
|
owner,
|
||||||
account1,
|
account1,
|
||||||
|
@ -483,7 +483,7 @@ test('invalid approve', async () => {
|
||||||
).rejects.toThrow();
|
).rejects.toThrow();
|
||||||
|
|
||||||
// account1Delegate is not a delegate account of account2
|
// account1Delegate is not a delegate account of account2
|
||||||
expect(
|
await expect(
|
||||||
testToken.approve(
|
testToken.approve(
|
||||||
owner,
|
owner,
|
||||||
account2,
|
account2,
|
||||||
|
@ -548,7 +548,7 @@ test.skip('fail on approve overspend', async () => {
|
||||||
expect(delegateAccountInfo.amount.toNumber()).toBe(0);
|
expect(delegateAccountInfo.amount.toNumber()).toBe(0);
|
||||||
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(2);
|
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(2);
|
||||||
|
|
||||||
expect(
|
await expect(
|
||||||
testToken.transfer(
|
testToken.transfer(
|
||||||
owner,
|
owner,
|
||||||
account1Delegate,
|
account1Delegate,
|
||||||
|
@ -557,3 +557,28 @@ test.skip('fail on approve overspend', async () => {
|
||||||
)
|
)
|
||||||
).rejects.toThrow();
|
).rejects.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('set owner', async () => {
|
||||||
|
if (mockRpcEnabled) {
|
||||||
|
console.log('non-live test skipped');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const connection = new Connection(url);
|
||||||
|
const owner = await newAccountWithTokens(connection);
|
||||||
|
const newOwner = await newAccountWithTokens(connection);
|
||||||
|
|
||||||
|
const account = await testToken.newAccount(owner);
|
||||||
|
|
||||||
|
await testToken.setOwner(owner, account, newOwner.publicKey);
|
||||||
|
await expect(
|
||||||
|
testToken.setOwner(owner, account, newOwner.publicKey)
|
||||||
|
).rejects.toThrow();
|
||||||
|
|
||||||
|
await testToken.setOwner(newOwner, account, owner.publicKey);
|
||||||
|
await expect(
|
||||||
|
testToken.setOwner(newOwner, account, owner.publicKey)
|
||||||
|
).rejects.toThrow();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue