generated from ton-community/twa-template
-
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.
new contract(leaderboardPage, getData, onChainRaceService, onChainRace)
- Loading branch information
Showing
4 changed files
with
176 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,147 @@ | ||
import { Address, beginCell, Cell, Contract, contractAddress, ContractProvider, Sender, SendMode } from '@ton/core'; | ||
import { | ||
Address, | ||
beginCell, | ||
Cell, | ||
Contract, | ||
contractAddress, | ||
ContractProvider, | ||
Sender, | ||
SendMode, | ||
toNano | ||
} from '@ton/core'; | ||
|
||
export type OnChainRaceConfig = {}; | ||
export type RaceEntry = { | ||
time: number; | ||
address: Address; | ||
}; | ||
|
||
export function onChainRaceConfigToCell(config: OnChainRaceConfig): Cell { | ||
return beginCell().endCell(); | ||
} | ||
export const Opcodes = { | ||
recordTime: 0x9fd3, | ||
distributePrize: 0xf8a7, | ||
withdrawFees: 0x0cab | ||
}; | ||
|
||
export class OnChainRace implements Contract { | ||
export class Race implements Contract { | ||
constructor(readonly address: Address, readonly init?: { code: Cell; data: Cell }) {} | ||
|
||
static createFromAddress(address: Address) { | ||
return new OnChainRace(address); | ||
return new Race(address); | ||
} | ||
|
||
static createFromConfig(config: OnChainRaceConfig, code: Cell, workchain = 0) { | ||
const data = onChainRaceConfigToCell(config); | ||
static createFromConfig(owner: Address, code: Cell, workchain = 0) { | ||
const data = Race.createDataCell(owner); | ||
const init = { code, data }; | ||
return new OnChainRace(contractAddress(workchain, init), init); | ||
return new Race(contractAddress(workchain, init), init); | ||
} | ||
|
||
static createDataCell(owner: Address): Cell { | ||
return beginCell() | ||
.storeAddress(owner) | ||
.storeUint(0, 64) // lastPayout | ||
.storeUint(0, 120) // totalFees | ||
.storeUint(0, 32) // currentTournamentNumber | ||
.storeUint(0, 64) // bestTime | ||
.storeAddress(Address.parse('0:0000000000000000000000000000000000000000000000000000000000000000')) // bestPlayer | ||
.storeDict(null) // playerEntries | ||
.endCell(); | ||
} | ||
|
||
async sendDeploy(provider: ContractProvider, via: Sender, value: bigint) { | ||
await provider.internal(via, { | ||
value, | ||
sendMode: SendMode.PAY_GAS_SEPARATELY, | ||
body: beginCell().endCell(), | ||
body: beginCell().storeUint(0x44, 32).endCell(), | ||
}); | ||
} | ||
|
||
async sendSetData(provider: ContractProvider, via: Sender, value: bigint, time: number, userAddress: Address) { | ||
console.log(`sendSetData called with time: ${time}, userAddress: ${userAddress.toString()}`); | ||
const body = beginCell() | ||
.storeUint(1, 32) // op code | ||
.storeUint(time, 64) | ||
.storeRef(beginCell().storeAddress(userAddress).endCell()) | ||
.endCell(); | ||
|
||
async sendRecordTime( | ||
provider: ContractProvider, | ||
via: Sender, | ||
opts: { | ||
time: number; | ||
value?: bigint; | ||
} | ||
) { | ||
await provider.internal(via, { | ||
value, | ||
body, | ||
value: opts.value ?? toNano('1.1'), | ||
sendMode: SendMode.PAY_GAS_SEPARATELY, | ||
body: beginCell() | ||
.storeUint(Opcodes.recordTime, 32) | ||
.storeUint(opts.time, 64) | ||
.endCell(), | ||
}); | ||
} | ||
|
||
|
||
async getRunTime(provider: ContractProvider): Promise<number> { | ||
const { stack } = await provider.get('get_run_time', []); // No arguments needed | ||
return Number(stack.readBigNumber()); // Convert BigNumber to number | ||
async sendDistributePrize( | ||
provider: ContractProvider, | ||
via: Sender, | ||
opts: { | ||
tournamentNumber: number; | ||
value?: bigint; | ||
} | ||
) { | ||
await provider.internal(via, { | ||
value: opts.value ?? toNano('0.1'), | ||
sendMode: SendMode.PAY_GAS_SEPARATELY, | ||
body: beginCell() | ||
.storeUint(Opcodes.distributePrize, 32) | ||
.storeUint(opts.tournamentNumber, 32) | ||
.endCell(), | ||
}); | ||
} | ||
|
||
async getUserAddress(provider: ContractProvider): Promise<Address> { | ||
const { stack } = await provider.get('get_user_address', []); // No arguments needed | ||
return stack.readAddress(); // Read Address from stack | ||
async sendWithdrawFees( | ||
provider: ContractProvider, | ||
via: Sender, | ||
opts: { | ||
value?: bigint; | ||
} | ||
) { | ||
await provider.internal(via, { | ||
value: opts.value ?? toNano('0.1'), | ||
sendMode: SendMode.PAY_GAS_SEPARATELY, | ||
body: beginCell() | ||
.storeUint(Opcodes.withdrawFees, 32) | ||
.endCell(), | ||
}); | ||
} | ||
|
||
async getInfo(provider: ContractProvider): Promise<{ | ||
bestTime: number; | ||
totalFees: bigint; | ||
currentTournamentNumber: number; | ||
bestPlayer: Address; | ||
}> { | ||
const result = await provider.get('get_info', []); | ||
return { | ||
bestTime: result.stack.readNumber(), | ||
totalFees: result.stack.readBigNumber(), | ||
currentTournamentNumber: result.stack.readNumber(), | ||
bestPlayer: result.stack.readAddress(), | ||
}; | ||
} | ||
|
||
async getPlayerEntry(provider: ContractProvider, address: Address): Promise<{ | ||
time: number; | ||
timestamp: number; | ||
tournamentNumber: number; | ||
} | null> { | ||
const result = await provider.get('get_player_entry', [ | ||
{ type: 'slice', cell: beginCell().storeAddress(address).endCell() }, | ||
]); | ||
|
||
const time = result.stack.readNumber(); | ||
const timestamp = result.stack.readNumber(); | ||
const tournamentNumber = result.stack.readNumber(); | ||
|
||
if (time === 0 && timestamp === 0 && tournamentNumber === 0) { | ||
return null; | ||
} | ||
|
||
return { | ||
time, | ||
timestamp, | ||
tournamentNumber, | ||
}; | ||
} | ||
} | ||
} |