Skip to content

Commit 12c2ca9

Browse files
committed
Rename example scripts. Add examples scripts for cross chain swapping AVAX and ANTs between the X-Chain and the C-Chain.
1 parent 3af500e commit 12c2ca9

10 files changed

+376
-5
lines changed

examples/avm/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ Tests for the Avalanche [AVM RPC](https://docs.avax.network/build/avalanchego-ap
66

77
* [buildBaseTx-ant.ts](./buildBaseTx-ant.ts)
88
* [buildBaseTx-avax.ts](./buildBaseTx-avax.ts)
9-
* [buildCreateAssetTx-ant.ts](./buildCreateAssetTx-ant.ts)
9+
* [buildCreateAssetTx.ts](./buildCreateAssetTx.ts)
1010
* [buildCreateNFTAssetTx.ts](./buildCreateNFTAssetTx.ts)
1111
* [buildCreateNFTMintTx.ts](./buildCreateNFTMintTx.ts)
12-
* [buildExportTx-PChain.ts](./buildExportTx-PChain.ts)
13-
* [buildImportTx-PChain.ts](./buildImportTx-PChain.ts)
12+
* [buildExportTx-cchain-ant.ts](./buildExportTx-cchain-ant.ts)
13+
* [buildExportTx-cchain-avax.ts](./buildExportTx-cchain-avax.ts)
14+
* [buildExportTx-pchain.ts](./buildExportTx-pchain.ts)
15+
* [buildImportTx-cchain.ts](./buildImportTx-cchain.ts)
16+
* [buildImportTx-pchain.ts](./buildImportTx-pchain.ts)
1417
* [buildNFTTransferTx.ts](./buildNFTTransferTx.ts)
15-
* [buildSECPMintTx-ant.ts](./buildSECPMintTx-ant.ts)
18+
* [buildSECPMintTx.ts](./buildSECPMintTx.ts)
1619

1720
## Manual
1821

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain,
10+
UTXOSet,
11+
UnsignedTx,
12+
Tx
13+
} from "../../src/apis/avm"
14+
import { KeyChain, EVMAPI } from "../../src/apis/evm";
15+
import { Defaults, UnixNow } from "../../src/utils"
16+
17+
const ip: string = "localhost"
18+
const port: number = 9650
19+
const protocol: string = "http"
20+
const networkID: number = 12345
21+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
22+
const xchain: AVMAPI = avalanche.XChain()
23+
const cchain: EVMAPI = avalanche.CChain()
24+
const bintools: BinTools = BinTools.getInstance()
25+
const xKeychain: AVMKeyChain = xchain.keyChain()
26+
const pKeychain: KeyChain = cchain.keyChain()
27+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
28+
xKeychain.importKey(privKey)
29+
pKeychain.importKey(privKey)
30+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
31+
const cAddressStrings: string[] = cchain.keyChain().getAddressStrings()
32+
const cChainBlockchainID: string = Defaults.network['12345'].C.blockchainID
33+
const locktime: BN = new BN(0)
34+
const asOf: BN = UnixNow()
35+
const memo: Buffer = bintools.stringToBuffer("AVM utility method buildExportTx to export ANT to the C-Chain from the X-Chain")
36+
37+
const main = async (): Promise<any> => {
38+
const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings)
39+
const utxoSet: UTXOSet = avmUTXOResponse.utxos
40+
const amount: BN = new BN(500)
41+
const threshold: number = 1
42+
const assetID: string = "2hrWPkPoNJRgtx24jimCfgiyzAf5DpG2hEBUpVfW8tT76RsRHh"
43+
44+
const unsignedTx: UnsignedTx = await xchain.buildExportTx(
45+
utxoSet,
46+
amount,
47+
cChainBlockchainID,
48+
cAddressStrings,
49+
xAddressStrings,
50+
xAddressStrings,
51+
memo,
52+
asOf,
53+
locktime,
54+
threshold,
55+
assetID
56+
)
57+
58+
const tx: Tx = unsignedTx.sign(xKeychain)
59+
const id: string = await xchain.issueTx(tx)
60+
console.log(id)
61+
}
62+
63+
main()
64+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain,
10+
UTXOSet,
11+
UnsignedTx,
12+
Tx
13+
} from "../../src/apis/avm"
14+
import { KeyChain, EVMAPI } from "../../src/apis/evm";
15+
import { Defaults, UnixNow } from "../../src/utils"
16+
17+
const ip: string = "localhost"
18+
const port: number = 9650
19+
const protocol: string = "http"
20+
const networkID: number = 12345
21+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
22+
const xchain: AVMAPI = avalanche.XChain()
23+
const cchain: EVMAPI = avalanche.CChain()
24+
const bintools: BinTools = BinTools.getInstance()
25+
const xKeychain: AVMKeyChain = xchain.keyChain()
26+
const pKeychain: KeyChain = cchain.keyChain()
27+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
28+
xKeychain.importKey(privKey)
29+
pKeychain.importKey(privKey)
30+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
31+
const cAddressStrings: string[] = cchain.keyChain().getAddressStrings()
32+
const cChainBlockchainID: string = Defaults.network['12345'].C.blockchainID
33+
const locktime: BN = new BN(0)
34+
const asOf: BN = UnixNow()
35+
const memo: Buffer = bintools.stringToBuffer("AVM utility method buildExportTx to export AVAX to the C-Chain from the X-Chain")
36+
const fee: BN = xchain.getDefaultTxFee()
37+
38+
const main = async (): Promise<any> => {
39+
const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings)
40+
const utxoSet: UTXOSet = avmUTXOResponse.utxos
41+
const avaxAssetID: Buffer = await xchain.getAVAXAssetID()
42+
const getBalanceResponse: any = await xchain.getBalance(xAddressStrings[0], bintools.cb58Encode(avaxAssetID))
43+
const balance: BN = new BN(getBalanceResponse.balance)
44+
const amount: BN = balance.sub(fee)
45+
const assetID: string = "2hrWPkPoNJRgtx24jimCfgiyzAf5DpG2hEBUpVfW8tT76RsRHh"
46+
47+
const unsignedTx: UnsignedTx = await xchain.buildExportTx(
48+
utxoSet,
49+
amount,
50+
cChainBlockchainID,
51+
cAddressStrings,
52+
xAddressStrings,
53+
xAddressStrings,
54+
memo,
55+
asOf,
56+
locktime
57+
)
58+
59+
const tx: Tx = unsignedTx.sign(xKeychain)
60+
const id: string = await xchain.issueTx(tx)
61+
console.log(id)
62+
}
63+
64+
main()
65+

