feat: expose ERC20 token originalAmount field
This commit is contained in:
parent
b66ca1a84d
commit
a9fc62d891
@ -114,6 +114,7 @@ declare module '@solana/web3.js' {
|
|||||||
owner: PublicKey;
|
owner: PublicKey;
|
||||||
amount: TokenAmount;
|
amount: TokenAmount;
|
||||||
source: null | PublicKey;
|
source: null | PublicKey;
|
||||||
|
originalAmount: TokenAmount;
|
||||||
|}
|
|}
|
||||||
declare type TokenAndPublicKey = [Token, PublicKey];
|
declare type TokenAndPublicKey = [Token, PublicKey];
|
||||||
|
|
||||||
|
@ -111,6 +111,12 @@ type TokenAccountInfo = {|
|
|||||||
* an allowance of tokens that may be transferred from the source account
|
* an allowance of tokens that may be transferred from the source account
|
||||||
*/
|
*/
|
||||||
source: null | PublicKey,
|
source: null | PublicKey,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Original amount of tokens this delegate account was authorized to spend
|
||||||
|
* If `source` is null, originalAmount is zero
|
||||||
|
*/
|
||||||
|
originalAmount: TokenAmount,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -122,6 +128,7 @@ const TokenAccountInfoLayout = BufferLayout.struct([
|
|||||||
Layout.uint64('amount'),
|
Layout.uint64('amount'),
|
||||||
BufferLayout.u8('sourceOption'),
|
BufferLayout.u8('sourceOption'),
|
||||||
Layout.publicKey('source'),
|
Layout.publicKey('source'),
|
||||||
|
Layout.uint64('originalAmount'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
@ -320,7 +327,13 @@ export class Token {
|
|||||||
tokenAccountInfo.token = new PublicKey(tokenAccountInfo.token);
|
tokenAccountInfo.token = new PublicKey(tokenAccountInfo.token);
|
||||||
tokenAccountInfo.owner = new PublicKey(tokenAccountInfo.owner);
|
tokenAccountInfo.owner = new PublicKey(tokenAccountInfo.owner);
|
||||||
tokenAccountInfo.amount = TokenAmount.fromBuffer(tokenAccountInfo.amount);
|
tokenAccountInfo.amount = TokenAmount.fromBuffer(tokenAccountInfo.amount);
|
||||||
tokenAccountInfo.source = tokenAccountInfo.sourceOption === 0 ? null : new PublicKey(tokenAccountInfo.source);
|
if (tokenAccountInfo.sourceOption === 0) {
|
||||||
|
tokenAccountInfo.source = null;
|
||||||
|
tokenAccountInfo.originalAmount = new TokenAmount();
|
||||||
|
} else {
|
||||||
|
tokenAccountInfo.source = new PublicKey(tokenAccountInfo.source);
|
||||||
|
tokenAccountInfo.originalAmount = TokenAmount.fromBuffer(tokenAccountInfo.originalAmount);
|
||||||
|
}
|
||||||
|
|
||||||
if (!tokenAccountInfo.token.equals(this.token)) {
|
if (!tokenAccountInfo.token.equals(this.token)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
@ -174,6 +174,7 @@ test('create new token', async () => {
|
|||||||
expect(accountInfo.owner.equals(initialOwner.publicKey)).toBe(true);
|
expect(accountInfo.owner.equals(initialOwner.publicKey)).toBe(true);
|
||||||
expect(accountInfo.amount.toNumber()).toBe(10000);
|
expect(accountInfo.amount.toNumber()).toBe(10000);
|
||||||
expect(accountInfo.source).toBe(null);
|
expect(accountInfo.source).toBe(null);
|
||||||
|
expect(accountInfo.originalAmount.toNumber()).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -379,6 +380,7 @@ test('approve/revoke', async () => {
|
|||||||
200, 1, 0, 0, 0, 0, 0, 0,
|
200, 1, 0, 0, 0, 0, 0, 0,
|
||||||
1,
|
1,
|
||||||
...initialOwnerTokenAccount.toBuffer(),
|
...initialOwnerTokenAccount.toBuffer(),
|
||||||
|
200, 1, 0, 0, 0, 0, 0, 0,
|
||||||
],
|
],
|
||||||
executable: false,
|
executable: false,
|
||||||
loader_program_id: [
|
loader_program_id: [
|
||||||
@ -393,6 +395,7 @@ test('approve/revoke', async () => {
|
|||||||
let delegateAccountInfo = await testToken.accountInfo(delegate);
|
let delegateAccountInfo = await testToken.accountInfo(delegate);
|
||||||
|
|
||||||
expect(delegateAccountInfo.amount.toNumber()).toBe(456);
|
expect(delegateAccountInfo.amount.toNumber()).toBe(456);
|
||||||
|
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(456);
|
||||||
if (delegateAccountInfo.source === null) {
|
if (delegateAccountInfo.source === null) {
|
||||||
throw new Error('source should not be null');
|
throw new Error('source should not be null');
|
||||||
} else {
|
} else {
|
||||||
@ -432,6 +435,7 @@ test('approve/revoke', async () => {
|
|||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
1,
|
1,
|
||||||
...initialOwnerTokenAccount.toBuffer(),
|
...initialOwnerTokenAccount.toBuffer(),
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
],
|
],
|
||||||
executable: false,
|
executable: false,
|
||||||
loader_program_id: [
|
loader_program_id: [
|
||||||
@ -444,8 +448,8 @@ test('approve/revoke', async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delegateAccountInfo = await testToken.accountInfo(delegate);
|
delegateAccountInfo = await testToken.accountInfo(delegate);
|
||||||
|
|
||||||
expect(delegateAccountInfo.amount.toNumber()).toBe(0);
|
expect(delegateAccountInfo.amount.toNumber()).toBe(0);
|
||||||
|
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(0);
|
||||||
if (delegateAccountInfo.source === null) {
|
if (delegateAccountInfo.source === null) {
|
||||||
throw new Error('source should not be null');
|
throw new Error('source should not be null');
|
||||||
} else {
|
} else {
|
||||||
@ -517,12 +521,9 @@ test.skip('fail on approve overspend', async () => {
|
|||||||
2
|
2
|
||||||
);
|
);
|
||||||
|
|
||||||
await testToken.transfer(
|
let delegateAccountInfo = await testToken.accountInfo(account1Delegate);
|
||||||
owner,
|
expect(delegateAccountInfo.amount.toNumber()).toBe(2);
|
||||||
account1Delegate,
|
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(2);
|
||||||
account2,
|
|
||||||
1,
|
|
||||||
);
|
|
||||||
|
|
||||||
await testToken.transfer(
|
await testToken.transfer(
|
||||||
owner,
|
owner,
|
||||||
@ -531,6 +532,21 @@ test.skip('fail on approve overspend', async () => {
|
|||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
delegateAccountInfo = await testToken.accountInfo(account1Delegate);
|
||||||
|
expect(delegateAccountInfo.amount.toNumber()).toBe(1);
|
||||||
|
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(2);
|
||||||
|
|
||||||
|
await testToken.transfer(
|
||||||
|
owner,
|
||||||
|
account1Delegate,
|
||||||
|
account2,
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
|
||||||
|
delegateAccountInfo = await testToken.accountInfo(account1Delegate);
|
||||||
|
expect(delegateAccountInfo.amount.toNumber()).toBe(0);
|
||||||
|
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(2);
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
testToken.transfer(
|
testToken.transfer(
|
||||||
owner,
|
owner,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user