Skip to content

Commit 48f032d

Browse files
authored
Merge pull request #424 from trustlines-protocol/fix/transfer_0_update
Fix trustline updates with transfer of "0"
2 parents 8b30ce1 + d867cf1 commit 48f032d

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

.circleci/config.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ jobs:
9595
DOCKER_REPO: trustlines/e2e
9696
LOCAL_IMAGE: e2e
9797
steps:
98-
- setup_remote_docker
98+
- setup_remote_docker:
99+
version: 20.10.7
99100
- checkout
100101
- run:
101102
name: Build docker image
@@ -117,7 +118,8 @@ jobs:
117118
DOCKER_REPO: trustlines/e2e
118119
LOCAL_IMAGE: e2e
119120
steps:
120-
- setup_remote_docker
121+
- setup_remote_docker:
122+
version: 20.10.7
121123
- attach_workspace:
122124
at: "~"
123125
- run:
@@ -140,7 +142,8 @@ jobs:
140142
DOCKER_REPO: trustlines/e2e
141143
LOCAL_IMAGE: e2e
142144
steps:
143-
- setup_remote_docker
145+
- setup_remote_docker:
146+
version: 20.10.7
144147
- attach_workspace:
145148
at: "~"
146149
- run:
@@ -163,7 +166,8 @@ jobs:
163166
DOCKER_REPO: trustlines/e2e
164167
LOCAL_IMAGE: e2e
165168
steps:
166-
- setup_remote_docker
169+
- setup_remote_docker:
170+
version: 20.10.7
167171
- attach_workspace:
168172
at: "~"
169173
- run:

src/Trustline.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export class Trustline {
144144
isFrozen,
145145
transfer
146146
} = options
147+
147148
const [
148149
decimals,
149150
{ customInterests, defaultInterestRate }
@@ -189,7 +190,16 @@ export class Trustline {
189190
),
190191
isFrozen
191192
]
192-
if (transfer !== undefined && transfer !== 0) {
193+
194+
if (typeof transfer === 'string' && isNaN(parseFloat(transfer))) {
195+
throw new Error('Transfer is not a number')
196+
}
197+
198+
if (
199+
transfer !== undefined &&
200+
((typeof transfer === 'string' && parseFloat(transfer) !== 0) ||
201+
(typeof transfer === 'number' && transfer !== 0))
202+
) {
193203
updateFuncName =
194204
'updateTrustline(address,uint64,uint64,int16,int16,bool,int72)'
195205
updateFuncArgs = [

tests/e2e/Trustline.test.ts

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,23 @@ describe('e2e', () => {
2323
const tl1 = new TLNetwork(config)
2424
const tl2 = new TLNetwork(config)
2525
const tl3 = new TLNetwork(config)
26+
const tl4 = new TLNetwork(config)
2627
let user1
2728
let user2
2829
let user3
30+
let user4
2931
let networks
3032
let networkDefaultInterestRates
3133
let networkCustomInterestRates
3234
let networkWithoutInterestRates
3335

3436
before(async () => {
3537
// set network and load users
36-
;[networks, [user1, user2, user3]] = await Promise.all([
38+
;[networks, [user1, user2, user3, user4]] = await Promise.all([
3739
tl1.currencyNetwork.getAll(),
38-
createAndLoadUsers([tl1, tl2, tl3])
40+
createAndLoadUsers([tl1, tl2, tl3, tl4])
3941
])
40-
await deployIdentities([tl1, tl2, tl3])
42+
await deployIdentities([tl1, tl2, tl3, tl4])
4143
// get network details
4244
const networksWithDetails = await Promise.all(
4345
networks.map(network => tl1.currencyNetwork.getInfo(network.address))
@@ -62,7 +64,8 @@ describe('e2e', () => {
6264
await Promise.all([
6365
tl1.user.requestEth(),
6466
tl2.user.requestEth(),
65-
tl3.user.requestEth()
67+
tl3.user.requestEth(),
68+
tl4.user.requestEth()
6669
])
6770
await wait()
6871
})
@@ -221,6 +224,85 @@ describe('e2e', () => {
221224
tl1.trustline.confirm(txFreezing.rawTx)
222225
).to.eventually.be.a('string')
223226
})
227+
228+
it('should return txHash for trustline update on existing trustline', async () => {
229+
const proposeAndAcceptTrustline = async () => {
230+
// Create and accept a trustline - start
231+
const txFreezing = await tl1.trustline.prepareUpdate(
232+
networkCustomInterestRates.address,
233+
user4.address,
234+
2000,
235+
1000,
236+
{
237+
interestRateGiven: 0.02,
238+
interestRateReceived: 0.01,
239+
isFrozen: false,
240+
transfer: 123
241+
}
242+
)
243+
await expect(
244+
tl1.trustline.confirm(txFreezing.rawTx)
245+
).to.eventually.be.a('string')
246+
247+
const acceptTl = await tl4.trustline.prepareAccept(
248+
networkCustomInterestRates.address,
249+
user1.address,
250+
1000,
251+
2000,
252+
{
253+
interestRateGiven: 0.01,
254+
interestRateReceived: 0.02,
255+
isFrozen: false,
256+
transfer: -123
257+
}
258+
)
259+
260+
await tl4.trustline.confirm(acceptTl.rawTx)
261+
wait()
262+
}
263+
264+
const proposeUpdateToExistingTrustline = async transfer => {
265+
const update = await tl1.trustline.prepareUpdate(
266+
networkCustomInterestRates.address,
267+
user4.address,
268+
2100,
269+
1100,
270+
{
271+
interestRateGiven: 0.02,
272+
interestRateReceived: 0.01,
273+
isFrozen: false,
274+
transfer
275+
}
276+
)
277+
278+
const confirmed = await tl1.trustline.confirm(update.rawTx)
279+
expect(confirmed).to.be.a('string')
280+
281+
await wait()
282+
283+
const txStatus = await tl1.transaction.getTxStatus(update.rawTx)
284+
285+
return txStatus
286+
}
287+
288+
await proposeAndAcceptTrustline()
289+
await expect(
290+
proposeUpdateToExistingTrustline('0')
291+
).to.eventually.have.property('status', 'success')
292+
await expect(
293+
proposeUpdateToExistingTrustline(0)
294+
).to.eventually.have.property('status', 'success')
295+
await expect(
296+
proposeUpdateToExistingTrustline(123)
297+
).to.eventually.have.property('status', 'failure')
298+
await expect(
299+
proposeUpdateToExistingTrustline('123')
300+
).to.eventually.have.property('status', 'failure')
301+
302+
await expect(
303+
proposeUpdateToExistingTrustline('')
304+
).to.eventually.be.rejectedWith(Error, 'Transfer is not a number')
305+
})
224306
})
225307

226308
describe('#prepareCancelTrustlineUpdate()', async () => {

0 commit comments

Comments
 (0)