Skip to content

Commit

Permalink
Returned the transaction for mainnet.
Browse files Browse the repository at this point in the history
  • Loading branch information
oable committed Sep 24, 2024
1 parent ad24248 commit 4aba846
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neonevm/token-transfer-core",
"version": "4.3.1-devnet",
"version": "4.3.1",
"description": "Core API for the @neonevm/token-transfer project",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ export const INCINERATOR_PUBKEY = '1nc1nerator11111111111111111111111111111111';
export const SYSVAR_INSTRUCTION_PUBKEY = 'Sysvar1nstructions1111111111111111111111111';
export const COMPUTE_BUDGET_ID = 'ComputeBudget111111111111111111111111111111';

export const NEON_TREASURY_POOL_COUNT = '128';
export const NEON_HEAP_FRAME = '262144';
export const NEON_COMPUTE_UNITS = '500000';
export const NEON_TOKEN_MINT_DEVNET = '89dre8rZjLNft7HoupGiyxu3MNftR577ZYu8bHe2kK7g';
export const SOL_TOKEN_MINT_DEVNET = '89dre8rZjLNft7HoupGiyxu3MNftR577ZYu8bHe2kK7g';
export const NEON_TOKEN_MINT_MAINNET = 'NeonTjSjsuo3rexg9o6vHuMXw62f9V7zvmu8M8Zut44';
export const SOL_TOKEN_MINT_MAINNET = 'NeonTjSjsuo3rexg9o6vHuMXw62f9V7zvmu8M8Zut44';

export const NEON_TREASURY_POOL_COUNT = 128;
export const NEON_HEAP_FRAME = 262144;
export const NEON_COMPUTE_UNITS = 500000;
export const RENT_EPOCH_ZERO = 0;
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './data';
export * from './models';
export * from './utils';
export * from './mint-transfer';
export * from './mint-transfer.mainnet';
export * from './neon-transfer';
export * from './multy-transfer';
82 changes: 82 additions & 0 deletions packages/core/src/mint-transfer.mainnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { getAssociatedTokenAddressSync } from '@solana/spl-token';
import {
AccountMeta,
Connection,
PublicKey,
SystemProgram,
Transaction,
TransactionInstruction
} from '@solana/web3.js';
import { Wallet } from 'ethers';
import {
authAccountAddress,
collateralPoolAddress,
neonBalanceProgramAddress,
neonBalanceProgramAddressV2,
toBytesInt32,
TransactionResult
} from './utils';
import { EvmInstruction, NeonHeapFrame, SolanaAccount, SPLToken } from './models';
import { COMPUTE_BUDGET_ID, NEON_HEAP_FRAME, NEON_TREASURY_POOL_COUNT } from './data';
import {
createAccountBalanceForLegacyAccountInstruction,
createAccountBalanceInstruction,
createApproveDepositInstruction,
createComputeBudgetHeapFrameInstruction
} from './mint-transfer';

export async function neonTransferMintTransactionMainnet<TxResult extends TransactionResult>(connection: Connection, neonEvmProgram: PublicKey, solanaWallet: PublicKey, neonWallet: string, emulateSigner: Wallet, neonKeys: AccountMeta[], legacyAccounts: SolanaAccount[], neonTransaction: TxResult, splToken: SPLToken, amount: bigint, chainId: number, neonHeapFrame: NeonHeapFrame = NEON_HEAP_FRAME, neonPoolCount = NEON_TREASURY_POOL_COUNT): Promise<Transaction> {
const computedBudgetProgram = new PublicKey(COMPUTE_BUDGET_ID);
const [delegatePDA] = authAccountAddress(emulateSigner.address, neonEvmProgram, splToken);
const [neonWalletBalanceAddress] = neonBalanceProgramAddress(neonWallet, neonEvmProgram, chainId);
const [emulateSignerBalanceAddress] = neonBalanceProgramAddress(emulateSigner.address, neonEvmProgram, chainId);
const neonWalletBalanceAccount = await connection.getAccountInfo(neonWalletBalanceAddress);
const emulateSignerBalanceAccount = await connection.getAccountInfo(emulateSignerBalanceAddress);
const associatedTokenAddress = getAssociatedTokenAddressSync(new PublicKey(splToken.address_spl), solanaWallet);
const transaction = new Transaction({ feePayer: solanaWallet });

transaction.add(createComputeBudgetHeapFrameInstruction(computedBudgetProgram, neonHeapFrame));
transaction.add(createApproveDepositInstruction(solanaWallet, delegatePDA, associatedTokenAddress, amount));

if (!neonWalletBalanceAccount) {
transaction.add(createAccountBalanceInstruction(solanaWallet, neonEvmProgram, neonWallet, chainId));
}

if (!emulateSignerBalanceAccount) {
transaction.add(createAccountBalanceInstruction(solanaWallet, neonEvmProgram, emulateSigner.address, chainId));
}

for (const account of legacyAccounts) {
const instruction = await createAccountBalanceForLegacyAccountInstruction(connection, account, solanaWallet, neonEvmProgram, chainId);
if (instruction) {
transaction.add(instruction);
}
}

if (neonTransaction?.rawTransaction) {
transaction.add(createExecFromDataInstructionV2Mainnet(solanaWallet, neonWallet, neonEvmProgram, neonTransaction.rawTransaction, neonKeys, chainId, neonPoolCount));
}

return transaction;
}


