Skip to content

Instantly share code, notes, and snippets.

@earthchie
Created August 21, 2022 08:34
Show Gist options
  • Save earthchie/9a7492e1343ee8ba116edda90c3f72e5 to your computer and use it in GitHub Desktop.
Save earthchie/9a7492e1343ee8ba116edda90c3f72e5 to your computer and use it in GitHub Desktop.
const ethers = require('ethers');
async function app() {
const privateKey = '...'; // bridge wallet, has permission to mint and burn
const abi = [
'event Transfer(address indexed from, address indexed to, uint256 value)',
'function mint(address receiver, uint256 amount)',
'function burn(address account, uint256 amount)'
];
const providerA = new ethers.providers.JsonRpcProvider('https://data-seed-prebsc-1-s1.binance.org:8545/'); // BSC TESTNET
const providerB = new ethers.providers.JsonRpcProvider('https://matic-mumbai.chainstacklabs.com'); // MUMBAI
const walletA = new ethers.Wallet(privateKey, providerA);
const walletB = new ethers.Wallet(privateKey, providerB);
const contractA = new ethers.Contract('0x..contract_address_chain_A...', abi, walletA);
const contractB = new ethers.Contract('0x..contract_address_chain_B...', abi, walletB);
console.log('Standing by...');
contractA.on('Transfer', async function(from, to, value){
if(to === walletA.address){
console.log('[A->B] incoming from ', from, 'amount', ethers.utils.formatUnits(value));
await contractA.burn(to, value);
console.log('burn on chain A success!');
await contractB.mint(from, value);
console.log('mint on chain B success!');
}
});
contractB.on('Transfer', async function(from, to, value){
if(to === walletA.address){
console.log('[B->A] incoming from ', from, 'amount', ethers.utils.formatUnits(value));
await contractB.burn(to, value);
console.log('burn on chain B success!');
await contractA.mint(from, value);
console.log('mint on chain A success!');
}
});
}
app();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment