-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
import { Address, toNano } from '@ton/core'; | ||
import { SimpleCounter } from '../wrappers/SimpleCounter'; | ||
import { NetworkProvider, sleep } from '@ton/blueprint'; | ||
import { Address, toNano } from "@ton/core"; | ||
import { SimpleCounter } from "../wrappers/SimpleCounter"; | ||
import { NetworkProvider, sleep } from "@ton/blueprint"; | ||
|
||
export async function run(provider: NetworkProvider, args: string[]) { | ||
const ui = provider.ui(); | ||
const ui = provider.ui(); | ||
|
||
const address = Address.parse(args.length > 0 ? args[0] : await ui.input('SimpleCounter address')); | ||
const address = Address.parse( | ||
args.length > 0 ? args[0] : await ui.input("SimpleCounter address"), | ||
); | ||
|
||
if (!(await provider.isContractDeployed(address))) { | ||
ui.write(`Error: Contract at address ${address} is not deployed!`); | ||
return; | ||
} | ||
if (!(await provider.isContractDeployed(address))) { | ||
ui.write(`Error: Contract at address ${address} is not deployed!`); | ||
return; | ||
} | ||
|
||
const simpleCounter = provider.open(SimpleCounter.createFromAddress(address)); | ||
const simpleCounter = provider.open(SimpleCounter.createFromAddress(address)); | ||
|
||
const counterBefore = await simpleCounter.getCounter(); | ||
const counterBefore = await simpleCounter.getCounter(); | ||
|
||
await simpleCounter.sendIncrease(provider.sender(), { | ||
increaseBy: 1, | ||
value: toNano('0.05'), | ||
}); | ||
await simpleCounter.sendIncrease(provider.sender(), { | ||
increaseBy: 1, | ||
value: toNano("0.05"), | ||
}); | ||
|
||
ui.write('Waiting for counter to increase...'); | ||
ui.write("Waiting for counter to increase..."); | ||
|
||
let counterAfter = await simpleCounter.getCounter(); | ||
let attempt = 1; | ||
while (counterAfter === counterBefore) { | ||
ui.setActionPrompt(`Attempt ${attempt}`); | ||
await sleep(2000); | ||
counterAfter = await simpleCounter.getCounter(); | ||
attempt++; | ||
} | ||
let counterAfter = await simpleCounter.getCounter(); | ||
let attempt = 1; | ||
while (counterAfter === counterBefore) { | ||
ui.setActionPrompt(`Attempt ${attempt}`); | ||
await sleep(2000); | ||
counterAfter = await simpleCounter.getCounter(); | ||
attempt++; | ||
} | ||
|
||
ui.clearActionPrompt(); | ||
ui.write('Counter increased successfully!'); | ||
ui.clearActionPrompt(); | ||
ui.write("Counter increased successfully!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,87 @@ | ||
import { Blockchain, SandboxContract, TreasuryContract } from '@ton/sandbox'; | ||
import { Cell, toNano } from '@ton/core'; | ||
import { SimpleCounter } from '../wrappers/SimpleCounter'; | ||
import '@ton/test-utils'; | ||
import { compile } from '@ton/blueprint'; | ||
|
||
describe('SimpleCounter', () => { | ||
let code: Cell; | ||
|
||
beforeAll(async () => { | ||
code = await compile('SimpleCounter'); | ||
}); | ||
|
||
let blockchain: Blockchain; | ||
let deployer: SandboxContract<TreasuryContract>; | ||
let simpleCounter: SandboxContract<SimpleCounter>; | ||
|
||
beforeEach(async () => { | ||
blockchain = await Blockchain.create(); | ||
|
||
simpleCounter = blockchain.openContract( | ||
SimpleCounter.createFromConfig( | ||
{ | ||
id: 0, | ||
counter: 0, | ||
}, | ||
code | ||
) | ||
); | ||
|
||
deployer = await blockchain.treasury('deployer'); | ||
|
||
const deployResult = await simpleCounter.sendDeploy(deployer.getSender(), toNano('0.05')); | ||
|
||
expect(deployResult.transactions).toHaveTransaction({ | ||
from: deployer.address, | ||
to: simpleCounter.address, | ||
deploy: true, | ||
success: true, | ||
}); | ||
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox"; | ||
import { Cell, toNano } from "@ton/core"; | ||
import { SimpleCounter } from "../wrappers/SimpleCounter"; | ||
import "@ton/test-utils"; | ||
import { compile } from "@ton/blueprint"; | ||
|
||
describe("SimpleCounter", () => { | ||
let code: Cell; | ||
|
||
beforeAll(async () => { | ||
code = await compile("SimpleCounter"); | ||
}); | ||
|
||
let blockchain: Blockchain; | ||
let deployer: SandboxContract<TreasuryContract>; | ||
let simpleCounter: SandboxContract<SimpleCounter>; | ||
|
||
beforeEach(async () => { | ||
blockchain = await Blockchain.create(); | ||
|
||
simpleCounter = blockchain.openContract( | ||
SimpleCounter.createFromConfig( | ||
{ | ||
id: 0, | ||
counter: 0, | ||
}, | ||
code, | ||
), | ||
); | ||
|
||
deployer = await blockchain.treasury("deployer"); | ||
|
||
const deployResult = await simpleCounter.sendDeploy( | ||
deployer.getSender(), | ||
toNano("0.05"), | ||
); | ||
|
||
expect(deployResult.transactions).toHaveTransaction({ | ||
from: deployer.address, | ||
to: simpleCounter.address, | ||
deploy: true, | ||
success: true, | ||
}); | ||
}); | ||
|
||
it('should deploy', async () => { | ||
// the check is done inside beforeEach | ||
// blockchain and simpleCounter are ready to use | ||
}); | ||
it("should deploy", async () => { | ||
// the check is done inside beforeEach | ||
// blockchain and simpleCounter are ready to use | ||
}); | ||
|
||
it('should increase counter', async () => { | ||
const increaseTimes = 3; | ||
for (let i = 0; i < increaseTimes; i++) { | ||
console.log(`increase ${i + 1}/${increaseTimes}`); | ||
it("should increase counter", async () => { | ||
const increaseTimes = 3; | ||
for (let i = 0; i < increaseTimes; i++) { | ||
console.log(`increase ${i + 1}/${increaseTimes}`); | ||
|
||
const increaser = await blockchain.treasury('increaser' + i); | ||
const increaser = await blockchain.treasury("increaser" + i); | ||
|
||
const counterBefore = await simpleCounter.getCounter(); | ||
const counterBefore = await simpleCounter.getCounter(); | ||
|
||
console.log('counter before increasing', counterBefore); | ||
console.log("counter before increasing", counterBefore); | ||
|
||
const increaseBy = Math.floor(Math.random() * 100); | ||
const increaseBy = Math.floor(Math.random() * 100); | ||
|
||
console.log('increasing by', increaseBy); | ||
console.log("increasing by", increaseBy); | ||
|
||
const increaseResult = await simpleCounter.sendIncrease(increaser.getSender(), { | ||
increaseBy, | ||
value: toNano('0.05'), | ||
}); | ||
const increaseResult = await simpleCounter.sendIncrease( | ||
increaser.getSender(), | ||
{ | ||
increaseBy, | ||
value: toNano("0.05"), | ||
}, | ||
); | ||
|
||
expect(increaseResult.transactions).toHaveTransaction({ | ||
from: increaser.address, | ||
to: simpleCounter.address, | ||
success: true, | ||
}); | ||
expect(increaseResult.transactions).toHaveTransaction({ | ||
from: increaser.address, | ||
to: simpleCounter.address, | ||
success: true, | ||
}); | ||
|
||
const counterAfter = await simpleCounter.getCounter(); | ||
const counterAfter = await simpleCounter.getCounter(); | ||
|
||
console.log('counter after increasing', counterAfter); | ||
console.log("counter after increasing", counterAfter); | ||
|
||
expect(counterAfter).toBe(counterBefore + increaseBy); | ||
} | ||
}); | ||
expect(counterAfter).toBe(counterBefore + increaseBy); | ||
} | ||
}); | ||
}); |