dApp is Waves account assigned with dApp script created for using Smart-contracts in WAVES. dApp script is a Ride script that contains callable functions that can be called externally by the Waves transactions
Say "Hello World" in WAVES Network
Each Ride script should start with directives. These directives tell the compiler that:
{-# STDLIB_VERSION 6 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
Callable function can be called externally by the invoke script transaction
Callable function result is a set of script actions that are performed on the blockchain
@Callable(i)
func helloWorld(name: String) = {
[
StringEntry("answer", "Hello World! Hello, " + name)
]
}
It s much easier than you imagine. Say "Hello World" in WAVES Network Insert this script into your project
{-# STDLIB_VERSION 6 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
@Callable(i)
func helloWorld(name: String) = {
[
StringEntry("answer", "Hello World! Hello, " + name)
]
}
Deploy your script into Waves Network
During deployment you'll need to create your account if you still haven't it
// Run test with help of Surfboard
const WAVES = 10 ** 8;
const FEE = 0.001 * WAVES;
const ADD_FEE = 0.004 * WAVES;
describe('HelloWorld dApp test suite', async function () {
this.timeout(100000);
// In the before section, we perform all the actions that we need to test the dApp and certain number of accounts with the specified balance is generated
before(async function () {
await setupAccounts({
dApp: 10 * WAVES,
});
//Deployment is carried out by the transaction of installing the script on the account
const dAppScript = compile(file('dApp.ride'));
const ssDAppTx = setScript({ script: dAppScript }, accounts.dApp);
await broadcast(ssDAppTx);
await waitForTx(ssDAppTx.id);
});
//Each section of it() serves to test some functionality
it('Should say hello', async function () {
const name = "Alex";
const iInitTx = invokeScript({
fee: FEE + ADD_FEE,
dApp: address(accounts.dApp),
call: {
function: "helloWorld",
args: [
{ type: 'string', value: name },
]
},
}, accounts.dApp);
await broadcast(iInitTx);
await waitForTx(iInitTx.id);
//Call of the script function is carried out by the invokeScript transaction
const result = await accountDataByKey("answer", address(accounts.dApp))
expect(result.value).to.be.equal("Hello World! Hello, " + name);
});
})
//In the end, we read the account data by key and check the correctness of the saved data
const result = await accountDataByKey("answer", address(accounts.dApp))
expect(result.value).to.be.equal("Hello World! Hello, " + name);
});
})
//An example of a function call from dApp
import com.wavesplatform.crypto.Crypto;
import com.wavesplatform.transactions.InvokeScriptTransaction;
import com.wavesplatform.transactions.SetScriptTransaction;
import com.wavesplatform.transactions.TransferTransaction;
import com.wavesplatform.transactions.account.PrivateKey;
import com.wavesplatform.transactions.common.Amount;
import com.wavesplatform.transactions.common.Base64String;
import com.wavesplatform.transactions.data.DataEntry;
import com.wavesplatform.transactions.invocation.Function;
import com.wavesplatform.transactions.invocation.StringArg;
import com.wavesplatform.wavesj.Node;
import com.wavesplatform.wavesj.Profile;
import com.wavesplatform.wavesj.exceptions.NodeException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.web3j.utils.Files;
import java.io.File;
import java.io.IOException;
public class DappTest {
static PrivateKey dApp;
static Node node;
static Base64String dAppScript;
// additional info can be found - https://hub.docker.com/r/wavesplatform/waves-private-node
static PrivateKey faucet = PrivateKey.fromSeed("waves private node seed with waves tokens");
// In "Before section", we perform all the actions that we need to test the dApp
@BeforeClass
public static void before() throws NodeException, IOException {
node = new Node(Profile.LOCAL);
// Certain number of accounts with the specified balance is generated
dApp = PrivateKey.fromSeed(Crypto.getRandomSeedBytes());
TransferTransaction transfer = TransferTransaction.builder(
dApp.address(), Amount.of(1_00_000_000)
).getSignedWith(faucet);
node.broadcast(transfer);
node.waitForTransaction(transfer.id());
// Deployment is carried out by the transaction of installing the script on the account
String source = Files.readString(new File("src/test/resources/dApp.ride"));
dAppScript = node.compileScript(source).script();
SetScriptTransaction ssTx = SetScriptTransaction.builder(dAppScript).getSignedWith(dApp);
node.broadcast(ssTx);
node.waitForTransaction(ssTx.id());
}
@Test
public void should_say_hello() throws NodeException, IOException {
String name = "Alex";
// Call of the script function is carried out by the invokeScript transaction
InvokeScriptTransaction iInitTx = InvokeScriptTransaction.builder(
dApp.address(),
Function.as("helloWorld", StringArg.as(name))
).getSignedWith(dApp);
node.broadcast(iInitTx);
node.waitForTransaction(iInitTx.id());
// At the end, some checks are usually made.
// In this case, we read the account data by key and check the correctness of the saved data
DataEntry result = node.getData(dApp.address(), "answer");
Assert.assertEquals(result.valueAsObject(), "Hello World! Hello, " + name);
}
}
You've been created your first dApp successfully. It has been published on the Waves.