Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require('dotenv').config();
const { ApiPromise, WsProvider, Keyring } = require('@polkadot/api');
const { blake2AsU8a } = require('@polkadot/util-crypto');
const BN = require('bn.js');
const ethers = require('ethers');
const BridgeJson = require('./Bridge.json');
const bn1e18 = new BN(10).pow(new BN(18));
const khalaParaId = 2004;
const karuraParaId = 2000;
const bridgeAddressOnRinkeby = '0x0712Cf53B9fA1A33018d180a4AbcC7f1803F55f4';
async function transferPhaFromEthereumToKarura(khalaApi, bridge, sender, recipient, amount) {
let khalaChainId = 1;
let phaResourceId = '0x00e6dfb61a2fb903df487c401663825643bb825d41695e63df8af6162ab145a6';
let dest = khalaApi.createType('XcmV1MultiLocation', {
// parents = 1 means we wanna send to other parachains or relaychain
parents: 1,
interior: khalaApi.createType('Junctions', {
X2: [
khalaApi.createType('XcmV1Junction', {
Parachain: khalaApi.createType('Compact<U32>', karuraParaId)
}),
khalaApi.createType('XcmV1Junction', {
AccountId32: {
network: khalaApi.createType('XcmV0JunctionNetworkId', 'Any'),
id: recipient,
}
}),
]
})
}).toHex();
let data = '0x' +
ethers.utils.hexZeroPad(ethers.BigNumber.from(amount.toString()).toHexString(), 32).substr(2) +
ethers.utils.hexZeroPad(ethers.utils.hexlify((dest.length - 2) / 2), 32).substr(2) +
dest.substr(2);
await bridge.deposit(khalaChainId, phaResourceId, data);
}
async function main() {
// Create khala api
const khalaEndpoint = process.env.ENDPOINT || 'ws://127.0.0.1:9944';
const khalaProvider = new WsProvider(khalaEndpoint);
const khalaApi = await ApiPromise.create({
provider: khalaProvider,
});
// Replace it with your owns, make sure private key used to sign transaction matchs address
const evmSender = "0xA29D4E0F035cb50C0d78c8CeBb56Ca292616Ab20";
let privateKey = process.env.KEY;
let provider = new ethers.providers.JsonRpcProvider('https://rinkeby.infura.io/v3/<project id>');
let ethereumWallet = new ethers.Wallet(privateKey, provider);
let bridge = new ethers.Contract(bridgeAddressOnMoonriver, BridgeJson.abi, ethereumWallet);
// Public key of the substrate account
const recipient = '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d';
await transferPhaFromEthereumToKarura(khalaApi, bridge, evmSender, recipient, bn1e18.mul(new BN(100)));
}
main().catch(console.error).finally(() => process.exit());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment