hackethberlin/src/main/scala/fluence/MakeVyperApp.scala

158 lines
4.2 KiB
Scala
Raw Normal View History

2018-09-08 17:34:35 +02:00
package fluence
2018-09-07 19:46:53 +02:00
2018-09-08 17:34:35 +02:00
import hackethberlin._
import hackethberlin.types._
2018-09-07 19:46:53 +02:00
import shapeless._
2018-09-08 11:42:13 +02:00
import Decorator._
2018-09-09 08:45:56 +03:00
import cats.free.Free
2018-09-08 12:45:46 +02:00
import syntax.singleton._
2018-09-07 19:46:53 +02:00
object MakeVyperApp extends App {
2018-09-08 15:04:58 +02:00
val struct = ProductType(
2018-09-08 12:45:46 +02:00
('address ->> address) ::
('owner ->> address) ::
('size ->> uint256) ::
('time ->> int128) :: HNil
2018-09-07 19:46:53 +02:00
)
2018-09-08 15:04:58 +02:00
val data = ProductType(
2018-09-08 12:45:46 +02:00
('address ->> address) ::
('owner ->> `public`(address)) ::
('holders ->> (address ~>> bool)) ::
('structMap ->> `public`(uint256 ~>> struct)) ::
('struct ->> `public`(struct)) ::
('struct2 ->> struct) :: HNil
)
2018-09-08 12:03:18 +02:00
val func = FuncDef(
2018-09-08 11:42:13 +02:00
"myFunc",
2018-09-08 14:40:32 +02:00
('addr ->> address) :: HNil,
address
)(args args.ref('addr).toReturn)
2018-09-08 12:45:46 +02:00
2018-09-08 15:04:58 +02:00
val recordStruct = ProductType(
2018-09-08 12:45:46 +02:00
('record_address ->> address) :: ('other_some ->> uint256) :: HNil
)
2018-09-08 14:40:32 +02:00
println(Console.RED + recordStruct.ref('other_some) + Console.RESET)
2018-09-08 13:18:56 +02:00
2018-09-08 12:45:46 +02:00
println(recordStruct.toVyper)
2018-09-08 15:04:58 +02:00
println(data.toDataVyper)
2018-09-07 19:46:53 +02:00
2018-09-08 11:42:13 +02:00
println(func.toVyper)
val sumArgs = ProductType(('a ->> uint256) :: ('b ->> uint256) :: HNil)
import Expr.Defs._
2018-09-08 19:01:12 +03:00
val f = `@public` @:
sumArgs.funcDef("sum", uint256) { args
2018-09-08 19:25:38 +02:00
for {
c 'c :=: `++`(args.ref('a), args.ref('b))
2018-09-09 10:15:49 +03:00
_ Free.pure(`++`(args.ref('a), args.ref('b)))
2018-09-08 19:25:38 +02:00
d 'd :=: `++`(args.ref('b), c)
2018-09-09 08:05:04 +02:00
_ d :=: c
2018-09-08 19:25:38 +02:00
sum `++`(args.ref('a), d).toReturn
} yield sum
}
2018-09-08 19:01:12 +03:00
val all = recordStruct :: data :: func :: f :: HNil
val c = new Contract(recordStruct :: struct :: data :: func :: f :: HNil)
println(c.toVyper)
2018-09-08 11:42:13 +02:00
2018-09-08 19:20:15 +02:00
println(
func(recordStruct.ref('record_address) :: HNil).toVyper
)
2018-09-08 23:08:06 +02:00
// println(s"MMMMMACRO\n\n ${new MyContract("abc", 123).toAST.toVyper}")
2018-09-08 12:45:46 +02:00
}
2018-09-09 08:45:56 +03:00
2018-09-09 10:15:49 +03:00
object Auction extends App {
2018-09-09 09:29:58 +03:00
import Expr.Defs._
val predef = ProductType(
(Symbol("block.timestamp") ->> timestamp) ::
2018-09-09 10:15:49 +03:00
(Symbol("msg.value") ->> wei_value) ::
2018-09-09 09:29:58 +03:00
(Symbol("msg.sender") ->> address) ::
2018-09-09 10:15:49 +03:00
(Symbol("True") ->> bool) ::
2018-09-09 09:29:58 +03:00
HNil
)
val `block.timestamp` = predef.ref(Symbol("block.timestamp"))
val `msg.value` = predef.ref(Symbol("msg.value"))
val `msg.sender` = predef.ref(Symbol("msg.sender"))
2018-09-09 10:15:49 +03:00
val `True` = predef.ref('True)
2018-09-09 08:05:04 +02:00
2018-09-09 08:45:56 +03:00
val data = ProductType(
('beneficiary ->> public(address)) ::
('auction_start ->> public(timestamp)) ::
('auction_end ->> public(timestamp)) ::
('highest_bidder ->> `public`(address)) ::
('highest_bid ->> `public`(wei_value)) ::
('ended ->> public(bool)) :: HNil
)
val beneficiary = data.ref('beneficiary)
val auction_start = data.ref('auction_start)
val auction_end = data.ref('auction_end)
val highest_bid = data.ref('highest_bid)
2018-09-09 09:29:58 +03:00
val highest_bidder = data.ref('highest_bidder)
2018-09-09 10:15:49 +03:00
val ended = data.ref('ended)
2018-09-09 08:45:56 +03:00
val initArgs = ProductType(('_beneficiary ->> address) :: ('_bidding_time ->> timedelta) :: HNil)
val _beneficiary = initArgs.ref('_beneficiary)
val _bidding_time = initArgs.ref('_bidding_time)
val init = `@public` @: initArgs.funcDef(
"__init__",
Void
) { args
for {
2018-09-09 08:05:04 +02:00
_ <- Free.pure(Void)
_ <- beneficiary :=: _beneficiary
2018-09-09 09:29:58 +03:00
_ <- auction_start :=: `block.timestamp`
2018-09-09 10:15:49 +03:00
_ <- auction_end :=: `+:+`(auction_start, _bidding_time)
2018-09-09 08:45:56 +03:00
} yield Void
}
2018-09-09 09:29:58 +03:00
val bidIf: () Free[Expr, Void] = { () =>
for {
2018-09-09 10:15:49 +03:00
_ <- FuncDef.send(highest_bidder :: highest_bid :: HNil).liftF
2018-09-09 09:29:58 +03:00
} yield Void
}
2018-09-09 10:15:49 +03:00
val bid = `@public` @: `@payable` @: ProductType.hNil.funcDef(
2018-09-09 08:45:56 +03:00
"bid",
Void
) { args
for {
2018-09-09 10:15:49 +03:00
_ <- `assertt`(`<<`(`block.timestamp`, auction_end)).liftF
_ <- `assertt`(`>>`(`msg.value`, highest_bid)).liftF
_ <- `if`(`not`(`:===:`(highest_bid, `msg.value`)), bidIf).liftF
2018-09-09 09:29:58 +03:00
_ <- highest_bidder :=: `msg.sender`
_ <- highest_bid :=: `msg.value`
2018-09-09 08:45:56 +03:00
} yield Void
2018-09-09 10:15:49 +03:00
}
val end_auction = `@public` @: ProductType.hNil.funcDef(
"end_auction",
Void
) { args
for {
_ <- `assertt`(`>=`(`block.timestamp`, auction_end)).liftF
_ <- `assertt`(`not`(ended)).liftF
_ <- ended :=: `True`
_ <- FuncDef.send(beneficiary :: highest_bid :: HNil).liftF
} yield Void
}
val contract = new Contract(data :: init :: bid :: end_auction :: HNil)
println(contract.toVyper)
2018-09-09 08:05:04 +02:00
}