export function createExecFromDataInstructionV2Mainnet(solanaWallet: PublicKey, neonWallet: string, neonEvmProgram: PublicKey, neonRawTransaction: string, neonKeys: AccountMeta[], chainId: number, neonPoolCount = NEON_TREASURY_POOL_COUNT): TransactionInstruction {
const count = neonPoolCount ?? NEON_TREASURY_POOL_COUNT;
const treasuryPoolIndex = Math.floor(Math.random() * count) % count;
const [balanceAccount] = neonBalanceProgramAddressV2(neonWallet, solanaWallet, neonEvmProgram, chainId);
const [treasuryPoolAddress] = collateralPoolAddress(neonEvmProgram, treasuryPoolIndex);
const a = Buffer.from([EvmInstruction.TransactionExecuteFromInstructionMainnet]);
const b = Buffer.from(toBytesInt32(treasuryPoolIndex));
const c = Buffer.from(neonRawTransaction.slice(2), 'hex');
const data = Buffer.concat([a, b, c]);
const keys: AccountMeta[] = [
{ pubkey: solanaWallet, isSigner: true, isWritable: true },
{ pubkey: treasuryPoolAddress, isSigner: false, isWritable: true },
{ pubkey: balanceAccount, isSigner: false, isWritable: true },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: true },
...neonKeys
];

return new TransactionInstruction({ programId: neonEvmProgram, keys, data });
}
4 changes: 2 additions & 2 deletions packages/core/src/mint-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ export async function neonTransferMintTransaction<W extends Provider, TxResult e

export function createComputeBudgetUtilsInstruction(programId: PublicKey, computeUnits: NeonComputeUnits = NEON_COMPUTE_UNITS): TransactionInstruction {
const a = Buffer.from([0x00]);
const b = Buffer.from(toBytesInt32(parseInt(computeUnits ?? NEON_COMPUTE_UNITS)));
const b = Buffer.from(toBytesInt32(computeUnits));
const c = Buffer.from(toBytesInt32(0));
const data = Buffer.concat([a, b, c]);
return new TransactionInstruction({ programId, data, keys: [] });
}

export function createComputeBudgetHeapFrameInstruction(programId: PublicKey, neonHeapFrame: NeonHeapFrame = NEON_HEAP_FRAME): TransactionInstruction {
const a = Buffer.from([0x01]);
const b = Buffer.from(toBytesInt32(parseInt(neonHeapFrame ?? NEON_HEAP_FRAME)));
const b = Buffer.from(toBytesInt32(neonHeapFrame));
const data = Buffer.concat([a, b]);
return new TransactionInstruction({ programId, data, keys: [] });
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/models/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export interface SettingsFormState {
neonProxyRpcApi: string;
}

export type NeonHeapFrame = string;
export type NeonComputeUnits = string;
export type NeonHeapFrame = number;
export type NeonComputeUnits = number;

export interface NeonProgramStatus {
neonAccountSeedVersion: number;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/models/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const enum EvmInstruction {
AccountCreateBalance = 0x30, // 48
DepositToBalance = 0x31, // 49
TransactionExecuteFromInstruction = 0x3D, // 61
TransactionExecuteFromInstructionMainnet = 0x32, // 50
}

export const enum AccountHex {
Expand All @@ -36,5 +37,5 @@ export interface CreateExecFromDataInstructionParams {
neonRawTransaction: string;
neonKeys: AccountMeta[];
chainId: number;
neonPoolCount: string;
neonPoolCount: number;
}
2 changes: 1 addition & 1 deletion packages/ethers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neonevm/token-transfer-ethers",
"version": "4.3.1-devnet",
"version": "4.3.1",
"description": "Neon EVM token transfer methods leveraging the ethers.js library",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/web3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[![workflows](https://github.com/neonlabsorg/neon-client-transfer/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/neonlabsorg/neon-client-transfer/actions)
[![npm](https://img.shields.io/npm/v/@neonevm/token-transfer-web3.svg)](https://www.npmjs.com/package/@neonevm/token-transfer-web3)

> **Note**: This package deprecated. Please use the `@neonevm/token-transfer-ethers` package.
The `@neonevm/token-transfer-web3` library is an extension to `@neonevm/token-transfer-core` using addition methods `web3.js`.
This is where the provider, read contract methods, create, sign and send transactions are used from Ethers.js.

Expand Down
2 changes: 1 addition & 1 deletion packages/web3/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neonevm/token-transfer-web3",
"version": "4.3.1-devnet",
"version": "4.3.1",
"description": "Neon EVM token transfer methods leveraging the web3.js library",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down

0 comments on commit 4aba846

Please sign in to comment.