Skip to content

Commit

Permalink
Add order price config code
Browse files Browse the repository at this point in the history
Signed-off-by: KayleCoder <[email protected]>
  • Loading branch information
KayleCoder committed Aug 1, 2024
1 parent 05d5150 commit 8c818c3
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
TON_BAG_ADDRESS=EQBOOMNqG0rvNm6vFGfR4qZl48BTDw_gYefVI4DQ70t9GoPC
TASK_PROVIDER_MNEMONIC=xxx ...
TON_FILE_DOWNLOAD_PATH=downloads
TON_FILE_MOUNT_PATH=/root
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@ton/crypto": "^3.2.0",
"@ton/ton": "^13.11.2",
"axios": "^1.7.2",
"bignumber.js": "^9.1.2",
"dotenv": "^16.4.5",
"js-sha256": "^0.11.0",
"sequelize": "^6.35.2",
Expand Down
3 changes: 2 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const configs = {
},
task: {
minReward: BigInt(getEnvOrExit("TASK_MIN_REWARD", "0", false)),
maxFileSize: BigInt(getEnvOrExit("TASK_MAX_FILE_SIZE", "10485760", false)),
maxFileSize: BigInt(getEnvOrExit("TASK_MAX_FILE_SIZE", "1073741824", false)),
orderMinPrice: BigInt(getEnvOrExit("TASK_MIN_PRICE_G_D", "0", false)),
providerMnemonic: getEnvOrExit("TASK_PROVIDER_MNEMONIC", ""),
providerMinBalance: getEnvOrExit("TASK_PROVIDER_MIN_BALANCE", "1000000000"),
submitStorageProofBefore: getEnvOrExit("TASK_SUBMIT_STORAGE_PROOF_BEFORE", "1800"),
Expand Down
18 changes: 18 additions & 0 deletions src/dao/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {sequelize} from "../db";
import {Order} from "./order";
import {Config} from "./config";
import {Task} from "./task";
import {Transaction} from "./transaction";

const models: any[] = [Order, Config, Task, Transaction]

export async function initDb() {
await sequelize.sync();
for (const model of models) {
await model.model.sync({alter: true});
}
}

export async function disconnect() {
await sequelize.close();
}
7 changes: 6 additions & 1 deletion src/dao/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export class Order {
allowNull: false,
defaultValue: "{}"
},
order_price: {
type: DataTypes.DECIMAL(20, 6),
allowNull: false,
defaultValue: 0
},
order_state: {
type: DataTypes.INTEGER,
allowNull: false,
Expand All @@ -91,7 +96,7 @@ export class Order {
},
{
fields: ["order_state"]
}
},
]
}
)
Expand Down
6 changes: 0 additions & 6 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,4 @@ export const sequelize = new Sequelize({
});


export async function initDb() {
await sequelize.sync();
}

export async function disconnect() {
await sequelize.close();
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {initDb} from "./db";
import {logger} from "./util/logger";
import {initDb} from "./dao/common";
import {job} from "./service/job";

async function main() {
Expand Down
10 changes: 10 additions & 0 deletions src/service/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {sleep} from "../util/common";
import {sequelize} from "../db";
import {Task} from "../dao/task";
import {LAST_TASK_GENERATE_ORDER_ID} from "./constants";
import {configs} from "../config";

export async function generateTasks() {
while(true) {
Expand All @@ -20,6 +21,15 @@ export async function generateTasks() {
},
order_state: {
[Op.gte]: OrderState.not_started
},
file_size_in_bytes: {
[Op.lte]: configs.task.maxFileSize.toString()
},
total_rewards: {
[Op.gte]: configs.task.minReward.toString()
},
order_price: {
[Op.gte]: configs.task.orderMinPrice.toString()
}
},
limit: 10,
Expand Down
13 changes: 11 additions & 2 deletions src/wrapper/StorageContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
import {Address} from "@ton/ton";
import {
op_recycle_undistributed_storage_fees, op_unregister_as_storage_provider, op_submit_storage_proof,
op_register_as_storage_provider, op_claim_storage_rewards
op_register_as_storage_provider, op_claim_storage_rewards, ONE_TON, ONE_DAY, ONE_GIGA
} from './constants';
import {proofsIntoBody} from "./proofsutils";
import {TonProvider} from "../util/ton";
import {logger} from "../util/logger";
import {OrderState} from "../type/common";
import {now} from "../util/common";
import BigNumber from "bignumber.js";

export type StorageContractConfig = {};

Expand Down Expand Up @@ -287,13 +287,15 @@ export class StorageContract implements Contract {
const cell = cells[0];
const orderInfo = this.loadOrderInfo(cell);
const rewardsParams = this.loadRewardsParams(cell);
const orderPrice = this.loadOrderPrice(orderInfo.file_size_in_bytes, orderInfo.storage_period_in_sec, rewardsParams.total_rewards);
return {
address: this.address.toString(),
...orderInfo,
started: rewardsParams.started,
total_rewards: rewardsParams.total_rewards,
period_finish: rewardsParams.period_finish,
order_detail: JSON.stringify(rewardsParams),
order_price: orderPrice,
order_state: this.parseOrderState(rewardsParams.started, rewardsParams.period_finish)
};
}
Expand Down Expand Up @@ -350,6 +352,13 @@ export class StorageContract implements Contract {
}
}

loadOrderPrice(file_size_in_bytes: number, storage_period_in_sec: number, total_rewards: number): string {
const tonReward = new BigNumber(total_rewards).dividedBy(ONE_TON);
const periodInDay = new BigNumber(storage_period_in_sec).dividedBy(ONE_DAY);
const fileSizeInGb = new BigNumber(file_size_in_bytes).dividedBy(ONE_GIGA);
return tonReward.dividedBy(periodInDay).dividedBy(fileSizeInGb).toFixed(5, 1)
}

loadRewardsParams(cell: Cell): {
started: number;
total_storage_providers: number;
Expand Down
3 changes: 3 additions & 0 deletions src/wrapper/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const ONE_DAY = 24 * 60 * 60;
export const ONE_TON = 1000000000; //10^9
export const ONE_GIGA = 1024 * 1024 * 1024;
export const default_storage_period = 60n * 60n * 24n * 180n;
export const default_max_storage_proof_span = 60n * 60n * 24n;
export const default_max_storage_providers_per_order = 30n;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ base64-js@^1.3.1:
resolved "https://registry.npmmirror.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

bignumber.js@^9.1.2:
version "9.1.2"
resolved "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c"
integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==

bindings@^1.5.0:
version "1.5.0"
resolved "https://registry.npmmirror.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
Expand Down

0 comments on commit 8c818c3

Please sign in to comment.