1.2.0 • Published 1 year ago
evm-program-serializer v1.2.0
EVM Program Serializer
SDK for interaction with the Velas EVM Native program.
SDK makes Vela's native instructions for executing the EVM program and changing the state of the Velas EVM.
Install
$ npm i evm-program-serializer
Usage
Swap Native VLX to EVM
Swap native VLX token to EVM
Arguments | Type | Required | Description |
---|---|---|---|
lamports | BN | Yes | VLX amount to swap in lamports 10^-9 |
evm_address | H160 | Yes | receiver EVM address |
import { EVMInstruction } from 'evm-program-serializer'
import { H160 } from '@polkadot/types'
import BN from "bn.js"
const nativeToEvm = EVMInstruction.swapNativeToEther(
new BN('1000000000'),
new H160('0x0101010101010101010101010101010101010101')
);
console.log("nativeToEvm: ", nativeToEvm.encode_data().toString('hex'))
// => nativeToEvm: ff0000ca9a3b000000000101010101010101010101010101010101010101
Swap Native VLX to EVM as Instruction
Arguments | Type | Required | Description |
---|---|---|---|
lamports | BN | Yes | VLX amount to swap in lamports 10^-9 |
evm_address | H160 | Yes | receiver EVM address |
nativeFromAddress | PublicKey | Yes | Native From address (signer in native transaction) |
import { swapNativeToEtherInstruction } from 'evm-program-serializer'
import { sendAndConfirmTransaction, Connection, Transaction, Account } from '@velas/web3';
import { H160 } from '@polkadot/types'
import BN from "bn.js"
const connection = new Connection("https://api.testnet.velas.com", 'single');
const fromAccount = new Account();
const nativeToEvmIx = swapNativeToEtherInstruction(
new BN('1000000000'),
new H160('0x0101010101010101010101010101010101010101'),
fromAccount.publicKey,
);
const signature = await sendAndConfirmTransaction(
connection,
new Transaction().add(nativeToEvmIx),
[fromAccount],
{
commitment: 'single',
skipPreflight: true,
},
);
Free ownership
Change owner of the native account from EVM program to system account
import { EVMInstruction } from 'evm-program-serializer'
const freeOwnership = EVMInstruction.freeOwnership();
console.log("freeOwnership: ", freeOwnership.encode_data().toString('hex'))
// => nativeToEvm: ff01
Free ownership as Instruction
import { freeOwnershipInstruction } from 'evm-program-serializer'
import { sendAndConfirmTransaction, Connection, Transaction, Account } from '@velas/web3';
const connection = new Connection("https://api.testnet.velas.com", 'single');
const accountOwnerToChange = new Account();
const freeOwnershipIx = freeOwnershipInstruction(accountOwnerToChange.publicKey);
const signature = await sendAndConfirmTransaction(
connection,
new Transaction().add(freeOwnershipIx),
[accountOwnerToChange],
{
commitment: 'single',
skipPreflight: true,
},
);
Execute EVM transaction (executeTransactionSigned)
Execute an EVM transaction with specific payer.
Arguments | Type | Required | Description |
---|---|---|---|
nonce | U256 | Yes | Ethereum account from nonce |
gasPrice | U256 | Yes | Gas is the pricing value required to conduct a transaction or execute a contract on the EVM |
gasLimit | U256 | Yes | Gas limit refers to the maximum amount of gas you are willing to consume on a transaction |
action | TransactionAction | Yes | Action to be executed (could be TransactionActionCall or TransactionActionCreate) |
value | U256 | Yes | Transaction value |
signature | TransactionSignature | Yes | From address signature |
input | Uint8Array | Yes | Transaction data to be executed |
feeType | FeePayerType | Yes | Fee payer of transaction could be FeePayerTypeEvm or FeePayerTypeNative |
import { EVMInstruction, ExecuteTransaction, FeePayerType, TransactionAction } from 'evm-program-serializer'
import { U256, H160 } from '@polkadot/types'
const executeTx = EVMInstruction.executeTransaction(
ExecuteTransaction.executeTransactionSigned(
new U256(10),
new U256(20),
new U256(30),
TransactionAction.transactionActionCreate(),
new U256(40),
TransactionSignature.from(
new BN(20),
new H256(new BN(6).toBuffer('le')),
new H256(new BN(6).toBuffer('le'))
),
Uint8Array.of(10, 20, 30, 40)
),
FeePayerType.feePayerTypeEvm()
);
console.log("txencoded: ", executeTx.encode_data().toString('hex'))
// => txencoded: ff0300010a0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000012800000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000006040000000a141e2800
Execute EVM transaction (executeTransactionSigned) As instruction
Execute an EVM transaction with specific payer.
Arguments | Type | Required | Description |
---|---|---|---|
nonce | BN or Uint8Array or number or string | Yes | Ethereum account from nonce |
gasPrice | BN or Uint8Array or number or string | Yes | Gas is the pricing value required to conduct a transaction or execute a contract on the EVM |
gasLimit | BN or Uint8Array or number or string | Yes | Gas limit refers to the maximum amount of gas you are willing to consume on a transaction |
action | TransactionAction | Yes | Action to be executed (could be TransactionActionCall or TransactionActionCreate) |
value | BN or Uint8Array or number or string | Yes | Transaction value |
signature | TransactionSignature | Yes | From address signature |
input | Buffer | Yes | Transaction data to be executed |
feeType | FeePayerType | Yes | Fee payer of transaction could be FeePayerTypeEvm or FeePayerTypeNative |
import { freeOwnershipInstruction } from 'evm-program-serializer'
import { sendAndConfirmTransaction, Connection, Transaction, Account } from '@velas/web3';
import { executeEvmTransactionSignedInstruction, FeePayerType, TransactionAction } from 'evm-program-serializer'
const connection = new Connection("https://api.testnet.velas.com", 'single');
const erc20TransferIx = executeEvmTransactionSignedInstruction(
'3',
'5000000000',
'21000',
TransactionAction.transactionActionCall(
'0x7c216e8ad57fE5254123eb4f682BBBbc2F7E4B09'
),
'0',
TransactionSignature.from(
new BN(257),
'0x7da4264ce1fd3f908d90ba24808144ad51e48f7ee8e98ae53b6882b3b1e7998d',
'0x370f0952135d7e52e0cd49fcdb91c0f2d3a2df0866d8f2c282e843c8976d1a02'
),
new Buffer(
'a9059cbb000000000000000000000000c2743fe764f2b638dcf577a24014e086222fa57e0000000000000000000000000000000000000000000000000000000000000001',
'hex'
),
FeePayerType.feePayerTypeNative(
fromAccount.publicKey
)
);
const signature = await sendAndConfirmTransaction(
connection,
new Transaction().add(...erc20TransferIx),
[fromAccount],
{
commitment: 'single',
skipPreflight: true,
},
);
console.log("ixData: ", erc20TransferIx[1].data.toString('hex'))
// => txencoded: ff030001030000000000000000000000000000000000000000000000000000000000000000f2052a010000000000000000000000000000000000000000000000000000000852000000000000000000000000000000000000000000000000000000000000007c216e8ad57fe5254123eb4f682bbbbc2f7e4b09000000000000000000000000000000000000000000000000000000000000000001010000000000007da4264ce1fd3f908d90ba24808144ad51e48f7ee8e98ae53b6882b3b1e7998d370f0952135d7e52e0cd49fcdb91c0f2d3a2df0866d8f2c282e843c8976d1a0244000000a9059cbb000000000000000000000000c2743fe764f2b638dcf577a24014e086222fa57e000000000000000000000000000000000000000000000000000000000000000101
Execute EVM transaction (executeTransactionProgramAuthorized)
Execute an EVM transaction with specific payer without signature (authorized by specific program).
Arguments | Type | Required | Description |
---|---|---|---|
nonce | U256 | Yes | Ethereum account from nonce |
gasPrice | U256 | Yes | Gas is the pricing value required to conduct a transaction or execute a contract on the EVM |
gasLimit | U256 | Yes | Gas limit refers to the maximum amount of gas you are willing to consume on a transaction |
action | TransactionAction | Yes | Action to be executed (could be TransactionActionCall or TransactionActionCreate) |
value | U256 | Yes | Transaction value |
input | Uint8Array | Yes | Transaction data to be executed |
from | H160 | Yes | Address from transaction (should be derrived from native program address ) |
feeType | FeePayerType | Yes | Fee payer of transaction could be FeePayerTypeEvm or FeePayerTypeNative |
import { EVMInstruction, ExecuteTransaction, FeePayerType, TransactionAction } from 'evm-program-serializer'
import { U256, H160 } from '@polkadot/types'
const executeTx = EVMInstruction.executeTransaction(
ExecuteTransaction.executeTransactionProgramAuthorized(
new U256(10),
new U256(20),
new U256(30),
TransactionAction.transactionActionCreate(),
new U256(40),
Uint8Array.of(10, 20, 30, 40),
new H160('0x0101010101010101010101010101010101010101')
),
FeePayerType.feePayerTypeEvm(),
);
console.log("txencoded: ", executeTx.encode_data().toString('hex'))
// => txencoded: ff0301010a0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000012800000000000000000000000000000000000000000000000000000000000000040000000a141e28010101010101010101010101010101010101010100
Execute EVM transaction (executeTransactionProgramAuthorized) as Instruction
Execute an EVM transaction with specific payer without signature (authorized by specific program).
Arguments | Type | Required | Description |
---|---|---|---|
nonce | BN or Uint8Array or number or string | Yes | Ethereum account from nonce |
gasPrice | BN or Uint8Array or number or string | Yes | Gas is the pricing value required to conduct a transaction or execute a contract on the EVM |
gasLimit | BN or Uint8Array or number or string | Yes | Gas limit refers to the maximum amount of gas you are willing to consume on a transaction |
action | TransactionAction | Yes | Action to be executed (could be TransactionActionCall or TransactionActionCreate) |
value | BN or Uint8Array or number or string | Yes | Transaction value |
input | Uint8Array | Yes | Transaction data to be executed |
from | H160 | Yes | Address from transaction (should be derrived from native program address ) |
feeType | FeePayerType | Yes | Fee payer of transaction could be FeePayerTypeEvm or FeePayerTypeNative |