Skip to content

Commit

Permalink
v9.7.0: add Token.solana initializeAccountInstruction + closeAccountI…
Browse files Browse the repository at this point in the history
…nstruction
  • Loading branch information
10xSebastian committed Oct 7, 2022
1 parent 4c6423b commit 7568df3
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Token.solana
// findAccount({ token, owner })
// createTransferInstruction({ token, amount, from, to })
// createAssociatedTokenAccountInstruction({ token, owner, payer })
// initializeAccountInstruction({ account, token, owner })
// closeAccountInstruction({ account, owner })
```

## Development
Expand Down
46 changes: 45 additions & 1 deletion dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ const TOKEN_LAYOUT = struct([
publicKey('closeAuthority')
]);

const INITIALIZE_LAYOUT = struct([
u8('instruction'),
publicKey('owner')
]);

const CLOSE_LAYOUT = struct([
u8('instruction')
]);

const createTransferInstruction = async ({ token, amount, from, to })=>{

let fromTokenAccount = await findProgramAddress({ token, owner: from });
Expand Down Expand Up @@ -134,10 +143,45 @@ const createAssociatedTokenAccountInstruction = async ({ token, owner, payer })
})
};

const initializeAccountInstruction = async ({ account, token, owner })=>{

const keys = [
{ pubkey: new PublicKey(account), isSigner: false, isWritable: true },
{ pubkey: new PublicKey(token), isSigner: false, isWritable: false },
];

const data = Buffer.alloc(INITIALIZE_LAYOUT.span);
INITIALIZE_LAYOUT.encode({
instruction: 18, // InitializeAccount3
owner: new PublicKey(owner)
}, data);

return new TransactionInstruction({ keys, programId: new PublicKey(TOKEN_PROGRAM), data })
};


const closeAccountInstruction = async ({ account, owner })=>{

const keys = [
{ pubkey: new PublicKey(account), isSigner: false, isWritable: true },
{ pubkey: new PublicKey(owner), isSigner: false, isWritable: true },
{ pubkey: new PublicKey(owner), isSigner: true, isWritable: false }
];

const data = Buffer.alloc(CLOSE_LAYOUT.span);
CLOSE_LAYOUT.encode({
instruction: 9 // CloseAccount
}, data);

return new TransactionInstruction({ keys, programId: new PublicKey(TOKEN_PROGRAM), data })
};

var instructions = /*#__PURE__*/Object.freeze({
__proto__: null,
createTransferInstruction: createTransferInstruction,
createAssociatedTokenAccountInstruction: createAssociatedTokenAccountInstruction
createAssociatedTokenAccountInstruction: createAssociatedTokenAccountInstruction,
initializeAccountInstruction: initializeAccountInstruction,
closeAccountInstruction: closeAccountInstruction
});

var allowanceOnEVM = ({ blockchain, address, api, owner, spender })=>{
Expand Down
46 changes: 45 additions & 1 deletion dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@
solanaWeb3_js.publicKey('closeAuthority')
]);

const INITIALIZE_LAYOUT = solanaWeb3_js.struct([
solanaWeb3_js.u8('instruction'),
solanaWeb3_js.publicKey('owner')
]);

const CLOSE_LAYOUT = solanaWeb3_js.struct([
solanaWeb3_js.u8('instruction')
]);

const createTransferInstruction = async ({ token, amount, from, to })=>{

let fromTokenAccount = await findProgramAddress({ token, owner: from });
Expand Down Expand Up @@ -135,10 +144,45 @@
})
};

const initializeAccountInstruction = async ({ account, token, owner })=>{

const keys = [
{ pubkey: new solanaWeb3_js.PublicKey(account), isSigner: false, isWritable: true },
{ pubkey: new solanaWeb3_js.PublicKey(token), isSigner: false, isWritable: false },
];

const data = solanaWeb3_js.Buffer.alloc(INITIALIZE_LAYOUT.span);
INITIALIZE_LAYOUT.encode({
instruction: 18, // InitializeAccount3
owner: new solanaWeb3_js.PublicKey(owner)
}, data);

return new solanaWeb3_js.TransactionInstruction({ keys, programId: new solanaWeb3_js.PublicKey(TOKEN_PROGRAM), data })
};


const closeAccountInstruction = async ({ account, owner })=>{

const keys = [
{ pubkey: new solanaWeb3_js.PublicKey(account), isSigner: false, isWritable: true },
{ pubkey: new solanaWeb3_js.PublicKey(owner), isSigner: false, isWritable: true },
{ pubkey: new solanaWeb3_js.PublicKey(owner), isSigner: true, isWritable: false }
];

const data = solanaWeb3_js.Buffer.alloc(CLOSE_LAYOUT.span);
CLOSE_LAYOUT.encode({
instruction: 9 // CloseAccount
}, data);

return new solanaWeb3_js.TransactionInstruction({ keys, programId: new solanaWeb3_js.PublicKey(TOKEN_PROGRAM), data })
};

var instructions = /*#__PURE__*/Object.freeze({
__proto__: null,
createTransferInstruction: createTransferInstruction,
createAssociatedTokenAccountInstruction: createAssociatedTokenAccountInstruction
createAssociatedTokenAccountInstruction: createAssociatedTokenAccountInstruction,
initializeAccountInstruction: initializeAccountInstruction,
closeAccountInstruction: closeAccountInstruction
});

var allowanceOnEVM = ({ blockchain, address, api, owner, spender })=>{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-tokens",
"moduleName": "Web3Tokens",
"version": "9.6.0",
"version": "9.7.0",
"description": "JavaScript library providing basic functionalities to interact with web3 tokens.",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down
37 changes: 36 additions & 1 deletion src/platforms/solana/instructions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import findProgramAddress from './findProgramAddress'
import { SystemProgram, PublicKey, TransactionInstruction, Buffer, BN } from '@depay/solana-web3.js'
import { TOKEN_PROGRAM, ASSOCIATED_TOKEN_PROGRAM } from './constants'
import { TRANSFER_LAYOUT } from './layouts'
import { TRANSFER_LAYOUT, INITIALIZE_LAYOUT, CLOSE_LAYOUT } from './layouts'

const createTransferInstruction = async ({ token, amount, from, to })=>{

Expand Down Expand Up @@ -47,7 +47,42 @@ const createAssociatedTokenAccountInstruction = async ({ token, owner, payer })
})
}

const initializeAccountInstruction = async ({ account, token, owner })=>{

const keys = [
{ pubkey: new PublicKey(account), isSigner: false, isWritable: true },
{ pubkey: new PublicKey(token), isSigner: false, isWritable: false },
]

const data = Buffer.alloc(INITIALIZE_LAYOUT.span)
INITIALIZE_LAYOUT.encode({
instruction: 18, // InitializeAccount3
owner: new PublicKey(owner)
}, data)

return new TransactionInstruction({ keys, programId: new PublicKey(TOKEN_PROGRAM), data })
}


const closeAccountInstruction = async ({ account, owner })=>{

const keys = [
{ pubkey: new PublicKey(account), isSigner: false, isWritable: true },
{ pubkey: new PublicKey(owner), isSigner: false, isWritable: true },
{ pubkey: new PublicKey(owner), isSigner: true, isWritable: false }
]

const data = Buffer.alloc(CLOSE_LAYOUT.span)
CLOSE_LAYOUT.encode({
instruction: 9 // CloseAccount
}, data)

return new TransactionInstruction({ keys, programId: new PublicKey(TOKEN_PROGRAM), data })
}

export {
createTransferInstruction,
createAssociatedTokenAccountInstruction,
initializeAccountInstruction,
closeAccountInstruction,
}
11 changes: 11 additions & 0 deletions src/platforms/solana/layouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,20 @@ const TOKEN_LAYOUT = struct([
publicKey('closeAuthority')
])

const INITIALIZE_LAYOUT = struct([
u8('instruction'),
publicKey('owner')
])

const CLOSE_LAYOUT = struct([
u8('instruction')
])

export {
MINT_LAYOUT,
METADATA_LAYOUT,
TRANSFER_LAYOUT,
TOKEN_LAYOUT,
INITIALIZE_LAYOUT,
CLOSE_LAYOUT,
}
37 changes: 37 additions & 0 deletions tests/units/platforms/solana/closeAccountInstruction.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { mock, resetMocks } from '@depay/web3-mock'
import { resetCache, provider } from '@depay/web3-client'
import { supported } from 'src/blockchains'
import { SystemProgram } from '@depay/solana-web3.js'
import { Token } from 'src'
import { TOKEN_PROGRAM } from 'src/platforms/solana/constants'

describe('closeAccountInstruction', () => {

supported.solana.forEach((blockchain)=>{

beforeEach(resetCache)
beforeEach(resetMocks)

it('provides a close account instruction', async ()=> {

let account = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'
let owner = '2UgCJaHU5y8NC4uWQcZYeV9a5RyYLF7iKYCybCsdFFD1'

mock({
blockchain,
provider: provider(blockchain),
})

let instruction = await Token.solana.closeAccountInstruction({
account,
owner,
})

expect(instruction.keys[0].pubkey.toString()).toEqual(account)
expect(instruction.keys[1].pubkey.toString()).toEqual(owner)
expect(instruction.keys[2].pubkey.toString()).toEqual(owner)
expect(instruction.programId.toString()).toEqual(Token.solana.TOKEN_PROGRAM)
})
})
})

37 changes: 37 additions & 0 deletions tests/units/platforms/solana/initializeAccountInstruction.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { mock, resetMocks } from '@depay/web3-mock'
import { resetCache, provider } from '@depay/web3-client'
import { supported } from 'src/blockchains'
import { SystemProgram } from '@depay/solana-web3.js'
import { Token } from 'src'
import { TOKEN_PROGRAM } from 'src/platforms/solana/constants'

describe('initializeAccountInstruction', () => {

supported.solana.forEach((blockchain)=>{

beforeEach(resetCache)
beforeEach(resetMocks)

it('provides a initialize token account instruction', async ()=> {

let account = 'EFjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'
let token = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'
let owner = '2UgCJaHU5y8NC4uWQcZYeV9a5RyYLF7iKYCybCsdFFD1'

mock({
blockchain,
provider: provider(blockchain),
})

let instruction = await Token.solana.initializeAccountInstruction({
account,
token,
owner,
})

expect(instruction.keys[0].pubkey.toString()).toEqual(account)
expect(instruction.keys[1].pubkey.toString()).toEqual(token)
expect(instruction.programId.toString()).toEqual(Token.solana.TOKEN_PROGRAM)
})
})
})

0 comments on commit 7568df3

Please sign in to comment.