agent-backend/contracts/BiddingExample/BiddingExample.yjs

80 lines
2.0 KiB
Plaintext
Raw Normal View History

2021-09-26 04:49:24 +00:00
contract BiddingExample{
function onCreate(arg) {
Global.owner = requester;
Global.bids = [];
Global.base_price = -1;
Global.winner = null;
Global.best_price = -1;
Global.second_price = -1;
}
export function getOwner(arg) {
return Global.owner;
}
export function bid(arg) {
var args = JSON.parse(arg);
for (var i = 0; i < Global.bids.length; i++) {
if (Global.bids[i]["bidder"] == requester) {
Global.bids[i] = {"bidder":requester, "price":args["price"]};
return "Success";
}
}
Global.bids.push({"bidder":requester, "price":args["price"]});
return "Success";
}
export function set_base_price(arg) {
if (requester != Global.owner) {
return "Access Denied";
}
var args = JSON.parse(arg);
Global.base_price = args["price"];
return "Success";
}
export function decideWinner(arg) {
if (requester != Global.owner) {
return "Access Denied";
}
for (var i = 0; i < Global.bids.length; i++) {
if (Global.bids[i]["price"] < Global.base_price) {
continue;
} else if (Global.bids[i]["price"] > Global.best_price) {
Global.second_price = Global.best_price;
Global.best_price = Global.bids[i]["price"];
Global.winner = Global.bids[i]["bidder"];
} else if (Global.bids[i]["price"] > Global.second_price){
Global.second_price = Global.bids[i]["price"];
}
}
return "Success";
}
@Cost({"countGas":true,"extraGas":"get_final_price"})
export function execute(arg) {
if (requester != Global.winner) {
return "Access Denied";
}
Global.bids = [];
Global.winner = null;
Global.best_price = -1;
Global.second_price = -1;
a=1+1;
return a;
}
export function get_final_price() {
if (requester != Global.winner) {
return 0;
} else {
if (Global.second_price == -1) {
return Global.base_price
} else {
return Global.second_price;
}
}
}
}