This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
AtomicSwapTest.js
169 lines (139 loc) · 5.81 KB
/
AtomicSwapTest.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint-env mocha */
const expect = require('chai').expect
const getGramsFromGiver = require('./giver.js')
const AtomicSwapWalletContract = require('../contracts/AtomicSwapWalletContract.js')
const AtomicSwapContract = require('../contracts/AtomicSwapContract.js')
const { TONClient } = require('ton-client-node-js')
describe('Atomic Swap Redeem', function () {
this.timeout(5000)
let client
let secretHash
let secret
let amount
let initiatorContract
let initiatorKeyPair
let participantContract
let participantKeyPair
let atomicSwapContract
before(async () => {
client = await TONClient.create({servers: ['http://0.0.0.0']})
initiatorKeyPair = await client.crypto.ed25519Keypair()
participantKeyPair = await client.crypto.ed25519Keypair()
secret = await client.crypto.randomGenerateBytes(32)
const codeAtomicSwap = await client.contracts.getCodeFromImage({
imageBase64: AtomicSwapContract.package.imageBase64
})
const constructorParams = { '_codeAtomicSwap': codeAtomicSwap.codeBase64 }
// Deploy Initiator Wallet
{
const address = await getFutureAddress(
client, AtomicSwapWalletContract.package, initiatorKeyPair, constructorParams
)
await getGramsFromGiver(client, address, 100_000_000_000)
initiatorContract = new AtomicSwapWalletContract(client, address, initiatorKeyPair)
await initiatorContract.deploy(constructorParams)
await client.queries.accounts.waitFor({
id: { eq: address },
acc_type_name: { eq: 'Active' }
}, 'id')
}
// Deploy Participant Wallet
{
const address = await getFutureAddress(
client, AtomicSwapWalletContract.package, participantKeyPair, constructorParams
)
await getGramsFromGiver(client, address, 1_000_000_000)
participantContract = new AtomicSwapWalletContract(client, address, participantKeyPair)
await participantContract.deploy(constructorParams)
await client.queries.accounts.waitFor({
id: { eq: address },
acc_type_name: { eq: 'Active' }
}, 'id')
}
secretHash = (await initiatorContract.hashSecretLocal({
secret: secret,
}))['value0']
amount = 10_000_000_000 // 10 ton
})
it('create Atomic Swap', async () => {
// Create Atomic Swap
const data = await client.contracts.getDeployData({
abi: AtomicSwapContract.package.abi,
initParams: {
secretHash: `${secretHash}`,
},
publicKeyHex: '0000000000000000000000000000000000000000000000000000000000000000'
})
const result = await initiatorContract.createSwap({
'initiator': initiatorContract.address,
'participant': participantContract.address,
'amount': 10_000_000_000,
'timeLock': parseInt(Date.now() / 1000) + 60,
'data': data.dataBase64
})
// Return address of Atomic Swap
const atomicSwapAddress = result['value0']
expect(atomicSwapAddress).to.be.not.equal(undefined)
atomicSwapContract = new AtomicSwapContract(client, atomicSwapAddress)
})
it('check params of Atomic Swap', async () => {
const params = await atomicSwapContract.paramsLocal()
// check params of Atomic Swap
expect(parseInt(params['_amount'], 16), 'amount').to.be.equal(amount)
expect(parseInt(params['_balance'], 16), 'balance gt').to.be.gt(amount)
expect(parseInt(params['_timeLock'], 16), 'expired time').to.be.gt(Date.now() / 1000)
expect(params['_initiator'], 'initiator').to.be.equal(`${initiatorContract.address}`)
expect(params['_participant'], 'participant').to.be.equal(`${participantContract.address}`)
expect(params['_secretHash'], 'secret hash').to.be.equal(`${secretHash}`)
})
it('redeem by participant', async () => {
// redeem Atomic Swap
const runBody = await client.contracts.createRunBody({
abi: AtomicSwapContract.package.abi,
function: "redeem",
params: {
secret: `${secret}`,
},
internal: true,
})
const params = {
dest: atomicSwapContract.address,
value: 10_000_000,
bounce: true,
flag: 1,
payload: runBody.bodyBase64
}
await participantContract.sendTransaction(params)
// check Redeemed event and acquire secret
{
const msg = await client.queries.messages.waitFor({
src: { eq: atomicSwapContract.address },
msg_type: { eq: 2 },
}, "body"
)
const event = await client.contracts.decodeOutputMessageBody({
abi: AtomicSwapContract.package.abi,
bodyBase64: msg['body'],
internal: false,
})
expect(event.function).to.be.equal("Redeemed")
expect(event.output.secret).to.be.equal(`${secret}`)
}
// check that balance of participant is more
const participantBalance = (await client.queries.accounts.query({
id: { eq: participantContract.address },
}, "balance"
))[0]['balance']
expect(parseInt(participantBalance, 16)).to.be.gt(10_999_000_000)
})
after(async () => {
await client.close()
})
})
async function getFutureAddress(client, package, keyPair, constructorParams = {}) {
return (await client.contracts.createDeployMessage({
package,
constructorParams,
keyPair,
})).address
}