airdrop.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* global artifacts, web3, contract */
  2. require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).should()
  3. const { takeSnapshot, revertSnapshot } = require('../scripts/ganacheHelper')
  4. const Airdrop = artifacts.require('./AirdropMock.sol')
  5. const Torn = artifacts.require('./TORNMock.sol')
  6. const { cap } = require('../config').torn
  7. const { toBN, toWei } = require('web3-utils')
  8. const RLP = require('rlp')
  9. async function getNextAddr(sender, offset = 0) {
  10. const nonce = await web3.eth.getTransactionCount(sender)
  11. return (
  12. '0x' +
  13. web3.utils
  14. .sha3(RLP.encode([sender, Number(nonce) + Number(offset)]))
  15. .slice(12)
  16. .substring(14)
  17. )
  18. }
  19. async function deploySefldestruct(contract, args, deployerPK) {
  20. const c = new web3.eth.Contract(contract.abi)
  21. const data = c
  22. .deploy({
  23. data: contract.bytecode,
  24. arguments: args,
  25. })
  26. .encodeABI()
  27. const signed = await web3.eth.accounts.signTransaction(
  28. {
  29. gas: 5e6,
  30. gasPrice: toWei('1', 'gwei'),
  31. data,
  32. },
  33. deployerPK,
  34. )
  35. await web3.eth.sendSignedTransaction(signed.rawTransaction)
  36. }
  37. contract('Airdrop', (accounts) => {
  38. let torn
  39. let snapshotId
  40. const airdropDeployer = accounts[8]
  41. const deployerPK = '0x0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669ff12b7c4'
  42. const recipient1 = accounts[2]
  43. const recipient2 = accounts[3]
  44. let half = toBN(cap).div(toBN(2)).toString()
  45. before(async () => {
  46. const newAddr = await getNextAddr(airdropDeployer)
  47. torn = await Torn.new(accounts[0], 0, [{ to: newAddr, amount: cap }])
  48. snapshotId = await takeSnapshot()
  49. })
  50. describe('#airdrop', () => {
  51. it('should work', async () => {
  52. // web3 throws when it tried to deploy a contract with selfdestruct() in constructor
  53. await deploySefldestruct(
  54. Airdrop,
  55. [
  56. torn.address,
  57. [
  58. { to: recipient1, amount: half },
  59. { to: recipient2, amount: half },
  60. ],
  61. ],
  62. deployerPK,
  63. )
  64. const bal1 = await torn.balanceOf(recipient1)
  65. const bal2 = await torn.balanceOf(recipient2)
  66. bal1.should.eq.BN(toBN(half))
  67. bal2.should.eq.BN(toBN(half))
  68. })
  69. // todo: how do we get the same deployed address without create2?
  70. // it('should throw on second attempt', async () => {
  71. // await Airdrop.new(torn.address, [accounts[1], accounts[2]], [half, half], { from: airdropDeployer })
  72. // })
  73. })
  74. afterEach(async () => {
  75. await revertSnapshot(snapshotId.result)
  76. // eslint-disable-next-line require-atomic-updates
  77. snapshotId = await takeSnapshot()
  78. })
  79. })