-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test:
initOf
for parent-child contracts (#520)
- Loading branch information
1 parent
7a7047a
commit 1be9e54
Showing
3 changed files
with
130 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
contract Self { | ||
|
||
init() { } | ||
|
||
receive() { } | ||
|
||
get fun testInitOfAddress(): Address { | ||
return contractAddress(initOf Self()); | ||
} | ||
|
||
get fun testMyAddress(): Address { | ||
return myAddress(); | ||
} | ||
} | ||
|
||
message ChildAddress { | ||
address: Address; | ||
} | ||
|
||
contract Child { | ||
|
||
owner: Address; | ||
|
||
init(owner: Address) { | ||
self.owner = owner; | ||
} | ||
|
||
receive() { | ||
send(SendParameters { | ||
to: sender(), | ||
value: 0, | ||
mode: SendRemainingValue | SendIgnoreErrors, | ||
bounce: false, | ||
body: ChildAddress { address: myAddress() }.toCell(), | ||
}); | ||
} | ||
} | ||
|
||
contract Parent { | ||
|
||
childMyAddress: Address; | ||
|
||
init() { | ||
self.childMyAddress = myAddress(); // dummy value to be replaced by the child | ||
} | ||
|
||
receive() { | ||
let ci = initOf Child(myAddress()); | ||
send(SendParameters { | ||
to: contractAddress(ci), | ||
value: 0, | ||
mode: SendRemainingValue | SendIgnoreErrors, | ||
bounce: false, | ||
body: beginCell().endCell(), | ||
code: ci.code, | ||
data: ci.data, | ||
}); | ||
} | ||
|
||
receive(msg: ChildAddress) { | ||
self.childMyAddress = msg.address; | ||
} | ||
|
||
get fun testInitOfAddressChild(): Address { | ||
return contractAddress(initOf Child(myAddress())); | ||
} | ||
|
||
get fun testMyAddressChild(): Address { | ||
return self.childMyAddress; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { toNano } from "@ton/core"; | ||
import { ContractSystem } from "@tact-lang/emulator"; | ||
import { __DANGER_resetNodeId } from "../../grammar/ast"; | ||
import { Self } from "./contracts/output/initof_Self"; | ||
import { Parent } from "./contracts/output/initof_Parent"; | ||
import { consoleLogger } from "../../logger"; | ||
|
||
describe("initOf", () => { | ||
beforeAll(() => { | ||
jest.spyOn(consoleLogger, "error").mockImplementation(() => {}); | ||
}); | ||
|
||
beforeEach(() => { | ||
__DANGER_resetNodeId(); | ||
}); | ||
|
||
afterAll(() => { | ||
(consoleLogger.error as jest.Mock).mockRestore(); | ||
}); | ||
|
||
afterEach(() => { | ||
(consoleLogger.error as jest.Mock).mockClear(); | ||
}); | ||
it("should implement initOf correctly - 1", async () => { | ||
// Init | ||
const system = await ContractSystem.create(); | ||
const treasure = system.treasure("treasure"); | ||
const contract = system.open(await Self.fromInit()); | ||
await contract.send(treasure, { value: toNano("10") }, null); | ||
await system.run(); | ||
|
||
const addr1 = (await contract.getTestInitOfAddress()).toRawString(); | ||
const addr2 = (await contract.getTestMyAddress()).toRawString(); | ||
expect(addr1).toEqual(addr2); | ||
}); | ||
it("should implement initOf correctly - 2", async () => { | ||
// Init | ||
const system = await ContractSystem.create(); | ||
const treasure = system.treasure("treasure"); | ||
const contract = system.open(await Parent.fromInit()); | ||
await contract.send(treasure, { value: toNano("10") }, null); | ||
await system.run(); | ||
const addrChildInitOf = ( | ||
await contract.getTestInitOfAddressChild() | ||
).toRawString(); | ||
const addrChildMyAddress = ( | ||
await contract.getTestMyAddressChild() | ||
).toRawString(); | ||
expect(addrChildInitOf).toEqual(addrChildMyAddress); | ||
}); | ||
}); |
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