Skip to content

Instantly share code, notes, and snippets.

@anoochit
Last active December 17, 2022 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoochit/e3932fe393ff88b4b21d6e4d8ee56a28 to your computer and use it in GitHub Desktop.
Save anoochit/e3932fe393ff88b4b21d6e4d8ee56a28 to your computer and use it in GitHub Desktop.
loyalty card
import 'package:web3dart/web3dart.dart';
// Assume that the contract address and ABI are stored in variables
// called 'contractAddress' and 'contractAbi', respectively
// Create a new instance of the EthereumClient class
final client = Web3Client(rpcUrl, httpClient: httpClient);
// Load the contract using the contract address and ABI
final contract = DeployedContract(
ContractAbi.fromJson(contractAbi, 'LoyaltyCard'),
EthereumAddress.fromHex(contractAddress),
);
// Create a function call request to call the 'addPoints' function
final request = contract.function('addPoints').call(
// Pass the member's address and the number of points to add as arguments
[memberAddress, pointsToAdd],
);
// Send the request to the Ethereum network
final result = await client.sendTransaction(
Transaction.callContract(
contract: contract,
function: request,
// Set the maximum amount of gas to use
maxGas: 1000000,
),
// Sign the transaction using the private key of the account sending the transaction
credentials,
);
// Check the result of the transaction to see if it was successful
if (result.status == TransactionReceiptStatus.success) {
print('Transaction was successful');
} else {
print('Transaction failed');
}
pragma solidity ^0.7.0;
// Define the contract
contract LoyaltyCard {
// Define a struct to store the data for each member
struct Member {
string name;
uint points;
}
// Define a mapping to store the data for each member
mapping (address => Member) public members;
// Define a function to add points to a member's balance
function addPoints(address _member, uint _points) public {
// Retrieve the member's data from the mapping
Member storage member = members[_member];
// Add the points to the member's balance
member.points += _points;
}
// Define a function to redeem points for a reward
function redeemPoints(address _member, uint _points) public {
// Retrieve the member's data from the mapping
Member storage member = members[_member];
// Ensure the member has enough points to redeem
require(member.points >= _points, "Not enough points to redeem");
// Subtract the points from the member's balance
member.points -= _points;
}
}
pragma solidity ^0.7.0;
// Define the contract
contract LoyaltyCard {
// Define a struct to represent a loyalty card
struct Card {
string name;
uint points;
}
// Define a struct to store the data for each member
struct Member {
address owner;
Card[] cards;
}
// Define a mapping to store the data for each member
mapping (address => Member) public members;
// Define a function to add a new loyalty card for a member
function addCard(address _member, string _name) public {
// Retrieve the member's data from the mapping
Member storage member = members[_member];
// Create a new loyalty card for the member
Card memory card = Card(_name, 0);
// Add the card to the member's array of cards
member.cards.push(card);
}
// Define a function to add points to a member's loyalty card
function addPoints(address _member, uint _cardIndex, uint _points) public {
// Retrieve the member's data from the mapping
Member storage member = members[_member];
// Retrieve the specified loyalty card from the member's array of cards
Card storage card = member.cards[_cardIndex];
// Add the points to the card's balance
card.points += _points;
}
// Define a function to redeem points for a reward
function redeemPoints(address _member, uint _cardIndex, uint _points) public {
// Retrieve the member's data from the mapping
Member storage member = members[_member];
// Retrieve the specified loyalty card from the member's array of cards
Card storage card = member.cards[_cardIndex];
// Ensure the card has enough points to redeem
require(card.points >= _points, "Not enough points to redeem");
// Subtract the points from the card's balance
card.points -= _points;
}
}
import 'package:web3dart/web3dart.dart';
// Assume that the contract address and ABI are stored in variables
// called 'contractAddress' and 'contractAbi', respectively
// Create a new instance of the EthereumClient class
final client = Web3Client(rpcUrl, httpClient: httpClient);
// Load the contract using the contract address and ABI
final contract = DeployedContract(
ContractAbi.fromJson(contractAbi, 'LoyaltyCard'),
EthereumAddress.fromHex(contractAddress),
);
// Create a function call request to call the 'redeemPoints' function
final request = contract.function('redeemPoints').call(
// Pass the member's address and the number of points to redeem as arguments
[memberAddress, pointsToRedeem],
);
// Send the request to the Ethereum network
final result = await client.sendTransaction(
Transaction.callContract(
contract: contract,
function: request,
// Set the maximum amount of gas to use
maxGas: 1000000,
),
// Sign the transaction using the private key of the account sending the transaction
credentials,
);
// Check the result of the transaction to see if it was successful
if (result.status == TransactionReceiptStatus.success) {
print('Transaction was successful');
} else {
print('Transaction failed');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment