Skip to content

Commit

Permalink
Update to PingPong2 so that checking sender_adress against hardcoded
Browse files Browse the repository at this point in the history
addresses works
  • Loading branch information
fabcotech committed Jul 24, 2024
1 parent 5d3e42c commit 68f188c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ A set of low, high value TON blockchain (The Open Network) FunC smart contracts
and test suites. New code coming in regularly.

- **Arithmetic** : TON Smart contract that performs basic add/substract/multiply
operations on a integer
operations on a integer. _Storing number on chain_, _performing operations on
a number_
- **Fibonacci** : TON Smart contract that stores two integers, and continues the
fibonacci sequence everytime it is touched.
- **Ten thousands transfers** : Two blockchain users exchange TON back and
Expand All @@ -17,7 +18,12 @@ and test suites. New code coming in regularly.
sent to three blockchain users, they do one thousand small token transfers.
Balance is checked after each transfer.
- **Ping pong** : A contract stores a string (as `func slice`), each time it is
called, `"ping"` is toggled to `"pong"` and vice versa.
called, `"ping"` is toggled to `"pong"` and vice versa. _Storing a string as
slice on chain_, _Comparing two slices one with another_
- **Ping pong (2)** : Same as Ping pong but only one address is authorized to
toggle ping->pong, and only one other address is authorized to toggle
pong->ping. _Checking sender address_, _Comparing sender address with another
arbitrary address_

```sh
yarn
Expand All @@ -38,4 +44,7 @@ yarn test tests/OneThousandTokenTransfers.spec.ts

# Only test ping pong
yarn test tests/PingPong.spec.ts

# Only test ping pong 2
yarn test tests/PingPong2.spec.ts
```
30 changes: 13 additions & 17 deletions contracts/pingpong2.fc
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include "imports/stdlib.fc";

int slice_hash(slice s) asm "HASHSU";
int equal_slices(slice a, slice b) asm "SDEQ";

;; storage variables
global cell ctx_str;
global cell ctx_address1;
global cell ctx_address2;

() load_data() impure {
var ds = get_data().begin_parse();
ctx_str = ds~load_ref();
ctx_address1 = ds~load_ref();
ctx_address2 = ds~load_ref();
ds.end_parse();
}

Expand All @@ -19,6 +20,7 @@ global cell ctx_address1;
begin_cell()
.store_ref(ctx_str)
.store_ref(ctx_address1)
.store_ref(ctx_address2)
.end_cell()
);
}
Expand All @@ -35,25 +37,19 @@ global cell ctx_address1;
slice current = ctx_str.begin_parse();
slice ping = begin_cell().store_slice("ping").end_cell().begin_parse();

~strdump("sender_address");
~dump(sender_address);
~strdump("ctx_address1");
~dump(ctx_address1.begin_parse());
if (equal_slices(sender_address, ctx_address1.begin_parse()) == -1) {
~strdump("sender_address and ctx_address1 are equal");
} else {
~strdump("sender_address and ctx_address1 are not equal");
}

if (equal_slices(current,ping) == -1) {
cell c = begin_cell().store_slice("pong").end_cell();
ctx_str = c;
save_data();
if (equal_slices(sender_address, ctx_address1.begin_parse()) == -1) {
cell c = begin_cell().store_slice("pong").end_cell();
ctx_str = c;
save_data();
}
return ();
} else {
cell c = begin_cell().store_slice("ping").end_cell();
ctx_str = c;
save_data();
if (equal_slices(sender_address, ctx_address2.begin_parse()) == -1) {
cell c = begin_cell().store_slice("ping").end_cell();
ctx_str = c;
save_data();
}
return ();
}
}
Expand Down
17 changes: 15 additions & 2 deletions tests/PingPong2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ describe('[PingPong2]', () => {
let deployer: SandboxContract<TreasuryContract>;
let pingPong2Contract: SandboxContract<PingPong2>;
let user1: null | SandboxContract<TreasuryContract> = null;
let user2: null | SandboxContract<TreasuryContract> = null;
let provider: null | ContractProvider = null;

beforeAll(async () => {
code = await compile('PingPong2');
blockchain = await Blockchain.create();
user1 = await blockchain.treasury('user1');
console.log(user1.address);
user2 = await blockchain.treasury('user2');
pingPong2Contract = blockchain.openContract(
PingPong2.createFromConfig({}, code)
);
Expand Down Expand Up @@ -59,7 +60,7 @@ describe('[PingPong2]', () => {
expect(decoded2).toBe('pong');

await pingPong2Contract.sendToggle(
(user1 as SandboxContract<TreasuryContract>).getSender(),
(user2 as SandboxContract<TreasuryContract>).getSender(),
{
value: toNano('0.05'),
}
Expand All @@ -68,5 +69,17 @@ describe('[PingPong2]', () => {
const str3Ok = (str3 as any).toString().replace('x{', '').replace('}', '');
const decoded3 = Buffer.from(str3Ok, 'hex').toString('utf8');
expect(decoded3).toBe('ping');

// user2 cannot toggle ping->pong
await pingPong2Contract.sendToggle(
(user2 as SandboxContract<TreasuryContract>).getSender(),
{
value: toNano('0.05'),
}
);
const str4 = await pingPong2Contract.getStr();
const str4Ok = (str4 as any).toString().replace('x{', '').replace('}', '');
const decoded4 = Buffer.from(str4Ok, 'hex').toString('utf8');
expect(decoded4).toBe('ping');
});
});
11 changes: 10 additions & 1 deletion wrappers/PingPong2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ export function pingPong2ConfigToCell(): Cell {
.storeRef(beginCell().storeStringTail('ping').endCell())
.storeRef(
beginCell()
.storeStringTail('EQCGetaGEWP4PhqPSV71o4NaU3rcw5yAG7kh7s1VdtGGynTA')
.storeAddress(
Address.parse('EQCGetaGEWP4PhqPSV71o4NaU3rcw5yAG7kh7s1VdtGGynTA')
)
.endCell()
)
.storeRef(
beginCell()
.storeAddress(
Address.parse('EQBOo7szgjcz5rYICfwEpHALKKP8QDXwRqKVY7w5axLDcEfY')
)
.endCell()
)
.endCell();
Expand Down

0 comments on commit 68f188c

Please sign in to comment.