Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

priance!!!!!!!! #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions smartcontract/index1.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Import and instance of the reach stdlib
import { loadStdlib } from '@reach-sh/stdlib';
// The following line is crucial to connect to the backend
import * as backend from './build/index.main.mjs';

const N = 3;
const names = ["Creator", "Alice", "Bob", "Carla"];

(async () => {
const stdlib = await loadStdlib(process.env);
// Each participant is given a balance of 10 algos. Parse the native currency and return a balance
const startingBalance = stdlib.parseCurrency(10);
const [accCreator, ...accBidders] =
await stdlib.newTestAccounts(1 + N, startingBalance);
// Create an NFT.
const theNFT = await stdlib.launchToken(accCreator, "beepboop", "NFT", { supply: 1 });

await Promise.all([accCreator, ...accBidders].map(async (acc, i) => {
acc.setDebugLabel(names[i]);
}));
// Deploy the contract
const ctcCreator = accCreator.contract(backend);
const showBalance = async (acc, i) => {
const amt = await stdlib.balanceOf(acc);
const amtNFT = await stdlib.balanceOf(acc, theNFT.id);
console.log(`${names[i]} has ${stdlib.formatCurrency(amt)} ${stdlib.standardUnit} and ${amtNFT} of the NFT`);
};

await Promise.all([
(async () => {
await showBalance(accCreator, 0);
const n = names[0];
await backend.Creator(ctcCreator, {
getSale: () => {
console.log(`${n} sets parameters of sale`);
return [theNFT.id, stdlib.parseCurrency(2), 30]
},
seeBid: (who, bid) => {
console.log(`${n} saw that ${stdlib.formatAddress(who)} bid ${stdlib.formatCurrency(bid)}`);
},
timeout: () => {
console.log(`${n} observes the auction has hit the timeout`);
},
showOutcome: (winner) => {
console.log(`${n} saw that ${stdlib.formatAddress(winner)} won`);
},
});
await showBalance(accCreator, 0);
})(),
...accBidders.map(async (acc, i) => {
await showBalance(acc, i + 1);
const n = names[i + 1];
const ctc = acc.contract(backend, ctcCreator.getInfo());
const bid = stdlib.parseCurrency(Math.random() * 10);
let IWon = false;
console.log(`${n} decides to bid ${stdlib.formatCurrency(bid)}`);
await backend.Bidder(ctc, {
showOutcome: (winner) => {
console.log(`${n} saw that ${stdlib.formatAddress(winner)} won`);
IWon = stdlib.addressEq(winner, acc);
},
seeParams: async ([nftId, reservePrice, end]) => {
console.log(`${n} sees that the NFT is ${nftId}, the reserve price is ${stdlib.formatCurrency(reservePrice)}, and that they have until ${end} to bid`);
await acc.tokenAccept(nftId);
},
getBid: (currentPrice) => {
if (currentPrice.lt(bid)) {
console.log(`${n} bids ${stdlib.formatCurrency(bid)} against ${stdlib.formatCurrency(currentPrice)}`);
return ['Some', bid];
} else {
console.log(`${n} does not bid because ${stdlib.formatCurrency(currentPrice)} is too high`);
return ['None', null];
}
},
});
await showBalance(acc, i + 1);
if (!IWon) {
await theNFT.optOut(acc);
}
return;
},
)]);
})();
34 changes: 34 additions & 0 deletions smartcontract/index1.rsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'reach 0.1';

export const main = Reach.App(() => {
const Creator = Participant('Creator', {
getnft: Fun([], Token),
nftprice: UInt
})
const Buyer = Participant('Buyer', {
payprice: Fun([UInt], Null)
})

init()

Creator.only(() => {
const nftId = declassify(interact.getnft())
const nftPrice = declassify(interact.nftprice)
})
Creator.publish(nftId, nftPrice)
const amt = 1;
commit();
Creator.pay([[amt, nftId]])

Buyer.only(() => {
interact.payprice(nftPrice)
})
commit()
Buyer.pay(nftPrice)

transfer(nftPrice).to(Creator)
transfer(amt, nftId).to(Buyer)

commit()

});