1.2.0 • Published 1 year ago

evm-program-serializer v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

ArgumentsTypeRequiredDescription
lamportsBNYesVLX amount to swap in lamports 10^-9
evm_addressH160Yesreceiver 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

ArgumentsTypeRequiredDescription
lamportsBNYesVLX amount to swap in lamports 10^-9
evm_addressH160Yesreceiver EVM address
nativeFromAddressPublicKeyYesNative 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.

ArgumentsTypeRequiredDescription
nonceU256YesEthereum account from nonce
gasPriceU256YesGas is the pricing value required to conduct a transaction or execute a contract on the EVM
gasLimitU256YesGas limit refers to the maximum amount of gas you are willing to consume on a transaction
actionTransactionActionYesAction to be executed (could be TransactionActionCall or TransactionActionCreate)
valueU256YesTransaction value
signatureTransactionSignatureYesFrom address signature
inputUint8ArrayYesTransaction data to be executed
feeTypeFeePayerTypeYesFee 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.

ArgumentsTypeRequiredDescription
nonceBN or Uint8Array or number or stringYesEthereum account from nonce
gasPriceBN or Uint8Array or number or stringYesGas is the pricing value required to conduct a transaction or execute a contract on the EVM
gasLimitBN or Uint8Array or number or stringYesGas limit refers to the maximum amount of gas you are willing to consume on a transaction
actionTransactionActionYesAction to be executed (could be TransactionActionCall or TransactionActionCreate)
valueBN or Uint8Array or number or stringYesTransaction value
signatureTransactionSignatureYesFrom address signature
inputBufferYesTransaction data to be executed
feeTypeFeePayerTypeYesFee 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).

ArgumentsTypeRequiredDescription
nonceU256YesEthereum account from nonce
gasPriceU256YesGas is the pricing value required to conduct a transaction or execute a contract on the EVM
gasLimitU256YesGas limit refers to the maximum amount of gas you are willing to consume on a transaction
actionTransactionActionYesAction to be executed (could be TransactionActionCall or TransactionActionCreate)
valueU256YesTransaction value
inputUint8ArrayYesTransaction data to be executed
fromH160YesAddress from transaction (should be derrived from native program address )
feeTypeFeePayerTypeYesFee 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).

ArgumentsTypeRequiredDescription
nonceBN or Uint8Array or number or stringYesEthereum account from nonce
gasPriceBN or Uint8Array or number or stringYesGas is the pricing value required to conduct a transaction or execute a contract on the EVM
gasLimitBN or Uint8Array or number or stringYesGas limit refers to the maximum amount of gas you are willing to consume on a transaction
actionTransactionActionYesAction to be executed (could be TransactionActionCall or TransactionActionCreate)
valueBN or Uint8Array or number or stringYesTransaction value
inputUint8ArrayYesTransaction data to be executed
fromH160YesAddress from transaction (should be derrived from native program address )
feeTypeFeePayerTypeYesFee payer of transaction could be FeePayerTypeEvm or FeePayerTypeNative
1.2.0

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago