Skip to content

Commit 0d719af

Browse files
committed
EVM example scripts
1 parent ccd8880 commit 0d719af

File tree

6 files changed

+345
-1
lines changed

6 files changed

+345
-1
lines changed

examples/avm/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# AVM RPC
1+
# AVM
22

33
Tests for the Avalanche [AVM RPC](https://docs.avax.network/build/avalanchego-apis/exchange-chain-x-chain-api)
44

examples/evm/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# EVM
2+
3+
Tests for the Avalanche [EVM RPC](https://docs.avax.network/build/avalanchego-apis/contract-chain-c-chain-api)
4+
5+
* [exportTx-ant-xchain.ts](./exportTx-ant-xchain.ts)
6+
* [exportTx-avax-xchain.ts](./exportTx-avax-xchain.ts)
7+
* [importTx-ant-xchain.ts](./importTx-ant-xchain.ts)
8+
* [importTx-avax-xchain.ts](./importTx-avax-xchain.ts)

examples/evm/exportTx-ant-xchain.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src"
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain
10+
} from "../../src/apis/avm"
11+
import {
12+
EVMAPI,
13+
KeyChain,
14+
UnsignedTx,
15+
Tx,
16+
EVMInput,
17+
ExportTx,
18+
SECPTransferOutput,
19+
TransferableOutput
20+
} from "../../src/apis/evm"
21+
import { RequestResponseData } from "../../src/common"
22+
import { Defaults } from "../../src/utils"
23+
24+
const ip: string = "localhost"
25+
const port: number = 9650
26+
const protocol: string = "http"
27+
const networkID: number = 12345
28+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
29+
const xchain: AVMAPI = avalanche.XChain()
30+
const cchain: EVMAPI = avalanche.CChain()
31+
const bintools: BinTools = BinTools.getInstance()
32+
const xKeychain: AVMKeyChain = xchain.keyChain()
33+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
34+
const cKeychain: KeyChain = cchain.keyChain()
35+
xKeychain.importKey(privKey)
36+
cKeychain.importKey(privKey)
37+
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
38+
const cAddresses: Buffer[] = cchain.keyChain().getAddresses()
39+
const xChainBlockchainIdStr: string = Defaults.network['12345'].X.blockchainID
40+
const xChainBlockchainIdBuf: Buffer = bintools.cb58Decode(xChainBlockchainIdStr)
41+
const cChainBlockchainIdStr: string = Defaults.network['12345'].C.blockchainID
42+
const cChainBlockchainIdBuf: Buffer = bintools.cb58Decode(cChainBlockchainIdStr)
43+
const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
44+
const evmInputs: EVMInput[] = []
45+
let exportedOuts: TransferableOutput[] = []
46+
const Web3 = require('web3');
47+
const path: string = '/ext/bc/C/rpc'
48+
const web3 = new Web3(`${protocol}://${ip}:${port}${path}`)
49+
const threshold: number = 1
50+
51+
const main = async (): Promise<any> => {
52+
const avaxAssetIDBuf: Buffer = await xchain.getAVAXAssetID()
53+
const avaxAssetIDStr: string = bintools.cb58Encode(avaxAssetIDBuf)
54+
const antAssetIDStr: string = "verma4Pa9biWKbjDGNsTXU47cYCyDSNGSU1iBkxucfVSFVXdv"
55+
const antAssetIDBuf: Buffer = bintools.cb58Decode(antAssetIDStr)
56+
const antAssetBalanceResponse: RequestResponseData = await cchain.callMethod("eth_getAssetBalance", [
57+
cHexAddress,
58+
"latest",
59+
antAssetIDStr
60+
], "ext/bc/C/rpc")
61+
const antAssetBalance: number = parseInt(antAssetBalanceResponse.data.result, 16)
62+
let avaxBalance: BN = await web3.eth.getBalance(cHexAddress)
63+
avaxBalance = new BN(avaxBalance.toString().substring(0, 15))
64+
const fee: BN = cchain.getDefaultTxFee()
65+
const txcount = await web3.eth.getTransactionCount(cHexAddress)
66+
const nonce: number = txcount;
67+
const locktime: BN = new BN(0)
68+
69+
let evmInput: EVMInput = new EVMInput(cHexAddress, avaxBalance, avaxAssetIDStr, nonce)
70+
evmInput.addSignatureIdx(0, cAddresses[0])
71+
evmInputs.push(evmInput)
72+
73+
evmInput = new EVMInput(cHexAddress, antAssetBalance, antAssetIDStr, nonce)
74+
evmInput.addSignatureIdx(0, cAddresses[0])
75+
evmInputs.push(evmInput)
76+
77+
let secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(avaxBalance.sub(fee), xAddresses, locktime, threshold)
78+
let transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetIDBuf, secpTransferOutput)
79+
exportedOuts.push(transferableOutput)
80+
81+
secpTransferOutput = new SECPTransferOutput(new BN(antAssetBalance), xAddresses, locktime, threshold)
82+
transferableOutput = new TransferableOutput(antAssetIDBuf, secpTransferOutput)
83+
exportedOuts.push(transferableOutput)
84+
exportedOuts = exportedOuts.sort(TransferableOutput.comparator());
85+
86+
const exportTx: ExportTx = new ExportTx(
87+
networkID,
88+
cChainBlockchainIdBuf,
89+
xChainBlockchainIdBuf,
90+
evmInputs,
91+
exportedOuts
92+
)
93+
94+
const unsignedTx: UnsignedTx = new UnsignedTx(exportTx)
95+
const tx: Tx = unsignedTx.sign(cKeychain)
96+
const id: string = await cchain.issueTx(tx)
97+
console.log(id)
98+
}
99+
100+
main()