examples/avm/buildImportTx-cchain.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
AVMAPI,
9+
KeyChain,
10+
UTXOSet,
11+
UnsignedTx,
12+
Tx
13+
} from "../../src/apis/avm"
14+
import { Defaults, UnixNow } from "../../src/utils"
15+
16+
const ip: string = "localhost"
17+
const port: number = 9650
18+
const protocol: string = "http"
19+
const networkID: number = 12345
20+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
21+
const xchain: AVMAPI = avalanche.XChain()
22+
const bintools: BinTools = BinTools.getInstance()
23+
const xKeychain: KeyChain = xchain.keyChain()
24+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
25+
xKeychain.importKey(privKey)
26+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
27+
const cChainBlockchainID: string = Defaults.network['12345'].C.blockchainID
28+
const threshold: number = 1
29+
const locktime: BN = new BN(0)
30+
const asOf: BN = UnixNow()
31+
const memo: Buffer = bintools.stringToBuffer("AVM utility method buildImportTx to import AVAX to the X-Chain from the C-Chain")
32+
33+
const main = async (): Promise<any> => {
34+
const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings, cChainBlockchainID)
35+
const utxoSet: UTXOSet = avmUTXOResponse.utxos
36+
37+
const unsignedTx: UnsignedTx = await xchain.buildImportTx(
38+
utxoSet,
39+
xAddressStrings,
40+
cChainBlockchainID,
41+
xAddressStrings,
42+
xAddressStrings,
43+
xAddressStrings,
44+
memo,
45+
asOf,
46+
locktime,
47+
threshold
48+
)
49+
const tx: Tx = unsignedTx.sign(xKeychain)
50+
const id: string = await xchain.issueTx(tx)
51+
console.log(id)
52+
}
53+
54+
main()
55+
File renamed without changes.

examples/evm/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# EVM
22

