-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathPaymentToken.test.js
145 lines (127 loc) · 4.77 KB
/
PaymentToken.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const truffleContract = require('@truffle/contract');
const { executeSafeTx } = require('./helpers/executeSafeTx');
const { estimateBaseGas, estimateTxGas } = require('./helpers/estimateGas');
const {
BigNumber,
maxGas,
extraGas,
inflation,
period,
symbol,
name,
signupBonus,
ZERO_ADDRESS,
timeout,
} = require('./helpers/constants');
const { bn, convertToBaseUnit } = require('./helpers/math');
const { createSafeWithProxy } = require('./helpers/createSafeWithProxy');
const safeArtifacts = require('@gnosis.pm/safe-contracts/build/artifacts/contracts/GnosisSafeL2.sol/GnosisSafeL2.json');
const proxyArtifacts = require('@gnosis.pm/safe-contracts/build/artifacts/contracts/proxies/GnosisSafeProxyFactory.sol/GnosisSafeProxyFactory.json');
const Hub = artifacts.require('MockHub');
const Token = artifacts.require('Token');
const GnosisSafe = truffleContract(safeArtifacts);
const ProxyFactory = truffleContract(proxyArtifacts);
GnosisSafe.setProvider(web3.currentProvider);
ProxyFactory.setProvider(web3.currentProvider);
require('chai')
.use(require('chai-bn')(BigNumber))
.should();
contract('Token payments', ([_, safeOwner, recipient, anotherAccount, systemOwner]) => { // eslint-disable-line no-unused-vars
let hub = null;
let safe = null;
let token = null;
let proxyFactory = null;
let userSafe = null;
const initialConverted = convertToBaseUnit(signupBonus);
beforeEach(async () => {
hub = await Hub
.new(
inflation,
period,
symbol,
name,
initialConverted,
initialConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
safe = await GnosisSafe.new({ from: systemOwner });
proxyFactory = await ProxyFactory.new({ from: systemOwner });
userSafe = await createSafeWithProxy(proxyFactory, safe, GnosisSafe, safeOwner);
const txParams = {
to: hub.address,
data: await hub.contract.methods.signup().encodeABI(),
};
await executeSafeTx(userSafe, txParams, safeOwner, 0, extraGas, safeOwner, web3);
const blockNumber = await web3.eth.getBlockNumber();
const signUpLogs = await hub.getPastEvents('Signup', { fromBlock: blockNumber - 1, toBlock: 'latest' });
token = await Token.at(signUpLogs[0].args.token);
});
describe('user can use their token as payment token', () => {
const amount = convertToBaseUnit(50);
const gasCosts = bn(89645);
it('should transfer tokens', async () => {
const to = token.address;
const data = await token.contract.methods
.transfer(recipient, amount.toString())
.encodeABI();
const safeTxGas = await estimateTxGas(userSafe, to, 0, data, 0);
const gasToken = token.address;
const nonce = (await userSafe.nonce()).toNumber();
const baseGas = await estimateBaseGas(userSafe, to, 0, data, 0,
safeTxGas, gasToken, ZERO_ADDRESS, 1, nonce);
const txParams = {
to,
data,
gasPrice: 1,
gasToken,
safeTxGas,
baseGas,
};
await executeSafeTx(userSafe, txParams, safeOwner, baseGas, safeTxGas, safeOwner, web3);
(await token.balanceOf(recipient)).should.be.bignumber.equal(amount);
});
it('should transfer gas to the tx origin', async () => {
const to = token.address;
const data = await token.contract.methods
.transfer(recipient, amount.toString())
.encodeABI();
const safeTxGas = await estimateTxGas(userSafe, to, 0, data, 0);
const gasToken = token.address;
const nonce = (await userSafe.nonce()).toNumber();
const baseGas = await estimateBaseGas(userSafe, to, 0, data, 0,
safeTxGas, gasToken, ZERO_ADDRESS, 1, nonce);
const txParams = {
to,
data,
gasPrice: 1,
gasToken,
safeTxGas,
baseGas,
};
await executeSafeTx(userSafe, txParams, safeOwner, baseGas, safeTxGas, safeOwner, web3);
(await token.balanceOf(safeOwner)).should.be.bignumber.equal(gasCosts);
});
it('safe should pay gas', async () => {
const to = token.address;
const data = await token.contract.methods
.transfer(recipient, amount.toString())
.encodeABI();
const safeTxGas = await estimateTxGas(userSafe, to, 0, data, 0);
const gasToken = token.address;
const nonce = (await userSafe.nonce()).toNumber();
const baseGas = await estimateBaseGas(userSafe, to, 0, data, 0,
safeTxGas, gasToken, ZERO_ADDRESS, 1, nonce);
const txParams = {
to,
data,
gasPrice: 1,
gasToken,
safeTxGas,
baseGas,
};
await executeSafeTx(userSafe, txParams, safeOwner, baseGas, safeTxGas, safeOwner, web3);
(await token.balanceOf(userSafe.address)).should.be.bignumber.equal(amount.sub(gasCosts));
});
});
});