examples/evm/exportTx-avax-xchain.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src"
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain
10+
} from "../../src/apis/avm"
11+
import {
12+
EVMAPI,
13+
KeyChain,
14+
UnsignedTx,
15+
Tx,
16+
EVMInput,
17+
ExportTx,
18+
SECPTransferOutput,
19+
TransferableOutput
20+
} from "../../src/apis/evm"
21+
import { Defaults } from "../../src/utils"
22+
23+
const ip: string = "localhost"
24+
const port: number = 9650
25+
const protocol: string = "http"
26+
const networkID: number = 12345
27+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
28+
const xchain: AVMAPI = avalanche.XChain()
29+
const cchain: EVMAPI = avalanche.CChain()
30+
const bintools: BinTools = BinTools.getInstance()
31+
const xKeychain: AVMKeyChain = xchain.keyChain()
32+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN-"
33+
const cKeychain: KeyChain = cchain.keyChain()
34+
xKeychain.importKey(privKey)
35+
cKeychain.importKey(privKey)
36+
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
37+
const cAddresses: Buffer[] = cchain.keyChain().getAddresses()
38+
const xChainBlockchainIdStr: string = Defaults.network['12345'].X.blockchainID
39+
const xChainBlockchainIdBuf: Buffer = bintools.cb58Decode(xChainBlockchainIdStr)
40+
const cChainBlockchainIdStr: string = Defaults.network['12345'].C.blockchainID
41+
const cChainBlockchainIdBuf: Buffer = bintools.cb58Decode(cChainBlockchainIdStr)
42+
const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
43+
const evmInputs: EVMInput[] = []
44+
const exportedOuts: TransferableOutput[] = []
45+
const Web3 = require('web3');
46+
const path: string = '/ext/bc/C/rpc'
47+
const web3 = new Web3(`${protocol}://${ip}:${port}${path}`)
48+
const threshold: number = 1
49+
50+
const main = async (): Promise<any> => {
51+
const avaxAssetIDBuf: Buffer = await xchain.getAVAXAssetID()
52+
const avaxAssetIDStr: string = bintools.cb58Encode(avaxAssetIDBuf)
53+
let balance: BN = await web3.eth.getBalance(cHexAddress)
54+
balance = new BN(balance.toString().substring(0, 15))
55+
const fee: BN = cchain.getDefaultTxFee()
56+
const txcount = await web3.eth.getTransactionCount(cHexAddress)
57+
const nonce: number = txcount;
58+
const locktime: BN = new BN(0)
59+
60+
const evmInput: EVMInput = new EVMInput(cHexAddress, balance, avaxAssetIDStr, nonce)
61+
evmInput.addSignatureIdx(0, cAddresses[0])
62+
evmInputs.push(evmInput)
63+
64+
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(balance.sub(fee), xAddresses, locktime, threshold)
65+
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetIDBuf, secpTransferOutput)
66+
exportedOuts.push(transferableOutput)
67+
68+
const exportTx: ExportTx = new ExportTx(
69+
networkID,
70+
cChainBlockchainIdBuf,
71+
xChainBlockchainIdBuf,
72+
evmInputs,
73+
exportedOuts
74+
)
75+
76+
const unsignedTx: UnsignedTx = new UnsignedTx(exportTx)
77+
const tx: Tx = unsignedTx.sign(cKeychain)
78+
const id: string = await cchain.issueTx(tx)
79+
console.log(id)
80+
}
81+
82+
main()