3-
Tests for the Avalanche [EVM RPC](https://docs.avax.network/build/avalanchego-apis/contract-chain-c-chain-api)
3+
Tests for the Avalanche [EVM](https://docs.avax.network/build/avalanchego-apis/contract-chain-c-chain-api)
44

5+
* [buildExportTx-xchain-ant.ts](./buildExportTx-xchain-ant.ts)
6+
* [buildExportTx-xchain-avax.ts](./buildExportTx-xchain-avax.ts)
7+
* [buildImportTx-xchain.ts](./buildImportTx-xchain.ts)
58
* [exportTx-ant-xchain.ts](./exportTx-ant-xchain.ts)
69
* [exportTx-avax-xchain.ts](./exportTx-avax-xchain.ts)
710
* [importTx-ant-xchain.ts](./importTx-ant-xchain.ts)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {
2+
Avalanche, BinTools, BN, Buffer
3+
} from "../../src"
4+
import {
5+
AVMAPI,
6+
KeyChain as AVMKeyChain
7+
} from "../../src/apis/avm"
8+
import {
9+
EVMAPI,
10+
KeyChain,
11+
UnsignedTx,
12+
Tx
13+
} from "../../src/apis/evm"
14+
import { Defaults } from "../../src/utils"
15+
16+
const ip: string = "localhost"
17+
const port: number = 9650
18+
const protocol: string = "http"
19+
const networkID: number = 12345
20+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
21+
const xchain: AVMAPI = avalanche.XChain()
22+
const cchain: EVMAPI = avalanche.CChain()
23+
const bintools: BinTools = BinTools.getInstance()
24+
const xKeychain: AVMKeyChain = xchain.keyChain()
25+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
26+
const cKeychain: KeyChain = cchain.keyChain()
27+
xKeychain.importKey(privKey)
28+
cKeychain.importKey(privKey)
29+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
30+
const cAddressStrings: string[] = cchain.keyChain().getAddressStrings()
31+
const xChainBlockchainIdStr: string = Defaults.network['12345'].X.blockchainID
32+
const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
33+
const Web3 = require('web3');
34+
const path: string = '/ext/bc/C/rpc'
35+
const web3 = new Web3(`${protocol}://${ip}:${port}${path}`)
36+
const threshold: number = 1
37+
38+
const main = async (): Promise<any> => {
39+
let balance: BN = await web3.eth.getBalance(cHexAddress)
40+
const fee: BN = cchain.getDefaultTxFee()
41+
balance = new BN(balance.toString().substring(0, 17))
42+
const txcount = await web3.eth.getTransactionCount(cHexAddress)
43+
const nonce: number = txcount;
44+
const locktime: BN = new BN(0)
45+
const amount: BN = new BN(500)
46+
const assetID: string = "2hrWPkPoNJRgtx24jimCfgiyzAf5DpG2hEBUpVfW8tT76RsRHh"
47+
48+
const unsignedTx: UnsignedTx = await cchain.buildExportTx(
49+
amount,
50+
assetID,
51+
xChainBlockchainIdStr,
52+
cHexAddress,
53+
cAddressStrings[0],
54+
xAddressStrings,
55+
nonce,
56+
locktime,
57+
threshold,
58+
)
59+
60+
const tx: Tx = unsignedTx.sign(cKeychain)
61+
const id: string = await cchain.issueTx(tx)
62+
console.log(id)
63+
}
64+
65+
main()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
Avalanche, BinTools, BN, Buffer
3+
} from "../../src"
4+
import {
5+
AVMAPI,
6+
KeyChain as AVMKeyChain
7+
} from "../../src/apis/avm"
8+
import {
9+
EVMAPI,
10+
KeyChain,
11+
UnsignedTx,
12+
Tx
13+
} from "../../src/apis/evm"
14+
import { Defaults } from "../../src/utils"
15+
16+
const ip: string = "localhost"
17+
const port: number = 9650
18+
const protocol: string = "http"
19+
const networkID: number = 12345
20+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
21+
const xchain: AVMAPI = avalanche.XChain()
22+
const cchain: EVMAPI = avalanche.CChain()
23+
const bintools: BinTools = BinTools.getInstance()
24+
const xKeychain: AVMKeyChain = xchain.keyChain()
25+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
26+
const cKeychain: KeyChain = cchain.keyChain()
27+
xKeychain.importKey(privKey)
28+
cKeychain.importKey(privKey)
29+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
30+
const cAddressStrings: string[] = cchain.keyChain().getAddressStrings()
31+
const xChainBlockchainIdStr: string = Defaults.network['12345'].X.blockchainID
32+
const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
33+
const Web3 = require('web3');
34+
const path: string = '/ext/bc/C/rpc'
35+
const web3 = new Web3(`${protocol}://${ip}:${port}${path}`)
36+
const threshold: number = 1
37+
38+
const main = async (): Promise<any> => {
39+
const avaxAssetIDBuf: Buffer = await xchain.getAVAXAssetID()
40+
const avaxAssetIDStr: string = bintools.cb58Encode(avaxAssetIDBuf)
41+
let balance: BN = await web3.eth.getBalance(cHexAddress)
42+
const fee: BN = cchain.getDefaultTxFee()
43+
balance = new BN(balance.toString().substring(0, 17))
44+
const txcount = await web3.eth.getTransactionCount(cHexAddress)
45+
const nonce: number = txcount;
46+
const locktime: BN = new BN(0)
47+
const avaxAmount: BN = balance.sub(fee)
48+
49+
const unsignedTx: UnsignedTx = await cchain.buildExportTx(
50+
avaxAmount,
51+
avaxAssetIDStr,
52+
xChainBlockchainIdStr,
53+
cHexAddress,
54+
cAddressStrings[0],
55+
xAddressStrings,
56+
nonce,
57+
locktime,
58+
threshold
59+
)
60+
61+
const tx: Tx = unsignedTx.sign(cKeychain)
62+
const id: string = await cchain.issueTx(tx)
63+
console.log(id)
64+
}
65+
66+
main()

0 commit comments

Comments
 (0)