Skip to content

Commit

Permalink
Merge pull request #849 from iotaledger/wasmvm
Browse files Browse the repository at this point in the history
Wasmvm
  • Loading branch information
BugFreeSoftware authored Mar 1, 2022
2 parents b30cb28 + d5d722a commit 9176315
Show file tree
Hide file tree
Showing 181 changed files with 1,290 additions and 835 deletions.
2 changes: 1 addition & 1 deletion contracts/wasm/dividend/src/dividend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub fn func_divide(ctx: &ScFuncContext, f: &DivideContext) {
// member address. The transfer_to_address() method receives the address
// value and the proxy to the new transfers map on the host, and will
// call the corresponding host sandbox function with these values.
ctx.transfer_to_address(&address, transfers);
ctx.send(&address, &transfers);
}
}
}
Expand Down
Binary file modified contracts/wasm/dividend/test/dividend_bg.wasm
Binary file not shown.
11 changes: 5 additions & 6 deletions contracts/wasm/dividend/test/dividend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package test

import (
"strings"
"testing"

"github.com/iotaledger/wasp/contracts/wasm/dividend/go/dividend"
Expand All @@ -17,7 +16,7 @@ func dividendMember(ctx *wasmsolo.SoloContext, agent *wasmsolo.SoloAgent, factor
member := dividend.ScFuncs.Member(ctx)
member.Params.Address().SetValue(agent.ScAddress())
member.Params.Factor().SetValue(factor)
member.Func.TransferIotas(1).Post()
member.Func.Post()
}

func dividendDivide(ctx *wasmsolo.SoloContext, amount uint64) {
Expand Down Expand Up @@ -51,9 +50,9 @@ func TestAddMemberFailMissingAddress(t *testing.T) {

member := dividend.ScFuncs.Member(ctx)
member.Params.Factor().SetValue(100)
member.Func.TransferIotas(1).Post()
member.Func.Post()
require.Error(t, ctx.Err)
require.True(t, strings.HasSuffix(ctx.Err.Error(), "missing mandatory address"))
require.Contains(t, ctx.Err.Error(), "missing mandatory address")
}

func TestAddMemberFailMissingFactor(t *testing.T) {
Expand All @@ -62,9 +61,9 @@ func TestAddMemberFailMissingFactor(t *testing.T) {
member1 := ctx.NewSoloAgent()
member := dividend.ScFuncs.Member(ctx)
member.Params.Address().SetValue(member1.ScAddress())
member.Func.TransferIotas(1).Post()
member.Func.Post()
require.Error(t, ctx.Err)
require.True(t, strings.HasSuffix(ctx.Err.Error(), "missing mandatory factor"))
require.Contains(t, ctx.Err.Error(), "missing mandatory factor")
}

func TestDivide1Member(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

func funcDonate(ctx wasmlib.ScFuncContext, f *DonateContext) {
amount := ctx.IncomingTransfer().Balance(wasmtypes.IOTA)
donation := &Donation{
Amount: ctx.IncomingTransfer().Balance(wasmtypes.IOTA),
Amount: amount,
Donator: ctx.Caller(),
Error: "",
Feedback: f.Params.Feedback().Value(),
Expand Down
7 changes: 4 additions & 3 deletions contracts/wasm/donatewithfeedback/src/donatewithfeedback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use crate::*;
use crate::structs::*;

pub fn func_donate(ctx: &ScFuncContext, f: &DonateContext) {
let amount = ctx.incoming().balance(&ScColor::IOTA);
let mut donation = Donation {
amount: ctx.incoming().balance(&ScColor::IOTA),
amount: amount,
donator: ctx.caller(),
error: String::new(),
feedback: f.params.feedback().value(),
Expand All @@ -17,7 +18,7 @@ pub fn func_donate(ctx: &ScFuncContext, f: &DonateContext) {
if donation.amount == 0 || donation.feedback.len() == 0 {
donation.error = "error: empty feedback or donated amount = 0".to_string();
if donation.amount > 0 {
ctx.transfer_to_address(&donation.donator.address(), ScTransfers::iotas(donation.amount));
ctx.send(&donation.donator.address(), &ScTransfers::iotas(donation.amount));
donation.amount = 0;
}
}
Expand All @@ -44,7 +45,7 @@ pub fn func_withdraw(ctx: &ScFuncContext, f: &WithdrawContext) {
}

let sc_creator = ctx.contract_creator().address();
ctx.transfer_to_address(&sc_creator, ScTransfers::iotas(amount));
ctx.send(&sc_creator, &ScTransfers::iotas(amount));
}

pub fn view_donation(_ctx: &ScViewContext, f: &DonationContext) {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import * as wasmtypes from "wasmlib/wasmtypes"
import * as sc from "./index";

export function funcDonate(ctx: wasmlib.ScFuncContext, f: sc.DonateContext): void {
const amount = ctx.incoming().balance(wasmtypes.IOTA);
let donation = new sc.Donation();
donation.amount = ctx.incoming().balance(wasmtypes.IOTA);
donation.amount = amount;
donation.donator = ctx.caller();
donation.error = "";
donation.feedback = f.params.feedback().value();
donation.timestamp = ctx.timestamp();
if (donation.amount == 0 || donation.feedback.length == 0) {
donation.error = "error: empty feedback or donated amount = 0".toString();
if (donation.amount > 0) {
ctx.transferToAddress(donation.donator.address(), wasmlib.ScTransfers.iotas(donation.amount));
ctx.send(donation.donator.address(), wasmlib.ScTransfers.iotas(donation.amount));
donation.amount = 0;
}
}
Expand All @@ -42,7 +43,7 @@ export function funcWithdraw(ctx: wasmlib.ScFuncContext, f: sc.WithdrawContext):
}

let scCreator = ctx.contractCreator().address();
ctx.transferToAddress(scCreator, wasmlib.ScTransfers.iotas(amount));
ctx.send(scCreator, wasmlib.ScTransfers.iotas(amount));
}

export function viewDonation(ctx: wasmlib.ScViewContext, f: sc.DonationContext): void {
Expand Down
Binary file modified contracts/wasm/erc20/test/erc20_bg.wasm
Binary file not shown.
6 changes: 3 additions & 3 deletions contracts/wasm/erc20/test/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ func approve(ctx *wasmsolo.SoloContext, from, to *wasmsolo.SoloAgent, amount uin
appr := erc20.ScFuncs.Approve(ctx.Sign(from))
appr.Params.Delegation().SetValue(to.ScAgentID())
appr.Params.Amount().SetValue(amount)
appr.Func.TransferIotas(1).Post()
appr.Func.Post()
return ctx.Err
}

func transfer(ctx *wasmsolo.SoloContext, from, to *wasmsolo.SoloAgent, amount uint64) error {
tx := erc20.ScFuncs.Transfer(ctx.Sign(from))
tx.Params.Account().SetValue(to.ScAgentID())
tx.Params.Amount().SetValue(amount)
tx.Func.TransferIotas(1).Post()
tx.Func.Post()
return ctx.Err
}

Expand All @@ -85,7 +85,7 @@ func transferFrom(ctx *wasmsolo.SoloContext, delegate, from, to *wasmsolo.SoloAg
tx.Params.Account().SetValue(from.ScAgentID())
tx.Params.Recipient().SetValue(to.ScAgentID())
tx.Params.Amount().SetValue(amount)
tx.Func.TransferIotas(1).Post()
tx.Func.Post()
return ctx.Err
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/wasm/erc721/src/erc721.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn transfer(ctx: &ScFuncContext, state: &MutableErc721State, from: &ScAgentID, t
}

fn zero() -> ScAgentID {
ScAgentID::from_bytes(&[])
agent_id_from_bytes(&[])
}

/////////////////////////// SC FUNCS ////////////////////////////
Expand Down
Binary file modified contracts/wasm/erc721/test/erc721_bg.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions contracts/wasm/erc721/test/erc721_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func approve(ctx *wasmsolo.SoloContext, owner, approved *wasmsolo.SoloAgent, tok
f.Params.Approved().SetValue(approved.ScAgentID())
}
f.Params.TokenID().SetValue(tokenID)
f.Func.TransferIotas(1).Post()
f.Func.Post()
}

func getApproved(t *testing.T, ctx *wasmsolo.SoloContext, tokenID wasmtypes.ScHash) *wasmtypes.ScAgentID {
Expand Down Expand Up @@ -189,7 +189,7 @@ func isApprovedForAll(t *testing.T, ctx *wasmsolo.SoloContext, owner, friend *wa
func mint(ctx *wasmsolo.SoloContext, owner *wasmsolo.SoloAgent, tokenID wasmtypes.ScHash) {
f := erc721.ScFuncs.Mint(ctx.Sign(owner))
f.Params.TokenID().SetValue(tokenID)
f.Func.TransferIotas(1).Post()
f.Func.Post()
}

func ownerOf(t *testing.T, ctx *wasmsolo.SoloContext, tokenID wasmtypes.ScHash) wasmtypes.ScAgentID {
Expand All @@ -204,13 +204,13 @@ func setApprovalForAll(ctx *wasmsolo.SoloContext, owner, operator *wasmsolo.Solo
f := erc721.ScFuncs.SetApprovalForAll(ctx.Sign(owner))
f.Params.Operator().SetValue(operator.ScAgentID())
f.Params.Approval().SetValue(approval)
f.Func.TransferIotas(1).Post()
f.Func.Post()
}

func transferFrom(ctx *wasmsolo.SoloContext, sender, from, to *wasmsolo.SoloAgent, tokenID wasmtypes.ScHash) {
f := erc721.ScFuncs.TransferFrom(ctx.Sign(sender))
f.Params.From().SetValue(from.ScAgentID())
f.Params.To().SetValue(to.ScAgentID())
f.Params.TokenID().SetValue(tokenID)
f.Func.TransferIotas(1).Post()
f.Func.Post()
}
2 changes: 1 addition & 1 deletion contracts/wasm/fairauction/go/fairauction/fairauction.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func funcStartAuction(ctx wasmlib.ScFuncContext, f *StartAuctionContext) {

fa := ScFuncs.FinalizeAuction(ctx)
fa.Params.Color().SetValue(auction.Color)
fa.Func.Delay(duration * 60).TransferIotas(1).Post()
fa.Func.Delay(duration * 60).Post()
}

func viewGetInfo(ctx wasmlib.ScViewContext, f *GetInfoContext) {
Expand Down
8 changes: 4 additions & 4 deletions contracts/wasm/fairauction/src/fairauction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn func_start_auction(ctx: &ScFuncContext, f: &StartAuctionContext) {
description: description,
duration: duration,
highest_bid: 0,
highest_bidder: ScAgentID::from_bytes(&[0; 37]),
highest_bidder: agent_id_from_bytes(&[]),
minimum_bid: minimum_bid,
num_tokens: num_tokens,
owner_margin: owner_margin,
Expand All @@ -177,7 +177,7 @@ pub fn func_start_auction(ctx: &ScFuncContext, f: &StartAuctionContext) {

let fa = ScFuncs::finalize_auction(ctx);
fa.params.color().set_value(&auction.color);
fa.func.delay(duration * 60).transfer_iotas(1).post();
fa.func.delay(duration * 60).post();
}

pub fn view_get_info(ctx: &ScViewContext, f: &GetInfoContext) {
Expand Down Expand Up @@ -205,10 +205,10 @@ pub fn view_get_info(ctx: &ScViewContext, f: &GetInfoContext) {
fn transfer_tokens(ctx: &ScFuncContext, agent: &ScAgentID, color: &ScColor, amount: u64) {
if agent.is_address() {
// send back to original Tangle address
ctx.transfer_to_address(&agent.address(), ScTransfers::transfer(color, amount));
ctx.send(&agent.address(), &ScTransfers::transfer(color, amount));
return;
}

// TODO not an address, deposit into account on chain
ctx.transfer_to_address(&agent.address(), ScTransfers::transfer(color, amount));
ctx.send(&agent.address(), &ScTransfers::transfer(color, amount));
}
Binary file modified contracts/wasm/fairauction/test/fairauction_bg.wasm
Binary file not shown.
26 changes: 18 additions & 8 deletions contracts/wasm/fairauction/test/fairauction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,22 @@ func TestFaNoBids(t *testing.T) {

func TestFaOneBidTooLow(t *testing.T) {
ctx := startAuction(t)
chain := ctx.Chain

bidder := ctx.NewSoloAgent()
placeBid := fairauction.ScFuncs.PlaceBid(ctx.Sign(bidder))
placeBid.Params.Color().SetValue(tokenColor)
placeBid.Func.TransferIotas(100).Post()
require.Error(t, ctx.Err)

// TODO this should be a simple 1 request to wait for, but sometimes
// the finalizeAuction will have already been triggered (bug), so
// instead of waiting for that single finalizeAuction request we
// will (erroneously) wait for the inbuf and outbuf counts to equalize
info := ctx.Chain.MempoolInfo()

// wait for finalize_auction
chain.Env.AdvanceClockBy(61 * time.Minute)
require.True(t, ctx.WaitForPendingRequests(1))
ctx.AdvanceClockBy(61 * time.Minute)
require.True(t, ctx.WaitForPendingRequests(-info.InBufCounter))

getInfo := fairauction.ScFuncs.GetInfo(ctx)
getInfo.Params.Color().SetValue(tokenColor)
Expand All @@ -120,24 +125,29 @@ func TestFaOneBidTooLow(t *testing.T) {

func TestFaOneBid(t *testing.T) {
ctx := startAuction(t)
chain := ctx.Chain

bidder := ctx.NewSoloAgent()
placeBid := fairauction.ScFuncs.PlaceBid(ctx.Sign(bidder))
placeBid.Params.Color().SetValue(tokenColor)
placeBid.Func.TransferIotas(500).Post()
placeBid.Func.TransferIotas(5000).Post()
require.NoError(t, ctx.Err)

// TODO this should be a simple 1 request to wait for, but sometimes
// the finalizeAuction will have already been triggered (bug), so
// instead of waiting for that single finalizeAuction request we
// will (erroneously) wait for the inbuf and outbuf counts to equalize
info := ctx.Chain.MempoolInfo()

// wait for finalize_auction
chain.Env.AdvanceClockBy(61 * time.Minute)
require.True(t, ctx.WaitForPendingRequests(1))
ctx.AdvanceClockBy(61 * time.Minute)
require.True(t, ctx.WaitForPendingRequests(-info.InBufCounter))

getInfo := fairauction.ScFuncs.GetInfo(ctx)
getInfo.Params.Color().SetValue(tokenColor)
getInfo.Func.Call()

require.NoError(t, ctx.Err)
require.EqualValues(t, 1, getInfo.Results.Bidders().Value())
require.EqualValues(t, 500, getInfo.Results.HighestBid().Value())
require.EqualValues(t, 5000, getInfo.Results.HighestBid().Value())
require.Equal(t, bidder.ScAddress().AsAgentID(), getInfo.Results.HighestBidder().Value())
}
8 changes: 4 additions & 4 deletions contracts/wasm/fairauction/ts/fairauction/fairauction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function funcFinalizeAuction(ctx: wasmlib.ScFuncContext, f: sc.FinalizeAu
let size = bidderList.length();
for (let i: u32 = 0; i < size; i++) {
let loser = bidderList.getAgentID(i).value();
if (loser != auction.highestBidder) {
if (!loser.equals(auction.highestBidder)) {
let bid = bids.getBid(loser).value();
transferTokens(ctx, loser, wasmtypes.IOTA, bid.amount);
}
Expand Down Expand Up @@ -172,7 +172,7 @@ export function funcStartAuction(ctx: wasmlib.ScFuncContext, f: sc.StartAuctionC

let fa = sc.ScFuncs.finalizeAuction(ctx);
fa.params.color().setValue(auction.color);
fa.func.delay(duration * 60).transferIotas(1).post();
fa.func.delay(duration * 60).post();
}

export function viewGetInfo(ctx: wasmlib.ScViewContext, f: sc.GetInfoContext): void {
Expand Down Expand Up @@ -200,10 +200,10 @@ export function viewGetInfo(ctx: wasmlib.ScViewContext, f: sc.GetInfoContext): v
function transferTokens(ctx: wasmlib.ScFuncContext, agent: wasmlib.ScAgentID, color: wasmlib.ScColor, amount: i64): void {
if (agent.isAddress()) {
// send back to original Tangle address
ctx.transferToAddress(agent.address(), wasmlib.ScTransfers.transfer(color, amount));
ctx.send(agent.address(), wasmlib.ScTransfers.transfer(color, amount));
return;
}

// TODO not an address, deposit into account on chain
ctx.transferToAddress(agent.address(), wasmlib.ScTransfers.transfer(color, amount));
ctx.send(agent.address(), wasmlib.ScTransfers.transfer(color, amount));
}
6 changes: 3 additions & 3 deletions contracts/wasm/fairroulette/go/fairroulette/fairroulette.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func funcPlaceBet(ctx wasmlib.ScFuncContext, f *PlaceBetContext) {
// amount of seconds. This will lock in the playing period, during which more bets can
// be placed. Once the 'payWinners' function gets triggered by the ISCP it will gather
// all bets up to that moment as the ones to consider for determining the winner.
ScFuncs.PayWinners(ctx).Func.Delay(playPeriod).TransferIotas(1).Post()
ScFuncs.PayWinners(ctx).Func.Delay(playPeriod).Post()
}
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func funcPayWinners(ctx wasmlib.ScFuncContext, f *PayWinnersContext) {
// of the winner. The transfer_to_address() method receives the address value and
// the proxy to the new transfers map on the host, and will call the corresponding
// host sandbox function with these values.
ctx.TransferToAddress(bet.Better.Address(), transfers)
ctx.Send(bet.Better.Address(), transfers)
}

// Announce who got sent what as event.
Expand All @@ -227,7 +227,7 @@ func funcPayWinners(ctx wasmlib.ScFuncContext, f *PayWinnersContext) {
transfers := wasmlib.NewScTransferIotas(remainder)

// Send the remainder to the contract creator.
ctx.TransferToAddress(ctx.ContractCreator().Address(), transfers)
ctx.Send(ctx.ContractCreator().Address(), transfers)
}

// Set round status to 0, send out event to notify that the round has ended
Expand Down
9 changes: 3 additions & 6 deletions contracts/wasm/fairroulette/src/fairroulette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ pub fn func_place_bet(ctx: &ScFuncContext, f: &PlaceBetContext) {
// amount of seconds. This will lock in the playing period, during which more bets can
// be placed. Once the 'payWinners' function gets triggered by the ISCP it will gather all
// bets up to that moment as the ones to consider for determining the winner.
ScFuncs::pay_winners(ctx).func
.delay(play_period)
.transfer_iotas(1)
.post();
ScFuncs::pay_winners(ctx).func.delay(play_period).post();
}
}
}
Expand Down Expand Up @@ -215,7 +212,7 @@ pub fn func_pay_winners(ctx: &ScFuncContext, f: &PayWinnersContext) {
// of the winner. The transfer_to_address() method receives the address value and
// the proxy to the new transfers map on the host, and will call the corresponding
// host sandbox function with these values.
ctx.transfer_to_address(&bet.better.address(), transfers);
ctx.send(&bet.better.address(), &transfers);
}

// Announce who got sent what as event.
Expand All @@ -230,7 +227,7 @@ pub fn func_pay_winners(ctx: &ScFuncContext, f: &PayWinnersContext) {
let transfers: ScTransfers = ScTransfers::iotas(remainder);

// Send the remainder to the contract creator.
ctx.transfer_to_address(&ctx.contract_creator().address(), transfers);
ctx.send(&ctx.contract_creator().address(), &transfers);
}

// Set round status to 0, send out event to notify that the round has ended
Expand Down
Binary file modified contracts/wasm/fairroulette/test/fairroulette_bg.wasm
Binary file not shown.
11 changes: 9 additions & 2 deletions contracts/wasm/fairroulette/test/fairroulette_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ func TestBets(t *testing.T) {
better[i] = ctx.NewSoloAgent()
placeBet := fairroulette.ScFuncs.PlaceBet(ctx.Sign(better[i]))
placeBet.Params.Number().SetValue(3)
placeBet.Func.TransferIotas(25).Post()
placeBet.Func.TransferIotas(1234).Post()
require.NoError(t, ctx.Err)
}
// TODO this should be a simple 1 request to wait for, but sometimes
// the payout will have already been triggered (bug), so instead of
// waiting for that single payout request we will (erroneously) wait
// for the inbuf and outbuf counts to equalize
info := ctx.Chain.MempoolInfo()

// wait for finalize_auction
ctx.AdvanceClockBy(1201 * time.Second)
require.True(t, ctx.WaitForPendingRequests(1))
require.True(t, ctx.WaitForPendingRequests(-info.InBufCounter))
}
Loading

0 comments on commit 9176315

Please sign in to comment.