examples/evm/importTx-ant-xchain.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src"
7+
import {
8+
EVMAPI,
9+
EVMOutput,
10+
ImportTx,
11+
TransferableInput,
12+
KeyChain,
13+
UTXO,
14+
UTXOSet,
15+
SECPTransferInput,
16+
AmountOutput,
17+
UnsignedTx,
18+
Tx
19+
} from "../../src/apis/evm"
20+
import { Defaults } from "../../src/utils"
21+
22+
const ip: string = "localhost"
23+
const port: number = 9650
24+
const protocol: string = "http"
25+
const networkID: number = 12345
26+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
27+
const cchain: EVMAPI = avalanche.CChain()
28+
const bintools: BinTools = BinTools.getInstance()
29+
const cKeychain: KeyChain = cchain.keyChain()
30+
const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
31+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
32+
cKeychain.importKey(privKey)
33+
const cAddresses: Buffer[] = cchain.keyChain().getAddresses()
34+
const cAddressStrings: string[] = cchain.keyChain().getAddressStrings()
35+
const cChainBlockchainIdStr: string = Defaults.network['12345'].C.blockchainID
36+
const cChainBlockchainIdBuf: Buffer = bintools.cb58Decode(cChainBlockchainIdStr)
37+
const xChainBlockchainIdStr: string = Defaults.network['12345'].X.blockchainID
38+
const xChainBlockchainIdBuf: Buffer = bintools.cb58Decode(xChainBlockchainIdStr)
39+
const importedIns: TransferableInput[] = []
40+
const evmOutputs: EVMOutput[] = []
41+
42+
const main = async (): Promise<any> => {
43+
const avaxAsetID: Buffer = await cchain.getAVAXAssetID()
44+
const u: any = await cchain.getUTXOs(cAddressStrings[0], "X")
45+
const utxoSet: UTXOSet = u.utxos
46+
const utxos: UTXO[] = utxoSet.getAllUTXOs()
47+
utxos.forEach((utxo: UTXO) => {
48+
const assetID: Buffer = utxo.getAssetID()
49+
const txid: Buffer = utxo.getTxID()
50+
const outputidx: Buffer = utxo.getOutputIdx()
51+
const output: AmountOutput = utxo.getOutput() as AmountOutput
52+
const amt: BN = output.getAmount().clone()
53+
const input: SECPTransferInput = new SECPTransferInput(amt)
54+
input.addSignatureIdx(0, cAddresses[0])
55+
const xferin: TransferableInput = new TransferableInput(txid, outputidx, assetID, input)
56+
importedIns.push(xferin)
57+
58+
const evmOutput: EVMOutput = new EVMOutput(cHexAddress, amt, assetID)
59+
evmOutputs.push(evmOutput)
60+
})
61+
62+
const importTx: ImportTx = new ImportTx(
63+
networkID,
64+
cChainBlockchainIdBuf,
65+
xChainBlockchainIdBuf,
66+
importedIns,
67+
evmOutputs
68+
)
69+
70+
const unsignedTx: UnsignedTx = new UnsignedTx(importTx)
71+
const tx: Tx = unsignedTx.sign(cKeychain)
72+
const id: string = await cchain.issueTx(tx)
73+
console.log(id)
74+
}
75+
76+
main()

