-
-
Save jamesflorentino/1a2ff564e62124b6ed7c030b5ef4aef7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as bip32 from 'bip32'; | |
import * as BufferLayout from 'buffer-layout'; | |
import _ from 'lodash'; | |
import nacl from 'tweetnacl'; | |
import { Service } from 'typedi'; | |
import { | |
Account, Connection, PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, | |
TransactionInstruction | |
} from '@solana/web3.js'; | |
import { env } from '../../env'; | |
import { SolanaAccountQuery } from '../controllers/requests/SolanaQuery'; | |
@Service() | |
export class SolanaService { | |
public async getTransactions( | |
solanaAccountQuery: SolanaAccountQuery | |
): Promise<object> { | |
const ACCOUNT_LAYOUT = BufferLayout.struct([ | |
BufferLayout.blob(32, 'mint'), | |
BufferLayout.blob(32, 'owner'), | |
BufferLayout.nu64('amount'), | |
BufferLayout.blob(93), | |
]); | |
const TOKEN_PROGRAM_ID = new PublicKey( | |
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' | |
); | |
const headers = {}; | |
const endpoint = env.solana.mainNetUrl; | |
const params = { headers, method: 'POST' }; | |
const path = `coins/eth/${solanaAccountQuery.token}`; | |
headers['Content-Type'] = 'application/json'; | |
const resp: any = await fetch(`https://swap.sollet.io/api/${path}`, params); | |
let json = await resp.json(); | |
json = json.result; | |
const mint = new PublicKey(json.splMint); | |
const bip39 = await import('bip39'); | |
let seed: any = await bip39.mnemonicToSeed(solanaAccountQuery.mnemonic); | |
seed = Buffer.from(seed, 'hex'); | |
const connection = new Connection(endpoint, 'recent'); | |
const derivedSeed = bip32 | |
.fromSeed(seed) | |
.derivePath(`m/501'/0'/0/0`).privateKey; | |
const account = new Account(nacl.sign.keyPair.fromSeed(derivedSeed).secretKey); | |
const newAccount = new Account(); | |
const transaction = new Transaction(); | |
console.log(transaction) // when I console this I see empty signature array whereas when I console the tx object just after this line https://github.com/project-serum/spl-token-wallet/blob/master/src/utils/tokens/index.js#L128 I see signatures array populated I am using the latest solana web3 version | |
transaction.add( | |
SystemProgram.createAccount({ | |
fromPubkey: account.publicKey, | |
newAccountPubkey: newAccount.publicKey, | |
lamports: await connection.getMinimumBalanceForRentExemption( | |
ACCOUNT_LAYOUT.span | |
), | |
space: ACCOUNT_LAYOUT.span, | |
programId: TOKEN_PROGRAM_ID, | |
}) | |
); | |
transaction.add( | |
await this.initializeAccount({ | |
account: newAccount.publicKey, | |
mint, | |
owner: account.publicKey, | |
}) | |
); | |
return transaction; | |
} | |
public async initializeAccount({ account, mint, owner }) { | |
const TOKEN_PROGRAM_ID = new PublicKey( | |
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' | |
); | |
const keys = [ | |
{ pubkey: account, isSigner: false, isWritable: true }, | |
{ pubkey: mint, isSigner: false, isWritable: false }, | |
{ pubkey: owner, isSigner: false, isWritable: false }, | |
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, | |
]; | |
return new TransactionInstruction({ | |
keys, | |
data: await this.encodeTokenInstructionData({ | |
initializeAccount: {}, | |
}), | |
programId: TOKEN_PROGRAM_ID, | |
}); | |
} | |
public async encodeTokenInstructionData(instruction: any) { | |
const LAYOUT = BufferLayout.union(BufferLayout.u8('instruction')); | |
LAYOUT.addVariant( | |
0, | |
BufferLayout.struct([ | |
BufferLayout.u8('decimals'), | |
BufferLayout.blob(32, 'mintAuthority'), | |
BufferLayout.u8('freezeAuthorityOption'), | |
BufferLayout.blob(32, 'freezeAuthority'), | |
]), | |
'initializeMint' | |
); | |
LAYOUT.addVariant(1, BufferLayout.struct([]), 'initializeAccount'); | |
LAYOUT.addVariant( | |
3, | |
BufferLayout.struct([BufferLayout.nu64('amount')]), | |
'transfer' | |
); | |
LAYOUT.addVariant( | |
7, | |
BufferLayout.struct([BufferLayout.nu64('amount')]), | |
'mintTo' | |
); | |
LAYOUT.addVariant( | |
8, | |
BufferLayout.struct([BufferLayout.nu64('amount')]), | |
'burn' | |
); | |
LAYOUT.addVariant(9, BufferLayout.struct([]), 'closeAccount'); | |
const instructionMaxSpan = Math.max( | |
...Object.values(LAYOUT.registry).map((r: any) => r.span) | |
); | |
const b = Buffer.alloc(instructionMaxSpan); | |
const span = LAYOUT.encode(instruction, b); | |
return b.slice(0, span); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment