This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update * fix tests * fast test * ethapp tests working * eth erc20 working * fix eth erc20 * done * no p * np P
- Loading branch information
Showing
9 changed files
with
375 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const TestToken = require('../../../ethereum/build/contracts/TestToken.json'); | ||
const EthClient = require('../../src/ethclient').EthClient; | ||
const SubClient = require('../../src/subclient').SubClient; | ||
|
||
const polkadotRecipient = "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"; | ||
const polkadotRecipientSS58 = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"; | ||
const polkadotSenderSS58 = polkadotRecipientSS58; | ||
const treasuryAddressSS58 = "5EYCAe5jHEaRUtbinpdbTLuTyGiVt2TJGQPi9fdvVpNLNfSS"; | ||
const ethEndpoint = 'ws://localhost:8545'; | ||
const parachainEndpoint = 'ws://localhost:11144'; | ||
const testNetworkID = '344'; | ||
|
||
const TestTokenAddress = TestToken.networks[testNetworkID].address; | ||
|
||
const ETH_TO_PARA_WAIT_TIME = 60000; | ||
const PARA_TO_ETH_WAIT_TIME = 100000; | ||
|
||
async function bootstrap() { | ||
const ethClient = new EthClient(ethEndpoint, testNetworkID); | ||
const subClient = new SubClient(parachainEndpoint); | ||
await subClient.connect(); | ||
await ethClient.initialize(); | ||
return { ethClient, subClient }; | ||
} | ||
|
||
module.exports = { | ||
bootstrap, polkadotRecipient, | ||
polkadotRecipientSS58, polkadotSenderSS58, treasuryAddressSS58, | ||
TestTokenAddress, | ||
ETH_TO_PARA_WAIT_TIME, PARA_TO_ETH_WAIT_TIME | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const Web3 = require('web3'); | ||
|
||
const BigNumber = require('bignumber.js'); | ||
|
||
const { expect } = require("chai") | ||
.use(require("chai-as-promised")) | ||
.use(require("chai-bignumber")(BigNumber)) | ||
|
||
const { treasuryAddressSS58, polkadotSenderSS58, | ||
polkadotRecipientSS58, polkadotRecipient, bootstrap } = require('../src/fixtures'); | ||
|
||
describe('Bridge', function () { | ||
|
||
let ethClient, subClient; | ||
before(async function () { | ||
const clients = await bootstrap(); | ||
ethClient = clients.ethClient; | ||
subClient = clients.subClient; | ||
}); | ||
|
||
describe('DOT App', function () { | ||
|
||
it('should transfer DOT from Substrate to Ethereum', async function () { | ||
const amount = BigNumber('100000000000000'); // 100 DOT (12 decimal places in this environment) | ||
const amountWrapped = BigNumber(Web3.utils.toWei('100', "ether")); // 100 SnowDOT (18 decimal places) | ||
const ethAccount = ethClient.accounts[1]; | ||
|
||
const beforeEthBalance = await ethClient.getDotBalance(ethAccount); | ||
const beforeSubBalance = await subClient.queryAccountBalance(polkadotSenderSS58); | ||
|
||
// lock DOT using basic channel | ||
await subClient.lockDOT(subClient.alice, ethAccount, amount.toFixed(), 0) | ||
await ethClient.waitForNextEventData({ appName: 'snowDOT', eventName: 'Minted' }); | ||
|
||
const afterEthBalance = await ethClient.getDotBalance(ethAccount); | ||
const afterSubBalance = await subClient.queryAccountBalance(polkadotSenderSS58); | ||
|
||
expect(afterEthBalance.minus(beforeEthBalance)).to.be.bignumber.equal(amountWrapped); | ||
expect(beforeSubBalance.minus(afterSubBalance)).to.be.bignumber.greaterThan(amount); | ||
}) | ||
|
||
it('should transfer DOT from Ethereum to Substrate (basic channel)', async function () { | ||
const amount = BigNumber('1000000000000'); // 1 DOT (12 decimal places in this environment) | ||
const amountWrapped = BigNumber(Web3.utils.toWei('1', "ether")); // 1 SnowDOT (18 decimal places) | ||
const ethAccount = ethClient.accounts[1]; | ||
const subBalances = await subClient.subscribeAccountBalances( | ||
polkadotRecipientSS58, 2 | ||
); | ||
|
||
const beforeEthBalance = await ethClient.getDotBalance(ethAccount); | ||
const beforeSubBalance = await subBalances[0]; | ||
|
||
await ethClient.burnDOT(ethAccount, amountWrapped, polkadotRecipient, 0); | ||
|
||
const afterEthBalance = await ethClient.getDotBalance(ethAccount); | ||
const afterSubBalance = await subBalances[1]; | ||
|
||
expect(beforeEthBalance.minus(afterEthBalance)).to.be.bignumber.equal(amountWrapped); | ||
expect(afterSubBalance.minus(beforeSubBalance)).to.be.bignumber.equal(amount); | ||
}) | ||
|
||
it('should transfer DOT from Ethereum to Substrate (incentivized channel)', async function () { | ||
const amount = BigNumber('1000000000000'); // 1 DOT (12 decimal places in this environment) | ||
const amountWrapped = BigNumber(Web3.utils.toWei('1', "ether")); // 1 SnowDOT (18 decimal places) | ||
const ethAccount = ethClient.accounts[1]; | ||
const fee = BigNumber(Web3.utils.toWei('1', "ether")) // 1 SnowDOT | ||
const treasuryReward = BigNumber("200000000000") // 0.2 DOT | ||
const subBalances = await subClient.subscribeAccountBalances( | ||
polkadotRecipientSS58, 2 | ||
); | ||
const treasuryBalances = await subClient.subscribeAccountBalances( | ||
treasuryAddressSS58, 2 | ||
); | ||
|
||
const beforeEthBalance = await ethClient.getDotBalance(ethAccount); | ||
const beforeSubBalance = await subBalances[0]; | ||
const beforeTreasuryBalance = await treasuryBalances[0]; | ||
|
||
await ethClient.burnDOT(ethAccount, amountWrapped, polkadotRecipient, 1); | ||
|
||
const afterEthBalance = await ethClient.getDotBalance(ethAccount); | ||
const afterSubBalance = await subBalances[1]; | ||
const afterTreasuryBalance = await treasuryBalances[1]; | ||
|
||
expect(beforeEthBalance.minus(afterEthBalance)).to.be.bignumber.equal(amountWrapped.plus(fee)); | ||
expect(afterSubBalance.minus(beforeSubBalance)).to.be.bignumber.equal(amount); | ||
expect(afterTreasuryBalance.minus(beforeTreasuryBalance)).to.be.bignumber.equal(treasuryReward); | ||
}) | ||
|
||
}) | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const BigNumber = require('bignumber.js'); | ||
|
||
const { expect } = require("chai") | ||
.use(require("chai-as-promised")) | ||
.use(require("chai-bignumber")(BigNumber)) | ||
|
||
const { TestTokenAddress, polkadotRecipientSS58, polkadotRecipient, bootstrap } = require('../src/fixtures'); | ||
|
||
describe('Bridge', function () { | ||
|
||
let ethClient, subClient; | ||
before(async function () { | ||
const clients = await bootstrap(); | ||
ethClient = clients.ethClient; | ||
subClient = clients.subClient; | ||
this.erc20AssetId = subClient.api.createType('AssetId', | ||
{ Token: TestTokenAddress } | ||
); | ||
}); | ||
|
||
describe('ERC20 App', function () { | ||
it('should transfer ERC20 tokens from Ethereum to Substrate', async function () { | ||
const amount = BigNumber('1000'); | ||
const ethAccount = ethClient.accounts[1]; | ||
const subBalances = await subClient.subscribeAssetBalances( | ||
polkadotRecipientSS58, this.erc20AssetId, 2 | ||
); | ||
|
||
const beforeEthBalance = await ethClient.getErc20Balance(ethAccount); | ||
const beforeSubBalance = await subBalances[0]; | ||
|
||
await ethClient.approveERC20(ethAccount, amount); | ||
await ethClient.lockERC20(ethAccount, amount, polkadotRecipient); | ||
|
||
const afterEthBalance = await ethClient.getErc20Balance(ethAccount); | ||
const afterSubBalance = await subBalances[1]; | ||
|
||
expect(afterEthBalance).to.be.bignumber.equal(beforeEthBalance.minus(amount)); | ||
expect(afterSubBalance).to.be.bignumber.equal(beforeSubBalance.plus(amount)); | ||
|
||
// conservation of value | ||
expect(beforeEthBalance.plus(beforeSubBalance)).to.be.bignumber.equal(afterEthBalance.plus(afterSubBalance)); | ||
}); | ||
|
||
it('should transfer ERC20 from Substrate to Ethereum', async function () { | ||
// Wait for new substrate block before tests, as queries may go to old block | ||
await subClient.waitForNextBlock(); | ||
|
||
const amount = BigNumber('1000'); | ||
const ethAccount = ethClient.accounts[1]; | ||
|
||
const beforeEthBalance = await ethClient.getErc20Balance(ethAccount); | ||
const beforeSubBalance = await subClient.queryAssetBalance(polkadotRecipientSS58, this.erc20AssetId); | ||
|
||
await subClient.burnERC20(subClient.alice, TestTokenAddress, ethAccount, amount.toFixed(), 1) | ||
await ethClient.waitForNextEventData({ appName: 'appERC20', eventName: 'Unlocked' }); | ||
|
||
const afterEthBalance = await ethClient.getErc20Balance(ethAccount); | ||
const afterSubBalance = await subClient.queryAssetBalance(polkadotRecipientSS58, this.erc20AssetId); | ||
|
||
expect(afterEthBalance.minus(beforeEthBalance)).to.be.bignumber.equal(amount); | ||
expect(beforeSubBalance.minus(afterSubBalance)).to.be.bignumber.equal(amount); | ||
// conservation of value | ||
expect(beforeEthBalance.plus(beforeSubBalance)).to.be.bignumber.equal(afterEthBalance.plus(afterSubBalance)); | ||
}) | ||
}) | ||
|
||
}); |
Oops, something went wrong.