examples/evm/importTx-avax-xchain.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src"
7+
import {
8+
EVMAPI,
9+
EVMOutput,
10+
ImportTx,
11+
TransferableInput,
12+
KeyChain,
13+
UTXO,
14+
UTXOSet,
15+
SECPTransferInput,
16+
AmountOutput,
17+
UnsignedTx,
18+
Tx
19+
} from "../../src/apis/evm"
20+
import { Defaults } from "../../src/utils"
21+
22+
const ip: string = "localhost"
23+
const port: number = 9650
24+
const protocol: string = "http"
25+
const networkID: number = 12345
26+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
27+
const cchain: EVMAPI = avalanche.CChain()
28+
const bintools: BinTools = BinTools.getInstance()
29+
const cKeychain: KeyChain = cchain.keyChain()
30+
const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
31+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
32+
cKeychain.importKey(privKey)
33+
const cAddresses: Buffer[] = cchain.keyChain().getAddresses()
34+
const cAddressStrings: string[] = cchain.keyChain().getAddressStrings()
35+
const cChainBlockchainIdStr: string = Defaults.network['12345'].C.blockchainID
36+
const cChainBlockchainIdBuf: Buffer = bintools.cb58Decode(cChainBlockchainIdStr)
37+
const xChainBlockchainIdStr: string = Defaults.network['12345'].X.blockchainID
38+
const xChainBlockchainIdBuf: Buffer = bintools.cb58Decode(xChainBlockchainIdStr)
39+
const importedIns: TransferableInput[] = []
40+
const evmOutputs: EVMOutput[] = []
41+
42+
const main = async (): Promise<any> => {
43+
const avaxAsetID: Buffer = await cchain.getAVAXAssetID()
44+
const u: any = await cchain.getUTXOs(cAddressStrings[0], "X")
45+
const utxoSet: UTXOSet = u.utxos
46+
const utxos: UTXO[] = utxoSet.getAllUTXOs()
47+
utxos.forEach((utxo: UTXO) => {
48+
const assetID: Buffer = utxo.getAssetID()
49+
const txid: Buffer = utxo.getTxID()
50+
const outputidx: Buffer = utxo.getOutputIdx()
51+
const output: AmountOutput = utxo.getOutput() as AmountOutput
52+
const amt: BN = output.getAmount().clone()
53+
if(avaxAsetID.toString("hex") === assetID.toString("hex")) {
54+
const input: SECPTransferInput = new SECPTransferInput(amt)
55+
input.addSignatureIdx(0, cAddresses[0])
56+
const xferin: TransferableInput = new TransferableInput(txid, outputidx, assetID, input)
57+
importedIns.push(xferin)
58+
59+
const evmOutput: EVMOutput = new EVMOutput(cHexAddress, amt, assetID)
60+
evmOutputs.push(evmOutput)
61+
}
62+
})
63+
64+
const importTx: ImportTx = new ImportTx(
65+
networkID,
66+
cChainBlockchainIdBuf,
67+
xChainBlockchainIdBuf,
68+
importedIns,
69+
evmOutputs
70+
)
71+
72+
const unsignedTx: UnsignedTx = new UnsignedTx(importTx)
73+
const tx: Tx = unsignedTx.sign(cKeychain)
74+
const id: string = await cchain.issueTx(tx)
75+
console.log(id)
76+
}
77+
78+
main()

0 commit comments

Comments
 (0)