tests: example rust contracts moved from warp-wasm-templates

This commit is contained in:
robal
2023-03-17 17:55:17 +01:00
parent c1d8fb7a52
commit dbe52e82cd
75 changed files with 17993 additions and 1 deletions

View File

@@ -0,0 +1,9 @@
const { deploy } = require('../scripts/deploy');
deploy(
'localhost',
1984,
'http',
'local',
'deploy/local/wallet_local.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { interactBalance } = require('../scripts/interact-balance');
interactBalance(
'localhost',
1984,
'http',
'local',
'deploy/local/wallet_local.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { interactTransfer } = require('../scripts/interact-transfer');
interactTransfer(
'localhost',
1984,
'http',
'local',
'deploy/local/wallet_local.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { readContractState } = require('../scripts/read-contract-state');
readContractState(
'localhost',
1984,
'http',
'local',
'deploy/local/wallet_local.json'
).finally();

View File

@@ -0,0 +1 @@
tom87dg3RgkUGTfOwIO-eNb5WCvoGkFCS44B7koVW_I

View File

@@ -0,0 +1,9 @@
const { deploy } = require('../scripts/deploy');
deploy(
'arweave.net',
443,
'https',
'mainnet',
'deploy/mainnet/wallet_mainnet.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { interactBalance } = require('../scripts/interact-balance');
interactBalance(
'arweave.net',
443,
'https',
'mainnet',
'deploy/mainnet/wallet_mainnet.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { interactTransfer } = require('../scripts/interact-transfer');
interactTransfer(
'arweave.net',
443,
'https',
'mainnet',
'deploy/mainnet/wallet_mainnet.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { readContractState } = require('../scripts/read-contract-state');
readContractState(
'arweave.net',
443,
'https',
'mainnet',
'deploy/mainnet/wallet_mainnet.json'
).finally();

View File

@@ -0,0 +1,56 @@
const fs = require('fs');
const path = require('path');
const { WarpFactory, defaultCacheOptions } = require('warp-contracts');
const { mineBlock } = require('./utils/mine-block');
const { loadWallet, walletAddress } = require('./utils/load-wallet');
const { connectArweave } = require('./utils/connect-arweave');
module.exports.deploy = async function (host, port, protocol, target, walletJwk) {
const arweave = connectArweave(host, port, protocol);
const warp = module.exports.getWarpInstance(port, target);
const wallet = await loadWallet(arweave, walletJwk, target);
const walletAddr = await walletAddress(arweave, wallet);
const contractSrc = fs.readFileSync(path.join(__dirname, '../../contract/implementation/pkg/rust-contract_bg.wasm'));
const stateFromFile = JSON.parse(fs.readFileSync(path.join(__dirname, '../state/init-state.json'), 'utf-8'));
const initialState = {
...stateFromFile,
...{
owner: walletAddr,
balances: {
...stateFromFile.balances,
[walletAddr]: 10000000,
},
},
};
const {contractTxId} = await warp.createContract.deploy(
{
wallet,
initState: JSON.stringify(initialState),
src: contractSrc,
wasmSrcCodeDir: path.join(__dirname, '../../contract/implementation/src'),
wasmGlueCode: path.join(__dirname, '../../contract/implementation/pkg/rust-contract.js'),
}
);
fs.writeFileSync(path.join(__dirname, `../${target}/contract-tx-id.txt`), contractTxId);
if (target == 'testnet' || target == 'local') {
await mineBlock(arweave);
}
if (target == 'testnet') {
console.log(`Check contract at https://sonar.warp.cc/#/app/contract/${contractTxId}?network=testnet`);
} else {
console.log('Contract tx id', contractTxId);
}
};
module.exports.getWarpInstance = function (port, target) {
if (target == 'local') {
return WarpFactory.forLocal(port);
} else if (target == 'testnet') {
return WarpFactory.forTestnet();
} else {
return WarpFactory.forMainnet({ ...defaultCacheOptions, inMemory: true });
}
}

View File

@@ -0,0 +1,23 @@
const { loadWallet } = require('./utils/load-wallet');
const { connectArweave } = require('./utils/connect-arweave');
const { connectPstContract } = require('./utils/connect-pst-contract');
const { contractTxId } = require('./utils/contract-tx-id');
module.exports.interactBalance = async function (
host,
port,
protocol,
target,
walletJwk
) {
const arweave = connectArweave(host, port, protocol);
const wallet = await loadWallet(arweave, walletJwk, target, true);
const walletAddress = await arweave.wallets.jwkToAddress(wallet);
const txId = contractTxId(target);
const pst = await connectPstContract(arweave, wallet, txId, target);
const balance = await pst.currentBalance(walletAddress);
console.log(balance);
};

View File

@@ -0,0 +1,29 @@
const { loadWallet } = require('./utils/load-wallet');
const { connectArweave } = require('./utils/connect-arweave');
const { connectPstContract } = require('./utils/connect-pst-contract');
const { contractTxId } = require('./utils/contract-tx-id');
const { mineBlock } = require('./utils/mine-block');
module.exports.interactTransfer = async function (host, port, protocol, target, walletJwk) {
const arweave = connectArweave(host, port, protocol);
const wallet = await loadWallet(arweave, walletJwk, target, true);
const txId = contractTxId(target);
const pst = await connectPstContract(arweave, wallet, txId, target);
const transferId = await pst.transfer({
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 555,
});
await mineBlock(arweave);
const state = await pst.currentState();
console.log('Updated state:', state);
console.log('Contract tx id', txId);
if (target == 'testnet') {
console.log(`Check transfer interaction at https://sonar.warp.cc/#/app/interaction/${transferId}?network=testnet`);
} else {
console.log('Transfer tx id', transferId);
}
};

View File

@@ -0,0 +1,22 @@
const { loadWallet } = require('./utils/load-wallet');
const { connectArweave } = require('./utils/connect-arweave');
const { connectContract } = require('./utils/connect-contract');
const { contractTxId } = require(`./utils/contract-tx-id`);
module.exports.readContractState = async function (
host,
port,
protocol,
target,
walletJwk
) {
const arweave = connectArweave(host, port, protocol);
const wallet = await loadWallet(arweave, walletJwk, target, true);
const txId = contractTxId(target);
const contract = await connectContract(arweave, wallet, txId, target);
const { cachedValue } = await contract.readState();
console.log('Current state:', cachedValue.state);
console.log('Contract tx id', txId);
};

View File

@@ -0,0 +1,4 @@
module.exports.addFunds = async function (arweave, wallet) {
const walletAddress = await arweave.wallets.getAddress(wallet);
await arweave.api.get(`/mint/${walletAddress}/1000000000000000`);
};

View File

@@ -0,0 +1,9 @@
const Arweave = require('arweave');
module.exports.connectArweave = function (host, port, protocol) {
return Arweave.init({
host: host,
port: port,
protocol: protocol,
});
};

View File

@@ -0,0 +1,15 @@
const { getWarpInstance } = require("../deploy");
module.exports.connectContract = async function (
arweave,
wallet,
contractTxId,
target
) {
console.log('Target:', target);
const warp = getWarpInstance(arweave.api.config.port, target);
return warp
.contract(contractTxId)
.connect(wallet);
};

View File

@@ -0,0 +1,14 @@
const { getWarpInstance } = require("../deploy");
module.exports.connectPstContract = async function (
arweave,
wallet,
contractTxId,
target
) {
const warp = getWarpInstance(arweave.api.config.port, target);
return warp
.pst(contractTxId)
.connect(wallet);
};

View File

@@ -0,0 +1,19 @@
const fs = require('fs');
const path = require('path');
module.exports.contractTxId = function (target) {
let txId;
try {
txId = fs
.readFileSync(
path.join(__dirname, `../../../deploy/${target}/contract-tx-id.txt`),
'utf-8'
)
.trim();
} catch (e) {
throw new Error(
'Contract tx id file not found! Please run deploy script first.'
);
}
return txId;
};

View File

@@ -0,0 +1,10 @@
const fs = require('fs');
const path = require('path');
module.exports.generateWallet = async function (arweave, target) {
const wallet = await arweave.wallets.generate();
fs.writeFileSync(
path.join(__dirname, `../../${target}/wallet_${target}.json`),
JSON.stringify(wallet)
);
};

View File

@@ -0,0 +1,33 @@
const fs = require('fs');
const { addFunds } = require('./addFunds');
const { generateWallet } = require('./create-testnet-wallet');
const path = require('path');
module.exports.loadWallet = async function (
arweave,
walletJwk,
target,
generated
) {
let wallet;
if (!generated) {
await generateWallet(arweave, target);
}
try {
wallet = JSON.parse(fs.readFileSync(path.join(walletJwk), 'utf-8'));
} catch (e) {
throw new Error('Wallet file not found! Please run deploy script first.');
}
if (target == 'testnet' || target == 'local') {
await addFunds(arweave, wallet);
}
return wallet;
};
module.exports.walletAddress = async function (arweave, wallet) {
return arweave.wallets.getAddress(wallet);
};

View File

@@ -0,0 +1,3 @@
module.exports.mineBlock = async function (arweave) {
await arweave.api.get('mine');
};

View File

@@ -0,0 +1,7 @@
{
"ticker": "PST TMPL RUST",
"name": "PST Template Rust",
"balances": {},
"canEvolve": true,
"evolve": null
}

View File

@@ -0,0 +1 @@
Y8sad_he0w0ryzxKvS7PGEXQbxBl142KX5OTpiPDYV0

View File

@@ -0,0 +1,9 @@
const { deploy } = require('../scripts/deploy');
deploy(
'arweave.net',
443,
'https',
'testnet',
'deploy/testnet/wallet_testnet.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { interactBalance } = require('../scripts/interact-balance');
interactBalance(
'testnet.redstone.tools',
443,
'https',
'testnet',
'deploy/testnet/wallet_testnet.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { interactTransfer } = require('../scripts/interact-transfer');
interactTransfer(
'testnet.redstone.tools',
443,
'https',
'testnet',
'deploy/testnet/wallet_testnet.json'
).finally();

View File

@@ -0,0 +1,9 @@
const { readContractState } = require('../scripts/read-contract-state');
readContractState(
'testnet.redstone.tools',
443,
'https',
'testnet',
'deploy/testnet/wallet_testnet.json'
).finally();

View File

@@ -0,0 +1 @@
{"kty":"RSA","n":"zvtUbqKK76RcA1nKTmOjcZgkqLEOw40mLEmWA_Q_TgBYN4NJcZHPfWehS2Vlbalhf6ZiNu_J0Q-zk3tnk12Fkt-UDpje0uxkkigihwxOGmpB2gi6yTRpQfFg4ccG3QY9nx-R2hEOJaY056dXm9UY4gFVI_dYvqZbPt0_N8FmUgNVq9OgtkUVY07PAZA55kiqh2POWycsbv3FPgPt1D7QDj7jDuqRKBfMdHPqZF9qBiEo5kIyAPm8QCbQ0DY-pIjEMY0ENL4b2yTGRQNz-WH0wIAddGFPo9oGma8RdU5Uk82IJWpXxFF2Z4BOqVQnBBnIxXZNJpAN2WQ6xmGDtwRVBnV0l4nfzzpELEOZ98nlyY1YMBqBPqjnKP2_9M2FjdzsV8pnX5kUQE7_SjH0yAKEhRtYXcsvMF7Hy61rAUjD1RgIxqx-aJeJHGugc9I5IpobocX5dzdoRujSOIg_jTT7LzzP6BE2CK71crq-5cYicGje-DkwBoeRxJaRIZbAkp0AOsg1yQnURm3oGZaRw3qoqmcFp1eku5_HVORZJBvG5u2KSxa8giO6sNC0RN1NyD-RsnNLZ82eedpO8sn4rV_9LlTDqFcHgCuoOjt5A8GDYdJN5v6RljqHAcUWIwPC_rC9b4y80WO1vmND1P6j2uUj2wMKKQbOHVphyq2IZTS3skM","e":"AQAB","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